-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
adafruit_driver.go
563 lines (511 loc) · 17.4 KB
/
adafruit_driver.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
package i2c
import (
"errors"
"log"
"math"
"time"
"gobot.io/x/gobot"
)
// AdafruitDirection declares a type for specification of the motor direction
type AdafruitDirection int
// AdafruitStepStyle declares a type for specification of the stepper motor rotation
type AdafruitStepStyle int
type adaFruitDCMotor struct {
pwmPin, in1Pin, in2Pin byte
}
type adaFruitStepperMotor struct {
pwmPinA, pwmPinB byte
ain1, ain2 byte
bin1, bin2 byte
secPerStep float64
currentStep, stepCounter, revSteps int
}
// AdafruitMotorHatDriver is a driver for the DC+Stepper Motor HAT from Adafruit.
// The HAT is a Raspberry Pi add-on that can drive up to 4 DC or 2 Stepper motors
// with full PWM speed control. It has a dedicated PWM driver chip onboard to
// control both motor direction and speed over I2C.
type AdafruitMotorHatDriver struct {
name string
connector Connector
motorHatConnection Connection
servoHatConnection Connection
Config
gobot.Commander
dcMotors []adaFruitDCMotor
stepperMotors []adaFruitStepperMotor
}
var adafruitDebug = false // Set this to true to see debug output
var (
// Each Adafruit HAT must have a unique I2C address. The default address for
// the DC and Stepper Motor HAT is 0x60. The addresses of the Motor HATs can
// range from 0x60 to 0x80 for a total of 32 unique addresses.
// The base address for the Adafruit PWM-Servo HAT is 0x40. Please consult
// the Adafruit documentation for soldering and addressing stacked HATs.
motorHatAddress = 0x60
servoHatAddress = 0x40
stepperMicrosteps = 8
stepperMicrostepCurve []int
step2coils = make(map[int][]int32)
)
const (
// Registers
_Mode1 = 0x00
_Mode2 = 0x01
_SubAdr1 = 0x02
_SubAdr2 = 0x03
_SubAdr3 = 0x04
_Prescale = 0xFE
_LedZeroOnL = 0x06
_LedZeroOnH = 0x07
_LedZeroOffL = 0x08
_LedZeroOffH = 0x09
_AllLedOnL = 0xFA
_AllLedOnH = 0xFB
_AllLedOffL = 0xFC
_AllLedOffH = 0xFD
// Bits
_Restart = 0x80
_Sleep = 0x10
_AllCall = 0x01
_Invrt = 0x10
_Outdrv = 0x04
)
const (
AdafruitForward AdafruitDirection = iota // 0
AdafruitBackward // 1
AdafruitRelease // 2
)
const (
AdafruitSingle AdafruitStepStyle = iota // 0
AdafruitDouble // 1
AdafruitInterleave // 2
AdafruitMicrostep // 3
)
// NewAdafruitMotorHatDriver initializes the internal DCMotor and StepperMotor types.
// Again the Adafruit Motor Hat supports up to four DC motors and up to two stepper motors.
// Params:
// conn Connector - the Adaptor to use with this Driver
//
// Optional params:
// i2c.WithBus(int): bus to use with this driver
// i2c.WithAddress(int): address to use with this driver
//
func NewAdafruitMotorHatDriver(conn Connector, options ...func(Config)) *AdafruitMotorHatDriver {
var dc []adaFruitDCMotor
var st []adaFruitStepperMotor
for i := 0; i < 4; i++ {
switch {
case i == 0:
dc = append(dc, adaFruitDCMotor{pwmPin: 8, in1Pin: 10, in2Pin: 9})
st = append(st, adaFruitStepperMotor{pwmPinA: 8, pwmPinB: 13,
ain1: 10, ain2: 9, bin1: 11, bin2: 12, revSteps: 200, secPerStep: 0.1})
case i == 1:
dc = append(dc, adaFruitDCMotor{pwmPin: 13, in1Pin: 11, in2Pin: 12})
st = append(st, adaFruitStepperMotor{pwmPinA: 2, pwmPinB: 7,
ain1: 4, ain2: 3, bin1: 5, bin2: 6, revSteps: 200, secPerStep: 0.1})
case i == 2:
dc = append(dc, adaFruitDCMotor{pwmPin: 2, in1Pin: 4, in2Pin: 3})
case i == 3:
dc = append(dc, adaFruitDCMotor{pwmPin: 7, in1Pin: 5, in2Pin: 6})
}
}
driver := &AdafruitMotorHatDriver{
name: gobot.DefaultName("AdafruitMotorHat"),
connector: conn,
Config: NewConfig(),
Commander: gobot.NewCommander(),
dcMotors: dc,
stepperMotors: st,
}
for _, option := range options {
option(driver)
}
// TODO: add API funcs
return driver
}
// SetMotorHatAddress sets the I2C address for the DC and Stepper Motor HAT.
// This addressing flexibility empowers "stacking" the HATs.
func (a *AdafruitMotorHatDriver) SetMotorHatAddress(addr int) (err error) {
motorHatAddress = addr
return
}
// SetServoHatAddress sets the I2C address for the PWM-Servo Motor HAT.
// This addressing flexibility empowers "stacking" the HATs.
func (a *AdafruitMotorHatDriver) SetServoHatAddress(addr int) (err error) {
servoHatAddress = addr
return
}
// Name identifies this driver object
func (a *AdafruitMotorHatDriver) Name() string { return a.name }
// SetName sets nae for driver
func (a *AdafruitMotorHatDriver) SetName(n string) { a.name = n }
// Connection identifies the particular adapter object
func (a *AdafruitMotorHatDriver) Connection() gobot.Connection { return a.connector.(gobot.Connection) }
func (a *AdafruitMotorHatDriver) startDriver(connection Connection) (err error) {
if err = a.setAllPWM(connection, 0, 0); err != nil {
return
}
reg := byte(_Mode2)
val := byte(_Outdrv)
if _, err = connection.Write([]byte{reg, val}); err != nil {
return
}
reg = byte(_Mode1)
val = byte(_AllCall)
if _, err = connection.Write([]byte{reg, val}); err != nil {
return
}
time.Sleep(5 * time.Millisecond)
// Read a byte from the I2C device. Note: no ability to read from a specified reg?
mode1 := []byte{0}
_, rerr := connection.Read(mode1)
if rerr != nil {
return rerr
}
if len(mode1) > 0 {
reg = byte(_Mode1)
val = mode1[0] & _Sleep
if _, err = connection.Write([]byte{reg, val}); err != nil {
return
}
time.Sleep(5 * time.Millisecond)
}
return
}
// Start initializes both I2C-addressable Adafruit Motor HAT drivers
func (a *AdafruitMotorHatDriver) Start() (err error) {
bus := a.GetBusOrDefault(a.connector.GetDefaultBus())
err = a.startServoHat(bus)
if adafruitDebug && err != nil {
log.Printf("[adafruit_driver] start servohat error: %s\n", err)
}
err = a.startMotorHat(bus)
if adafruitDebug && err != nil {
log.Printf("[adafruit_driver] start motorhat error: %s\n", err)
}
return
}
// startServoHat starts the Servo motors connection.
func (a *AdafruitMotorHatDriver) startServoHat(bus int) (err error) {
if a.servoHatConnection, err = a.connector.GetConnection(servoHatAddress, bus); err != nil {
return
}
if err = a.startDriver(a.servoHatConnection); err != nil {
return
}
return
}
// startMotorHat starts the DC motors connection.
func (a *AdafruitMotorHatDriver) startMotorHat(bus int) (err error) {
if a.motorHatConnection, err = a.connector.GetConnection(motorHatAddress, bus); err != nil {
return
}
if err = a.startDriver(a.motorHatConnection); err != nil {
return
}
return
}
// Halt returns true if devices is halted successfully
func (a *AdafruitMotorHatDriver) Halt() (err error) { return }
// setPWM sets the start (on) and end (off) of the high-segment of the PWM pulse
// on the specific channel (pin).
func (a *AdafruitMotorHatDriver) setPWM(conn Connection, pin byte, on, off int32) (err error) {
// register and values to be written to that register
regVals := make(map[int][]byte)
regVals[0] = []byte{byte(_LedZeroOnL + 4*pin), byte(on & 0xff)}
regVals[1] = []byte{byte(_LedZeroOnH + 4*pin), byte(on >> 8)}
regVals[2] = []byte{byte(_LedZeroOffL + 4*pin), byte(off & 0xff)}
regVals[3] = []byte{byte(_LedZeroOffH + 4*pin), byte(off >> 8)}
for i := 0; i < len(regVals); i++ {
if _, err = conn.Write(regVals[i]); err != nil {
return
}
}
return
}
// SetServoMotorFreq sets the frequency for the currently addressed PWM Servo HAT.
func (a *AdafruitMotorHatDriver) SetServoMotorFreq(freq float64) (err error) {
if err = a.setPWMFreq(a.servoHatConnection, freq); err != nil {
return
}
return
}
// SetServoMotorPulse is a convenience function to specify the 'tick' value,
// between 0-4095, when the signal will turn on, and when it will turn off.
func (a *AdafruitMotorHatDriver) SetServoMotorPulse(channel byte, on, off int32) (err error) {
if err = a.setPWM(a.servoHatConnection, channel, on, off); err != nil {
return
}
return
}
// setPWMFreq adjusts the PWM frequency which determines how many full
// pulses per second are generated by the integrated circuit. The frequency
// determines how "long" each pulse is in duration from start to finish,
// taking into account the high and low segments of the pulse.
func (a *AdafruitMotorHatDriver) setPWMFreq(conn Connection, freq float64) (err error) {
// 25MHz
preScaleVal := 25000000.0
// 12-bit
preScaleVal /= 4096.0
preScaleVal /= freq
preScaleVal -= 1.0
preScale := math.Floor(preScaleVal + 0.5)
if adafruitDebug {
log.Printf("Setting PWM frequency to: %.2f Hz", freq)
log.Printf("Estimated pre-scale: %.2f", preScaleVal)
log.Printf("Final pre-scale: %.2f", preScale)
}
// default (and only) reads register 0
oldMode := []byte{0}
_, err = conn.Read(oldMode)
if err != nil {
return
}
// sleep?
if len(oldMode) > 0 {
newMode := (oldMode[0] & 0x7F) | 0x10
reg := byte(_Mode1)
if _, err = conn.Write([]byte{reg, newMode}); err != nil {
return
}
reg = byte(_Prescale)
val := byte(math.Floor(preScale))
if _, err = conn.Write([]byte{reg, val}); err != nil {
return
}
reg = byte(_Mode1)
if _, err = conn.Write([]byte{reg, oldMode[0]}); err != nil {
return
}
time.Sleep(5 * time.Millisecond)
if _, err = conn.Write([]byte{reg, (oldMode[0] | 0x80)}); err != nil {
return
}
}
return
}
// setAllPWM sets all PWM channels for the given address
func (a *AdafruitMotorHatDriver) setAllPWM(conn Connection, on, off int32) (err error) {
// register and values to be written to that register
regVals := make(map[int][]byte)
regVals[0] = []byte{byte(_AllLedOnL), byte(on & 0xff)}
regVals[1] = []byte{byte(_AllLedOnH), byte(on >> 8)}
regVals[2] = []byte{byte(_AllLedOffL), byte(off & 0xFF)}
regVals[3] = []byte{byte(_AllLedOffH), byte(off >> 8)}
for i := 0; i < len(regVals); i++ {
if _, err = conn.Write(regVals[i]); err != nil {
return
}
}
return
}
func (a *AdafruitMotorHatDriver) setPin(conn Connection, pin byte, value int32) (err error) {
if value == 0 {
return a.setPWM(conn, pin, 0, 4096)
}
if value == 1 {
return a.setPWM(conn, pin, 4096, 0)
}
return errors.New("Invalid pin")
}
// SetDCMotorSpeed will set the appropriate pins to run the specified DC motor
// for the given speed.
func (a *AdafruitMotorHatDriver) SetDCMotorSpeed(dcMotor int, speed int32) (err error) {
if err = a.setPWM(a.motorHatConnection, a.dcMotors[dcMotor].pwmPin, 0, speed*16); err != nil {
return
}
return
}
// RunDCMotor will set the appropriate pins to run the specified DC motor for
// the given direction
func (a *AdafruitMotorHatDriver) RunDCMotor(dcMotor int, dir AdafruitDirection) (err error) {
switch {
case dir == AdafruitForward:
if err = a.setPin(a.motorHatConnection, a.dcMotors[dcMotor].in2Pin, 0); err != nil {
return
}
if err = a.setPin(a.motorHatConnection, a.dcMotors[dcMotor].in1Pin, 1); err != nil {
return
}
case dir == AdafruitBackward:
if err = a.setPin(a.motorHatConnection, a.dcMotors[dcMotor].in1Pin, 0); err != nil {
return
}
if err = a.setPin(a.motorHatConnection, a.dcMotors[dcMotor].in2Pin, 1); err != nil {
return
}
case dir == AdafruitRelease:
if err = a.setPin(a.motorHatConnection, a.dcMotors[dcMotor].in1Pin, 0); err != nil {
return
}
if err = a.setPin(a.motorHatConnection, a.dcMotors[dcMotor].in2Pin, 0); err != nil {
return
}
}
return
}
func (a *AdafruitMotorHatDriver) oneStep(motor int, dir AdafruitDirection, style AdafruitStepStyle) (steps int, err error) {
pwmA := 255
pwmB := 255
// Determine the stepping procedure
switch {
case style == AdafruitSingle:
if (a.stepperMotors[motor].currentStep / (stepperMicrosteps / 2) % 2) != 0 {
// we're at an odd step
if dir == AdafruitForward {
a.stepperMotors[motor].currentStep += stepperMicrosteps / 2
} else {
a.stepperMotors[motor].currentStep -= stepperMicrosteps / 2
}
} else {
// go to next even step
if dir == AdafruitForward {
a.stepperMotors[motor].currentStep += stepperMicrosteps
} else {
a.stepperMotors[motor].currentStep -= stepperMicrosteps
}
}
case style == AdafruitDouble:
if (a.stepperMotors[motor].currentStep / (stepperMicrosteps / 2) % 2) == 0 {
// we're at an even step, weird
if dir == AdafruitForward {
a.stepperMotors[motor].currentStep += stepperMicrosteps / 2
} else {
a.stepperMotors[motor].currentStep -= stepperMicrosteps / 2
}
} else {
// go to next odd step
if dir == AdafruitForward {
a.stepperMotors[motor].currentStep += stepperMicrosteps
} else {
a.stepperMotors[motor].currentStep -= stepperMicrosteps
}
}
case style == AdafruitInterleave:
if dir == AdafruitForward {
a.stepperMotors[motor].currentStep += stepperMicrosteps / 2
} else {
a.stepperMotors[motor].currentStep -= stepperMicrosteps / 2
}
case style == AdafruitMicrostep:
if dir == AdafruitForward {
a.stepperMotors[motor].currentStep++
} else {
a.stepperMotors[motor].currentStep--
}
// go to next step and wrap around
a.stepperMotors[motor].currentStep += stepperMicrosteps * 4
a.stepperMotors[motor].currentStep %= stepperMicrosteps * 4
pwmA = 0
pwmB = 0
currStep := a.stepperMotors[motor].currentStep
if currStep >= 0 && currStep < stepperMicrosteps {
pwmA = stepperMicrostepCurve[stepperMicrosteps-currStep]
pwmB = stepperMicrostepCurve[currStep]
} else if currStep >= stepperMicrosteps && currStep < stepperMicrosteps*2 {
pwmA = stepperMicrostepCurve[currStep-stepperMicrosteps]
pwmB = stepperMicrostepCurve[stepperMicrosteps*2-currStep]
} else if currStep >= stepperMicrosteps*2 && currStep < stepperMicrosteps*3 {
pwmA = stepperMicrostepCurve[stepperMicrosteps*3-currStep]
pwmB = stepperMicrostepCurve[currStep-stepperMicrosteps*2]
} else if currStep >= stepperMicrosteps*3 && currStep < stepperMicrosteps*4 {
pwmA = stepperMicrostepCurve[currStep-stepperMicrosteps*3]
pwmB = stepperMicrostepCurve[stepperMicrosteps*4-currStep]
}
} //switch
//go to next 'step' and wrap around
a.stepperMotors[motor].currentStep += stepperMicrosteps * 4
a.stepperMotors[motor].currentStep %= stepperMicrosteps * 4
//only really used for microstepping, otherwise always on!
if err = a.setPWM(a.motorHatConnection, a.stepperMotors[motor].pwmPinA, 0, int32(pwmA*16)); err != nil {
return
}
if err = a.setPWM(a.motorHatConnection, a.stepperMotors[motor].pwmPinB, 0, int32(pwmB*16)); err != nil {
return
}
var coils []int32
currStep := a.stepperMotors[motor].currentStep
if style == AdafruitMicrostep {
switch {
case currStep >= 0 && currStep < stepperMicrosteps:
coils = []int32{1, 1, 0, 0}
case currStep >= stepperMicrosteps && currStep < stepperMicrosteps*2:
coils = []int32{0, 1, 1, 0}
case currStep >= stepperMicrosteps*2 && currStep < stepperMicrosteps*3:
coils = []int32{0, 0, 1, 1}
case currStep >= stepperMicrosteps*3 && currStep < stepperMicrosteps*4:
coils = []int32{1, 0, 0, 1}
}
} else {
// step-2-coils is initialized in init()
coils = step2coils[(currStep / (stepperMicrosteps / 2))]
}
if adafruitDebug {
log.Printf("[adafruit_driver] currStep: %d, index into step2coils: %d\n",
currStep, (currStep / (stepperMicrosteps / 2)))
log.Printf("[adafruit_driver] coils state = %v", coils)
}
if err = a.setPin(a.motorHatConnection, a.stepperMotors[motor].ain2, coils[0]); err != nil {
return
}
if err = a.setPin(a.motorHatConnection, a.stepperMotors[motor].bin1, coils[1]); err != nil {
return
}
if err = a.setPin(a.motorHatConnection, a.stepperMotors[motor].ain1, coils[2]); err != nil {
return
}
if err = a.setPin(a.motorHatConnection, a.stepperMotors[motor].bin2, coils[3]); err != nil {
return
}
return a.stepperMotors[motor].currentStep, nil
}
// SetStepperMotorSpeed sets the seconds-per-step for the given Stepper Motor.
func (a *AdafruitMotorHatDriver) SetStepperMotorSpeed(stepperMotor int, rpm int) (err error) {
revSteps := a.stepperMotors[stepperMotor].revSteps
a.stepperMotors[stepperMotor].secPerStep = 60.0 / float64(revSteps*rpm)
a.stepperMotors[stepperMotor].stepCounter = 0
return
}
// Step will rotate the stepper motor the given number of steps, in the given direction and step style.
func (a *AdafruitMotorHatDriver) Step(motor, steps int, dir AdafruitDirection, style AdafruitStepStyle) (err error) {
secPerStep := a.stepperMotors[motor].secPerStep
latestStep := 0
if style == AdafruitInterleave {
secPerStep = secPerStep / 2.0
}
if style == AdafruitMicrostep {
secPerStep /= float64(stepperMicrosteps)
steps *= stepperMicrosteps
}
if adafruitDebug {
log.Printf("[adafruit_driver] %f seconds per step", secPerStep)
}
for i := 0; i < steps; i++ {
if latestStep, err = a.oneStep(motor, dir, style); err != nil {
return
}
time.Sleep(time.Duration(secPerStep) * time.Second)
}
// As documented in the Adafruit python driver:
// This is an edge case, if we are in between full steps, keep going to end on a full step
if style == AdafruitMicrostep {
for latestStep != 0 && latestStep != stepperMicrosteps {
if latestStep, err = a.oneStep(motor, dir, style); err != nil {
return
}
time.Sleep(time.Duration(secPerStep) * time.Second)
}
}
return
}
func init() {
stepperMicrostepCurve = []int{0, 50, 98, 142, 180, 212, 236, 250, 255}
step2coils[0] = []int32{1, 0, 0, 0}
step2coils[1] = []int32{1, 1, 0, 0}
step2coils[2] = []int32{0, 1, 0, 0}
step2coils[3] = []int32{0, 1, 1, 0}
step2coils[4] = []int32{0, 0, 1, 0}
step2coils[5] = []int32{0, 0, 1, 1}
step2coils[6] = []int32{0, 0, 0, 1}
step2coils[7] = []int32{1, 0, 0, 1}
}