-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactionsC2D.go
370 lines (341 loc) · 12.7 KB
/
actionsC2D.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package parrotbebop
import (
"context"
"log"
"github.com/eiannone/keyboard"
)
type inputAction int
const (
// Standard actions.
//
ActionPcmdFlag inputAction = iota
ActionPcmdRollLeft inputAction = iota
ActionPcmdRollRight inputAction = iota
ActionPcmdPitchForward inputAction = iota
ActionPcmdPitchBackward inputAction = iota
ActionPcmdYawClockwise inputAction = iota
ActionPcmdYawCounterClockwise inputAction = iota
ActionPcmdHover inputAction = iota
ActionPcmdGazInc inputAction = iota
ActionPcmdGazDec inputAction = iota
ActionPcmdRepeatLastCmd inputAction = iota
ActionTakeoff inputAction = iota
ActionLanding inputAction = iota
ActionEmergency inputAction = iota
ActionNavigateHomeStart inputAction = iota // Check how to implement it in xml line 153
ActionNavigateHomeStop inputAction = iota // Check how to implement it in xml line 153
ActionMoveBy inputAction = iota // Check how to implement it in xml line 181
ActionUserTakeoff inputAction = iota
ActionMoveTo inputAction = iota // Check how to implement it in xml line 259
ActionCancelMoveTo inputAction = iota
ActionStartPilotedPOI inputAction = iota
ActionStopPilotedPOI inputAction = iota
ActionCancelMoveBy inputAction = iota
ActionMoveToSetLatInc inputAction = iota // Direction North
ActionMoveToSetLatDec inputAction = iota // Direction South
ActionMoveToSetLonInc inputAction = iota // Direction East
ActionMoveToSetLonDec inputAction = iota // Direction West
ActionMoveToExecute inputAction = iota // Execute moveTo next waypoint
ActionMoveToCancel inputAction = iota // Cancel all moveTo operation
ActionMoveToSetBufferCurrentPosition inputAction = iota // Set buffer to current position
// Custom actions.
//
ActionHow inputAction = iota
// Flattrim should be performed before a takeoff
// to calibrate the drone.
ActionFlatTrim inputAction = iota
// TODO: Also check out the <class name="PilotingSettings" id="2">"
// starting at line 1400 in the ardrone3.xml document, for more
// commands to eventually implement.
)
// readKeyBoardEvent will read keys pressed on the keyboard,
// and pass on the correct action to be executed.
//
// TODO: Make more source to create inputActions than keyboard...
// Geofencing ?
// Map route ?
func (d *Drone) readKeyBoardEvent() {
keysEvents, err := keyboard.GetKeys(10)
if err != nil {
panic(err)
}
defer func() {
err := keyboard.Close()
if err != nil {
log.Printf("error: failed to close keyboard: %v\n", err)
}
}()
// Since we are resetting the the go routines and thus are stopping
// the listener of the input action channel we need to check if it
// is a listener available to avoid deadlock.
// checkChOpen is a little helper function for just that.
//
// NB: This function will drop input actions given if the channel is
// closed. A benefit of doing just that is that we avoid any commands
// that might have been given while the connection was gone to
// suddenly be executed when the connection comes back, but this also
// implies that we have mechanism's in place to handle continous
// flight of the drone incase there is a drop, or the connection have
// to be re-established for some reason
checkChOpen := func(ch chan inputAction, ia inputAction) {
select {
case ch <- ia:
default:
}
}
for {
select {
case event := <-keysEvents:
if event.Err != nil {
panic(event.Err)
}
switch {
case event.Key == keyboard.KeyEsc:
d.chQuit <- struct{}{}
case event.Rune == 'q':
// Initiate a reconnect of the network.
select {
case d.chNetworkConnect <- struct{}{}:
default:
}
case event.Rune == 't':
checkChOpen(d.chInputActions, ActionTakeoff)
case event.Rune == 'l':
checkChOpen(d.chInputActions, ActionLanding)
case event.Rune == 'r':
checkChOpen(d.chInputActions, ActionNavigateHomeStart)
case event.Rune == 'R':
checkChOpen(d.chInputActions, ActionNavigateHomeStop)
case event.Rune == 'w':
checkChOpen(d.chInputActions, ActionPcmdGazInc)
case event.Rune == 's':
checkChOpen(d.chInputActions, ActionPcmdGazDec)
case event.Rune == 'a':
checkChOpen(d.chInputActions, ActionPcmdYawCounterClockwise)
case event.Rune == 'd':
checkChOpen(d.chInputActions, ActionPcmdYawClockwise)
case event.Key == keyboard.KeyArrowUp:
checkChOpen(d.chInputActions, ActionPcmdPitchForward)
case event.Key == keyboard.KeyArrowDown:
checkChOpen(d.chInputActions, ActionPcmdPitchBackward)
case event.Key == keyboard.KeyArrowLeft:
checkChOpen(d.chInputActions, ActionPcmdRollLeft)
case event.Key == keyboard.KeyArrowRight:
checkChOpen(d.chInputActions, ActionPcmdRollRight)
case event.Key == keyboard.KeySpace:
checkChOpen(d.chInputActions, ActionPcmdRepeatLastCmd)
case event.Key == keyboard.KeyCtrlW:
checkChOpen(d.chInputActions, ActionMoveToSetLatInc)
case event.Key == keyboard.KeyCtrlS:
checkChOpen(d.chInputActions, ActionMoveToSetLatDec)
case event.Key == keyboard.KeyCtrlA:
checkChOpen(d.chInputActions, ActionMoveToSetLonDec)
case event.Key == keyboard.KeyCtrlD:
checkChOpen(d.chInputActions, ActionMoveToSetLonInc)
case event.Key == keyboard.KeyCtrlX:
checkChOpen(d.chInputActions, ActionMoveToSetBufferCurrentPosition)
case event.Key == keyboard.KeyCtrlSpace:
checkChOpen(d.chInputActions, ActionMoveToExecute)
case event.Key == keyboard.KeyCtrlQ:
checkChOpen(d.chInputActions, ActionMoveToCancel)
case event.Rune == 'h':
checkChOpen(d.chInputActions, ActionPcmdHover)
}
}
}
}
// handleInputAction is where we specify what package to send to the drone
// based on what action came out of the readKeyboardEvent method.
//
// The reason we have this function and don't encode the packets directly
// in readKeyBoardEvent, is that we might want to have other input methods
// then the keyboard to control the drone.
// This function will execute the commands that arrives on the d.chInputActions.
func (d *Drone) handleInputAction(packetCreator udpPacketCreator, ctx context.Context) {
for {
select {
case <-ctx.Done():
log.Println("info: exiting handleInputAction")
return
case action := <-d.chInputActions:
// --------------Standard actions
switch action {
case ActionTakeoff:
p := packetCreator.encodeCmd(Command(PilotingTakeOff), &Ardrone3PilotingTakeOffArguments{})
d.chSendingUDPPacket <- p
case ActionLanding:
p := packetCreator.encodeCmd(Command(PilotingLanding), &Ardrone3PilotingLandingArguments{})
d.chSendingUDPPacket <- p
case ActionNavigateHomeStart:
p := packetCreator.encodeCmd(Command(PilotingNavigateHome), &Ardrone3PilotingNavigateHomeArguments{Start: 1})
d.chSendingUDPPacket <- p
case ActionNavigateHomeStop:
p := packetCreator.encodeCmd(Command(PilotingNavigateHome), &Ardrone3PilotingNavigateHomeArguments{Start: 0})
d.chSendingUDPPacket <- p
// --------------emulation of rc-controller sticks
// using a,w,s,d and arrow keys.
case ActionPcmdGazInc:
if d.pcmd.Gaz < 0 {
d.pcmd.Gaz = 0
}
d.pcmd.Flag = 1
d.pcmd.Gaz++
d.pcmd.Gaz = d.CheckLimitPcmdField(d.pcmd.Gaz)
arg := &Ardrone3PilotingPCMDArguments{
Flag: 1,
Gaz: d.pcmd.Gaz,
}
d.chPcmdPacketScheduler <- packetCreator.encodeCmd(Command(PilotingPCMD), arg)
case ActionPcmdGazDec:
if d.pcmd.Gaz > 0 {
d.pcmd.Gaz = 0
}
d.pcmd.Flag = 1
d.pcmd.Gaz--
d.pcmd.Gaz = d.CheckLimitPcmdField(d.pcmd.Gaz)
arg := &Ardrone3PilotingPCMDArguments{
Flag: 1,
Gaz: d.pcmd.Gaz,
}
d.chPcmdPacketScheduler <- packetCreator.encodeCmd(Command(PilotingPCMD), arg)
case ActionPcmdYawCounterClockwise:
if d.pcmd.Yaw > 0 {
d.pcmd.Yaw = 0
}
d.pcmd.Flag = 1
d.pcmd.Yaw--
d.pcmd.Yaw = d.CheckLimitPcmdField(d.pcmd.Yaw)
arg := &Ardrone3PilotingPCMDArguments{
Flag: 1,
Yaw: d.pcmd.Yaw,
}
d.chPcmdPacketScheduler <- packetCreator.encodeCmd(Command(PilotingPCMD), arg)
case ActionPcmdYawClockwise:
if d.pcmd.Yaw < 0 {
d.pcmd.Yaw = 0
}
d.pcmd.Flag = 1
d.pcmd.Yaw++
d.pcmd.Yaw = d.CheckLimitPcmdField(d.pcmd.Yaw)
arg := &Ardrone3PilotingPCMDArguments{
Flag: 1,
Yaw: d.pcmd.Yaw,
}
d.chPcmdPacketScheduler <- packetCreator.encodeCmd(Command(PilotingPCMD), arg)
case ActionPcmdHover:
d.pcmd = Ardrone3PilotingPCMDArguments{
Flag: 0, // TODO: maybe set this one to ZERO ?
Gaz: 0,
Pitch: 0,
Roll: 0,
TimestampAndSeqNum: 0,
Yaw: 0,
}
arg := d.pcmd
d.chPcmdPacketScheduler <- packetCreator.encodeCmd(Command(PilotingPCMD), arg)
case ActionPcmdPitchForward:
if d.pcmd.Pitch < 0 {
d.pcmd.Pitch = 0
}
d.pcmd.Flag = 1
d.pcmd.Pitch++
d.pcmd.Pitch = d.CheckLimitPcmdField(d.pcmd.Pitch)
arg := &Ardrone3PilotingPCMDArguments{
Flag: 1,
Pitch: d.pcmd.Pitch,
}
d.chPcmdPacketScheduler <- packetCreator.encodeCmd(Command(PilotingPCMD), arg)
case ActionPcmdPitchBackward:
if d.pcmd.Pitch > 0 {
d.pcmd.Pitch = 0
}
d.pcmd.Flag = 1
d.pcmd.Pitch--
d.pcmd.Pitch = d.CheckLimitPcmdField(d.pcmd.Pitch)
arg := &Ardrone3PilotingPCMDArguments{
Flag: 1,
Pitch: d.pcmd.Pitch,
}
d.chPcmdPacketScheduler <- packetCreator.encodeCmd(Command(PilotingPCMD), arg)
case ActionPcmdRollLeft:
if d.pcmd.Roll > 0 {
d.pcmd.Roll = 0
}
d.pcmd.Flag = 1
d.pcmd.Roll--
d.pcmd.Roll = d.CheckLimitPcmdField(d.pcmd.Roll)
arg := &Ardrone3PilotingPCMDArguments{
Flag: 1,
Roll: d.pcmd.Roll,
}
d.chPcmdPacketScheduler <- packetCreator.encodeCmd(Command(PilotingPCMD), arg)
case ActionPcmdRollRight:
if d.pcmd.Roll < 0 {
d.pcmd.Roll = 0
}
d.pcmd.Flag = 1
d.pcmd.Roll--
d.pcmd.Roll = d.CheckLimitPcmdField(d.pcmd.Roll)
arg := &Ardrone3PilotingPCMDArguments{
Flag: 1,
Roll: d.pcmd.Roll,
}
d.chPcmdPacketScheduler <- packetCreator.encodeCmd(Command(PilotingPCMD), arg)
case ActionPcmdRepeatLastCmd:
d.chPcmdPacketScheduler <- packetCreator.encodeCmd(Command(PilotingPCMD), d.pcmd)
// --------------moveTo
// The commands below is a bit overly complicated to use, but they
// are implemented to manually be able to test out the moveTo feature.
case ActionMoveToSetLatInc:
if d.gps.latitudeMoveTo != 500 {
d.gps.latitudeMoveTo = d.gps.latitudeMoveTo + 0.00001
log.Printf("moveTo: %#v\n", d.gps)
} else {
log.Printf("ActionMoveToLatInc: failed, no connection with GPS: %v\n", d.gps.latitude)
}
case ActionMoveToSetLatDec:
if d.gps.latitudeMoveTo != 500 {
d.gps.latitudeMoveTo = d.gps.latitudeMoveTo - 0.00001
log.Printf("moveTo: %#v\n", d.gps)
} else {
log.Printf("ActionMoveToLatDec: failed, no connection with GPS: %v\n", d.gps.latitude)
}
case ActionMoveToSetLonDec:
if d.gps.longitudeMoveTo != 500 {
d.gps.latitudeMoveTo = d.gps.latitudeMoveTo - 0.00001
log.Printf("moveTo: %#v\n", d.gps)
} else {
log.Printf("ActionMoveToLatDec: failed, no connection with GPS: %v\n", d.gps.latitude)
}
case ActionMoveToSetLonInc:
if d.gps.longitudeMoveTo != 500 {
d.gps.latitudeMoveTo = d.gps.latitudeMoveTo + 0.00001
log.Printf("moveTo: %#v\n", d.gps)
} else {
log.Printf("ActionMoveToLatInc: failed, no connection with GPS: %v\n", d.gps.latitude)
}
case ActionMoveToSetBufferCurrentPosition:
if d.gps.latitude != 500 || d.gps.longitude != 500 {
d.gps.latitudeMoveTo = d.gps.latitude
d.gps.longitudeMoveTo = d.gps.longitude
} else {
log.Printf("ActionMoveToSetBufferCurrentPosition: failed, no connection with GPS: %v\n", d.gps.latitude)
}
case ActionMoveToExecute:
// TODO:
// The idea here is to use this action with a moveTo command to the drone,
// and giving the current moveTo variables as arguments to the moveTo
// command.
d.gps.doingMoveTo = true
d.gps.chMoveToExecute <- struct{}{}
// TODO: send the moveTo command here!!!
log.Printf("*************************************************************\n")
log.Printf("ActionMoveToExecute: current value of buffer: %#v\n", d.gps)
log.Printf("*************************************************************\n")
case ActionMoveToCancel:
d.gps.doingMoveTo = false
d.gps.chMoveToCancel <- struct{}{}
}
}
}
}