-
Notifications
You must be signed in to change notification settings - Fork 110
/
builtin.go
583 lines (509 loc) · 15.9 KB
/
builtin.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
// Package builtin implements a remote control for a base.
package builtin
import (
"context"
"math"
"sync"
"sync/atomic"
"time"
"github.com/edaniels/golog"
"github.com/golang/geo/r3"
"github.com/pkg/errors"
vutils "go.viam.com/utils"
"go.viam.com/rdk/components/base"
"go.viam.com/rdk/components/input"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/services/baseremotecontrol"
"go.viam.com/rdk/session"
)
// Constants for the system including the max speed and angle (TBD: allow to be set as config vars)
// as well as the various control modes including oneJoystick (control via a joystick), triggerSpeed
// (triggers control speed and joystick angle), button (four buttons X, Y, A, B to control speed and
// angle) and arrow (arrows buttons used to control speed and angle).
const (
joyStickControl = controlMode(iota)
triggerSpeedControl
buttonControl
arrowControl
droneControl
)
func init() {
resource.RegisterService(baseremotecontrol.API, resource.DefaultServiceModel, resource.Registration[baseremotecontrol.Service, *Config]{
Constructor: NewBuiltIn,
})
}
// ControlMode is the control type for the remote control.
type controlMode uint8
// Config describes how to configure the service.
type Config struct {
BaseName string `json:"base"`
InputControllerName string `json:"input_controller"`
ControlModeName string `json:"control_mode,omitempty"`
MaxAngularVelocity float64 `json:"max_angular_deg_per_sec,omitempty"`
MaxLinearVelocity float64 `json:"max_linear_mm_per_sec,omitempty"`
}
// Validate creates the list of implicit dependencies.
func (conf *Config) Validate(path string) ([]string, error) {
var deps []string
if conf.InputControllerName == "" {
return nil, vutils.NewConfigValidationFieldRequiredError(path, "input_controller")
}
deps = append(deps, conf.InputControllerName)
if conf.BaseName == "" {
return nil, vutils.NewConfigValidationFieldRequiredError(path, "base")
}
deps = append(deps, conf.BaseName)
return deps, nil
}
// builtIn is the structure of the remote service.
type builtIn struct {
resource.Named
mu sync.RWMutex
base base.Base
inputController input.Controller
controlMode controlMode
config *Config
state throttleState
logger golog.Logger
cancel func()
cancelCtx context.Context
activeBackgroundWorkers sync.WaitGroup
events chan (struct{})
instance atomic.Int64
}
// NewBuiltIn returns a new remote control service for the given robot.
func NewBuiltIn(
ctx context.Context,
deps resource.Dependencies,
conf resource.Config,
logger golog.Logger,
) (baseremotecontrol.Service, error) {
cancelCtx, cancel := context.WithCancel(context.Background())
remoteSvc := &builtIn{
Named: conf.ResourceName().AsNamed(),
logger: logger,
cancelCtx: cancelCtx,
cancel: cancel,
events: make(chan struct{}, 1),
}
remoteSvc.state.init()
if err := remoteSvc.Reconfigure(ctx, deps, conf); err != nil {
return nil, err
}
remoteSvc.eventProcessor()
return remoteSvc, nil
}
func (svc *builtIn) Reconfigure(
ctx context.Context,
deps resource.Dependencies,
conf resource.Config,
) error {
svcConfig, err := resource.NativeConfig[*Config](conf)
if err != nil {
return err
}
base1, err := base.FromDependencies(deps, svcConfig.BaseName)
if err != nil {
return err
}
controller, err := input.FromDependencies(deps, svcConfig.InputControllerName)
if err != nil {
return err
}
var controlMode1 controlMode
switch svcConfig.ControlModeName {
case "triggerSpeedControl":
controlMode1 = triggerSpeedControl
case "buttonControl":
controlMode1 = buttonControl
case "joystickControl":
controlMode1 = joyStickControl
case "droneControl":
controlMode1 = droneControl
default:
controlMode1 = arrowControl
}
svc.mu.Lock()
svc.base = base1
svc.inputController = controller
svc.controlMode = controlMode1
svc.config = svcConfig
svc.mu.Unlock()
svc.instance.Add(1)
if err := svc.registerCallbacks(ctx, &svc.state); err != nil {
return errors.Errorf("error with starting remote control service: %q", err)
}
return nil
}
// registerCallbacks registers events from controller to base.
func (svc *builtIn) registerCallbacks(ctx context.Context, state *throttleState) error {
var lastTS time.Time
lastTSPerEvent := map[input.Control]map[input.EventType]time.Time{}
var onlyOneAtATime sync.Mutex
instance := svc.instance.Load()
updateLastEvent := func(event input.Event) bool {
if event.Time.After(lastTS) {
lastTS = event.Time
}
if event.Time.Before(lastTSPerEvent[event.Control][event.Event]) {
return false
}
lastTSPerEventControl := lastTSPerEvent[event.Control]
if lastTSPerEventControl == nil {
lastTSPerEventControl = map[input.EventType]time.Time{}
lastTSPerEvent[event.Control] = lastTSPerEventControl
}
lastTSPerEventControl[event.Event] = event.Time
return true
}
remoteCtl := func(ctx context.Context, event input.Event) {
onlyOneAtATime.Lock()
defer onlyOneAtATime.Unlock()
if svc.instance.Load() != instance {
return
}
if svc.cancelCtx.Err() != nil {
return
}
if !updateLastEvent(event) {
return
}
svc.processEvent(ctx, state, event)
}
connect := func(ctx context.Context, event input.Event) {
onlyOneAtATime.Lock()
defer onlyOneAtATime.Unlock()
if svc.instance.Load() != instance {
return
}
// Connect and Disconnect events should both stop the base completely.
svc.mu.RLock()
defer svc.mu.RUnlock()
err := svc.base.Stop(ctx, map[string]interface{}{})
if err != nil {
svc.logger.Error(err)
}
if !updateLastEvent(event) {
return
}
}
for _, control := range svc.ControllerInputs() {
if err := func() error {
svc.mu.RLock()
defer svc.mu.RUnlock()
var err error
if svc.controlMode == buttonControl {
err = svc.inputController.RegisterControlCallback(
ctx,
control,
[]input.EventType{input.ButtonChange},
remoteCtl,
map[string]interface{}{},
)
} else {
err = svc.inputController.RegisterControlCallback(ctx,
control,
[]input.EventType{input.PositionChangeAbs},
remoteCtl,
map[string]interface{}{},
)
}
if err != nil {
return err
}
err = svc.inputController.RegisterControlCallback(ctx,
control,
[]input.EventType{input.Connect, input.Disconnect},
connect,
map[string]interface{}{},
)
if err != nil {
return err
}
return nil
}(); err != nil {
return err
}
}
return nil
}
// Close out of all remote control related systems.
func (svc *builtIn) Close(_ context.Context) error {
svc.cancel()
svc.activeBackgroundWorkers.Wait()
return nil
}
// ControllerInputs returns the list of inputs from the controller that are being monitored for that control mode.
func (svc *builtIn) ControllerInputs() []input.Control {
svc.mu.RLock()
defer svc.mu.RUnlock()
switch svc.controlMode {
case triggerSpeedControl:
return []input.Control{input.AbsoluteX, input.AbsoluteZ, input.AbsoluteRZ}
case arrowControl:
return []input.Control{input.AbsoluteHat0X, input.AbsoluteHat0Y}
case buttonControl:
return []input.Control{input.ButtonNorth, input.ButtonSouth, input.ButtonEast, input.ButtonWest}
case joyStickControl:
return []input.Control{input.AbsoluteX, input.AbsoluteY}
case droneControl:
return []input.Control{input.AbsoluteX, input.AbsoluteY, input.AbsoluteRX, input.AbsoluteRY}
}
return []input.Control{}
}
func (svc *builtIn) eventProcessor() {
var currentLinear, currentAngular r3.Vector
var nextLinear, nextAngular r3.Vector
var inRetry bool
svc.activeBackgroundWorkers.Add(1)
vutils.ManagedGo(func() {
for {
if svc.cancelCtx.Err() != nil {
return
}
if inRetry {
select {
case <-svc.cancelCtx.Done():
case <-svc.events:
default:
}
} else {
select {
case <-svc.cancelCtx.Done():
case <-svc.events:
}
}
svc.state.mu.Lock()
nextLinear, nextAngular = svc.state.linearThrottle, svc.state.angularThrottle
svc.state.mu.Unlock()
if func() bool {
svc.mu.RLock()
defer svc.mu.RUnlock()
if currentLinear != nextLinear || currentAngular != nextAngular {
if svc.config.MaxAngularVelocity > 0 && svc.config.MaxLinearVelocity > 0 {
if err := svc.base.SetVelocity(
svc.cancelCtx,
r3.Vector{
X: svc.config.MaxLinearVelocity * nextLinear.X,
Y: svc.config.MaxLinearVelocity * nextLinear.Y,
Z: svc.config.MaxLinearVelocity * nextLinear.Z,
},
r3.Vector{
X: svc.config.MaxAngularVelocity * nextAngular.X,
Y: svc.config.MaxAngularVelocity * nextAngular.Y,
Z: svc.config.MaxAngularVelocity * nextAngular.Z,
},
nil,
); err != nil {
svc.logger.Errorw("error setting velocity", "error", err)
if !vutils.SelectContextOrWait(svc.cancelCtx, 10*time.Millisecond) {
return true
}
inRetry = true
return false
}
} else {
if err := svc.base.SetPower(svc.cancelCtx, nextLinear, nextAngular, nil); err != nil {
svc.logger.Errorw("error setting power", "error", err)
if !vutils.SelectContextOrWait(svc.cancelCtx, 10*time.Millisecond) {
return true
}
inRetry = true
return false
}
}
inRetry = false
currentLinear = nextLinear
currentAngular = nextAngular
}
return false
}() {
return
}
}
}, svc.activeBackgroundWorkers.Done)
}
func (svc *builtIn) processEvent(ctx context.Context, state *throttleState, event input.Event) {
// Order of who processes what event is *not* guaranteed. It depends on the mutex
// fairness mode. Ordering logic must be handled at a higher level in the robot.
// Other than that, values overwrite each other.
state.mu.Lock()
oldLinear := state.linearThrottle
oldAngular := state.angularThrottle
newLinear := oldLinear
newAngular := oldAngular
svc.mu.RLock()
defer svc.mu.RUnlock()
switch svc.controlMode {
case joyStickControl:
newLinear.Y, newAngular.Z = oneJoyStickEvent(event, state.linearThrottle.Y, state.angularThrottle.Z)
case droneControl:
newLinear, newAngular = droneEvent(event, state.linearThrottle, state.angularThrottle)
case triggerSpeedControl:
newLinear.Y, newAngular.Z = triggerSpeedEvent(event, state.linearThrottle.Y, state.angularThrottle.Z)
case buttonControl:
newLinear.Y, newAngular.Z, state.buttons = buttonControlEvent(event, state.buttons)
case arrowControl:
newLinear.Y, newAngular.Z, state.arrows = arrowEvent(event, state.arrows)
}
state.linearThrottle = newLinear
state.angularThrottle = newAngular
state.mu.Unlock()
if similar(newLinear, oldLinear, .05) && similar(newAngular, oldAngular, .05) {
return
}
// If we do not manage to send the event, that means the processor
// is working and it is about to see our state change anyway. This
// actls like a condition variable signal.
select {
case <-ctx.Done():
case svc.events <- struct{}{}:
default:
}
session.SafetyMonitor(ctx, svc.base)
}
// triggerSpeedEvent takes inputs from the gamepad allowing the triggers to control speed and the left joystick to
// control the angle.
func triggerSpeedEvent(event input.Event, speed, angle float64) (float64, float64) {
switch event.Control {
case input.AbsoluteZ:
speed -= 0.05
speed = math.Max(-1, speed)
case input.AbsoluteRZ:
speed += 0.05
speed = math.Min(1, speed)
case input.AbsoluteX:
angle = event.Value
case input.AbsoluteHat0X, input.AbsoluteHat0Y, input.AbsoluteRX, input.AbsoluteRY, input.AbsoluteY,
input.ButtonEStop, input.ButtonEast, input.ButtonLT, input.ButtonLT2, input.ButtonLThumb, input.ButtonMenu,
input.ButtonNorth, input.ButtonRT, input.ButtonRT2, input.ButtonRThumb, input.ButtonRecord,
input.ButtonSelect, input.ButtonSouth, input.ButtonStart, input.ButtonWest, input.AbsolutePedalAccelerator,
input.AbsolutePedalBrake, input.AbsolutePedalClutch:
fallthrough
default:
}
return speed, angle
}
// buttonControlEvent takes inputs from the gamepad allowing the X and B buttons to control speed and Y and A buttons to control angle.
func buttonControlEvent(event input.Event, buttons map[input.Control]bool) (float64, float64, map[input.Control]bool) {
var speed float64
var angle float64
switch event.Event {
case input.ButtonPress:
buttons[event.Control] = true
case input.ButtonRelease:
buttons[event.Control] = false
case input.AllEvents, input.ButtonChange, input.ButtonHold, input.Connect, input.Disconnect,
input.PositionChangeAbs, input.PositionChangeRel:
fallthrough
default:
}
if buttons[input.ButtonNorth] == buttons[input.ButtonSouth] {
speed = 0.0
} else {
if buttons[input.ButtonNorth] {
speed = 1.0
} else {
speed = -1.0
}
}
if buttons[input.ButtonEast] == buttons[input.ButtonWest] {
angle = 0.0
} else {
if buttons[input.ButtonEast] {
angle = -1.0
} else {
angle = 1.0
}
}
return speed, angle, buttons
}
// arrowControlEvent takes inputs from the gamepad allowing the arrow buttons to control speed and angle.
func arrowEvent(event input.Event, arrows map[input.Control]float64) (float64, float64, map[input.Control]float64) {
arrows[event.Control] = -1.0 * event.Value
speed := arrows[input.AbsoluteHat0Y]
angle := arrows[input.AbsoluteHat0X]
return speed, angle, arrows
}
// oneJoyStickEvent (default) takes inputs from the gamepad allowing the left joystick to control speed and angle.
func oneJoyStickEvent(event input.Event, y, x float64) (float64, float64) {
switch event.Control {
case input.AbsoluteY:
y = -1.0 * event.Value
case input.AbsoluteX:
x = -1.0 * event.Value
case input.AbsoluteHat0X, input.AbsoluteHat0Y, input.AbsoluteRX, input.AbsoluteRY, input.AbsoluteRZ,
input.AbsoluteZ, input.ButtonEStop, input.ButtonEast, input.ButtonLT, input.ButtonLT2, input.ButtonLThumb,
input.ButtonMenu, input.ButtonNorth, input.ButtonRT, input.ButtonRT2, input.ButtonRThumb,
input.ButtonRecord, input.ButtonSelect, input.ButtonSouth, input.ButtonStart, input.ButtonWest, input.AbsolutePedalAccelerator,
input.AbsolutePedalBrake, input.AbsolutePedalClutch:
fallthrough
default:
}
return scaleThrottle(y), scaleThrottle(x)
}
// right joystick is forward/back, strafe right/left
// left joystick is spin right/left & up/down.
func droneEvent(event input.Event, linear, angular r3.Vector) (r3.Vector, r3.Vector) {
switch event.Control {
case input.AbsoluteX:
angular.Z = scaleThrottle(-1.0 * event.Value)
case input.AbsoluteY:
linear.Z = scaleThrottle(-1.0 * event.Value)
case input.AbsoluteRX:
linear.X = scaleThrottle(event.Value)
case input.AbsoluteRY:
linear.Y = scaleThrottle(-1.0 * event.Value)
case input.AbsoluteHat0X, input.AbsoluteHat0Y, input.AbsoluteRZ, input.AbsoluteZ, input.ButtonEStop,
input.ButtonEast, input.ButtonLT, input.ButtonLT2, input.ButtonLThumb, input.ButtonMenu, input.ButtonNorth,
input.ButtonRT, input.ButtonRT2, input.ButtonRThumb, input.ButtonRecord, input.ButtonSelect,
input.ButtonSouth, input.ButtonStart, input.ButtonWest, input.AbsolutePedalAccelerator,
input.AbsolutePedalBrake, input.AbsolutePedalClutch:
fallthrough
default:
}
return linear, angular
}
func similar(a, b r3.Vector, deltaThreshold float64) bool {
if math.Abs(a.X-b.X) > deltaThreshold {
return false
}
if math.Abs(a.Y-b.Y) > deltaThreshold {
return false
}
if math.Abs(a.Z-b.Z) > deltaThreshold {
return false
}
return true
}
func scaleThrottle(a float64) float64 {
//nolint:ifshort
neg := a < 0
a = math.Abs(a)
if a <= .27 {
return 0
}
a = math.Ceil(a*10) / 10.0
if neg {
a *= -1
}
return a
}
type throttleState struct {
mu sync.Mutex
linearThrottle, angularThrottle r3.Vector
buttons map[input.Control]bool
arrows map[input.Control]float64
}
func (ts *throttleState) init() {
ts.buttons = map[input.Control]bool{
input.ButtonNorth: false,
input.ButtonSouth: false,
input.ButtonEast: false,
input.ButtonWest: false,
}
ts.arrows = map[input.Control]float64{
input.AbsoluteHat0X: 0.0,
input.AbsoluteHat0Y: 0.0,
}
}