Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added move and reset to multistepper #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/MultiStepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,44 @@ void MultiStepper::moveTo(long absolute[])
}
}

void MultiStepper::reset(){
uint8_t i;
for (i = 0; i < _num_steppers; i++)
{
_steppers[i]->setCurrentPosition(0);
}
}

void MultiStepper::move(long relative[])
{
// First find the stepper that will take the longest time to move
float longestTime = 0.0;

uint8_t i;
for (i = 0; i < _num_steppers; i++)
{
float thisTime = abs(relative[i]) / _steppers[i]->maxSpeed();

if (thisTime > longestTime)
longestTime = thisTime;
}

if (longestTime > 0.0)
{
// Now work out a new max speed for each stepper so they will all
// arrived at the same time of longestTime
for (i = 0; i < _num_steppers; i++)
{
float thisSpeed = relative[i] / longestTime;
_steppers[i]->move(relative[i]); // New target position (resets speed)
_steppers[i]->setSpeed(thisSpeed); // New speed
}
}
}




// Returns true if any motor is still running to the target position.
boolean MultiStepper::run()
{
Expand Down
12 changes: 12 additions & 0 deletions src/MultiStepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ class MultiStepper
/// the absolute position of the first stepper added by addStepper() etc. The array must be at least as long as
/// the number of steppers that have been added by addStepper, else results are undefined.
void moveTo(long absolute[]);

/// Set the target positions of all managed steppers
/// according to a coordinate array.
/// New speeds will be computed for each stepper so they will all arrive at their
/// respective targets at very close to the same time.
/// \param[in] relative An array of desired relative stepper positions. relative[0] will be used to set
/// the relative position of the first stepper added by addStepper() etc. The array must be at least as long as
/// the number of steppers that have been added by addStepper, else results are undefined.
void move(long relative[]);

/// Sets current position to zero for all steppers and stops them.
void reset();

/// Calls runSpeed() on all the managed steppers
/// that have not acheived their target position.
Expand Down