-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDCMotor.cpp
More file actions
305 lines (251 loc) · 6.92 KB
/
Copy pathDCMotor.cpp
File metadata and controls
305 lines (251 loc) · 6.92 KB
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
//==========================================================
//
// FILE : DCMotor.cpp
//
// PROJECT : Driving DC Motors with PWM
//
// AUTHOR : Bill Daniels ([email protected])
// See LICENSE.md
//
//==========================================================
#include <Arduino.h>
#include <string.h>
#include "DCMotor.h"
//--- Constructor ------------------------------------------
DCMotor::DCMotor (int PWMPin1, int PWMPin2, int LLSwitchPin, int ULSwitchPin)
{
// Save the specified pins for the both driver types
pwmPin1 = PWMPin1;
pwmPin2 = PWMPin2;
llSwitchPin = LLSwitchPin;
ulSwitchPin = ULSwitchPin;
// Set pin modes
pinMode (pwmPin1, OUTPUT);
pinMode (pwmPin2, OUTPUT);
if (llSwitchPin != 0)
pinMode (llSwitchPin, INPUT_PULLUP);
if (ulSwitchPin != 0)
pinMode (ulSwitchPin, INPUT_PULLUP);
// Maker sure motor is stopped
analogWrite (pwmPin1, 0);
analogWrite (pwmPin2, 0);
state = STOPPED;
// Default ramping
SetRamp (4);
}
//--- Run -------------------------------------------------
// Must be called inside your loop function with no delay.
RunReturn DCMotor::Run ()
{
// Is the motor RUNNING
if (state != STOPPED)
{
// Check limit switches, if specified
if ((llSwitchPin != 0) && digitalRead (llSwitchPin) == LOW)
{
// Lower limit switch triggered
EStop ();
return LIMIT_SWITCH_LOWER;
}
if ((ulSwitchPin != 0) && digitalRead (ulSwitchPin) == LOW)
{
// Upper limit switch triggered
EStop ();
return LIMIT_SWITCH_UPPER;
}
// Is motor still ramping up/down?
if (state != AT_SPEED)
{
// Is it time to adjust speed?
now = micros ();
if (now >= nextPWMMicros)
{
// Adjust speed
currentPWM += pwmIncrement; // +1 for ramping up, -1 for ramping down
if (currentDirection == CW)
analogWrite (pwmPin2, currentPWM);
else
analogWrite (pwmPin1, currentPWM);
// Has the motor reached target speed?
if (currentPWM == targetPWM)
{
if (targetPWM == 0)
state = STOPPED;
else
state = AT_SPEED;
}
else
// Still ramping, set new speed
nextPWMMicros = now + rampPeriod;
}
}
}
return OKAY;
}
//--- SetRamp ---------------------------------------------
void DCMotor::SetRamp (int ramp)
{
// Must be 0..9
if (ramp >= 0 && ramp <= 9)
{
// Set run time for each ramping speed in microseconds
rampPeriod = 2000L * ramp + 2000L;
}
}
//--- Go --------------------------------------------------
void DCMotor::Go (Direction dir, int speed)
{
// We need to handle this command from any MotorState.
// If STOPPED, we just ramp up to speed.
// If running in the same direction, we alter our speed
// and either ramp up or down to the new speed.
// If running in the opposite direction, we need to
// ramp down to a stop, then ramp back up to speed
// in the opposite direction.
// Check speed param
if (speed > 100) speed = 100;
else if (speed < 0) speed = 0;
if (state != STOPPED)
{
//--- Already running ---
if (dir == currentDirection)
{
//--- Same direction ---
targetPWM = speed * 250 / 100; // new target speed
// Do we need to ramp up or down to the new speed?
if (targetPWM > currentPWM)
{
pwmIncrement = 1; // ramp up
state = RAMPING_UP;
}
else if (targetPWM < currentPWM)
{
pwmIncrement = -1; // ramp down
state = RAMPING_DOWN;
}
return; // Ignore same speed request
}
else
{
//--- Reverse direction ---
Stop ();
while (state != STOPPED) Run (); // Wait for motor to stop
// Fall into code below to begin new motion
}
}
if (speed != 0)
{
// Begin ramping up to speed from STOPPED:
// Speed is specified as a percentage (0-100) of max pulse width value of 250
// 50% speed would be a pulse width of 125
// Set direction
currentDirection = dir;
if (currentDirection == CW)
digitalWrite (pwmPin1, LOW); // pwmPin2 will be the PWM pin
else
digitalWrite (pwmPin2, LOW); // pwmPin1 will be the PWM pin
currentPWM = 0; // starting from stopped
targetPWM = speed * 250 / 100; // pwm is % of 250
pwmIncrement = 1; // ramping up
// Begin motion
nextPWMMicros = micros ();
state = RAMPING_UP;
}
}
//--- Stop ------------------------------------------------
void DCMotor::Stop ()
{
if (state != STOPPED && currentPWM != 0)
{
// Begin ramping down to stop:
targetPWM = 0;
pwmIncrement = -1; // ramping down
// Begin motion
nextPWMMicros = micros ();
state = RAMPING_DOWN;
}
}
//--- EStop -----------------------------------------------
void DCMotor::EStop ()
{
// Emergency Stop
analogWrite (pwmPin1, 0);
analogWrite (pwmPin2, 0);
state = STOPPED;
}
//--- GetState --------------------------------------------
MotorState DCMotor::GetState ()
{
// Return current motor state
return state;
}
//=========================================================
// ExecuteCommand
//=========================================================
const char * DCMotor::ExecuteCommand (const char *packet)
{
char command[3];
int ramp, speed;
// Initialize return string
ecReturnString[0] = 0;
// Command string must be at least 2 chars
if (strlen(packet) < 2)
{
strcpy (ecReturnString, "Bad command");
return ecReturnString;
}
// Set 2-Char Command and parse all commands
strncpy (command, packet, 2);
command[2] = 0;
//--- E-Stop ---
if (strcmp (command, "ES") == 0)
EStop ();
//--- Set Ramp ---
else if (strcmp (command, "SR") == 0)
{
// Check for value
if (strlen (packet) != 3)
strcpy (ecReturnString, "Missing ramp value 0-9");
else
{
ramp = atoi (packet+2);
// Check specified ramp value
if (ramp >= 0 && ramp <= 9)
SetRamp (ramp);
}
}
//--- GO ---
else if (strcmp (command, "GO") == 0)
{
// GO command must be at least 4 chars
if (strlen (packet) < 4)
strcpy (ecReturnString, "Bad command");
else
{
Direction dir = CW;
// Parse direction and speed
if (packet[2] == '<')
dir = CCW;
speed = atoi (packet + 3);
Go (dir, speed);
}
}
//--- STOP ---
else if (strcmp (command, "ST") == 0)
Stop ();
//--- GET STATE ---
else if (strcmp (command, "GS") == 0)
{
switch (state)
{
case STOPPED : strcpy (ecReturnString, "ST"); break;
case RAMPING_UP : strcpy (ecReturnString, "RU"); break;
case RAMPING_DOWN : strcpy (ecReturnString, "RD"); break;
case AT_SPEED : strcpy (ecReturnString, "AS"); break;
default : strcpy (ecReturnString, "??"); break;
}
}
else
strcpy (ecReturnString, "Unknown command");
return ecReturnString;
}