-
Notifications
You must be signed in to change notification settings - Fork 110
/
gpiostepper.go
491 lines (414 loc) · 14.2 KB
/
gpiostepper.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
// Package gpiostepper implements a GPIO based stepper motor
package gpiostepper
// This package is meant to be used with bipolar stepper motors connected to drivers that drive motors
// using high/low direction pins and pulses to step the motor armatures, the package can also set enable
// pins high or low if the driver needs them to power the stepper motor armatures
/*
Compatibility tested:
Stepper Motors: NEMA
Motor Driver: DRV8825, A4998, L298N igus-drylin D8(X)
Resources:
DRV8825: https://lastminuteengineers.com/drv8825-stepper-motor-driver-arduino-tutorial/
A4998: https://lastminuteengineers.com/a4988-stepper-motor-driver-arduino-tutorial/
L298N: https://lastminuteengineers.com/stepper-motor-l298n-arduino-tutorial/
This driver will drive the motor using a step pulse with a delay that matches the speed calculated by:
stepperDelay (ns) := 1min / (rpm (revs_per_minute) * spr (steps_per_revolution))
The motor will then step and increment its position until it has reached a target or has been stopped.
Configuration:
Required pins: a step pin to send pulses and a direction pin to set the direction.
Enabling current to flow through the armature and holding a position can be done by setting enable pins on
hardware that supports that functionality.
An optional configurable stepper_delay parameter configures the minimum delay to set a pulse to high
for a particular stepper motor. This is usually motor specific and can be calculated using phase
resistance and induction data from the datasheet of your stepper motor.
*/
import (
"context"
"fmt"
"math"
"sync"
"time"
"github.com/pkg/errors"
"go.uber.org/multierr"
"go.viam.com/utils"
"go.viam.com/rdk/components/board"
"go.viam.com/rdk/components/motor"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/operation"
"go.viam.com/rdk/resource"
rdkutils "go.viam.com/rdk/utils"
)
var model = resource.DefaultModelFamily.WithModel("gpiostepper")
// PinConfig defines the mapping of where motor are wired.
type PinConfig struct {
Step string `json:"step"`
Direction string `json:"dir"`
EnablePinHigh string `json:"en_high,omitempty"`
EnablePinLow string `json:"en_low,omitempty"`
}
// Config describes the configuration of a motor.
type Config struct {
Pins PinConfig `json:"pins"`
BoardName string `json:"board"`
StepperDelay int `json:"stepper_delay_usec,omitempty"` // When using stepper motors, the time to remain high
TicksPerRotation int `json:"ticks_per_rotation"`
}
// Validate ensures all parts of the config are valid.
func (cfg *Config) Validate(path string) ([]string, error) {
var deps []string
if cfg.BoardName == "" {
return nil, resource.NewConfigValidationFieldRequiredError(path, "board")
}
if cfg.TicksPerRotation == 0 {
return nil, resource.NewConfigValidationFieldRequiredError(path, "ticks_per_rotation")
}
if cfg.Pins.Direction == "" {
return nil, resource.NewConfigValidationFieldRequiredError(path, "dir")
}
if cfg.Pins.Step == "" {
return nil, resource.NewConfigValidationFieldRequiredError(path, "step")
}
deps = append(deps, cfg.BoardName)
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) {
actualBoard, motorConfig, err := getBoardFromRobotConfig(deps, conf)
if err != nil {
return nil, err
}
return newGPIOStepper(ctx, actualBoard, *motorConfig, conf.ResourceName(), logger)
},
})
}
func getBoardFromRobotConfig(deps resource.Dependencies, conf resource.Config) (board.Board, *Config, error) {
motorConfig, err := resource.NativeConfig[*Config](conf)
if err != nil {
return nil, nil, err
}
if motorConfig.BoardName == "" {
return nil, nil, errors.New("expected board name in config for motor")
}
b, err := board.FromDependencies(deps, motorConfig.BoardName)
if err != nil {
return nil, nil, err
}
return b, motorConfig, nil
}
func newGPIOStepper(
ctx context.Context,
b board.Board,
mc Config,
name resource.Name,
logger logging.Logger,
) (motor.Motor, error) {
if b == nil {
return nil, errors.New("board is required")
}
if mc.TicksPerRotation == 0 {
return nil, errors.New("expected ticks_per_rotation in config for motor")
}
m := &gpioStepper{
Named: name.AsNamed(),
theBoard: b,
stepsPerRotation: mc.TicksPerRotation,
logger: logger,
opMgr: operation.NewSingleOperationManager(),
}
var err error
// only set enable pins if they exist
if mc.Pins.EnablePinHigh != "" {
m.enablePinHigh, err = b.GPIOPinByName(mc.Pins.EnablePinHigh)
if err != nil {
return nil, err
}
}
if mc.Pins.EnablePinLow != "" {
m.enablePinLow, err = b.GPIOPinByName(mc.Pins.EnablePinLow)
if err != nil {
return nil, err
}
}
// set the required step and direction pins
m.stepPin, err = b.GPIOPinByName(mc.Pins.Step)
if err != nil {
return nil, err
}
m.dirPin, err = b.GPIOPinByName(mc.Pins.Direction)
if err != nil {
return nil, err
}
if mc.StepperDelay > 0 {
m.minDelay = time.Duration(mc.StepperDelay * int(time.Microsecond))
}
err = m.enable(ctx, false)
if err != nil {
return nil, err
}
m.startThread()
return m, nil
}
type gpioStepper struct {
resource.Named
resource.AlwaysRebuild
resource.TriviallyCloseable
// config
theBoard board.Board
stepsPerRotation int
stepperDelay time.Duration
minDelay time.Duration
enablePinHigh, enablePinLow board.GPIOPin
stepPin, dirPin board.GPIOPin
logger logging.Logger
// state
lock sync.Mutex
opMgr *operation.SingleOperationManager
stepPosition int64
threadStarted bool
targetStepPosition int64
cancel context.CancelFunc
waitGroup sync.WaitGroup
}
// SetPower sets the percentage of power the motor should employ between 0-1.
func (m *gpioStepper) SetPower(ctx context.Context, powerPct float64, extra map[string]interface{}) error {
if math.Abs(powerPct) <= .0001 {
m.stop()
return nil
}
if m.minDelay == 0 {
return errors.Errorf(
"if you want to set the power, set 'stepper_delay_usec' in the motor config at "+
"the minimum time delay between pulses for your stepper motor (%s)",
m.Name().Name)
}
m.stepperDelay = time.Duration(float64(m.minDelay) / math.Abs(powerPct))
if powerPct < 0 {
m.targetStepPosition = math.MinInt64
} else {
m.targetStepPosition = math.MaxInt64
}
return nil
}
func (m *gpioStepper) startThread() {
m.lock.Lock()
defer m.lock.Unlock()
if m.threadStarted {
return
}
m.logger.Debugf("starting control thread for motor (%s)", m.Name().Name)
var ctxWG context.Context
ctxWG, m.cancel = context.WithCancel(context.Background())
m.threadStarted = true
m.waitGroup.Add(1)
go func() {
defer m.waitGroup.Done()
for {
sleep, err := m.doCycle(ctxWG)
if err != nil {
m.logger.Warnf("error cycling gpioStepper (%s) %s", m.Name().Name, err.Error())
}
if !utils.SelectContextOrWait(ctxWG, sleep) {
// context done
return
}
}
}()
}
func (m *gpioStepper) doCycle(ctx context.Context) (time.Duration, error) {
m.lock.Lock()
defer m.lock.Unlock()
// thread waits until something changes the target position in the
// gpiostepper struct
if m.stepPosition == m.targetStepPosition {
return 5 * time.Millisecond, nil
}
// TODO: Setting PWM here works much better than steps to set speed
// Redo this part with PWM logic, but also be aware that parallel
// logic to the PWM call will need to be implemented to account for position
// reporting
err := m.doStep(ctx, m.stepPosition < m.targetStepPosition)
if err != nil {
return time.Second, fmt.Errorf("error stepping motor (%s) %w", m.Name().Name, err)
}
// wait the stepper delay to return from the doRun for loop or select
// context if the duration has not elapsed.
return 0, nil
}
// have to be locked to call.
func (m *gpioStepper) doStep(ctx context.Context, forward bool) error {
err := multierr.Combine(
m.dirPin.Set(ctx, forward, nil),
m.stepPin.Set(ctx, true, nil))
if err != nil {
return err
}
// stay high for half the delay
time.Sleep(m.stepperDelay / 2.0)
if err := m.stepPin.Set(ctx, false, nil); err != nil {
return err
}
// stay low for the other half
time.Sleep(m.stepperDelay / 2.0)
if forward {
m.stepPosition++
} else {
m.stepPosition--
}
return nil
}
// GoFor instructs the motor to go in a specific direction for a specific amount of
// revolutions at a given speed in revolutions per minute. Both the RPM and the revolutions
// can be assigned negative values to move in a backwards direction. Note: if both are negative
// the motor will spin in the forward direction.
func (m *gpioStepper) GoFor(ctx context.Context, rpm, revolutions float64, extra map[string]interface{}) error {
ctx, done := m.opMgr.New(ctx)
defer done()
err := m.enable(ctx, true)
if err != nil {
return errors.Wrapf(err, "error enabling motor in GoFor from motor (%s)", m.Name().Name)
}
err = m.goForInternal(ctx, rpm, revolutions)
if err != nil {
return multierr.Combine(
m.enable(ctx, false),
errors.Wrapf(err, "error in GoFor from motor (%s)", m.Name().Name))
}
// this is a long-running operation, do not wait for Stop, do not disable enable pins
if revolutions == 0 {
return nil
}
return multierr.Combine(
m.opMgr.WaitTillNotPowered(ctx, time.Millisecond, m, m.Stop),
m.enable(ctx, false))
}
func (m *gpioStepper) goForInternal(ctx context.Context, rpm, revolutions float64) error {
if revolutions == 0 {
// go a large number of revolutions if 0 is passed in, at the desired speed
revolutions = math.MaxInt64
}
speed := math.Abs(rpm)
if speed < 0.1 {
m.logger.CWarn(ctx, "motor speed is nearly 0 rev_per_min")
return m.Stop(ctx, nil)
}
var d int64 = 1
if math.Signbit(revolutions) != math.Signbit(rpm) {
d = -1
}
m.lock.Lock()
defer m.lock.Unlock()
// calculate delay between steps for the thread in the gorootuine that we started in component creation
m.stepperDelay = time.Duration(int64(float64(time.Minute) / (math.Abs(rpm) * float64(m.stepsPerRotation))))
if m.stepperDelay < m.minDelay {
m.stepperDelay = m.minDelay
m.logger.CDebugf(ctx,
"calculated delay less than the minimum delay for stepper motor setting to %+v", m.stepperDelay,
)
}
if !m.threadStarted {
return errors.New("thread not started")
}
m.targetStepPosition += d * int64(math.Abs(revolutions)*float64(m.stepsPerRotation))
return nil
}
// GoTo instructs the motor to go to a specific position (provided in revolutions from home/zero),
// at a specific RPM. Regardless of the directionality of the RPM this function will move the motor
// towards the specified target.
func (m *gpioStepper) GoTo(ctx context.Context, rpm, positionRevolutions float64, extra map[string]interface{}) error {
curPos, err := m.Position(ctx, extra)
if err != nil {
return errors.Wrapf(err, "error in GoTo from motor (%s)", m.Name().Name)
}
moveDistance := positionRevolutions - curPos
// if you call GoFor with 0 revolutions, the motor will spin forever. If we are at the target,
// we must avoid this by not calling GoFor.
if rdkutils.Float64AlmostEqual(moveDistance, 0, 0.1) {
m.logger.CDebugf(ctx, "GoTo distance nearly zero for motor (%s), not moving", m.Name().Name)
return nil
}
m.logger.CDebugf(ctx, "motor (%s) going to %.2f at rpm %.2f", m.Name().Name, moveDistance, math.Abs(rpm))
return m.GoFor(ctx, math.Abs(rpm), moveDistance, extra)
}
// Set the current position (+/- offset) to be the new zero (home) position.
func (m *gpioStepper) ResetZeroPosition(ctx context.Context, offset float64, extra map[string]interface{}) error {
m.lock.Lock()
defer m.lock.Unlock()
m.stepPosition = int64(-1 * offset * float64(m.stepsPerRotation))
m.targetStepPosition = m.stepPosition
return nil
}
// Position reports the position of the motor based on its encoder. If it's not supported, the returned
// data is undefined. The unit returned is the number of revolutions which is intended to be fed
// back into calls of GoFor.
func (m *gpioStepper) Position(ctx context.Context, extra map[string]interface{}) (float64, error) {
m.lock.Lock()
defer m.lock.Unlock()
return float64(m.stepPosition) / float64(m.stepsPerRotation), nil
}
// Properties returns the status of whether the motor supports certain optional properties.
func (m *gpioStepper) Properties(ctx context.Context, extra map[string]interface{}) (motor.Properties, error) {
return motor.Properties{
PositionReporting: true,
}, nil
}
// IsMoving returns if the motor is currently moving.
func (m *gpioStepper) IsMoving(ctx context.Context) (bool, error) {
m.lock.Lock()
defer m.lock.Unlock()
return m.stepPosition != m.targetStepPosition, nil
}
// Stop turns the power to the motor off immediately, without any gradual step down.
func (m *gpioStepper) Stop(ctx context.Context, extra map[string]interface{}) error {
m.stop()
m.lock.Lock()
defer m.lock.Unlock()
return m.enable(ctx, false)
}
func (m *gpioStepper) stop() {
m.lock.Lock()
defer m.lock.Unlock()
m.targetStepPosition = m.stepPosition
}
// IsPowered returns whether or not the motor is currently on. It also returns the percent power
// that the motor has, but stepper motors only have this set to 0% or 100%, so it's a little
// redundant.
func (m *gpioStepper) IsPowered(ctx context.Context, extra map[string]interface{}) (bool, float64, error) {
on, err := m.IsMoving(ctx)
if err != nil {
return on, 0.0, errors.Wrapf(err, "error in IsPowered from motor (%s)", m.Name().Name)
}
percent := 0.0
if on {
percent = 1.0
}
return on, percent, err
}
func (m *gpioStepper) enable(ctx context.Context, on bool) error {
var err error
if m.enablePinHigh != nil {
err = multierr.Combine(err, m.enablePinHigh.Set(ctx, on, nil))
}
if m.enablePinLow != nil {
err = multierr.Combine(err, m.enablePinLow.Set(ctx, !on, nil))
}
return err
}
func (m *gpioStepper) Close(ctx context.Context) error {
err := m.Stop(ctx, nil)
m.lock.Lock()
if m.cancel != nil {
m.logger.CDebugf(ctx, "stopping control thread for motor (%s)", m.Name().Name)
m.cancel()
m.cancel = nil
m.threadStarted = false
}
m.lock.Unlock()
m.waitGroup.Wait()
return err
}