Skip to content
Snippets Groups Projects
Commit 4ac27494 authored by Alberto Nidasio's avatar Alberto Nidasio
Browse files

[Stepper] Updated microstepping implementation

parent 3fa9b5b7
No related branches found
No related tags found
No related merge requests found
......@@ -125,6 +125,7 @@ protected:
miosix::GpioPin directionPin;
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
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment