-
Notifications
You must be signed in to change notification settings - Fork 109
/
sensorcontrolled.go
304 lines (258 loc) · 8.67 KB
/
sensorcontrolled.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
// Package sensorcontrolled base implements a base with feedback control from a movement sensor
package sensorcontrolled
import (
"context"
"sync"
"time"
"github.com/golang/geo/r3"
"github.com/pkg/errors"
"go.viam.com/rdk/components/base"
"go.viam.com/rdk/components/movementsensor"
"go.viam.com/rdk/control"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/operation"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/spatialmath"
rdkutils "go.viam.com/rdk/utils"
)
const (
yawPollTime = 5 * time.Millisecond
velocitiesPollTime = 5 * time.Millisecond
sensorDebug = false
typeLinVel = "linear_velocity"
typeAngVel = "angular_velocity"
)
var (
// Model is the name of the sensor_controlled model of a base component.
model = resource.DefaultModelFamily.WithModel("sensor-controlled")
errNoGoodSensor = errors.New("no appropriate sensor for orientation or velocity feedback")
)
// Config configures a sensor controlled base.
type Config struct {
MovementSensor []string `json:"movement_sensor"`
Base string `json:"base"`
ControlParameters []control.PIDConfig `json:"control_parameters,omitempty"`
}
// Validate validates all parts of the sensor controlled base config.
func (cfg *Config) Validate(path string) ([]string, error) {
deps := []string{}
if len(cfg.MovementSensor) == 0 {
return nil, resource.NewConfigValidationError(path, errors.New("need at least one movement sensor for base"))
}
deps = append(deps, cfg.MovementSensor...)
if cfg.Base == "" {
return nil, resource.NewConfigValidationFieldRequiredError(path, "base")
}
deps = append(deps, cfg.Base)
return deps, nil
}
type sensorBase struct {
resource.Named
conf *Config
logger logging.Logger
mu sync.Mutex
activeBackgroundWorkers sync.WaitGroup
controlledBase base.Base // the inherited wheeled base
opMgr *operation.SingleOperationManager
allSensors []movementsensor.MovementSensor
velocities movementsensor.MovementSensor
position movementsensor.MovementSensor
// headingFunc returns the current angle between (-180,180) and whether Spin is supported
headingFunc func(ctx context.Context) (float64, bool, error)
controlLoopConfig control.Config
blockNames map[string][]string
loop *control.Loop
}
func init() {
resource.RegisterComponent(
base.API,
model,
resource.Registration[base.Base, *Config]{Constructor: createSensorBase})
}
func createSensorBase(
ctx context.Context,
deps resource.Dependencies,
conf resource.Config,
logger logging.Logger,
) (base.Base, error) {
sb := &sensorBase{
logger: logger,
Named: conf.ResourceName().AsNamed(),
opMgr: operation.NewSingleOperationManager(),
}
if err := sb.Reconfigure(ctx, deps, conf); err != nil {
return nil, err
}
return sb, nil
}
func (sb *sensorBase) Reconfigure(ctx context.Context, deps resource.Dependencies, conf resource.Config) error {
newConf, err := resource.NativeConfig[*Config](conf)
sb.conf = newConf
if err != nil {
return err
}
if sb.loop != nil {
sb.loop.Stop()
sb.loop = nil
}
sb.mu.Lock()
defer sb.mu.Unlock()
// reset all sensors
sb.allSensors = nil
sb.velocities = nil
var orientation movementsensor.MovementSensor
var compassHeading movementsensor.MovementSensor
sb.position = nil
sb.controlledBase = nil
for _, name := range newConf.MovementSensor {
ms, err := movementsensor.FromDependencies(deps, name)
if err != nil {
return errors.Wrapf(err, "no movement sensor named (%s)", name)
}
sb.allSensors = append(sb.allSensors, ms)
}
for _, ms := range sb.allSensors {
props, err := ms.Properties(context.Background(), nil)
if err == nil && props.OrientationSupported {
// return first sensor that does not error that satisfies the properties wanted
orientation = ms
sb.logger.CInfof(ctx, "using sensor %s as orientation sensor for base", orientation.Name().ShortName())
break
}
}
for _, ms := range sb.allSensors {
props, err := ms.Properties(context.Background(), nil)
if err == nil && props.AngularVelocitySupported && props.LinearVelocitySupported {
// return first sensor that does not error that satisfies the properties wanted
sb.velocities = ms
sb.logger.CInfof(ctx, "using sensor %s as velocity sensor for base", sb.velocities.Name().ShortName())
break
}
}
for _, ms := range sb.allSensors {
props, err := ms.Properties(context.Background(), nil)
if err == nil && props.PositionSupported {
// return first sensor that does not error that satisfies the properties wanted
sb.position = ms
sb.logger.CInfof(ctx, "using sensor %s as position sensor for base", sb.position.Name().ShortName())
break
}
}
for _, ms := range sb.allSensors {
props, err := ms.Properties(context.Background(), nil)
if err == nil && props.CompassHeadingSupported {
// return first sensor that does not error that satisfies the properties wanted
compassHeading = ms
sb.logger.CInfof(ctx, "using sensor %s as compassHeading sensor for base", compassHeading.Name().ShortName())
break
}
}
sb.determineHeadingFunc(ctx, orientation, compassHeading)
if orientation == nil && sb.velocities == nil {
return errNoGoodSensor
}
sb.controlledBase, err = base.FromDependencies(deps, newConf.Base)
if err != nil {
return errors.Wrapf(err, "no base named (%s)", newConf.Base)
}
if sb.velocities != nil && len(newConf.ControlParameters) != 0 {
// assign linear and angular PID correctly based on the given type
var linear, angular control.PIDConfig
for _, c := range newConf.ControlParameters {
switch c.Type {
case typeLinVel:
linear = c
case typeAngVel:
angular = c
default:
sb.logger.Warn("control_parameters type must be 'linear_velocity' or 'angular_velocity'")
}
}
// unlock the mutex before setting up the control loop so that the motors
// are not locked, and can run if any auto-tuning is necessary
sb.mu.Unlock()
if err := sb.setupControlLoop(linear, angular); err != nil {
sb.mu.Lock()
return err
}
// relock the mutex after setting up the control loop since there is still a defer unlock
sb.mu.Lock()
}
return nil
}
func (sb *sensorBase) SetPower(
ctx context.Context, linear, angular r3.Vector, extra map[string]interface{},
) error {
sb.opMgr.CancelRunning(ctx)
if sb.loop != nil {
sb.loop.Pause()
}
return sb.controlledBase.SetPower(ctx, linear, angular, extra)
}
func (sb *sensorBase) Stop(ctx context.Context, extra map[string]interface{}) error {
sb.opMgr.CancelRunning(ctx)
if sb.loop != nil {
sb.loop.Pause()
}
return sb.controlledBase.Stop(ctx, extra)
}
func (sb *sensorBase) IsMoving(ctx context.Context) (bool, error) {
return sb.controlledBase.IsMoving(ctx)
}
func (sb *sensorBase) Properties(ctx context.Context, extra map[string]interface{}) (base.Properties, error) {
return sb.controlledBase.Properties(ctx, extra)
}
func (sb *sensorBase) Geometries(ctx context.Context, extra map[string]interface{}) ([]spatialmath.Geometry, error) {
return sb.controlledBase.Geometries(ctx, extra)
}
func (sb *sensorBase) Close(ctx context.Context) error {
if err := sb.Stop(ctx, nil); err != nil {
return err
}
if sb.loop != nil {
sb.loop.Stop()
sb.loop = nil
}
sb.activeBackgroundWorkers.Wait()
return nil
}
// determineHeadingFunc determines which movement sensor endpoint should be used for control.
// The priority is Orientation -> Heading -> No heading control.
func (sb *sensorBase) determineHeadingFunc(ctx context.Context,
orientation, compassHeading movementsensor.MovementSensor,
) {
switch {
case orientation != nil:
sb.logger.CInfof(ctx, "using sensor %s as angular heading sensor for base %v", orientation.Name().ShortName(), sb.Name().ShortName())
sb.headingFunc = func(ctx context.Context) (float64, bool, error) {
orient, err := orientation.Orientation(ctx, nil)
if err != nil {
return 0, false, err
}
// this returns (-180-> 180)
yaw := rdkutils.RadToDeg(orient.EulerAngles().Yaw)
return yaw, true, nil
}
case compassHeading != nil:
sb.logger.CInfof(ctx, "using sensor %s as angular heading sensor for base %v", compassHeading.Name().ShortName(), sb.Name().ShortName())
sb.headingFunc = func(ctx context.Context) (float64, bool, error) {
compass, err := compassHeading.CompassHeading(ctx, nil)
if err != nil {
return 0, false, err
}
// flip compass heading to be CCW/Z up
compass = 360 - compass
// make the compass heading (-180->180)
if compass > 180 {
compass -= 360
}
return compass, true, nil
}
default:
sb.logger.CInfof(ctx, "base %v cannot control heading, no heading related sensor given",
sb.Name().ShortName())
sb.headingFunc = func(ctx context.Context) (float64, bool, error) {
return 0, false, nil
}
}
}