1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-04 22:17:39 +08:00

263 lines
6.0 KiB
Go
Raw Normal View History

2014-04-26 18:07:04 -07:00
package sphero
import (
"fmt"
"time"
2014-07-09 18:32:27 -07:00
"github.com/hybridgroup/gobot"
)
type packet struct {
header []uint8
body []uint8
checksum uint8
}
2014-04-28 11:40:20 -07:00
type SpheroDriver struct {
gobot.Driver
2014-06-10 15:16:11 -07:00
seq uint8
asyncResponse [][]uint8
syncResponse [][]uint8
packetChannel chan *packet
responseChannel chan []uint8
}
2014-05-22 21:20:16 -07:00
func NewSpheroDriver(a *SpheroAdaptor, name string) *SpheroDriver {
s := &SpheroDriver{
2014-07-07 17:42:19 -07:00
Driver: *gobot.NewDriver(
name,
"SpheroDriver",
a,
),
2014-06-10 15:16:11 -07:00
packetChannel: make(chan *packet, 1024),
responseChannel: make(chan []uint8, 1024),
}
2014-07-07 17:42:19 -07:00
s.AddEvent("collision")
2014-07-09 18:32:27 -07:00
s.AddCommand("SetRGB", func(params map[string]interface{}) interface{} {
r := uint8(params["r"].(float64))
g := uint8(params["g"].(float64))
b := uint8(params["b"].(float64))
s.SetRGB(r, g, b)
return nil
})
2014-07-09 18:32:27 -07:00
s.AddCommand("Roll", func(params map[string]interface{}) interface{} {
speed := uint8(params["speed"].(float64))
heading := uint16(params["heading"].(float64))
s.Roll(speed, heading)
return nil
})
2014-07-09 18:32:27 -07:00
s.AddCommand("Stop", func(params map[string]interface{}) interface{} {
s.Stop()
return nil
})
2014-07-09 18:32:27 -07:00
s.AddCommand("GetRGB", func(params map[string]interface{}) interface{} {
return s.GetRGB()
})
2014-07-09 18:32:27 -07:00
s.AddCommand("SetBackLED", func(params map[string]interface{}) interface{} {
level := uint8(params["level"].(float64))
s.SetBackLED(level)
return nil
})
2014-07-09 18:32:27 -07:00
s.AddCommand("SetHeading", func(params map[string]interface{}) interface{} {
heading := uint16(params["heading"].(float64))
s.SetHeading(heading)
return nil
})
2014-07-09 18:32:27 -07:00
s.AddCommand("SetStabilization", func(params map[string]interface{}) interface{} {
on := params["heading"].(bool)
s.SetStabilization(on)
return nil
})
return s
}
func (s *SpheroDriver) adaptor() *SpheroAdaptor {
2014-07-09 18:32:27 -07:00
return s.Adaptor().(*SpheroAdaptor)
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) Init() bool {
return true
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) Start() bool {
go func() {
for {
2014-06-10 15:16:11 -07:00
packet := <-s.packetChannel
2014-04-26 18:07:04 -07:00
s.write(packet)
}
}()
go func() {
for {
2014-06-10 15:16:11 -07:00
response := <-s.responseChannel
s.syncResponse = append(s.syncResponse, response)
}
}()
go func() {
for {
2014-04-26 18:07:04 -07:00
header := s.readHeader()
if header != nil && len(header) != 0 {
2014-04-26 18:07:04 -07:00
body := s.readBody(header[4])
if header[1] == 0xFE {
async := append(header, body...)
2014-06-10 15:16:11 -07:00
s.asyncResponse = append(s.asyncResponse, async)
} else {
2014-06-10 15:16:11 -07:00
s.responseChannel <- append(header, body...)
}
}
}
}()
go func() {
for {
var evt []uint8
2014-06-10 15:16:11 -07:00
for len(s.asyncResponse) != 0 {
evt, s.asyncResponse = s.asyncResponse[len(s.asyncResponse)-1], s.asyncResponse[:len(s.asyncResponse)-1]
if evt[2] == 0x07 {
2014-04-26 18:07:04 -07:00
s.handleCollisionDetected(evt)
}
}
time.Sleep(100 * time.Millisecond)
}
}()
2014-04-26 18:07:04 -07:00
s.configureCollisionDetection()
s.enableStopOnDisconnect()
return true
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) Halt() bool {
2014-07-10 19:24:57 -07:00
gobot.Every(10*time.Millisecond, func() {
s.Stop()
})
time.Sleep(1 * time.Second)
return true
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) SetRGB(r uint8, g uint8, b uint8) {
s.packetChannel <- s.craftPacket([]uint8{r, g, b, 0x01}, 0x02, 0x20)
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) GetRGB() []uint8 {
return s.getSyncResponse(s.craftPacket([]uint8{}, 0x02, 0x22))
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) SetBackLED(level uint8) {
s.packetChannel <- s.craftPacket([]uint8{level}, 0x02, 0x21)
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) SetHeading(heading uint16) {
s.packetChannel <- s.craftPacket([]uint8{uint8(heading >> 8), uint8(heading & 0xFF)}, 0x02, 0x01)
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) SetStabilization(on bool) {
b := uint8(0x01)
if on == false {
b = 0x00
}
s.packetChannel <- s.craftPacket([]uint8{b}, 0x02, 0x02)
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) Roll(speed uint8, heading uint16) {
s.packetChannel <- s.craftPacket([]uint8{speed, uint8(heading >> 8), uint8(heading & 0xFF), 0x01}, 0x02, 0x30)
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) Stop() {
2014-04-26 18:07:04 -07:00
s.Roll(0, 0)
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) configureCollisionDetection() {
s.packetChannel <- s.craftPacket([]uint8{0x01, 0x40, 0x40, 0x50, 0x50, 0x60}, 0x02, 0x12)
}
func (s *SpheroDriver) enableStopOnDisconnect() {
s.packetChannel <- s.craftPacket([]uint8{0x00, 0x00, 0x00, 0x01}, 0x02, 0x37)
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) handleCollisionDetected(data []uint8) {
2014-07-07 17:42:19 -07:00
gobot.Publish(s.Event("collision"), data)
}
2014-06-10 15:16:11 -07:00
func (s *SpheroDriver) getSyncResponse(packet *packet) []byte {
s.packetChannel <- packet
for i := 0; i < 500; i++ {
2014-06-10 15:16:11 -07:00
for key := range s.syncResponse {
if s.syncResponse[key][3] == packet.header[4] && len(s.syncResponse[key]) > 6 {
var response []byte
2014-06-10 15:16:11 -07:00
response, s.syncResponse = s.syncResponse[len(s.syncResponse)-1], s.syncResponse[:len(s.syncResponse)-1]
return response
}
}
time.Sleep(10 * time.Microsecond)
}
2014-06-10 15:16:11 -07:00
return []byte{}
}
func (s *SpheroDriver) craftPacket(body []uint8, did byte, cid byte) *packet {
packet := new(packet)
packet.body = body
dlen := len(packet.body) + 1
packet.header = []uint8{0xFF, 0xFF, did, cid, s.seq, uint8(dlen)}
2014-04-26 18:07:04 -07:00
packet.checksum = s.calculateChecksum(packet)
return packet
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) write(packet *packet) {
buf := append(packet.header, packet.body...)
buf = append(buf, packet.checksum)
length, err := s.adaptor().sp.Write(buf)
if err != nil {
2014-04-26 18:07:04 -07:00
fmt.Println(s.Name, err)
s.adaptor().Disconnect()
2014-04-28 11:40:20 -07:00
fmt.Println("Reconnecting to SpheroDriver...")
s.adaptor().Connect()
return
} else if length != len(buf) {
2014-04-26 18:07:04 -07:00
fmt.Println("Not enough bytes written", s.Name)
}
2014-06-10 15:16:11 -07:00
s.seq++
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) calculateChecksum(packet *packet) uint8 {
buf := append(packet.header, packet.body...)
buf = buf[2:]
var calculatedChecksum uint16
for i := range buf {
calculatedChecksum += uint16(buf[i])
}
return uint8(^(calculatedChecksum % 256))
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) readHeader() []uint8 {
return s.readNextChunk(5)
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) readBody(length uint8) []uint8 {
return s.readNextChunk(int(length))
}
func (s *SpheroDriver) readNextChunk(length int) []uint8 {
read := make([]uint8, length)
bytesRead := 0
for bytesRead < length {
time.Sleep(1 * time.Millisecond)
n, err := s.adaptor().sp.Read(read[bytesRead:])
if err != nil {
return nil
}
bytesRead += n
}
2014-06-10 15:16:11 -07:00
return read
}