Skip to content

Commit 90baf78

Browse files
committed
update SmoothStepper to version 1.0.1
1 parent a7b2d5f commit 90baf78

File tree

2 files changed

+23
-28
lines changed

2 files changed

+23
-28
lines changed

firmware/Bpod_stepper/SmoothStepper.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void SmoothStepper::setInvertDirection(bool invertDirection) {
3737
_invertDirection = invertDirection;
3838
}
3939

40-
void SmoothStepper::setStepsPerRev(unsigned long stepsPerRev) {
40+
void SmoothStepper::setStepsPerRev(uint32_t stepsPerRev) {
4141
_stepsPerRev = stepsPerRev;
4242
}
4343

@@ -54,7 +54,7 @@ void SmoothStepper::setMaxSpeed(float vMax) {
5454
_vMax = vMax;
5555
}
5656

57-
void SmoothStepper::setPulseWidth(unsigned int pulseWidth) {
57+
void SmoothStepper::setPulseWidth(uint16_t pulseWidth) {
5858
_pulseWidth = pulseWidth;
5959
}
6060

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

69-
void SmoothStepper::moveSteps(long nSteps) {
70-
float n2;
71-
float n3;
72-
float ci;
73-
float m;
74-
69+
void SmoothStepper::moveSteps(int32_t nSteps) {
7570
if (nSteps < 0) {
7671
nSteps = nSteps * -1;
7772
digitalWrite(_pinDirection, HIGH ^ _invertDirection);
@@ -82,19 +77,19 @@ void SmoothStepper::moveSteps(long nSteps) {
8277
if (nSteps == 0) // nothing to do for nSteps == 0
8378
return;
8479

85-
if (nSteps == 1) { // a single step doesn't require fancy formulas
86-
step();
80+
step(); // first step
81+
if (nSteps == 1) // a single step doesn't require fancy formulas
8782
return;
88-
}
8983

9084
// calculate transition points ("linear-factor method")
91-
m = (float) nSteps;
92-
n2 = round(_vMax * _vMax / (0.736 * _a)); // eq24
85+
float m = (float) nSteps;
86+
float n2 = round(_vMax * _vMax / (0.736 * _a)); // eq24
9387
n2 = floor(min(n2, m / 2.0)); // limit n2 to m/2
94-
n3 = nSteps - n2; // n3 is symmetric to n2
88+
float n3 = m - n2; // n3 is symmetric to n2
89+
float ci;
9590

9691
// run the step sequence
97-
for (long i = 1; i <= nSteps-1; i++) {
92+
for (int32_t i = 1; i < nSteps; i++) {
9893
if (i == 1)
9994
ci = _c0; // first interval
10095
else if (i < n2)
@@ -103,14 +98,13 @@ void SmoothStepper::moveSteps(long nSteps) {
10398
ci = ci; // top speed
10499
else
105100
ci = ci - 2.0*ci/(4.0*(i-m)+1.0) * (i-n3)/(m-n3-1.0); // deceleration (eq25)
106-
step(); // step once
107101
delayMicroseconds(ci - _pulseWidth); // delay for ci microseconds
102+
step();
108103
}
109-
step(); // final step
110104
}
111105

112106
void SmoothStepper::moveDegrees(float degrees) {
113-
long nSteps = round(degrees * _stepsPerRev / 360.0);
107+
int32_t nSteps = round(degrees * _stepsPerRev / 360.0);
114108
moveSteps(nSteps);
115109
}
116110

firmware/Bpod_stepper/SmoothStepper.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ _______________________________________________________________________________
1818
1919
REVISION HISTORY
2020
21-
version 1.0 Initial release
21+
version 1.0.0 initial release
22+
version 1.0.1 various cleanups / style fixes (thank you: Florian Uekermann)
2223
2324
_______________________________________________________________________________
2425
@@ -50,7 +51,7 @@ class SmoothStepper {
5051
void setInvertDirection(bool invert);
5152

5253
// set the number of steps per revolution
53-
void setStepsPerRev(unsigned long stepsPerRev);
54+
void setStepsPerRev(uint32_t stepsPerRev);
5455

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

6162
// set the duration of step pulses (µs)
62-
void setPulseWidth(unsigned int pulseWidth);
63+
void setPulseWidth(uint16_t pulseWidth);
6364

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

7071
// move by n steps
71-
void moveSteps(long nSteps);
72+
void moveSteps(int32_t nSteps);
7273

7374
// move by n degrees
7475
void moveDegrees(float degrees);
7576

76-
77+
7778
private:
7879
void step(); // step function
79-
uint8_t _pinStep; // pin number: step
80+
bool _invertDirection = false; // invert the direction pin?
81+
bool _invertEnable = false; // invert the enable pin?
8082
uint8_t _pinDirection; // pin number: direction
8183
uint8_t _pinEnable; // pin number: enable
84+
uint8_t _pinStep; // pin number: step
85+
uint16_t _pulseWidth = 1; // duration of step pulses (µs)
86+
uint32_t _stepsPerRev = 200; // steps per revolution
8287
float _a; // acceleration (steps / s^2)
8388
float _vMax; // maximum speed (steps / s)
8489
float _c0; // duration of first interval (µs)
85-
unsigned int _pulseWidth = 1; // duration of step pulses (µs)
86-
unsigned long _stepsPerRev = 200; // steps per revolution
87-
bool _invertEnable = false; // invert the enable pin?
88-
bool _invertDirection = false; // invert the direction pin?
8990
};
9091

9192
#endif

0 commit comments

Comments
 (0)