Skip to content

Instantly share code, notes, and snippets.

@jtmorris
Last active April 12, 2019 00:00
Show Gist options
  • Save jtmorris/520aabe11524ff9420e992617c9d3541 to your computer and use it in GitHub Desktop.
Save jtmorris/520aabe11524ff9420e992617c9d3541 to your computer and use it in GitHub Desktop.
/**
* Drive a collection of stepper motors, 1 step at a time in sequence, a desired number of steps.
* Used to approximate simultaneous motion of each stepper by not blocking until motion is completed.
*
* steppers = Array of Stepper library stepper motors.
* numSteppers = length of steppers array.
* numStepsMap = number of total steps to advance by. Positve number moves forward. Negative number moves backward.
*/
void simultaneousStepperDrive(Stepper steppers[], int numSteppers, int numStepsMap[]) {
int stepsExecuted = 0;
int numSteps,
numStepsAbs;
while(true) {
bool stepsRemaining = false;
for(int i=0; i < numSteppers; i++) {
numSteps = numStepsMap[i];
numStepsAbs = abs(numSteps);
if(numStepsAbs > stepsExecuted) {
steppers[i].step(numSteps/numStepsAbs); // One step in desired direction
// Determine if we're done after this step or not. If not, indicate steps remaining.
if((numStepsAbs - stepsExecuted -1) > 0) {
stepsRemaining = true;
}
}
}
if(!stepsRemaining) {
break;
}
stepsExecuted++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment