-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.go
344 lines (302 loc) · 8.83 KB
/
serial.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package main
import (
"errors"
serialPackage "go.bug.st/serial"
"log"
"strconv"
"time"
)
const CmdSerialBaud = 38400
const CmdSync0 byte = 33
const CmdSync1 byte = 42
const CmdTermByte byte = 255
const CmdRegPower byte = 5
const CmdRegSpeed byte = 10
const CmdRegGaintMode byte = 15
const CmdRegBalanceMode byte = 20
const CmdRegBodyHeight byte = 25
const CmdRegTranslate byte = 35
const CmdRegWalk byte = 40
const CmdRegRotate byte = 45
const CmdRegDoubleHeight byte = 50
const CmdRegDoubleLength byte = 55
const CmdRegSingleLegPos byte = 60
const CmdRegSound byte = 65
const CmdRegOut byte = 70
const CmdRegStatusLed byte = 75
const CmdRegSaLeg byte = 100
const CmdRegAkku byte = 105
const CmdRegPs2Active byte = 110
const CmdRegIsWalking byte = 115
const CmdRegIsPowerOn byte = 120
const CmdRegReadPs2Values byte = 125
const CmdRegIn1 byte = 130
const CmdRegReset byte = 255
const StatusAckOk byte = 64
const StatusErrTerm byte = 1
const StatusErrState byte = 2
const StatusErrCrc byte = 3
const StatusErrCmd byte = 255
const Walkmode byte = 0
const TranslateMode byte = 1
const RotateMode byte = 2
const SingleLegMode byte = 3
const BalancemodeOn byte = 1
const BalancemodeOff byte = 0
type cmd struct {
name string
params map[string]string
answer chan string
}
type Serial struct {
port serialPackage.Port
cmds chan cmd
readChannel chan []byte
}
func Open() (*Serial, error) {
mode := &serialPackage.Mode{
BaudRate: CmdSerialBaud,
}
port, err := serialPackage.Open("/dev/ttyAMA0", mode)
if err != nil {
return nil, err
}
s := &Serial{port: port, cmds: make(chan cmd), readChannel: make(chan []byte)}
go s.runCmdHandler()
go s.runReadHandler()
return s, nil
}
func InvokeCommand(serial *Serial, cmdName string, params map[string]string) string {
cmd := cmd{
cmdName,
params,
make(chan string),
}
serial.cmds <- cmd
return <-cmd.answer
}
func (serial *Serial) runCmdHandler() {
for {
select {
case cmd := <-serial.cmds:
serial.runCommand(cmd)
}
}
}
func (serial *Serial) sendArray(data []byte) ([]byte, error) {
for len(data) != 5 {
data = append(data, 0)
}
return serial.sendBytes(data[0], data[1], data[2], data[3], data[4])
}
func (serial *Serial) sendBytes(cmd byte, data1 byte, data2 byte, data3 byte, data4 byte) ([]byte, error) {
var crc = CmdSync0 ^ CmdSync1 ^ cmd ^ data1 ^ data2 ^ data3 ^ data4
err := serial.write([]byte{CmdSync0, CmdSync1, crc, cmd, data1, data2, data3, data4, CmdTermByte})
if err != nil {
return nil, err
}
return serial.readResponse(cmd)
}
func (serial *Serial) write(bytes []byte) error {
//for i, readByte := range bytes {
// log.Println("send byte[" + strconv.Itoa(i) + "]: " + strconv.Itoa(int(readByte)))
//}
_, err := serial.port.Write(bytes)
return err
}
func (serial *Serial) runCommand(cmd cmd) {
serialCmd, ok := cmdMapping[cmd.name]
if !ok {
cmd.answer <- "unknown command"
} else {
answer := serialCmd.call(serial, cmd.params)
cmd.answer <- answer
}
close(cmd.answer)
}
func (serial *Serial) readResponse(cmd byte) ([]byte, error) {
select {
case response := <-serial.readChannel:
return response[5:], nil
case <-time.After(1 * time.Second):
return nil, errors.New("timeout when reading serial")
}
}
func (serial *Serial) runReadHandler() {
var readBytes = make([]byte, 0, 20)
var buf = make([]byte, 20)
scanPosition := 0
responseStart := -1
for {
read, err := serial.port.Read(buf)
if err != nil {
log.Println("error reading:", err)
continue
}
readBytes = append(readBytes, buf[0:read]...)
for ; scanPosition < len(readBytes); scanPosition++ {
if readBytes[scanPosition] == CmdSync0 {
responseStart = scanPosition
}
if readBytes[scanPosition] == CmdTermByte && responseStart != -1 {
bytes, err := validate(readBytes[responseStart : scanPosition+1])
if err != nil {
log.Println("error reading response", err)
} else {
serial.readChannel <- bytes
}
readBytes = make([]byte, 0, 20)
responseStart = -1
scanPosition = 0
break
}
}
}
}
func validate(readBytes []byte) ([]byte, error) {
//for i, readByte := range readBytes {
// log.Println("receive byte[" + strconv.Itoa(i) + "]: " + strconv.Itoa(int(readByte)))
//}
if readBytes[0] != CmdSync0 {
return nil, errors.New("wrong sync byte 0 :( " + strconv.Itoa(int(readBytes[0])))
}
if readBytes[1] != CmdSync1 {
return nil, errors.New("wrong sync byte 1 :( " + strconv.Itoa(int(readBytes[1])))
}
var crc = calcCrc(readBytes)
if readBytes[2] != crc {
return nil, errors.New("wrong crc :( " + strconv.Itoa(int(crc)))
}
if readBytes[3] != StatusAckOk {
return nil, errors.New("wrong status :( " + strconv.Itoa(int(readBytes[3])))
}
return readBytes, nil
}
func calcCrc(bytes []byte) byte {
var crc byte = 0
for i := 0; i < len(bytes)-1; i++ {
if i != 2 {
crc ^= bytes[i]
}
}
return crc
}
var cmdMapping = map[string]serialCmd{
"reset": &okCall{[]byte{CmdRegReset, 100, 100, 100}},
"power_on": &okCall{[]byte{CmdRegPower, 1}},
"power_off": &okCall{[]byte{CmdRegPower, 0}},
"walk_stop": &okCall{[]byte{CmdRegWalk, 128, 128, 128}},
"walk_forward": &okCall{[]byte{CmdRegWalk, 128, 0, 128}},
"walk_back": &okCall{[]byte{CmdRegWalk, 128, 255, 128}},
"walk_left": &okCall{[]byte{CmdRegWalk, 0, 128, 128}},
"walk_right": &okCall{[]byte{CmdRegWalk, 255, 128, 128}},
"walk_turn_left": &okCall{[]byte{CmdRegWalk, 128, 128, 0}},
"walk_turn_right": &okCall{[]byte{CmdRegWalk, 128, 128, 255}},
"sound": ¶meterizedOkCall{CmdRegSound, []string{"duration", "frequency"}, []convertParam{paramToByte, mapping(0, 2550, 0, 255)}},
"body_height": ¶meterizedOkCall{CmdRegBodyHeight, []string{"height"}, []convertParam{percentMapping(0, 130)}},
"speed": ¶meterizedOkCall{CmdRegSpeed, []string{"speed"}, []convertParam{percentMapping(100, 0)}},
"walk": ¶meterizedOkCall{CmdRegWalk, []string{"side", "forward", "turn"}, []convertParam{mapping(-100, 100, 1, 255), mapping(-100, 100, 255, 1), mapping(-100, 100, 1, 255)}},
"akku_charge": &onlyReturnCall{[]byte{CmdRegAkku}, convertAkkuCharge},
}
type serialCmd interface {
call(*Serial, map[string]string) string
}
type okCall struct {
data []byte
}
type parameterizedOkCall struct {
cmd byte
params []string
converter []convertParam
}
type onlyReturnCall struct {
data []byte
converter convertReturnValues
}
type convertReturnValues func([]byte) string
type convertParam func(string) (byte, error)
func (cmd *okCall) call(serial *Serial, _ map[string]string) string {
_, err := serial.sendArray(cmd.data)
if err != nil {
return err.Error()
}
return "ok"
}
func (cmd *parameterizedOkCall) call(serial *Serial, paramsMap map[string]string) string {
params, err := gatherParams(paramsMap, cmd.params)
if err != nil {
return err.Error()
}
data := make([]byte, len(cmd.params))
for i := 0; i < len(data); i++ {
paramAsByte, err := cmd.converter[i](params[i])
if err != nil {
return err.Error()
}
data[i] = paramAsByte
}
_, err = serial.sendArray(append(append(make([]byte, 0, 20), cmd.cmd), data...))
if err != nil {
return err.Error()
}
return "ok"
}
func (cmd *onlyReturnCall) call(serial *Serial, _ map[string]string) string {
returnData, err := serial.sendArray(cmd.data)
if err != nil {
return err.Error()
}
return cmd.converter(returnData)
}
func gatherParams(paramsMap map[string]string, paramNames []string) ([]string, error) {
var params = make([]string, 0, 4)
for _, paramName := range paramNames {
param, ok := paramsMap[paramName]
if !ok {
return nil, errors.New("missing param: " + paramName)
}
params = append(params, param)
}
return params, nil
}
func convertAkkuCharge(bytes []byte) string {
//TODO experiment with convert to percentage?
var charge = int(bytes[0])
charge = charge << 8
charge |= int(bytes[1])
return strconv.Itoa(charge)
}
func percentMapping(start int, end int) convertParam {
return mapping(0, 100, start, end)
}
func mapping(inStart int, inEnd int, outStart int, outEnd int) convertParam {
return func(param string) (byte, error) {
paramAsInt, err := strconv.Atoi(param)
if err != nil {
return 0, err
}
return doMapping(paramAsInt, inStart, inEnd, outStart, outEnd)
}
}
func doMapping(inValue int, inStart int, inEnd int, outStart int, outEnd int) (byte, error) {
if inStart >= inEnd {
return 0, errors.New("inStart has to be smaller then inEnd")
}
if inValue < inStart || inValue > inEnd {
return 0, errors.New("param has to be in inStart and inEnd range")
}
inSteps := inEnd - inStart
outRange := outEnd - outStart
value := outStart + ((outRange)*(inValue-inStart))/inSteps
return byte(value), nil
}
func paramToByte(param string) (byte, error) {
paramAsInt, err := strconv.Atoi(param)
if err != nil {
return 0, err
}
if paramAsInt < 0 || paramAsInt > 255 {
return 0, errors.New("value " + param + " out of byte range")
}
return byte(paramAsInt), nil
}