Skip to content

Support generic drivers for Stepper library #4257

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Generic constructors without zero parameters
matthijskooijman wrote:
> I don't think the generic constructor's arguments are
> really easy to use, having to pass zeroes for unused
> pins. Why not just add versions of the current
> constructors, but with the added phases matrix
> arguments?

Fixed with this commit.
  • Loading branch information
skniazev committed Dec 19, 2015
commit c1442a5f17d2bae1afb4ef1b5ed7bca9e47c458b
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const unsigned char phasesMatrix[8] PROGMEM = {
};

//initialize the stepper library on pins 8 through 11, phasesMatrix, pin_count, phase_count:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11, 0, (unsigned char*)phasesMatrix, 4, 8);
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11, 0, 4, (unsigned char*)phasesMatrix, 8);

void setup() {
// 17 is the maximum for counterclockwise revolution of motor 28BYJ-48
Expand Down
72 changes: 63 additions & 9 deletions libraries/Stepper/src/Stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
Stepper::Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1)
{
initMotor(number_of_steps, motor_pin_0, motor_pin_1,
0, 0, 0, (unsigned char*)phasesMatrix2, 2, 4);
0, 0, 0, 2, (unsigned char*)phasesMatrix2, 4);
}

/*
Expand All @@ -103,7 +103,7 @@ Stepper::Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
{
initMotor(number_of_steps, motor_pin_0, motor_pin_1,
motor_pin_2, motor_pin_3,
0, (unsigned char*)phasesMatrix4, 4, 4);
0, 4, (unsigned char*)phasesMatrix4, 4);
}

/*
Expand All @@ -116,7 +116,61 @@ Stepper::Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
{
initMotor(number_of_steps, motor_pin_0, motor_pin_1,
motor_pin_2, motor_pin_3,
motor_pin_4, (unsigned char*)phasesMatrix5, 5, 10);
motor_pin_4, 5, (unsigned char*)phasesMatrix5, 10);
}

/*
* universal constructor for motors with only one wire
* Sets which wires should control the motor, pointer to the phases matrix, number of pins and phases.
*/
Stepper::Stepper(unsigned number_of_steps, int motor_pin_0,
int pin_count, unsigned char *phasesMatrix, int phase_count)
{
initMotor(number_of_steps, motor_pin_0, 0,
0, 0,
0, pin_count,
phasesMatrix,phase_count);
}

/*
* universal constructor for motors with maximum two wires
* Sets which wires should control the motor, pointer to the phases matrix, number of pins and phases.
*/
Stepper::Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
int pin_count, unsigned char *phasesMatrix, int phase_count)
{
initMotor(number_of_steps, motor_pin_0, motor_pin_1,
0, 0,
0, pin_count,
phasesMatrix,phase_count);
}

/*
* universal constructor for motors with maximum three wires
* Sets which wires should control the motor, pointer to the phases matrix, number of pins and phases.
*/
Stepper::Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
int motor_pin_2, int pin_count,
unsigned char *phasesMatrix, int phase_count)
{
initMotor(number_of_steps, motor_pin_0, motor_pin_1,
motor_pin_2, 0,
0, pin_count,
phasesMatrix,phase_count);
}

/*
* universal constructor for motors with maximum four wires
* Sets which wires should control the motor, pointer to the phases matrix, number of pins and phases.
*/
Stepper::Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
int motor_pin_2, int motor_pin_3, int pin_count,
unsigned char *phasesMatrix, int phase_count)
{
initMotor(number_of_steps, motor_pin_0, motor_pin_1,
motor_pin_2, motor_pin_3,
0, pin_count,
phasesMatrix,phase_count);
}

/*
Expand All @@ -125,13 +179,13 @@ Stepper::Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
*/
Stepper::Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
int motor_pin_2, int motor_pin_3,
int motor_pin_4, unsigned char *phasesMatrix,
int pin_count, int phase_count)
int motor_pin_4, int pin_count,
unsigned char *phasesMatrix, int phase_count)
{
initMotor(number_of_steps, motor_pin_0, motor_pin_1,
motor_pin_2, motor_pin_3,
motor_pin_4, phasesMatrix,
pin_count, phase_count);
motor_pin_4, pin_count,
phasesMatrix,phase_count);
}

/*
Expand Down Expand Up @@ -200,8 +254,8 @@ void Stepper::stepMotor(unsigned char thisPhase)
*/
void Stepper::initMotor(unsigned number_of_steps, unsigned char motor_pin_0, unsigned char motor_pin_1,
unsigned char motor_pin_2, unsigned char motor_pin_3,
unsigned char motor_pin_4, unsigned char *phasesMatrix,
unsigned char pin_count, unsigned char phase_count)
unsigned char motor_pin_4, unsigned char pin_count,
unsigned char *phasesMatrix,unsigned char phase_count)
{
this->step_number = 0; // which step the motor is on
this->number_of_steps = number_of_steps; // total number of steps for this motor
Expand Down
18 changes: 14 additions & 4 deletions libraries/Stepper/src/Stepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,20 @@ class Stepper {
Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
int motor_pin_2, int motor_pin_3,
int motor_pin_4);
Stepper(unsigned number_of_steps, int motor_pin_0,
int pin_count, unsigned char *phasesMatrix, int phase_count);
Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
int pin_count, unsigned char *phasesMatrix, int phase_count);
Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
int motor_pin_2, int pin_count,
unsigned char *phasesMatrix, int phase_count);
Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
int motor_pin_2, int motor_pin_3, int pin_count,
unsigned char *phasesMatrix, int phase_count);
Stepper(unsigned number_of_steps, int motor_pin_0, int motor_pin_1,
int motor_pin_2, int motor_pin_3,
int motor_pin_4, unsigned char *phasesMatrix,
int pin_count, int phase_count);
int motor_pin_4, int pin_count,
unsigned char *phasesMatrix, int phase_count);

// speed setter method:
void setSpeed(long whatSpeed);
Expand All @@ -144,8 +154,8 @@ class Stepper {
void stepMotor(unsigned char thisPhase);
void initMotor(unsigned number_of_steps, unsigned char motor_pin_0, unsigned char motor_pin_1,
unsigned char motor_pin_2, unsigned char motor_pin_3,
unsigned char motor_pin_4, unsigned char *phasesMatrix,
unsigned char pin_count, unsigned char phase_count);
unsigned char motor_pin_4, unsigned char pin_count,
unsigned char *phasesMatrix,unsigned char phase_count);

unsigned long step_delay; // delay between steps, in ms, based on speed
unsigned number_of_steps; // total number of steps this motor can take
Expand Down