diff --git a/src/shared/actuators/Stepper.h b/src/shared/actuators/Stepper.h index 614058fc077a95f6228ae3380261dedf3c546fa2..04e1f31cab38c8569ee5d00a1f4748f5b98701e2 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 6f381e4bd8ab4a17402f97e2ef6a83cd0d20d99f..a6fea77b6a15838a8255b5ef7ddcd9edf8df4699 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)