-
Notifications
You must be signed in to change notification settings - Fork 110
/
motor.go
395 lines (338 loc) · 9.67 KB
/
motor.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
// Package fake implements a fake motor.
package fake
import (
"context"
"math"
"sync"
"time"
"github.com/pkg/errors"
"go.viam.com/utils"
"go.viam.com/rdk/components/board"
"go.viam.com/rdk/components/encoder"
"go.viam.com/rdk/components/encoder/fake"
"go.viam.com/rdk/components/motor"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/operation"
"go.viam.com/rdk/resource"
)
var model = resource.DefaultModelFamily.WithModel("fake")
const defaultMaxRpm = 100
// PinConfig defines the mapping of where motor are wired.
type PinConfig struct {
Direction string `json:"dir"`
PWM string `json:"pwm"`
}
// Config describes the configuration of a motor.
type Config struct {
Pins PinConfig `json:"pins"`
BoardName string `json:"board"`
MinPowerPct float64 `json:"min_power_pct,omitempty"`
MaxPowerPct float64 `json:"max_power_pct,omitempty"`
PWMFreq uint `json:"pwm_freq,omitempty"`
Encoder string `json:"encoder,omitempty"`
MaxRPM float64 `json:"max_rpm,omitempty"`
TicksPerRotation int `json:"ticks_per_rotation,omitempty"`
DirectionFlip bool `json:"direction_flip,omitempty"`
}
// Validate ensures all parts of the config are valid.
func (cfg *Config) Validate(path string) ([]string, error) {
var deps []string
if cfg.BoardName != "" {
deps = append(deps, cfg.BoardName)
}
if cfg.Encoder != "" {
if cfg.TicksPerRotation <= 0 {
return nil, utils.NewConfigValidationError(path, errors.New("need nonzero TicksPerRotation for encoded motor"))
}
deps = append(deps, cfg.Encoder)
}
return deps, nil
}
func init() {
resource.RegisterComponent(motor.API, model, resource.Registration[motor.Motor, *Config]{
Constructor: func(
ctx context.Context,
deps resource.Dependencies,
conf resource.Config,
logger logging.Logger,
) (motor.Motor, error) {
m := &Motor{
Named: conf.ResourceName().AsNamed(),
Logger: logger,
OpMgr: operation.NewSingleOperationManager(),
}
if err := m.Reconfigure(ctx, deps, conf); err != nil {
return nil, err
}
return m, nil
},
})
}
// A Motor allows setting and reading a set power percentage and
// direction.
type Motor struct {
resource.Named
resource.TriviallyCloseable
mu sync.Mutex
powerPct float64
Board string
PWM board.GPIOPin
PositionReporting bool
Encoder fake.Encoder
MaxRPM float64
DirFlip bool
TicksPerRotation int
OpMgr *operation.SingleOperationManager
Logger logging.Logger
}
// Reconfigure atomically reconfigures this motor in place based on the new config.
func (m *Motor) Reconfigure(ctx context.Context, deps resource.Dependencies, conf resource.Config) error {
m.mu.Lock()
defer m.mu.Unlock()
newConf, err := resource.NativeConfig[*Config](conf)
if err != nil {
return err
}
if newConf.BoardName != "" {
m.Board = newConf.BoardName
b, err := board.FromDependencies(deps, m.Board)
if err != nil {
return err
}
if newConf.Pins.PWM != "" {
m.PWM, err = b.GPIOPinByName(newConf.Pins.PWM)
if err != nil {
return err
}
if err = m.PWM.SetPWMFreq(ctx, newConf.PWMFreq, nil); err != nil {
return err
}
}
}
m.MaxRPM = newConf.MaxRPM
if m.MaxRPM == 0 {
m.Logger.Infof("Max RPM not provided to a fake motor, defaulting to %v", defaultMaxRpm)
m.MaxRPM = defaultMaxRpm
}
if newConf.Encoder != "" {
m.TicksPerRotation = newConf.TicksPerRotation
e, err := encoder.FromDependencies(deps, newConf.Encoder)
if err != nil {
return err
}
fakeEncoder, ok := e.(fake.Encoder)
if !ok {
return resource.TypeError[fake.Encoder](e)
}
m.Encoder = fakeEncoder
m.PositionReporting = true
} else {
m.PositionReporting = false
}
m.DirFlip = false
if newConf.DirectionFlip {
m.DirFlip = true
}
return nil
}
// Position returns motor position in rotations.
func (m *Motor) Position(ctx context.Context, extra map[string]interface{}) (float64, error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.Encoder == nil {
return 0, errors.New("encoder is not defined")
}
ticks, _, err := m.Encoder.Position(ctx, encoder.PositionTypeUnspecified, extra)
if err != nil {
return 0, err
}
if m.TicksPerRotation == 0 {
return 0, errors.New("need nonzero TicksPerRotation for motor")
}
return ticks / float64(m.TicksPerRotation), nil
}
// Properties returns the status of whether the motor supports certain optional properties.
func (m *Motor) Properties(ctx context.Context, extra map[string]interface{}) (motor.Properties, error) {
return motor.Properties{
PositionReporting: m.PositionReporting,
}, nil
}
// SetPower sets the given power percentage.
func (m *Motor) SetPower(ctx context.Context, powerPct float64, extra map[string]interface{}) error {
m.mu.Lock()
defer m.mu.Unlock()
m.OpMgr.CancelRunning(ctx)
m.Logger.Debugf("Motor SetPower %f", powerPct)
m.setPowerPct(powerPct)
if m.Encoder != nil {
if m.TicksPerRotation <= 0 {
return errors.New("need positive nonzero TicksPerRotation")
}
newSpeed := (m.MaxRPM * m.powerPct) * float64(m.TicksPerRotation)
err := m.Encoder.SetSpeed(ctx, newSpeed)
if err != nil {
return err
}
}
return nil
}
func (m *Motor) setPowerPct(powerPct float64) {
m.powerPct = powerPct
}
// PowerPct returns the set power percentage.
func (m *Motor) PowerPct() float64 {
m.mu.Lock()
defer m.mu.Unlock()
if m.DirFlip {
m.powerPct *= -1
}
return m.powerPct
}
// Direction returns the set direction.
func (m *Motor) Direction() int {
m.mu.Lock()
defer m.mu.Unlock()
switch {
case m.powerPct > 0:
return 1
case m.powerPct < 0:
return -1
}
return 0
}
// If revolutions is 0, the returned wait duration will be 0 representing that
// the motor should run indefinitely.
func goForMath(maxRPM, rpm, revolutions float64) (float64, time.Duration, float64) {
// need to do this so time is reasonable
if rpm > maxRPM {
rpm = maxRPM
} else if rpm < -1*maxRPM {
rpm = -1 * maxRPM
}
if rpm == 0 {
return 0, 0, revolutions / math.Abs(revolutions)
}
if revolutions == 0 {
powerPct := rpm / maxRPM
return powerPct, 0, 1
}
dir := rpm * revolutions / math.Abs(revolutions*rpm)
powerPct := math.Abs(rpm) / maxRPM * dir
waitDur := time.Duration(math.Abs(revolutions/rpm)*60*1000) * time.Millisecond
return powerPct, waitDur, dir
}
// GoFor sets the given direction and an arbitrary power percentage.
// If rpm is 0, the motor should immediately move to the final position.
func (m *Motor) GoFor(ctx context.Context, rpm, revolutions float64, extra map[string]interface{}) error {
switch speed := math.Abs(rpm); {
case speed < 0.1:
m.Logger.Warn("motor speed is nearly 0 rev_per_min")
return motor.NewZeroRPMError()
case m.MaxRPM > 0 && speed > m.MaxRPM-0.1:
m.Logger.Warnf("motor speed is nearly the max rev_per_min (%f)", m.MaxRPM)
default:
}
powerPct, waitDur, dir := goForMath(m.MaxRPM, rpm, revolutions)
var finalPos float64
if m.Encoder != nil {
curPos, err := m.Position(ctx, nil)
if err != nil {
return err
}
finalPos = curPos + dir*math.Abs(revolutions)
}
err := m.SetPower(ctx, powerPct, nil)
if err != nil {
return err
}
if revolutions == 0 {
return nil
}
if m.OpMgr.NewTimedWaitOp(ctx, waitDur) {
err = m.Stop(ctx, nil)
if err != nil {
return err
}
if m.Encoder != nil {
return m.Encoder.SetPosition(ctx, int64(finalPos*float64(m.TicksPerRotation)))
}
}
return nil
}
// GoTo sets the given direction and an arbitrary power percentage for now.
func (m *Motor) GoTo(ctx context.Context, rpm, pos float64, extra map[string]interface{}) error {
if m.Encoder == nil {
return errors.New("encoder is not defined")
}
switch speed := math.Abs(rpm); {
case speed < 0.1:
m.Logger.Warn("motor speed is nearly 0 rev_per_min")
case m.MaxRPM > 0 && speed > m.MaxRPM-0.1:
m.Logger.Warnf("motor speed is nearly the max rev_per_min (%f)", m.MaxRPM)
default:
}
curPos, err := m.Position(ctx, nil)
if err != nil {
return err
}
if curPos == pos {
return nil
}
revolutions := pos - curPos
powerPct, waitDur, _ := goForMath(m.MaxRPM, math.Abs(rpm), revolutions)
err = m.SetPower(ctx, powerPct, nil)
if err != nil {
return err
}
if revolutions == 0 {
return nil
}
if m.OpMgr.NewTimedWaitOp(ctx, waitDur) {
err = m.Stop(ctx, nil)
if err != nil {
return err
}
return m.Encoder.SetPosition(ctx, int64(pos*float64(m.TicksPerRotation)))
}
return nil
}
// Stop has the motor pretend to be off.
func (m *Motor) Stop(ctx context.Context, extra map[string]interface{}) error {
m.mu.Lock()
defer m.mu.Unlock()
m.Logger.Debug("Motor Stopped")
m.setPowerPct(0.0)
if m.Encoder != nil {
err := m.Encoder.SetSpeed(ctx, 0.0)
if err != nil {
return errors.Wrapf(err, "error in Stop from motor (%s)", m.Name())
}
}
return nil
}
// ResetZeroPosition resets the zero position.
func (m *Motor) ResetZeroPosition(ctx context.Context, offset float64, extra map[string]interface{}) error {
if m.Encoder == nil {
return errors.New("encoder is not defined")
}
if m.TicksPerRotation == 0 {
return errors.New("need nonzero TicksPerRotation for motor")
}
err := m.Encoder.SetPosition(ctx, int64(-1*offset))
if err != nil {
return errors.Wrapf(err, "error in ResetZeroPosition from motor (%s)", m.Name())
}
return nil
}
// IsPowered returns if the motor is pretending to be on or not, and its power level.
func (m *Motor) IsPowered(ctx context.Context, extra map[string]interface{}) (bool, float64, error) {
m.mu.Lock()
defer m.mu.Unlock()
return math.Abs(m.powerPct) >= 0.005, m.powerPct, nil
}
// IsMoving returns if the motor is pretending to be moving or not.
func (m *Motor) IsMoving(ctx context.Context) (bool, error) {
m.mu.Lock()
defer m.mu.Unlock()
return math.Abs(m.powerPct) >= 0.005, nil
}