-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththunder.go
343 lines (283 loc) · 9.07 KB
/
thunder.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
package main
import (
"fmt"
"log"
"os/exec"
"github.com/baloo/gousb/usb"
"sync"
"time"
)
const (
X_TIME = 5360
X_POSITIONS = 100
Y_TIME = 1000
Y_POSITIONS = 10
)
const (
MOTOR_UP = 0x02
MOTOR_DOWN = 0x01
MOTOR_LEFT = 0x04
MOTOR_RIGHT = 0x08
MOTOR_FIRE = 0x10
MOTOR_STOP = 0x20
)
type Action byte
const (
DIR_UP Action = 0x00
DIR_DOWN Action = 0x01
DIR_LEFT Action = 0x02
DIR_RIGHT Action = 0x03
FIRE Action = 0x04 // TODO
RELOAD Action = 0x05 // TODO
WAIT Action = 0xFE
STOP Action = 0xFF
)
type Thunder struct {
usb_context *usb.Context
device_id string
subdevice *usb.Device
mutex sync.Mutex
channel chan Command
current_action byte
current_x int
current_y int
dirty bool
moves int
}
func NewThunder(device_id string) (*Thunder, error) {
usb_context := usb.NewContext()
dev, err := FindDevice(usb_context, device_id)
if err != nil {
return nil, err
}
c := &Thunder{
usb_context: usb_context,
device_id: device_id,
subdevice: dev,
channel: make(chan Command),
dirty: true,
}
return c, nil
}
func (c *Thunder) Close() error {
if c.subdevice != nil {
c.subdevice.Close()
}
c.usb_context.Close()
return nil
}
func FindDevice(ctx *usb.Context, device string) (*usb.Device, error) {
// Lookup for devices
devs, err := ctx.ListDevices(func(desc *usb.Descriptor) bool {
if fmt.Sprintf("%s:%s", desc.Vendor, desc.Product) != device {
return false
}
return true
})
if err != nil {
return nil, fmt.Errorf("thunder: Error while looking for devices. %s", err)
}
if len(devs) == 0 {
return nil, fmt.Errorf("thunder: Unable to find any devices")
}
// Keep one device
dev := devs[0]
devs = devs[1:]
// Close any unused devices
defer func() {
for _, d := range devs {
d.Close()
}
}()
dev.DetachKernelDriver(0)
return dev, nil
}
func (c *Thunder) Stop() error {
return c.Control(0x20)
}
func (c *Thunder) Control(msg byte) error {
log.Printf("Current state: %d", c.current_action)
data := []byte{0x02,msg,0x00,0x00,0x00,0x00,0x00,0x00}
ep, err := c.subdevice.Control(
0x21,
0x09, //request
0x00, //wvalue
0x00, //windex
data)
_ = ep
_ = err
return nil // TODO
}
type Command struct {
action Action
duration time.Duration
}
type CommandType byte
func (thunder *Thunder) CleanAll() error {
close(thunder.channel)
thunder.channel = make(chan Command)
thunder.RegisterStop()
return nil
}
func (thunder *Thunder) RegisterMove(action Action) error {
log.Printf("register action %s", action)
thunder.channel <- Command { action: action, duration: time.Since(time.Now()), }
return nil
}
func (thunder *Thunder) RegisterStop() error {
thunder.channel <- Command { action: STOP, duration: time.Since(time.Now()), }
return nil
}
func (thunder *Thunder) RegisterFire() error {
thunder.channel <- Command { action: FIRE, duration: time.Since(time.Now()), }
return nil
}
func (thunder *Thunder) RegisterReload() error {
thunder.channel <- Command { action: RELOAD, duration: time.Since(time.Now()), }
return nil
}
func (thunder *Thunder) RegisterWait(duration time.Duration) error {
thunder.channel <- Command { action: WAIT, duration: duration, }
return nil
}
// to be launched as a goroutine
func (thunder *Thunder) Run() error {
log.Printf("Run()")
command := <- thunder.channel
log.Printf("got command %s", command)
switch(command.action) {
case FIRE:
thunder.current_action = thunder.current_action | MOTOR_FIRE
thunder.Control(thunder.current_action)
grabMjpegFrame()
case RELOAD:
thunder.current_action = thunder.current_action & (0xFF - MOTOR_FIRE)
thunder.Control(thunder.current_action)
case STOP:
thunder.current_action = MOTOR_STOP
thunder.Control(thunder.current_action)
thunder.current_action = 0x00
thunder.Control(thunder.current_action)
case DIR_UP:
thunder.current_action = (thunder.current_action & (0xFF - MOTOR_DOWN)) | MOTOR_UP
thunder.Control(thunder.current_action)
case DIR_DOWN:
thunder.current_action = (thunder.current_action & (0xFF - MOTOR_UP)) | MOTOR_DOWN
thunder.Control(thunder.current_action)
case DIR_LEFT:
thunder.current_action = (thunder.current_action & (0xFF - MOTOR_RIGHT)) | MOTOR_LEFT
thunder.Control(thunder.current_action)
case DIR_RIGHT:
thunder.current_action = (thunder.current_action & (0xFF - MOTOR_LEFT)) | MOTOR_RIGHT
thunder.Control(thunder.current_action)
case WAIT:
log.Printf("wait for %s", command.duration)
time.Sleep(command.duration)
log.Printf("awake")
}
return thunder.Run()
}
func (c *Thunder) ResetPosition() {
c.mutex.Lock()
defer c.mutex.Unlock()
c.RegisterMove(DIR_DOWN)
c.RegisterMove(DIR_LEFT)
c.RegisterWait(time.Duration(8)*time.Second)
c.RegisterStop()
c.dirty = false
c.current_x = 0
c.current_y = 0
}
func (c *Thunder) SetPosition(x int, y int, fire bool) {
c.mutex.Lock()
defer c.mutex.Unlock()
// Check precision
if c.moves >= 5 {
c.dirty = true
c.moves = 0
}
// Check we did not move (c) John Drummond
if c.dirty {
c.RegisterMove(DIR_DOWN)
c.RegisterMove(DIR_LEFT)
c.RegisterWait(time.Duration(8)*time.Second)
c.RegisterStop()
c.dirty = false
c.current_x = 0
c.current_y = 0
}
delta_x := x - c.current_x
c.current_x = x
log.Printf("will delta x: %d", delta_x)
if delta_x > 0 {
wait := X_TIME / X_POSITIONS * delta_x
c.RegisterMove(DIR_RIGHT)
c.RegisterWait(time.Duration(wait)*time.Millisecond)
}
if delta_x < 0 {
wait := X_TIME / X_POSITIONS * delta_x * -1
c.RegisterMove(DIR_LEFT)
c.RegisterWait(time.Duration(wait)*time.Millisecond)
}
c.RegisterStop()
delta_y := y - c.current_y
c.current_y = y
log.Printf("will delta y: %d", delta_y)
if delta_y > 0 {
wait := Y_TIME / Y_POSITIONS * delta_y
c.RegisterMove(DIR_UP)
c.RegisterWait(time.Duration(wait)*time.Millisecond)
}
if delta_y < 0 {
wait := Y_TIME / Y_POSITIONS * delta_y * -1
c.RegisterMove(DIR_DOWN)
c.RegisterWait(time.Duration(wait)*time.Millisecond)
}
c.RegisterStop()
if fire {
c.RegisterFire()
c.RegisterWait(time.Duration(5000)*time.Millisecond)
c.RegisterReload()
}
c.moves = c.moves + 1
}
func (thunder *Thunder) Put(action Action) error {
thunder.mutex.Lock()
defer thunder.mutex.Unlock()
thunder.RegisterMove(action)
thunder.dirty = true
return nil
}
func (thunder *Thunder) Fire() error {
thunder.mutex.Lock()
defer thunder.mutex.Unlock()
thunder.RegisterFire()
thunder.RegisterWait(time.Duration(5000)*time.Millisecond)
thunder.RegisterReload()
return nil
}
func (thunder *Thunder) Delete(action Action) error {
thunder.mutex.Lock()
defer thunder.mutex.Unlock()
thunder.current_action = (thunder.current_action & (0xFF - (byte)(action)))
thunder.Control(thunder.current_action)
thunder.dirty = true
return nil
}
func grabMjpegFrame() {
shotImg := "lastShot.jpg"
hostWithPort := "10.0.25.113:8081"
scriptName := "grab_mjpeg_frame.py"
cmd := exec.Command("python", scriptName, hostWithPort, shotImg)
err := cmd.Start()
if err != nil {
log.Printf("2")
log.Fatal(err)
}
err = cmd.Wait()
if err != nil {
log.Printf("Error while calling the script. Error: %v\n", err)
} else {
log.Printf("Image successfully created.")
}
}