Skip to content

Commit

Permalink
update SmoothStepper to version 1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
bimac committed Oct 16, 2020
1 parent a7b2d5f commit 90baf78
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
30 changes: 12 additions & 18 deletions firmware/Bpod_stepper/SmoothStepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void SmoothStepper::setInvertDirection(bool invertDirection) {
_invertDirection = invertDirection;
}

void SmoothStepper::setStepsPerRev(unsigned long stepsPerRev) {
void SmoothStepper::setStepsPerRev(uint32_t stepsPerRev) {
_stepsPerRev = stepsPerRev;
}

Expand All @@ -54,7 +54,7 @@ void SmoothStepper::setMaxSpeed(float vMax) {
_vMax = vMax;
}

void SmoothStepper::setPulseWidth(unsigned int pulseWidth) {
void SmoothStepper::setPulseWidth(uint16_t pulseWidth) {
_pulseWidth = pulseWidth;
}

Expand All @@ -66,12 +66,7 @@ void SmoothStepper::disableDriver() {
digitalWrite(_pinEnable, LOW ^ _invertEnable);
}

void SmoothStepper::moveSteps(long nSteps) {
float n2;
float n3;
float ci;
float m;

void SmoothStepper::moveSteps(int32_t nSteps) {
if (nSteps < 0) {
nSteps = nSteps * -1;
digitalWrite(_pinDirection, HIGH ^ _invertDirection);
Expand All @@ -82,19 +77,19 @@ void SmoothStepper::moveSteps(long nSteps) {
if (nSteps == 0) // nothing to do for nSteps == 0
return;

if (nSteps == 1) { // a single step doesn't require fancy formulas
step();
step(); // first step
if (nSteps == 1) // a single step doesn't require fancy formulas
return;
}

// calculate transition points ("linear-factor method")
m = (float) nSteps;
n2 = round(_vMax * _vMax / (0.736 * _a)); // eq24
float m = (float) nSteps;
float n2 = round(_vMax * _vMax / (0.736 * _a)); // eq24
n2 = floor(min(n2, m / 2.0)); // limit n2 to m/2
n3 = nSteps - n2; // n3 is symmetric to n2
float n3 = m - n2; // n3 is symmetric to n2
float ci;

// run the step sequence
for (long i = 1; i <= nSteps-1; i++) {
for (int32_t i = 1; i < nSteps; i++) {
if (i == 1)
ci = _c0; // first interval
else if (i < n2)
Expand All @@ -103,14 +98,13 @@ void SmoothStepper::moveSteps(long nSteps) {
ci = ci; // top speed
else
ci = ci - 2.0*ci/(4.0*(i-m)+1.0) * (i-n3)/(m-n3-1.0); // deceleration (eq25)
step(); // step once
delayMicroseconds(ci - _pulseWidth); // delay for ci microseconds
step();
}
step(); // final step
}

void SmoothStepper::moveDegrees(float degrees) {
long nSteps = round(degrees * _stepsPerRev / 360.0);
int32_t nSteps = round(degrees * _stepsPerRev / 360.0);
moveSteps(nSteps);
}

Expand Down
21 changes: 11 additions & 10 deletions firmware/Bpod_stepper/SmoothStepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ _______________________________________________________________________________
REVISION HISTORY
version 1.0 Initial release
version 1.0.0 initial release
version 1.0.1 various cleanups / style fixes (thank you: Florian Uekermann)
_______________________________________________________________________________
Expand Down Expand Up @@ -50,7 +51,7 @@ class SmoothStepper {
void setInvertDirection(bool invert);

// set the number of steps per revolution
void setStepsPerRev(unsigned long stepsPerRev);
void setStepsPerRev(uint32_t stepsPerRev);

// set the acceleration (steps / s^2)
void setAcceleration(float acceleration);
Expand All @@ -59,7 +60,7 @@ class SmoothStepper {
void setMaxSpeed(float maxSpeed);

// set the duration of step pulses (µs)
void setPulseWidth(unsigned int pulseWidth);
void setPulseWidth(uint16_t pulseWidth);

// enables the stepper motor driver (via _pinEnable)
void enableDriver();
Expand All @@ -68,24 +69,24 @@ class SmoothStepper {
void disableDriver();

// move by n steps
void moveSteps(long nSteps);
void moveSteps(int32_t nSteps);

// move by n degrees
void moveDegrees(float degrees);


private:
void step(); // step function
uint8_t _pinStep; // pin number: step
bool _invertDirection = false; // invert the direction pin?
bool _invertEnable = false; // invert the enable pin?
uint8_t _pinDirection; // pin number: direction
uint8_t _pinEnable; // pin number: enable
uint8_t _pinStep; // pin number: step
uint16_t _pulseWidth = 1; // duration of step pulses (µs)
uint32_t _stepsPerRev = 200; // steps per revolution
float _a; // acceleration (steps / s^2)
float _vMax; // maximum speed (steps / s)
float _c0; // duration of first interval (µs)
unsigned int _pulseWidth = 1; // duration of step pulses (µs)
unsigned long _stepsPerRev = 200; // steps per revolution
bool _invertEnable = false; // invert the enable pin?
bool _invertDirection = false; // invert the direction pin?
};

#endif

0 comments on commit 90baf78

Please sign in to comment.