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
+ 54
10
Compare changes
  • Side-by-side
  • Inline
@@ -32,6 +32,12 @@ namespace Boardcore
class Stepper
{
public:
enum class PinConfiguration
{
COMMON_ANODE,
COMMON_CATHODE,
};
/**
* @brief Construct a new Stepper object.
*
@@ -44,10 +50,12 @@ public:
* @param stepAngle Angle covered by one motor step.
* @param revertDirection Whether or not revert the direction signal.
*/
Stepper(miosix::GpioPin stepPin, miosix::GpioPin directionPin,
float speed = 1, float stepAngle = 1.8,
bool revertDirection = false, uint16_t microStep = 1,
miosix::GpioPin enablePin = MockGpioPin());
Stepper(
miosix::GpioPin stepPin, miosix::GpioPin directionPin, float speed = 1,
float stepAngle = 1.8, bool revertDirection = false,
uint16_t microStep = 1,
PinConfiguration pinConfiguration = PinConfiguration::COMMON_CATHODE,
miosix::GpioPin enablePin = MockGpioPin());
void enable();
@@ -118,6 +126,7 @@ protected:
float stepAngle; // [deg/step]
bool revertDirection;
uint16_t microStep;
PinConfiguration pinConfig;
miosix::GpioPin enablePin;
float currentPositionDeg = 0; // Absolute position counter [degrees]
@@ -125,10 +134,11 @@ protected:
inline Stepper::Stepper(miosix::GpioPin stepPin, miosix::GpioPin directionPin,
float speed, float stepAngle, bool revertDirection,
uint16_t microStep, miosix::GpioPin enablePin)
uint16_t microStep, PinConfiguration pinConfiguration,
miosix::GpioPin enablePin)
: stepPin(stepPin), directionPin(directionPin), speed(speed),
stepAngle(stepAngle), revertDirection(revertDirection),
microStep(microStep), enablePin(enablePin)
microStep(microStep), pinConfig(pinConfiguration), enablePin(enablePin)
{
if (speed < 0)
speed = 0;
@@ -160,17 +170,51 @@ void Stepper::move(int16_t steps)
unsigned int halfStepDelay = 1e6 / (speed * 360 / stepAngle * microStep);
if (revertDirection == (steps >= 0))
directionPin.low();
{
if (pinConfig == PinConfiguration::COMMON_CATHODE)
{
directionPin.low();
}
else
{
directionPin.high();
}
}
else
directionPin.high();
{
if (pinConfig == PinConfiguration::COMMON_CATHODE)
{
directionPin.high();
}
else
{
directionPin.low();
}
}
miosix::delayUs(halfStepDelay);
int16_t stepsAbs = steps > 0 ? steps : -steps;
for (int i = 0; i < stepsAbs; i++)
{
stepPin.high();
if (pinConfig == PinConfiguration::COMMON_CATHODE)
{
stepPin.high();
}
else
{
stepPin.low();
}
miosix::delayUs(halfStepDelay);
stepPin.low();
if (pinConfig == PinConfiguration::COMMON_CATHODE)
{
stepPin.low();
}
else
{
stepPin.high();
}
miosix::delayUs(halfStepDelay);
}
Loading