From 4ac2749452a748dac4b1c076bef0af861af98646 Mon Sep 17 00:00:00 2001 From: Alberto Nidasio <alberto.nidasio@skywarder.eu> Date: Fri, 12 May 2023 01:01:44 +0200 Subject: [PATCH] [Stepper] Updated microstepping implementation --- src/shared/actuators/Stepper.h | 18 ++++++++---------- src/shared/actuators/StepperPWM.h | 4 ++-- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/shared/actuators/Stepper.h b/src/shared/actuators/Stepper.h index 614058fc0..04e1f31ca 100644 --- a/src/shared/actuators/Stepper.h +++ b/src/shared/actuators/Stepper.h @@ -123,8 +123,9 @@ protected: miosix::GpioPin stepPin; miosix::GpioPin directionPin; - float speed; // [rev/s] - float stepAngle; // [deg/step] + float speed; // [rev/s] + float stepAngle; // [deg/step] + float microStepAngle; // [deg/step] bool revertDirection; MicroStep microStep; miosix::GpioPin enablePin; @@ -163,6 +164,7 @@ inline void Stepper::setSpeed(float speed) { this->speed = speed; } inline void Stepper::setMicroStepping(MicroStep microStep) { this->microStep = microStep; + microStepAngle = stepAngle / static_cast<float>(microStep); switch (microStep) { @@ -201,8 +203,7 @@ inline void Stepper::move(int16_t steps) if (speed == 0) return; - unsigned int halfStepDelay = - 1e6 / (speed * 360 / stepAngle * static_cast<int>(microStep)); + unsigned int halfStepDelay = 1e6 / (speed * 360 / microStepAngle); if (revertDirection == (steps >= 0)) directionPin.low(); @@ -222,10 +223,7 @@ inline void Stepper::move(int16_t steps) currentPosition += steps; } -inline void Stepper::moveDeg(float degrees) -{ - move(degrees / stepAngle * static_cast<int>(microStep)); -} +inline void Stepper::moveDeg(float degrees) { move(degrees / microStepAngle); } inline void Stepper::setPosition(int16_t steps) { @@ -234,7 +232,7 @@ inline void Stepper::setPosition(int16_t steps) inline void Stepper::setPositionDeg(float position) { - setPosition(position / stepAngle * static_cast<int>(microStep)); + setPosition(position / microStepAngle); } inline int16_t Stepper::getCurrentPosition() { return currentPosition; } @@ -242,7 +240,7 @@ inline int16_t Stepper::getCurrentPosition() { return currentPosition; } inline float Stepper::getCurrentDegPosition() { return static_cast<float>(getCurrentPosition()) * stepAngle / - static_cast<int>(microStep); + static_cast<float>(microStep); } } // namespace Boardcore \ No newline at end of file diff --git a/src/shared/actuators/StepperPWM.h b/src/shared/actuators/StepperPWM.h index 6f381e4bd..a6fea77b6 100644 --- a/src/shared/actuators/StepperPWM.h +++ b/src/shared/actuators/StepperPWM.h @@ -117,13 +117,13 @@ inline StepperPWM::StepperPWM(CountedPWM &pwm, miosix::GpioPin stepPin, inline void StepperPWM::setSpeed(float speed) { this->speed = speed; - pwm.setFrequency(speed * 360 / stepAngle * static_cast<int>(microStep)); + pwm.setFrequency(speed * 360 / microStepAngle); } inline void StepperPWM::setMicroStepping(MicroStep microStep) { Stepper::setMicroStepping(microStep); - pwm.setFrequency(speed * 360 / stepAngle * static_cast<int>(microStep)); + pwm.setFrequency(speed * 360 / microStepAngle); } inline void StepperPWM::move(int16_t steps) -- GitLab