Skip to content
Snippets Groups Projects

[CountedPWM][Stepper][StepperPWM] Enhanced PWM module to implement a non-blocking Stepper driver

Merged Emilio Corigliano requested to merge stepper-driver-update into main
1 file
+ 56
28
Compare changes
  • Side-by-side
  • Inline
@@ -39,6 +39,7 @@ public:
MICROSTEP_4,
MICROSTEP_8,
MICROSTEP_16,
UNDEFINED
};
/**
@@ -62,6 +63,25 @@ public:
miosix::GpioPin ms2Pin = MockGpioPin(),
miosix::GpioPin ms3Pin = MockGpioPin());
/**
* @brief Construct a new Stepper object.
*
* This constructor must be choosen when the amount of the
* microsteps can't be changed programmatically (e.g. is set by physical
* switches). By default the direction pin is held low when moving forward.
* If the given speed is negative the motor wont move.
*
* @param stepPin Pin connected to the step signal of the driver.
* @param directionPin Pin connected to the direction signal of the driver.
* @param speed Number of rotations per second.
* @param stepAngle Angle covered by one motor step.
* @param revertDirection Whether or not revert the direction signal.
* @param microsteps The number of microsteps
*/
Stepper(miosix::GpioPin stepPin, miosix::GpioPin directionPin, float speed,
float stepAngle, bool revertDirection, uint8_t microsteps,
miosix::GpioPin enablePin = MockGpioPin());
void enable();
void disable();
@@ -73,6 +93,9 @@ public:
/**
* @brief Set the motor driver microstepping configuration.
*
* This method has no effect if microsteps are changed via physical
* switches.
*/
void setMicrostepping(Microstep microstep);
@@ -117,14 +140,13 @@ public:
float getCurrentDegPosition();
private:
int getMicrosteppingValue();
miosix::GpioPin stepPin;
miosix::GpioPin directionPin;
float speed;
float stepAngle; ///< Degrees per step.
bool revertDirection;
Microstep microstep;
uint8_t numMicrosteps;
miosix::GpioPin enablePin;
miosix::GpioPin ms1Pin;
miosix::GpioPin ms2Pin;
@@ -140,8 +162,8 @@ inline Stepper::Stepper(miosix::GpioPin stepPin, miosix::GpioPin directionPin,
miosix::GpioPin ms3Pin)
: stepPin(stepPin), directionPin(directionPin), speed(speed),
stepAngle(stepAngle), revertDirection(revertDirection),
microstep(microstep), enablePin(enablePin), ms1Pin(ms1Pin),
ms2Pin(ms2Pin), ms3Pin(ms3Pin)
microstep(microstep), numMicrosteps(1), enablePin(enablePin),
ms1Pin(ms1Pin), ms2Pin(ms2Pin), ms3Pin(ms3Pin)
{
if (speed < 0)
speed = 0;
@@ -152,6 +174,22 @@ inline Stepper::Stepper(miosix::GpioPin stepPin, miosix::GpioPin directionPin,
disable();
}
inline Stepper::Stepper(miosix::GpioPin stepPin, miosix::GpioPin directionPin,
float speed, float stepAngle, bool revertDirection,
uint8_t microsteps, miosix::GpioPin enablePin)
: stepPin(stepPin), directionPin(directionPin), speed(speed),
stepAngle(stepAngle), revertDirection(revertDirection),
microstep(Microstep::UNDEFINED), numMicrosteps(microsteps),
enablePin(enablePin), ms1Pin{MockGpioPin()}, ms2Pin{MockGpioPin()},
ms3Pin{MockGpioPin()}
{
if (speed < 0)
speed = 0;
// Start with the motor disabled
disable();
}
inline void Stepper::enable() { enablePin.low(); }
inline void Stepper::disable() { enablePin.high(); }
@@ -168,26 +206,33 @@ inline void Stepper::setMicrostepping(Microstep microstep)
ms1Pin.low();
ms2Pin.low();
ms3Pin.low();
numMicrosteps = 1;
break;
case Microstep::MICROSTEP_2:
ms1Pin.high();
ms2Pin.low();
ms3Pin.low();
numMicrosteps = 2;
break;
case Microstep::MICROSTEP_4:
ms1Pin.low();
ms2Pin.high();
ms3Pin.low();
numMicrosteps = 4;
break;
case Microstep::MICROSTEP_8:
ms1Pin.high();
ms2Pin.high();
ms3Pin.low();
numMicrosteps = 8;
break;
case Microstep::MICROSTEP_16:
ms1Pin.high();
ms2Pin.high();
ms3Pin.high();
numMicrosteps = 16;
break;
case Microstep::UNDEFINED:
break;
}
}
@@ -199,8 +244,10 @@ inline void Stepper::move(int32_t steps)
if (speed == 0)
return;
printf("Num steps: %d\n", steps);
unsigned int halfStepDelay =
1e6 / (speed * 360 / stepAngle * getMicrosteppingValue());
1e6 / (speed * 360 / stepAngle * numMicrosteps);
if (revertDirection == (steps >= 0))
directionPin.low();
@@ -222,7 +269,7 @@ inline void Stepper::move(int32_t steps)
inline void Stepper::moveDeg(float degrees)
{
move(degrees / stepAngle * getMicrosteppingValue());
move(degrees / stepAngle * numMicrosteps);
}
inline void Stepper::setPosition(int32_t steps)
@@ -232,33 +279,14 @@ inline void Stepper::setPosition(int32_t steps)
inline void Stepper::setPositionDeg(float position)
{
setPosition(position / stepAngle * getMicrosteppingValue());
setPosition(position / stepAngle * numMicrosteps);
}
inline uint32_t Stepper::getCurrentPosition() { return currentPosition; }
inline float Stepper::getCurrentDegPosition()
{
return currentPosition * stepAngle / getMicrosteppingValue();
}
inline int Stepper::getMicrosteppingValue()
{
switch (microstep)
{
case Microstep::MICROSTEP_1:
return 1;
case Microstep::MICROSTEP_2:
return 2;
case Microstep::MICROSTEP_4:
return 4;
case Microstep::MICROSTEP_8:
return 8;
case Microstep::MICROSTEP_16:
return 16;
}
return 1;
return currentPosition * stepAngle / numMicrosteps;
}
} // namespace Boardcore
} // namespace Boardcore
\ No newline at end of file
Loading