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

Add support for angles, shortest path. #48

Open
DanielCardiel opened this issue Nov 3, 2024 · 2 comments
Open

Add support for angles, shortest path. #48

DanielCardiel opened this issue Nov 3, 2024 · 2 comments

Comments

@DanielCardiel
Copy link

DanielCardiel commented Nov 3, 2024

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:

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);
}
@pkerspe
Copy link
Owner

pkerspe commented Nov 4, 2024

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

@DanielCardiel
Copy link
Author

Will do,
I created the helper function, but I mean, if the library can have it from scratch. Would be nice :)

Will do later when I go back to my project :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants