forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
easy_driver.go
281 lines (225 loc) · 6.5 KB
/
easy_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
package gpio
import (
"errors"
"strconv"
"time"
"gobot.io/x/gobot/v2"
)
// EasyDriver object
type EasyDriver struct {
gobot.Commander
name string
connection DigitalWriter
stepPin string
dirPin string
enPin string
sleepPin string
angle float32
rpm uint
dir int8
moving bool
stepNum int
enabled bool
sleeping bool
}
// NewEasyDriver returns a new EasyDriver from SparkFun (https://www.sparkfun.com/products/12779)
// TODO: Support selecting phase input instead of hard-wiring MS1 and MS2 to board truth table
// This should also work for the BigEasyDriver (untested)
// A - DigitalWriter
// stepPin - Pin corresponding to step input on EasyDriver
// dirPin - Pin corresponding to dir input on EasyDriver. Optional
// enPin - Pin corresponding to enabled input on EasyDriver. Optional
// sleepPin - Pin corresponding to sleep input on EasyDriver. Optional
// angle - Step angle of motor
func NewEasyDriver(a DigitalWriter, angle float32, stepPin string, dirPin string, enPin string, sleepPin string) *EasyDriver {
d := &EasyDriver{
Commander: gobot.NewCommander(),
name: gobot.DefaultName("EasyDriver"),
connection: a,
stepPin: stepPin,
dirPin: dirPin,
enPin: enPin,
sleepPin: sleepPin,
angle: angle,
rpm: 1,
dir: 1,
enabled: true,
sleeping: false,
}
// panic if step pin isn't set
if stepPin == "" {
panic("Step pin is not set")
}
// 1/4 of max speed. Not too fast, not too slow
d.rpm = d.GetMaxSpeed() / 4
d.AddCommand("Move", func(params map[string]interface{}) interface{} {
degs, _ := strconv.Atoi(params["degs"].(string))
return d.Move(degs)
})
d.AddCommand("Step", func(params map[string]interface{}) interface{} {
return d.Step()
})
d.AddCommand("Run", func(params map[string]interface{}) interface{} {
return d.Run()
})
d.AddCommand("Stop", func(params map[string]interface{}) interface{} {
return d.Stop()
})
return d
}
// Name of EasyDriver
func (d *EasyDriver) Name() string { return d.name }
// SetName sets name for EasyDriver
func (d *EasyDriver) SetName(n string) { d.name = n }
// Connection returns EasyDriver's connection
func (d *EasyDriver) Connection() gobot.Connection { return d.connection.(gobot.Connection) }
// Start implements the Driver interface
func (d *EasyDriver) Start() (err error) { return }
// Halt implements the Driver interface; stops running the stepper
func (d *EasyDriver) Halt() (err error) {
d.Stop()
return
}
// Move the motor given number of degrees at current speed.
func (d *EasyDriver) Move(degs int) (err error) {
if d.moving {
// don't do anything if already moving
return
}
d.moving = true
steps := int(float32(degs) / d.angle)
for i := 0; i < steps; i++ {
if !d.moving {
// don't continue to step if driver is stopped
break
}
d.Step()
}
d.moving = false
return
}
// Step the stepper 1 step
func (d *EasyDriver) Step() (err error) {
stepsPerRev := d.GetMaxSpeed()
// a valid steps occurs for a low to high transition
d.connection.DigitalWrite(d.stepPin, 0)
// 1 minute / steps per revolution / revolutions per minute
// let's keep it as Microseconds so we only have to do integer math
time.Sleep(time.Duration(60*1000*1000/stepsPerRev/d.rpm) * time.Microsecond)
d.connection.DigitalWrite(d.stepPin, 1)
// increment or decrement the number of steps by 1
d.stepNum += int(d.dir)
return
}
// Run the stepper continuously
func (d *EasyDriver) Run() (err error) {
if d.moving {
// don't do anything if already moving
return
}
d.moving = true
go func() {
for d.moving {
d.Step()
}
}()
return
}
// Stop running the stepper
func (d *EasyDriver) Stop() (err error) {
d.moving = false
return
}
// SetDirection sets the direction to be moving. Valid directions are "cw" or "ccw"
func (d *EasyDriver) SetDirection(dir string) (err error) {
// can't change direct if dirPin isn't set
if d.dirPin == "" {
return errors.New("dirPin is not set")
}
if dir == "ccw" {
d.dir = -1
d.connection.DigitalWrite(d.dirPin, 1) // high is ccw
} else { // default to cw, even if user specified wrong value
d.dir = 1
d.connection.DigitalWrite(d.dirPin, 0) // low is cw
}
return
}
// SetSpeed sets the speed of the motor in RPMs. 1 is the lowest and GetMaxSpeed is the highest
func (d *EasyDriver) SetSpeed(rpm uint) (err error) {
if rpm < 1 {
d.rpm = 1
} else if rpm > d.GetMaxSpeed() {
d.rpm = d.GetMaxSpeed()
} else {
d.rpm = rpm
}
return
}
// GetMaxSpeed returns the max speed of the stepper
func (d *EasyDriver) GetMaxSpeed() uint {
return uint(360 / d.angle)
}
// GetCurrentStep returns current step number
func (d *EasyDriver) GetCurrentStep() int {
return d.stepNum
}
// IsMoving returns a bool stating whether motor is currently in motion
func (d *EasyDriver) IsMoving() bool {
return d.moving
}
// Enable enables all motor output
func (d *EasyDriver) Enable() (err error) {
// can't enable if enPin isn't set. This is fine normally since it will be enabled by default
if d.enPin == "" {
return errors.New("enPin is not set. Board is enabled by default")
}
d.enabled = true
d.connection.DigitalWrite(d.enPin, 0) // enPin is active low
return
}
// Disable disables all motor output
func (d *EasyDriver) Disable() (err error) {
// can't disable if enPin isn't set
if d.enPin == "" {
return errors.New("enPin is not set")
}
// let's stop the motor first
d.Stop()
d.enabled = false
d.connection.DigitalWrite(d.enPin, 1) // enPin is active low
return
}
// IsEnabled returns a bool stating whether motor is enabled
func (d *EasyDriver) IsEnabled() bool {
return d.enabled
}
// Sleep puts the driver to sleep and disables all motor output. Low power mode.
func (d *EasyDriver) Sleep() (err error) {
// can't sleep if sleepPin isn't set
if d.sleepPin == "" {
return errors.New("sleepPin is not set")
}
// let's stop the motor first
d.Stop()
d.sleeping = true
d.connection.DigitalWrite(d.sleepPin, 0) // sleepPin is active low
return
}
// Wake wakes up the driver
func (d *EasyDriver) Wake() (err error) {
// can't wake if sleepPin isn't set
if d.sleepPin == "" {
return errors.New("sleepPin is not set")
}
d.sleeping = false
d.connection.DigitalWrite(d.sleepPin, 1) // sleepPin is active low
// we need to wait 1ms after sleeping before doing a step to charge the step pump (according to data sheet)
// this will ensure that happens
time.Sleep(1 * time.Millisecond)
return
}
// IsSleeping returns a bool stating whether motor is enabled
func (d *EasyDriver) IsSleeping() bool {
return d.sleeping
}