Skip to content

Update stepper library: High-speed stepping mod and timer rollover fix #1701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions libraries/Stepper/Stepper.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/*
Stepper.h - - Stepper library for Wiring/Arduino - Version 0.4
Stepper.h - - Stepper library for Wiring/Arduino - Version 0.5

Original library (0.1) by Tom Igoe.
Two-wire modifications (0.2) by Sebastian Gassner
Combination version (0.3) by Tom Igoe and David Mellis
Bug fix for four-wire (0.4) by Tom Igoe, bug fix from Noah Shibley
High-speed stepping mod and timer rollover fix (0.5) by Eugene Kozlenko

Drives a unipolar or bipolar stepper motor using 2 wires or 4 wires

Expand All @@ -18,7 +19,8 @@
A slightly modified circuit around a Darlington transistor array or an L293 H-bridge
connects to only 2 microcontroler pins, inverts the signals received,
and delivers the 4 (2 plus 2 inverted ones) output signals required
for driving a stepper motor.
for driving a stepper motor. Similarly the Arduino motor shields 2 direction pins
may be used.

The sequence of control signals for 4 control wires is as follows:

Expand Down Expand Up @@ -65,7 +67,7 @@ class Stepper {

int direction; // Direction of rotation
int speed; // Speed in RPMs
unsigned long step_delay; // delay between steps, in ms, based on speed
unsigned long step_delay; // delay between steps, in us, based on speed
int number_of_steps; // total number of steps this motor can take
int pin_count; // whether you're driving the motor with 2 or 4 pins
int step_number; // which step the motor is on
Expand All @@ -76,7 +78,7 @@ class Stepper {
int motor_pin_3;
int motor_pin_4;

long last_step_time; // time stamp in ms of when the last step was taken
unsigned long last_step_time; // time stamp in us of when the last step was taken
};

#endif
Expand Down
18 changes: 10 additions & 8 deletions libraries/Stepper/Stepper.cpp → libraries/Stepper/stepper.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/*
Stepper.cpp - - Stepper library for Wiring/Arduino - Version 0.4
Stepper.cpp - - Stepper library for Wiring/Arduino - Version 0.5

Original library (0.1) by Tom Igoe.
Two-wire modifications (0.2) by Sebastian Gassner
Combination version (0.3) by Tom Igoe and David Mellis
Bug fix for four-wire (0.4) by Tom Igoe, bug fix from Noah Shibley
High-speed stepping mod and timer rollover fix (0.5) by Eugene Kozlenko

Drives a unipolar or bipolar stepper motor using 2 wires or 4 wires

Expand All @@ -18,7 +19,8 @@
A slightly modified circuit around a Darlington transistor array or an L293 H-bridge
connects to only 2 microcontroler pins, inverts the signals received,
and delivers the 4 (2 plus 2 inverted ones) output signals required
for driving a stepper motor.
for driving a stepper motor. Similarly the Arduino motor shields 2 direction pins
may be used.

The sequence of control signals for 4 control wires is as follows:

Expand Down Expand Up @@ -57,7 +59,7 @@ Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2)
this->step_number = 0; // which step the motor is on
this->speed = 0; // the motor speed, in revolutions per minute
this->direction = 0; // motor direction
this->last_step_time = 0; // time stamp in ms of the last step taken
this->last_step_time = 0; // time stamp in us of the last step taken
this->number_of_steps = number_of_steps; // total number of steps for this motor

// Arduino pins for the motor control connection:
Expand Down Expand Up @@ -87,7 +89,7 @@ Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int moto
this->step_number = 0; // which step the motor is on
this->speed = 0; // the motor speed, in revolutions per minute
this->direction = 0; // motor direction
this->last_step_time = 0; // time stamp in ms of the last step taken
this->last_step_time = 0; // time stamp in us of the last step taken
this->number_of_steps = number_of_steps; // total number of steps for this motor

// Arduino pins for the motor control connection:
Expand All @@ -112,7 +114,7 @@ Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int moto
*/
void Stepper::setSpeed(long whatSpeed)
{
this->step_delay = 60L * 1000L / this->number_of_steps / whatSpeed;
this->step_delay = 60L * 1000L * 1000L / this->number_of_steps / whatSpeed;
}

/*
Expand All @@ -131,9 +133,9 @@ void Stepper::step(int steps_to_move)
// decrement the number of steps, moving one step each time:
while(steps_left > 0) {
// move only if the appropriate delay has passed:
if (millis() - this->last_step_time >= this->step_delay) {
if (micros() - this->last_step_time >= this->step_delay || micros() < this->last_step_time) {
// get the timeStamp of when you stepped:
this->last_step_time = millis();
this->last_step_time = micros();
// increment or decrement the step number,
// depending on direction:
if (this->direction == 1) {
Expand Down Expand Up @@ -216,5 +218,5 @@ void Stepper::stepMotor(int thisStep)
*/
int Stepper::version(void)
{
return 4;
return 5;
}