1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-26 13:48:49 +08:00

225 lines
4.9 KiB
Go
Raw Normal View History

2014-04-26 18:07:04 -07:00
package sphero
import (
"fmt"
"github.com/hybridgroup/gobot"
"time"
)
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
Adaptor *SpheroAdaptor
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 {
2014-04-28 11:40:20 -07:00
return &SpheroDriver{
2014-04-26 18:07:04 -07:00
Driver: gobot.Driver{
2014-06-11 11:37:20 -07:00
Name: name,
Events: map[string]*gobot.Event{
"Collision": gobot.NewEvent(),
},
2014-04-26 18:07:04 -07:00
Commands: []string{
"SetRGBC",
"RollC",
"StopC",
"GetRGBC",
"SetBackLEDC",
"SetHeadingC",
"SetStabilizationC",
},
},
2014-06-10 15:16:11 -07:00
Adaptor: a,
packetChannel: make(chan *packet, 1024),
responseChannel: make(chan []uint8, 1024),
}
}
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()
return true
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) Halt() bool {
go func() {
for {
2014-04-26 18:07:04 -07:00
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) {
2014-06-10 15:16:11 -07:00
s.packetChannel <- s.craftPacket([]uint8{r, g, b, 0x01}, 0x20)
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) GetRGB() []uint8 {
2014-06-10 15:16:11 -07:00
return s.getSyncResponse(s.craftPacket([]uint8{}, 0x22))
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) SetBackLED(level uint8) {
2014-06-10 15:16:11 -07:00
s.packetChannel <- s.craftPacket([]uint8{level}, 0x21)
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) SetHeading(heading uint16) {
2014-06-10 15:16:11 -07:00
s.packetChannel <- s.craftPacket([]uint8{uint8(heading >> 8), uint8(heading & 0xFF)}, 0x01)
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) SetStabilization(on bool) {
b := uint8(0x01)
if on == false {
b = 0x00
}
2014-06-10 15:16:11 -07:00
s.packetChannel <- s.craftPacket([]uint8{b}, 0x02)
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) Roll(speed uint8, heading uint16) {
2014-06-10 15:16:11 -07:00
s.packetChannel <- s.craftPacket([]uint8{speed, uint8(heading >> 8), uint8(heading & 0xFF), 0x01}, 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() {
2014-06-10 15:16:11 -07:00
s.packetChannel <- s.craftPacket([]uint8{0x01, 0x40, 0x40, 0x50, 0x50, 0x60}, 0x12)
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) handleCollisionDetected(data []uint8) {
2014-04-26 18:07:04 -07:00
gobot.Publish(s.Events["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{}
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) craftPacket(body []uint8, cid byte) *packet {
packet := new(packet)
packet.body = body
dlen := len(packet.body) + 1
2014-04-26 18:07:04 -07:00
packet.header = []uint8{0xFF, 0xFF, 0x02, cid, s.seq, uint8(dlen)}
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)
2014-04-26 18:07:04 -07:00
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...")
2014-04-26 18:07:04 -07:00
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 {
2014-04-26 18:07:04 -07:00
data := s.readNextChunk(5)
if data == nil {
return nil
}
2014-06-10 15:16:11 -07:00
return data
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) readBody(length uint8) []uint8 {
2014-04-26 18:07:04 -07:00
data := s.readNextChunk(length)
if data == nil {
return nil
}
2014-06-10 15:16:11 -07:00
return data
}
2014-04-28 11:40:20 -07:00
func (s *SpheroDriver) readNextChunk(length uint8) []uint8 {
time.Sleep(1000 * time.Microsecond)
var read = make([]uint8, int(length))
2014-04-26 18:07:04 -07:00
l, err := s.Adaptor.sp.Read(read[:])
if err != nil || length != uint8(l) {
return nil
}
2014-06-10 15:16:11 -07:00
return read
}