Skip to content

Commit

Permalink
Add SocketCAN Support for Telemetry (#37)
Browse files Browse the repository at this point in the history
* Initial commit, WIPX

* - Added socketcan as another input
- Changed serial's input flags to no longer default to /dev/ttyUSB0
- Tested to work on the car

* Updated startup script to start can0 using PCAN dongles

* Undid changes made during testing to the fake.go file

* Added new line to fake.go to match original version
  • Loading branch information
taipingli authored Jun 11, 2019
1 parent 63ef01b commit 02279e1
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 18 deletions.
8 changes: 7 additions & 1 deletion driver_display_startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
# Kill any old instances of telemetry.
pkill telemetry

# Initialize CAN (PCAN dongle for now)
sudo modprobe can
sudo modprobe can_raw
sudo modprobe vcan
sudo ip link set can0 up type can bitrate 500000

# Start serving the website. Use 'nice' so the process plays nice with
# scheduling priority otherwise it can overrun.
nice ./bin/telemetry start --tty=/dev/ttyAMA0 --db=can.db \
nice ./bin/telemetry start --socketcan=can0 --db=can.db \
--schema=can_messages.asciipb &

# Start a very minimal version of Chromium in fullscreen.
Expand Down
15 changes: 8 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@ require (
github.com/AlekSi/gocov-xml v0.0.0-20190121064608-3a14fb1c4737 // indirect
github.com/axw/gocov v0.0.0-20170322000131-3a69a0d2a4ef // indirect
github.com/go-chi/chi v0.0.0-20170216185340-5917107c1cec // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/golang/protobuf v0.0.0-20180612185929-5831880292e7 // indirect
github.com/gorilla/websocket v0.0.0-20170218162710-3f3e394da2b8 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jacobsa/go-serial v0.0.0-20180131005756-15cf729a72d4 // indirect
github.com/jacobsa/go-serial v0.0.0-20180131005756-15cf729a72d4
github.com/linklayer/go-socketcan v0.0.0-20190403144728-e9fc2da5204c
github.com/mattn/go-sqlite3 v0.0.0-20170305140206-eac1dfa2a61e // indirect
github.com/mrVanboy/go-simple-cobs v0.0.0-20180102074149-bc19984395e8 // indirect
github.com/mrVanboy/go-simple-cobs v0.0.0-20180102074149-bc19984395e8
github.com/pkg/errors v0.0.0-20161029093637-248dadf4e906 // indirect
github.com/spf13/cobra v0.0.0-20170217164507-ee4055870c2d // indirect
github.com/spf13/pflag v0.0.0-20160820154156-103ce5cd2042 // indirect
github.com/uw-midsun/telemetry/pkg/canmsgdefs v0.0.0 // indirect
github.com/uw-midsun/telemetry/pkg/canmsgdefs v0.0.0
github.com/uw-midsun/telemetry/pkg/cli v0.0.0
github.com/uw-midsun/telemetry/pkg/db v0.0.0 // indirect
github.com/uw-midsun/telemetry/pkg/msgs v0.0.0 // indirect
github.com/uw-midsun/telemetry/pkg/msgs v0.0.0
github.com/uw-midsun/telemetry/pkg/protos v0.0.0-00010101000000-000000000000 // indirect
github.com/uw-midsun/telemetry/pkg/pubsub v0.0.0-00010101000000-000000000000 // indirect
github.com/uw-midsun/telemetry/pkg/pubsub v0.0.0-00010101000000-000000000000
github.com/uw-midsun/telemetry/pkg/sources/fake v0.0.0-00010101000000-000000000000 // indirect
github.com/uw-midsun/telemetry/pkg/sources/serial v0.0.0-00010101000000-000000000000 // indirect
github.com/uw-midsun/telemetry/pkg/util/randutil v0.0.0-00010101000000-000000000000 // indirect
github.com/uw-midsun/telemetry/pkg/util/randutil v0.0.0-00010101000000-000000000000
github.com/uw-midsun/telemetry/pkg/ws v0.0.0-00010101000000-000000000000 // indirect
github.com/wadey/gocovmerge v0.0.0-20160331181800-b5bfa59ec0ad // indirect
golang.org/x/lint v0.0.0-20190409202823-959b441ac422 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jacobsa/go-serial v0.0.0-20180131005756-15cf729a72d4 h1:G2ztCwXov8mRvP0ZfjE6nAlaCX2XbykaeHdbT6KwDz0=
github.com/jacobsa/go-serial v0.0.0-20180131005756-15cf729a72d4/go.mod h1:2RvX5ZjVtsznNZPEt4xwJXNJrM3VTZoQf7V6gk0ysvs=
github.com/linklayer/go-socketcan v0.0.0-20190403144728-e9fc2da5204c h1:sFvdRVieiBlHshbjVq7N/pzI47fYmsQnBtHwst2vZQs=
github.com/linklayer/go-socketcan v0.0.0-20190403144728-e9fc2da5204c/go.mod h1:JCz++4MSYhycvIXriUF4g5bC5Z0f/6eQjFmLQrl7lGc=
github.com/mattn/go-sqlite3 v0.0.0-20170305140206-eac1dfa2a61e h1:AI4kVwkgPj55D76TxcJHTNl3y7TiOZ0IDpEiwunOexI=
github.com/mattn/go-sqlite3 v0.0.0-20170305140206-eac1dfa2a61e/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mrVanboy/go-simple-cobs v0.0.0-20180102074149-bc19984395e8 h1:6e7FpwhHlRWVOOHiFTpajP0EHNYFLDw54ZMlx2mmdGk=
Expand Down
6 changes: 4 additions & 2 deletions pkg/cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,19 @@ var serverPort int
var ttyPort string
var dbName string
var schemaFile string
var socketCanPort string

func init() {
startCmd.Flags().StringVarP(&schemaFile, "schema", "s", "", "s")
startCmd.Flags().IntVarP(&serverPort, "port", "p", 8080, "port")
startCmd.Flags().BoolVarP(&fake, "fake", "f", false, "fake")
startCmd.Flags().StringVarP(&ttyPort, "tty", "t", "/dev/ttyUSB0", "tty")
startCmd.Flags().StringVarP(&ttyPort, "tty", "t", "", "tty")
startCmd.Flags().StringVarP(&socketCanPort, "socketcan", "c", "can0", "socketcan")
startCmd.Flags().StringVarP(&dbName, "db", "d", "", "db")
}

func setupURLRouting(r *chi.Mux, messageBus *pubsub.MessageBus) {
r.Get("/ws", ws.ServeHTTP(messageBus, ttyPort, fake))
r.Get("/ws", ws.ServeHTTP(messageBus, ttyPort, fake, socketCanPort))
workDir, _ := os.Getwd()
filesDir := filepath.Join(workDir, "client", "src")
r.FileServer("/", http.Dir(filesDir))
Expand Down
41 changes: 41 additions & 0 deletions pkg/sources/socketcan/socketcan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package socketcan

import (
"encoding/binary"
"fmt"
"github.com/linklayer/go-socketcan/pkg/socketcan"
"os"

"github.com/uw-midsun/telemetry/pkg/msgs"
"github.com/uw-midsun/telemetry/pkg/pubsub"

log "github.com/golang/glog"
)

// Run starts reading canPacket from the specified serial port and serving those on bus.
func Run(port string, bus *pubsub.MessageBus) {
log.Infof("SocketCAN %s", port)
device, err := socketcan.NewRawInterface(port)

if err != nil {
fmt.Printf("could not open interface %s: %v\n",
port, err)
os.Exit(1)
}

defer device.Close()

for {
frame, err := device.RecvFrame()
if err != nil {
fmt.Printf("error receiving frame: %v", err)
os.Exit(1)
}
//dataStr := dataToString(frame.Data)
//dataInt, _ := strconv.ParseInt(string(frame.Data), 10, 64)
//fmt.Printf(" %s\t%03X\t[%d]\t%s\n", device.IfName, frame.ArbId, frame.Dlc, dataStr)
//log.Infof("Device: %s, Data: %i, DLC: %s\n", frame.ArbId, dataInt, frame.Dlc)
data := binary.LittleEndian.Uint64(frame.Data)
bus.Publish("CAN", msgs.NewCAN(frame.ArbId, uint64(data), uint8(frame.Dlc)))
}
}
21 changes: 13 additions & 8 deletions pkg/ws/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
"github.com/uw-midsun/telemetry/pkg/pubsub"
"github.com/uw-midsun/telemetry/pkg/sources/fake"
"github.com/uw-midsun/telemetry/pkg/sources/serial"

"github.com/uw-midsun/telemetry/pkg/sources/socketcan"
"sync"

"github.com/gorilla/websocket"

)

const (
Expand All @@ -30,7 +31,7 @@ var upgrader = websocket.Upgrader{
}

// handleMessages handles messages on the websocket
func handleMessages(bus *pubsub.MessageBus, conn *websocket.Conn, tty string, uf bool) {
func handleMessages(bus *pubsub.MessageBus, conn *websocket.Conn, tty string, uf bool, socketCan string) {
// pingTicker := time.NewTicker(pingPeriod)
// defer pingTicker.Stop()

Expand All @@ -42,22 +43,26 @@ func handleMessages(bus *pubsub.MessageBus, conn *websocket.Conn, tty string, uf
defer l.Unlock()
conn.WriteJSON(msg)
})

if uf {
if uf { // Fake Data
for {
fake.GenFake(bus)
time.Sleep(time.Millisecond * 500)
}
} else {
} else if tty == "" { // SocketCAN

socketcan.Run(socketCan, bus)

} else { // Serial
serial.Run(tty, bus)
}
}

// ServeHTTP serves the websocket connection
func ServeHTTP(b *pubsub.MessageBus, tty string, fake bool) func(http.ResponseWriter, *http.Request) {
func ServeHTTP(b *pubsub.MessageBus, tty string, fake bool, socketCan string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)

defer func() {
if conn != nil {
conn.Close()
Expand All @@ -68,6 +73,6 @@ func ServeHTTP(b *pubsub.MessageBus, tty string, fake bool) func(http.ResponseWr
return
}

handleMessages(b, conn, tty, fake)
handleMessages(b, conn, tty, fake, socketCan)
}
}

0 comments on commit 02279e1

Please sign in to comment.