forked from hybridgroup/gobot-sphero
/
sphero_driver.go
165 lines (144 loc) · 3.65 KB
/
sphero_driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package gobotSphero
import (
"fmt"
"github.com/hybridgroup/gobot"
)
type packet struct {
header []uint8
body []uint8
checksum uint8
}
type SpheroDriver struct {
gobot.Driver
SpheroAdaptor *SpheroAdaptor
seq uint8
messages [][]uint8
}
func NewSphero(sa *SpheroAdaptor) *SpheroDriver {
s := new(SpheroDriver)
s.Events = make(map[string]chan interface{})
s.SpheroAdaptor = sa
s.Commands = []string{
"SetRGBC",
"RollC",
"StopC",
}
return s
}
func (sd *SpheroDriver) StartDriver() {
sd.ConfigureCollisionDetection()
gobot.Every(sd.Interval, func() {
sd.handleMessageEvents()
})
}
func (sd *SpheroDriver) Roll(speed uint8, heading uint16) {
packet := new(packet)
packet.body = []uint8{speed, uint8(heading >> 8), uint8(heading & 0xFF), 0x01}
dlen := len(packet.body) + 1
packet.header = []uint8{0xFF, 0xFF, 0x02, 0x30, sd.seq, uint8(dlen)}
packet.checksum = sd.calculateChecksum(packet)
sd.write(packet)
}
func (sd *SpheroDriver) Stop() {
sd.Roll(0, 0)
}
func (sd *SpheroDriver) SetRGB(r uint8, g uint8, b uint8) {
packet := new(packet)
packet.body = []uint8{r, g, b, 0x01}
dlen := len(packet.body) + 1
packet.header = []uint8{0xFF, 0xFF, 0x02, 0x20, sd.seq, uint8(dlen)}
packet.checksum = sd.calculateChecksum(packet)
sd.write(packet)
}
func (sd *SpheroDriver) ConfigureCollisionDetection() {
packet := new(packet)
packet.body = []uint8{0x01, 0x40, 0x40, 0x50, 0x50, 0x60}
dlen := len(packet.body) + 1
packet.header = []uint8{0xFF, 0xFF, 0x02, 0x12, sd.seq, uint8(dlen)}
packet.checksum = sd.calculateChecksum(packet)
sd.Events["Collision"] = make(chan interface{})
sd.write(packet)
}
func (sd *SpheroDriver) write(packet *packet) []uint8 {
var header []uint8
var body []uint8
buf := append(packet.header, packet.body...)
buf = append(buf, packet.checksum)
length, err := sd.SpheroAdaptor.sp.Write(buf)
if err != nil {
fmt.Println(sd.Name, err)
sd.SpheroAdaptor.Disconnect()
sd.SpheroAdaptor.Connect()
return nil
} else if length != len(buf) {
fmt.Println("Not enough bytes written", sd.Name)
}
sd.seq += 1
header = sd.readHeader()
if header != nil {
body = sd.readBody(header[4])
}
for header != nil && header[1] == 0xFE {
async := append(header, body...)
sd.messages = append(sd.messages, async)
header := sd.readHeader()
if header != nil {
body = sd.readBody(header[4])
} else {
body = nil
}
}
if len(header) != 0 && header[2] == 0 {
return append(header, body...)
} else {
fmt.Println("Unable to write to Sphero!", sd.Name)
return nil
}
}
func (sd *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))
}
func (sd *SpheroDriver) handleMessageEvents() {
var evt []uint8
for len(sd.messages) != 0 {
evt, sd.messages = sd.messages[len(sd.messages)-1], sd.messages[:len(sd.messages)-1]
if evt[2] == 0x07 {
fmt.Println(sd.Name, evt)
sd.handleCollisionDetected(evt)
}
}
}
func (sd *SpheroDriver) handleCollisionDetected(data []uint8) {
sd.Events["Collision"] <- data
}
func (sd *SpheroDriver) readHeader() []uint8 {
data := sd.readNextChunk(5)
if data == nil {
return nil
} else {
return data
}
}
func (sd *SpheroDriver) readBody(length uint8) []uint8 {
data := sd.readNextChunk(length)
if data == nil {
return nil
} else {
return data
}
}
func (sd *SpheroDriver) readNextChunk(length uint8) []uint8 {
var read = make([]uint8, int(length))
l, err := sd.SpheroAdaptor.sp.Read(read[:])
if err != nil || length != uint8(l) {
return nil
} else {
return read
}
}