|
| 1 | +# Stepper Library |
| 2 | + |
| 3 | +For driving stepper motors with 2, 4 or 5 phases. |
| 4 | +Derived from the Arduino Stepper library v1.8.0. |
| 5 | + |
| 6 | +This library has a slightly diffent user interface than the usual singleton |
| 7 | +libraries. This allows for handling more than one stepper per Sketch but it |
| 8 | +requires some more attention when porting an existing sketch from C++ to C. |
| 9 | + |
| 10 | +Each Stepper instance is represented by a file-descriptor-like structure. |
| 11 | +This structure is initialized by a constructor-like function returning a |
| 12 | +pointer to this stucture. This pointer needs to be given as the first |
| 13 | +argument for ever other function call to identify the stepper instance. |
| 14 | + |
| 15 | +## API table |
| 16 | + |
| 17 | +Arduino method | sduino function |
| 18 | +-------------- | --------------- |
| 19 | + | Stepper myStepper; // global variable |
| 20 | +Stepper myStepper(steps, pin1,pin2); | myStepper = Stepper_2phase(steps, pin1,pin2); |
| 21 | +Stepper myStepper(steps, pin1,pin2,pin3,pin4); | myStepper = Stepper_4phase(steps,pin1,pin2,pin3,pin4); |
| 22 | +Stepper myStepper(steps, pin1,pin2,pin3,pin4,pin5);| myStepper = Stepper_5phase(steps,pin1,pin2,pin3,pin4,pin5); |
| 23 | +myStepper.setSpeed(speed); | Stepper_setSpeed(myStepper, speed); |
| 24 | +myStepper.step(n); | Stepper_step(myStepper, n); |
| 25 | +myStepper.version(); | Stepper_version(); |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +## Example |
| 30 | + |
| 31 | +(available in the examples folder as `stepper_oneRevolution.c`) |
| 32 | +Notice the Variable `myStepper` of type `Stepper` that is intialized by a |
| 33 | +constructor-like function `Stepper_4phase`: |
| 34 | + |
| 35 | +C-Version for use with sduino: |
| 36 | + |
| 37 | +```c |
| 38 | +#include <Arduino.h> |
| 39 | +#include <Stepper.h> |
| 40 | + |
| 41 | +const int stepsPerRevolution = 200; // change this to fit the number of steps p |
| 42 | +// for your motor |
| 43 | + |
| 44 | +Stepper myStepper; |
| 45 | + |
| 46 | +void setup() { |
| 47 | + // initialize the stepper library on pins 8 through 11: |
| 48 | + myStepper = Stepper_4phase(stepsPerRevolution, 8, 9, 10, 11); |
| 49 | + // set the speed at 60 rpm: |
| 50 | + Stepper_setSpeed(myStepper, 60); |
| 51 | +} |
| 52 | + |
| 53 | +void loop() { |
| 54 | + // step one revolution in one direction: |
| 55 | + Stepper_step(myStepper, stepsPerRevolution); |
| 56 | + delay(500); |
| 57 | + |
| 58 | + // step one revolution in the other direction: |
| 59 | + Stepper_step(myStepper, -stepsPerRevolution); |
| 60 | + delay(500); |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +Original Arduino C++-Sytax: |
| 65 | + |
| 66 | +```c++ |
| 67 | +#include <Stepper.h> |
| 68 | + |
| 69 | +const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution |
| 70 | +// for your motor |
| 71 | + |
| 72 | +// initialize the stepper library on pins 8 through 11: |
| 73 | +Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); |
| 74 | + |
| 75 | +void setup() { |
| 76 | + // set the speed at 60 rpm: |
| 77 | + myStepper.setSpeed(60); |
| 78 | +} |
| 79 | + |
| 80 | +void loop() { |
| 81 | + // step one revolution in one direction: |
| 82 | + myStepper.step(stepsPerRevolution); |
| 83 | + delay(500); |
| 84 | + |
| 85 | + // step one revolution in the other direction: |
| 86 | + myStepper.step(-stepsPerRevolution); |
| 87 | + delay(500); |
| 88 | +} |
| 89 | +``` |
| 90 | +
|
| 91 | +
|
| 92 | +## API |
| 93 | +
|
| 94 | +``` |
| 95 | +#include <Stepper.h> |
| 96 | + |
| 97 | +// plain C interface for use with SDCC |
| 98 | + |
| 99 | +// use a pointer to struct Stepper_s as a class instance aquivalent |
| 100 | +typedef struct Stepper_s *Stepper; |
| 101 | + |
| 102 | +// "constructor" function |
| 103 | +Stepper Stepper_5phase( |
| 104 | + unsigned int number_of_steps, |
| 105 | + unsigned char motor_pin_1, |
| 106 | + unsigned char motor_pin_2, |
| 107 | + unsigned char motor_pin_3, |
| 108 | + unsigned char motor_pin_4, |
| 109 | + unsigned char motor_pin_5); |
| 110 | + |
| 111 | +// use #defines to emulate the polymorph class constructor in C++ |
| 112 | +#define Stepper_2phase(N,A,B) Stepper_5phase((N),(A),(B),0,0,0) |
| 113 | +#define Stepper_4phase(N,A,B,C,D) Stepper_5phase((N),(A),(B),(C),(D),0) |
| 114 | + |
| 115 | +// speed setter method: |
| 116 | +void Stepper_setSpeed(Stepper this, long whatSpeed); |
| 117 | + |
| 118 | +// mover method: |
| 119 | +void Stepper_step(Stepper this, int number_of_steps); |
| 120 | + |
| 121 | +// replace the version() method with a simple define |
| 122 | +#define Stepper_version() 5 |
| 123 | + |
| 124 | +// (not so) private: |
| 125 | +void Stepper_stepMotor(Stepper s, int this_step); |
| 126 | +``` |
0 commit comments