Skip to content

Commit 5276cb3

Browse files
committed
Add STOP command
1 parent 3d86937 commit 5276cb3

5 files changed

Lines changed: 46 additions & 7 deletions

File tree

ArduinoStepperController.pde

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ void handler( CommandInterpreter::Message *msg ) {
6262
case M_GO:
6363
handleGO(msg->fields[0], msg->fields[1], msg->fields[2]);
6464
break;
65+
case M_STOP:
66+
handleSTOP();
67+
break;
6568
case M_SET:
6669
handleSET(msg->fields[0], msg->fields[1], msg->fields[2]);
6770
break;
@@ -98,6 +101,14 @@ void handleGO(uint8_t axis, long position, long time) {
98101
}
99102

100103

104+
void handleSTOP() {
105+
for ( uint8_t axis = 1; axis <= Stepper::count(); axis++) {
106+
Stepper::getStepper(axis).stop();
107+
}
108+
109+
commander.sendACK("STOP");
110+
}
111+
101112
void handleGET(uint8_t parameterName, uint8_t axis) {
102113
char buffer[50];
103114
boolean good = false;

commands.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
enum MESSAGE_TYPE {
1010
M_GO,
11+
M_STOP,
1112
M_SET,
1213
M_GET,
1314
M_HOME,

commands.pde

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
* ACK is sent by the controller immediately after GO recvd. POSITION and TIME may be modified if POSITION was out of range or TIME was too short.
1212
* NOTICE DONE is sent when the move completes
1313
*
14+
* STOP Tells the controller to stop the motion of all axis immediately
15+
* ACK is sent by the controller immediately after DONE recvd.
16+
* NOTICE DONE will be recieved on any axis that were moving
17+
*
1418
* SET param value
1519
* ACK SET param value
1620
* Sets PARAM to VALUE. Valid PARAMs are: VERSION, INDEX, MAX_VELOCITY, ACCEL, POS
@@ -68,7 +72,7 @@ CommandInterpreter::ParameterDefinition CommandInterpreter::parameterTypes[] = {
6872

6973
CommandInterpreter::MessageTypeDefinition CommandInterpreter::messageTypes[] = {
7074
{ "GO", M_GO , GO_VALUES }, // GO axis position time
71-
{ "SET", M_SET , SET_VALUES }, // SET param axis value
75+
{ "STOP", M_STOP , NO_VALUES }, // STOP
7276
{ "GET", M_GET , GET_VALUES }, // GET param axis
7377
{ "HOME", M_HOME , HOME_VALUES }, // HOME axis
7478
{ "STATE", M_STATE , NO_VALUES }, // STATE

stepper.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ enum STEPPER_STATE {
2222
S_HOMING_A,
2323
S_HOMING_B,
2424
S_FINISHED_HOMING,
25+
S_USER_STOP,
2526
S_ERROR,
2627
};
2728

@@ -67,6 +68,9 @@ class Stepper {
6768
// Find the limit switch, and set it to the 0 position
6869
boolean home();
6970

71+
// Unconditionally stops the stepper
72+
void stop();
73+
7074
// Get the position
7175
long getPosition();
7276

@@ -113,6 +117,8 @@ class Stepper {
113117
uint8_t limitPin;
114118

115119
STEPPER_STATE state;
120+
121+
boolean forceStop; //< Flag to signal to cause the interrupt task to sop motion
116122

117123
long position; //< Current position
118124
long stepsLeft; //< Number of counts until final position is reached

stepper.pde

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Stepper::Stepper(uint8_t enablePin_, uint8_t stepPin_, uint8_t directionPin_, ui
111111

112112
void Stepper::doReset() {
113113
state = S_READY;
114-
114+
forceStop = false;
115115
position = 0;
116116

117117
digitalWrite(directionPin, LOW);
@@ -185,7 +185,7 @@ boolean Stepper::moveRelative(long steps, long& time) {
185185
}
186186

187187
boolean Stepper::home() {
188-
if ( busy() ) {
188+
if ( busy() ) {
189189
return false;
190190
}
191191

@@ -208,6 +208,14 @@ boolean Stepper::home() {
208208
return canMove;
209209
}
210210

211+
void Stepper::stop() {
212+
if ( !busy() ) {
213+
return;
214+
}
215+
216+
forceStop = true;
217+
}
218+
211219
/*
212220
function line(x0, x1, y0, y1)
213221
int deltax := x1 - x0
@@ -232,9 +240,14 @@ void Stepper::doInterrupt() {
232240
}
233241

234242
bool doneMoving = false;
235-
243+
244+
if ( forceStop ) {
245+
state = S_USER_STOP;
246+
doneMoving = true;
247+
forceStop = false;
248+
}
236249
// Check if we just walked into the limit switch
237-
if( settings.canHome && digitalRead(limitPin) == LOW ) {
250+
else if( settings.canHome && digitalRead(limitPin) == LOW ) {
238251
if ( ( direction < 0 && settings.homeDirection == H_BACKWARD ) ||
239252
( direction > 0 && settings.homeDirection == H_FORWARD ) )
240253
{
@@ -296,8 +309,8 @@ void Stepper::doInterrupt() {
296309
}
297310
}
298311

299-
// Stopping motion tasks
300-
if (doneMoving) {
312+
// Stopping motion tasks (doneMoving == TRUE)
313+
else {
301314
if (settings.stopMode = S_DISABLE) {
302315
digitalWrite(enablePin, HIGH);
303316
}
@@ -377,6 +390,10 @@ boolean Stepper::checkFinished() {
377390
state = S_READY;
378391
return true;
379392
}
393+
else if (state == S_USER_STOP) {
394+
state = S_READY;
395+
return true;
396+
}
380397

381398
return false;
382399
}

0 commit comments

Comments
 (0)