You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the current iteration, if you move for example from rev 0.9 to .01, instead of moving the shortest part, it will go back doing the long direction.
now if I go from 259 to 1°, given the following code:
void StepperControl::moveStepperToAngle(float angle)
{
// Ensure angle is within 0-360 range for safety
angle = fmod(angle, 360.0);
if (angle < 0)
{
angle += 360.0;
}
long steps = static_cast<long>((angle / 360.0) * STEPPER_STEPS_REV);
Serial.println("Moving to: " + String(angle) + "°");
Serial.println("Moving to position: " + String(steps));
// Move to the calculated position in steps
stepper.moveToPositionInSteps(steps);
}
It will go back on the long direction not the shortest path.
Something like this
void StepperControl::moveStepperToAngle(float angle)
{
// Normalize the target angle to the range [0, 360)
angle = fmod(angle, 360.0);
if (angle < 0)
{
angle += 360.0;
}
// Convert target angle to steps
long targetSteps = static_cast<long>((angle / 360.0) * STEPPER_STEPS_REV);
// Get the current position in steps
long currentSteps = stepper.getCurrentPositionInSteps();
// Calculate the difference in steps, considering the shortest path
long deltaSteps = targetSteps - currentSteps;
// If delta is greater than half a revolution, adjust to take the shorter path
if (deltaSteps > STEPPER_STEPS_REV / 2)
{
deltaSteps -= STEPPER_STEPS_REV;
}
else if (deltaSteps < -STEPPER_STEPS_REV / 2)
{
deltaSteps += STEPPER_STEPS_REV;
}
Serial.println("Moving directly to angle: " + String(angle) + "°");
stepper.moveRelativeInSteps(deltaSteps);
}
void StepperControl::moveStepperToStepPosition(long stepPosition)
{
// Normalize stepPosition to ensure it's within [0, STEPPER_STEPS_REV)
stepPosition = stepPosition % STEPPER_STEPS_REV;
if (stepPosition < 0)
{
stepPosition += STEPPER_STEPS_REV;
}
// Convert stepPosition to an angle (0 to 360 degrees)
float angle = (stepPosition / static_cast<float>(STEPPER_STEPS_REV)) * 360.0;
// Get the current position in steps
long currentSteps = stepper.getCurrentPositionInSteps();
// Calculate the shortest path in steps
long deltaSteps = stepPosition - currentSteps;
if (deltaSteps > STEPPER_STEPS_REV / 2)
{
deltaSteps -= STEPPER_STEPS_REV;
}
else if (deltaSteps < -STEPPER_STEPS_REV / 2)
{
deltaSteps += STEPPER_STEPS_REV;
}
Serial.println("Moving to step position: " + String(stepPosition) + " (angle: " + String(angle) + "°)");
stepper.moveRelativeInSteps(deltaSteps);
}
The text was updated successfully, but these errors were encountered:
Please Feel free to create a pull request, then I will review it.
In general so far the library was more intended for linear movements but I do not see a reason to add such functions as long as they do not impact performance.
For the blocking function set it should not make much difference, for the non blocking part this feature might be a bit more complex / involve more changes depending on the implementation route. But if all you need is basically a helper function to calculate an angle into steps, it should be no problem
Add support for angles.
In the current iteration, if you move for example from rev 0.9 to .01, instead of moving the shortest part, it will go back doing the long direction.
now if I go from 259 to 1°, given the following code:
It will go back on the long direction not the shortest path.
Something like this
The text was updated successfully, but these errors were encountered: