-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.go
352 lines (284 loc) · 8.12 KB
/
base.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
package viamboatbase
import (
"context"
"errors"
"math"
"sync"
"time"
"github.com/edaniels/golog"
"github.com/golang/geo/r3"
"go.uber.org/multierr"
"go.viam.com/utils"
"go.viam.com/rdk/components/base"
"go.viam.com/rdk/components/motor"
"go.viam.com/rdk/components/movementsensor"
"go.viam.com/rdk/operation"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/spatialmath"
rdkutils "go.viam.com/rdk/utils"
)
var Model = resource.ModelNamespace("erh").WithFamily("base").WithModel("boat")
const pidLoopTime = time.Millisecond * 500
func init() {
boatComp := resource.Registration[base.Base, *Config]{
Constructor: func(
ctx context.Context, deps resource.Dependencies, conf resource.Config, logger golog.Logger,
) (base.Base, error) {
return createBoat(deps, conf, logger)
},
}
resource.RegisterComponent(base.API, Model, boatComp)
}
func createBoat(deps resource.Dependencies, conf resource.Config, logger golog.Logger) (base.LocalBase, error) {
newConf, err := resource.NativeConfig[*Config](conf)
if err != nil {
return nil, err
}
theBoat := &boat{
Named: conf.ResourceName().AsNamed(),
cfg: newConf,
logger: logger,
}
theBoat.state.angularPID.setDefaults()
theBoat.state.linearPID.setDefaults()
for _, mc := range newConf.Motors {
m, err := motor.FromDependencies(deps, mc.Name)
if err != nil {
return nil, err
}
theBoat.motors = append(theBoat.motors, m)
}
if newConf.MovementSensor != "" {
var err error
theBoat.movementSensor, err = movementsensor.FromDependencies(deps, newConf.MovementSensor)
if err != nil {
return nil, err
}
}
return theBoat, nil
}
type controlMode int
const (
controlNone controlMode = 0
controlVelocity = 1
controlHeading = 2
)
type boatState struct {
threadStarted bool
controlState controlMode
angularPID, linearPID pidState
velocityLinearGoal, velocityAngularGoal r3.Vector
compassGoal float64
spinVelocity float64
}
type boat struct {
resource.Named
resource.AlwaysRebuild
cfg *Config
motors []motor.Motor
movementSensor movementsensor.MovementSensor
opMgr operation.SingleOperationManager
state boatState
stateMutex sync.Mutex
cancel context.CancelFunc
waitGroup sync.WaitGroup
logger golog.Logger
}
func (b *boat) MoveStraight(ctx context.Context, distanceMm int, mmPerSec float64, extra map[string]interface{}) error {
if distanceMm < 0 {
mmPerSec *= -1
distanceMm *= -1
}
err := b.SetVelocity(ctx, r3.Vector{Y: mmPerSec}, r3.Vector{}, extra)
if err != nil {
return err
}
s := time.Duration(float64(time.Millisecond) * math.Abs(float64(distanceMm)))
utils.SelectContextOrWait(ctx, s)
return b.Stop(ctx, nil)
}
func (b *boat) Spin(ctx context.Context, angleDeg, degsPerSec float64, extra map[string]interface{}) error {
if b.movementSensor == nil {
return errors.New("no movementSensor")
}
compass, err := b.movementSensor.CompassHeading(ctx, nil)
if err != nil {
return err
}
goal := compass + angleDeg
b.logger.Infof("Spin angleDeg: %v degsPerSec: %v compass: %v goal: %v", angleDeg, degsPerSec, compass, goal)
_, done := b.opMgr.New(ctx)
defer done()
b.stateMutex.Lock()
b.state.controlState = controlHeading
b.state.compassGoal = goal
b.state.velocityLinearGoal = r3.Vector{}
b.state.spinVelocity = degsPerSec
b.state.velocityAngularGoal = r3.Vector{0, 0, 0}
err = b.startVelocityThreadInLock()
b.stateMutex.Unlock()
if err != nil {
return err
}
return b.opMgr.WaitForSuccess(ctx, time.Second, func(ctx context.Context) (bool, error) {
compass, err := b.movementSensor.CompassHeading(ctx, nil)
if err != nil {
return false, err
}
return rdkutils.AngleDiffDeg(goal, compass) < 1, nil
})
}
func (b *boat) startVelocityThreadInLock() error {
if b.state.threadStarted {
return nil
}
if b.movementSensor == nil {
return errors.New("no movementSensor")
}
var ctx context.Context
ctx, b.cancel = context.WithCancel(context.Background())
b.waitGroup.Add(1)
go func() {
defer b.waitGroup.Done()
for {
utils.SelectContextOrWait(ctx, pidLoopTime)
err := b.velocityThreadLoop(ctx)
if err != nil {
if errors.Is(err, context.Canceled) {
return
}
b.logger.Warn(err)
}
}
}()
b.state.threadStarted = true
return nil
}
func (b *boat) velocityThreadLoop(ctx context.Context) error {
// TODO(erh) optimize how we get all sensor stuff
av, err := b.movementSensor.AngularVelocity(ctx, make(map[string]interface{}))
if err != nil {
return err
}
lv, err := b.movementSensor.LinearVelocity(ctx, make(map[string]interface{}))
if err != nil {
return err
}
heading, err := b.movementSensor.CompassHeading(ctx, nil)
if err != nil {
return err
}
// ------
b.stateMutex.Lock()
if b.state.controlState == controlNone {
b.stateMutex.Unlock()
return nil
}
var linear, angular r3.Vector
if b.state.controlState == controlVelocity {
linear, angular = computeNextPower(&b.state, lv, av, b.logger)
} else if b.state.controlState == controlHeading {
updateVelocityGoalForHeading(&b.state, heading)
b.logger.Infof("heading control compass: %v goal: %v angular z: %v", heading, b.state.compassGoal, b.state.velocityAngularGoal.Z)
linear, angular = computeNextPower(&b.state, lv, av, b.logger)
}
b.stateMutex.Unlock()
return b.setPowerInternal(ctx, linear, angular)
}
func updateVelocityGoalForHeading(state *boatState, heading float64) {
diff := heading - state.compassGoal
if diff < -5 {
state.velocityAngularGoal.Z = -1 * state.spinVelocity
} else if diff > 5 {
state.velocityAngularGoal.Z = state.spinVelocity
} else if diff < -1 {
state.velocityAngularGoal.Z = (diff * -1 / 5) * state.spinVelocity
} else if diff > 1 {
state.velocityAngularGoal.Z = (diff / 5) * state.spinVelocity
} else {
state.velocityAngularGoal.Z = 0
}
}
func computeNextPower(
state *boatState,
linearVelocity r3.Vector,
angularVelocity spatialmath.AngularVelocity,
logger golog.Logger) (r3.Vector, r3.Vector) {
return r3.Vector{0, state.linearPID.Control(state.velocityLinearGoal.Y, linearVelocity.Y, pidLoopTime), 0},
r3.Vector{0, 0, state.angularPID.Control(state.velocityAngularGoal.Z, angularVelocity.Z, pidLoopTime)}
}
func (b *boat) SetVelocity(ctx context.Context, linear, angular r3.Vector, extra map[string]interface{}) error {
b.logger.Debugf("SetVelocity %v %v", linear, angular)
_, done := b.opMgr.New(ctx)
defer done()
b.stateMutex.Lock()
defer b.stateMutex.Unlock()
err := b.startVelocityThreadInLock()
if err != nil {
return err
}
b.state.controlState = controlVelocity
b.state.velocityLinearGoal = linear
b.state.velocityAngularGoal = angular
return nil
}
func (b *boat) SetPower(ctx context.Context, linear, angular r3.Vector, extra map[string]interface{}) error {
b.logger.Debugf("SetPower %v %v", linear, angular)
ctx, done := b.opMgr.New(ctx)
defer done()
b.stateMutex.Lock()
b.state.controlState = controlNone
b.stateMutex.Unlock()
return b.setPowerInternal(ctx, linear, angular)
}
func (b *boat) setPowerInternal(ctx context.Context, linear, angular r3.Vector) error {
power, err := b.cfg.ComputePower(linear, angular)
if err != nil {
return err
}
for idx, p := range power {
err := b.motors[idx].SetPower(ctx, p, nil)
if err != nil {
return multierr.Combine(b.Stop(ctx, nil), err)
}
if ctx.Err() != nil {
return ctx.Err()
}
}
return nil
}
func (b *boat) Stop(ctx context.Context, extra map[string]interface{}) error {
b.stateMutex.Lock()
b.state.velocityLinearGoal = r3.Vector{}
b.state.velocityAngularGoal = r3.Vector{}
b.stateMutex.Unlock()
b.opMgr.CancelRunning(ctx)
var err error
for _, m := range b.motors {
err = multierr.Combine(m.Stop(ctx, nil), err)
}
return err
}
func (b *boat) Width(ctx context.Context) (int, error) {
return int(b.cfg.WidthMM), nil
}
func (b *boat) IsMoving(ctx context.Context) (bool, error) {
for _, m := range b.motors {
isMoving, _, err := m.IsPowered(ctx, nil)
if err != nil {
return false, err
}
if isMoving {
return true, err
}
}
return false, nil
}
func (b *boat) Close(ctx context.Context) error {
if b.cancel != nil {
b.cancel()
b.cancel = nil
b.waitGroup.Wait()
}
return b.Stop(ctx, nil)
}