/
StepperMotor.cs
358 lines (333 loc) · 10.9 KB
/
StepperMotor.cs
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
using Meadow.Foundation.ICs.IOExpanders;
using System;
using System.Threading;
namespace Meadow.Foundation.FeatherWings
{
/// <summary>
/// Motor style
/// </summary>
public enum Style
{
/// <summary>
/// Single
/// </summary>
SINGLE = 1,
/// <summary>
/// Double
/// </summary>
DOUBLE = 2,
/// <summary>
/// Interleave
/// </summary>
INTERLEAVE = 3,
/// <summary>
/// Microstep
/// </summary>
MICROSTEP = 4
}
/// <summary>
/// Motor direction
/// </summary>
public enum Direction
{
/// <summary>
/// Forward motor direction
/// </summary>
FORWARD,
/// <summary>
/// Backwards moto direction
/// </summary>
BACKWARD
}
/// <summary>
/// Represents a Stepper Motor
/// </summary>
public class StepperMotor : Motor
{
int _currentstep;
double _rpmDelay;
readonly int _motorSteps;
readonly byte _pwmA;
readonly byte _AIN2;
readonly byte _AIN1;
readonly byte _pwmB;
readonly byte _BIN2;
readonly byte _BIN1;
const short MICROSTEPS = 8;
readonly byte[] _microStepCurve = { 0, 50, 98, 142, 180, 212, 236, 250, 255 };
//private readonly int[] microstepcurve = {0, 25, 50, 74, 98, 120, 141, 162, 180,197, 212, 225, 236, 244, 250, 253, 255}//MICROSTEPS == 16
/// <summary>
/// Creates a Stepper motor objet un-initialized
/// </summary>
/// <param name="steps">The number of steps per revolution</param>
/// <param name="num">The Stepper motor port</param>
/// <param name="pca9685">The PCS968 diver object</param>
public StepperMotor(int steps, int num, Pca9685 pca9685) : base(pca9685)
{
if (num == 0)
{
_pwmA = 8;
_AIN2 = 9;
_AIN1 = 10;
_pwmB = 13;
_BIN2 = 12;
_BIN1 = 11;
}
else if (num == 1)
{
_pwmA = 2;
_AIN2 = 3;
_AIN1 = 4;
_pwmB = 7;
_BIN2 = 6;
_BIN1 = 5;
}
else
{
throw new ArgumentException("Stepper num must be 0 or 1");
}
_motorSteps = steps;
SetSpeed(15);
_currentstep = 0;
}
/// <summary>
/// Set the delay for the Stepper Motor speed in RPM
/// </summary>
/// <param name="rpm">The desired RPM</param>
public override void SetSpeed(short rpm)
{
_rpmDelay = 60000.0 / (_motorSteps * rpm);
}
/// <summary>
/// Move the stepper with the given RPM
/// </summary>
/// <param name="steps">The number of steps to move. Negative number moves the stepper backwards</param>
/// <param name="style">How to perform the step</param>
public virtual void Step(int steps = 1, Style style = Style.SINGLE)
{
if (steps > 0)
{
Step(steps, Direction.FORWARD, style);
}
else
{
Step(Math.Abs(steps), Direction.BACKWARD, style);
}
}
/// <summary>
/// Move the stepper with the given RPM
/// </summary>
/// <param name="steps">The number of steps to move</param>
/// <param name="direction">The direction to go</param>
/// <param name="style">How to perform the step</param>
protected virtual void Step(int steps, Direction direction, Style style)
{
int delay = (int)_rpmDelay;
if (style == Style.INTERLEAVE)
{
delay /= 2;
}
else if (style == Style.MICROSTEP)
{
delay /= MICROSTEPS;
steps *= MICROSTEPS;
}
while (steps >= 0)
{
Step(direction, style);
Thread.Sleep(delay);
steps--;
}
}
/// <summary>
/// Move the stepper one step only
/// </summary>
/// <param name="direction">The direction to go</param>
/// <param name="style">How to perform the step</param>
/// <returns>The current location</returns>
protected virtual int Step(Direction direction, Style style)
{
int ocrb, ocra;
ocra = ocrb = 255;
if (style == Style.SINGLE)
{
if ((_currentstep / (MICROSTEPS / 2)) % 2 != 0) // we're at an odd step, weird
{
if (direction == Direction.FORWARD)
{
_currentstep += MICROSTEPS / 2;
}
else
{
_currentstep -= MICROSTEPS / 2;
}
}
else
{ // go to the next even step
if (direction == Direction.FORWARD)
{
_currentstep += MICROSTEPS;
}
else
{
_currentstep -= MICROSTEPS;
}
}
}
else if (style == Style.DOUBLE)
{
if (((_currentstep / (MICROSTEPS / 2) % 2)) != 0)
{ // we're at an even step, weird
if (direction == Direction.FORWARD)
{
_currentstep += MICROSTEPS / 2;
}
else
{
_currentstep -= MICROSTEPS / 2;
}
}
else
{ // go to the next odd step
if (direction == Direction.FORWARD)
{
_currentstep += MICROSTEPS;
}
else
{
_currentstep -= MICROSTEPS;
}
}
}
else if (style == Style.INTERLEAVE)
{
if (direction == Direction.FORWARD)
{
_currentstep += MICROSTEPS / 2;
}
else
{
_currentstep -= MICROSTEPS / 2;
}
}
else if (style == Style.MICROSTEP)
{
if (direction == Direction.FORWARD)
{
_currentstep++;
}
else
{
// BACKWARDS
_currentstep--;
}
_currentstep += MICROSTEPS * 4;
_currentstep %= MICROSTEPS * 4;
ocra = ocrb = 0;
if ((_currentstep >= 0) && (_currentstep < MICROSTEPS))
{
ocra = _microStepCurve[MICROSTEPS - _currentstep];
ocrb = _microStepCurve[_currentstep];
}
else if ((_currentstep >= MICROSTEPS) && (_currentstep < MICROSTEPS * 2))
{
ocra = _microStepCurve[_currentstep - MICROSTEPS];
ocrb = _microStepCurve[MICROSTEPS * 2 - _currentstep];
}
else if ((_currentstep >= MICROSTEPS * 2) &&
(_currentstep < MICROSTEPS * 3))
{
ocra = _microStepCurve[MICROSTEPS * 3 - _currentstep];
ocrb = _microStepCurve[_currentstep - MICROSTEPS * 2];
}
else if ((_currentstep >= MICROSTEPS * 3) &&
(_currentstep < MICROSTEPS * 4))
{
ocra = _microStepCurve[_currentstep - MICROSTEPS * 3];
ocrb = _microStepCurve[MICROSTEPS * 4 - _currentstep];
}
}
_currentstep += MICROSTEPS * 4;
_currentstep %= MICROSTEPS * 4;
pca9685.SetPwm(_pwmA, 0, ocra * 16);
pca9685.SetPwm(_pwmB, 0, ocrb * 16);
// release all
int latch_state = 0; // all motor pins to 0
// Serial.println(step, DEC);
if (style == Style.MICROSTEP)
{
if ((_currentstep >= 0) && (_currentstep < MICROSTEPS))
latch_state |= 0x03;
if ((_currentstep >= MICROSTEPS) && (_currentstep < MICROSTEPS * 2))
latch_state |= 0x06;
if ((_currentstep >= MICROSTEPS * 2) && (_currentstep < MICROSTEPS * 3))
latch_state |= 0x0C;
if ((_currentstep >= MICROSTEPS * 3) && (_currentstep < MICROSTEPS * 4))
latch_state |= 0x09;
}
else
{
switch (_currentstep / (MICROSTEPS / 2))
{
case 0:
latch_state |= 0x1; // energize coil 1 only
break;
case 1:
latch_state |= 0x3; // energize coil 1+2
break;
case 2:
latch_state |= 0x2; // energize coil 2 only
break;
case 3:
latch_state |= 0x6; // energize coil 2+3
break;
case 4:
latch_state |= 0x4; // energize coil 3 only
break;
case 5:
latch_state |= 0xC; // energize coil 3+4
break;
case 6:
latch_state |= 0x8; // energize coil 4 only
break;
case 7:
latch_state |= 0x9; // energize coil 1+4
break;
}
}
if ((latch_state & 0x1) == 0x1)
{
pca9685.SetPin(_AIN2, true);
}
else
{
pca9685.SetPin(_AIN2, false);
}
if ((latch_state & 0x2) == 0x2)
{
pca9685.SetPin(_BIN1, true);
}
else
{
pca9685.SetPin(_BIN1, false);
}
if ((latch_state & 0x4) == 0x4)
{
pca9685.SetPin(_AIN1, true);
}
else
{
pca9685.SetPin(_AIN1, false);
}
if ((latch_state & 0x8) == 0x8)
{
pca9685.SetPin(_BIN2, true);
}
else
{
pca9685.SetPin(_BIN2, false);
}
return _currentstep;
}
}
}