Control stepper motor
Fork of ArduinoMotorShield by
ArduinoMotorShield.h@1:5f2774bf44a5, 2013-11-07 (annotated)
- Committer:
- piniels
- Date:
- Thu Nov 07 12:44:13 2013 +0000
- Revision:
- 1:5f2774bf44a5
- Parent:
- 0:f4e71504732d
added enable pins to be enabled when stepper is running:-)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
piniels | 0:f4e71504732d | 1 | /** Interface to control a stepper motor. |
piniels | 0:f4e71504732d | 2 | * Specially design for Arduino motor shield, which has a L298 H-brigde. |
piniels | 0:f4e71504732d | 3 | * |
piniels | 0:f4e71504732d | 4 | * First version is written by Peter Ilsoe. |
piniels | 0:f4e71504732d | 5 | * |
piniels | 0:f4e71504732d | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
piniels | 0:f4e71504732d | 7 | * of this software and associated documentation files (the "Software"), to deal |
piniels | 0:f4e71504732d | 8 | * in the Software without restriction, including without limitation the rights |
piniels | 0:f4e71504732d | 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
piniels | 0:f4e71504732d | 10 | * copies of the Software, and to permit persons to whom the Software is |
piniels | 0:f4e71504732d | 11 | * furnished to do so, subject to the following conditions: |
piniels | 0:f4e71504732d | 12 | * |
piniels | 0:f4e71504732d | 13 | * The above copyright notice and this permission notice shall be included in |
piniels | 0:f4e71504732d | 14 | * all copies or substantial portions of the Software. |
piniels | 0:f4e71504732d | 15 | * |
piniels | 0:f4e71504732d | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
piniels | 0:f4e71504732d | 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
piniels | 0:f4e71504732d | 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
piniels | 0:f4e71504732d | 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
piniels | 0:f4e71504732d | 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
piniels | 0:f4e71504732d | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
piniels | 0:f4e71504732d | 22 | * THE SOFTWARE. |
piniels | 0:f4e71504732d | 23 | */ |
piniels | 0:f4e71504732d | 24 | |
piniels | 0:f4e71504732d | 25 | #ifndef ArduinoMotorShield_H |
piniels | 0:f4e71504732d | 26 | #define ArduinoMotorShield_H |
piniels | 0:f4e71504732d | 27 | |
piniels | 0:f4e71504732d | 28 | #include "mbed.h" |
piniels | 0:f4e71504732d | 29 | |
piniels | 0:f4e71504732d | 30 | class ArduinoMotorShield |
piniels | 0:f4e71504732d | 31 | { |
piniels | 0:f4e71504732d | 32 | |
piniels | 0:f4e71504732d | 33 | public: |
piniels | 0:f4e71504732d | 34 | |
piniels | 0:f4e71504732d | 35 | /** Create a DC/Stepper motor control interface. This class is able to control either DC motor or a Stepper motor. |
piniels | 0:f4e71504732d | 36 | * This is specially designed for the Arduino motor shield board |
piniels | 0:f4e71504732d | 37 | * |
piniels | 0:f4e71504732d | 38 | * @param numberOfSteps The number of steps of the stepper motor used. |
piniels | 0:f4e71504732d | 39 | * @param brakeA A DigitalOut, set high when the motor should brake. |
piniels | 0:f4e71504732d | 40 | * @param brakeB A DigitalOut, set high when the motor should brake. |
piniels | 0:f4e71504732d | 41 | * @param dirA A DigitalOut, set high when the DC motor A should go forward, set low when going backwards. For the stepper motor the logic will control this. |
piniels | 0:f4e71504732d | 42 | * @param dirB A DigitalOut, set high when the DC motor B should go forward, set low when going backwards. For the stepper motor the logic will control this. |
piniels | 0:f4e71504732d | 43 | * @param pvmA A DigitalOut/PVM, Depending on what mode (DC motor or stepper motor) this pin will become PVM(DC motor) or digital output(stepper). |
piniels | 0:f4e71504732d | 44 | * @param pvmB A DigitalOut/PVM, Depending on what mode (DC motor or stepper motor) this pin will become PVM(DC motor) or digital output(stepper). |
piniels | 0:f4e71504732d | 45 | */ |
piniels | 0:f4e71504732d | 46 | ArduinoMotorShield(int numberOfSteps, PinName brakeA, PinName brakeB, PinName dirA, PinName dirB, PinName pvmA, PinName pvmB); |
piniels | 0:f4e71504732d | 47 | |
piniels | 0:f4e71504732d | 48 | /** Set the speed of a Stepper motor |
piniels | 0:f4e71504732d | 49 | * |
piniels | 0:f4e71504732d | 50 | * @param whatSpeed The speed of the Stepper motor as a RPM |
piniels | 0:f4e71504732d | 51 | */ |
piniels | 0:f4e71504732d | 52 | void setStepperSpeed(long whatSpeed); |
piniels | 0:f4e71504732d | 53 | |
piniels | 0:f4e71504732d | 54 | /** Move the stepper a number of steps |
piniels | 0:f4e71504732d | 55 | * |
piniels | 0:f4e71504732d | 56 | * @param numberOfSteps The number of steps. If the number is negative, |
piniels | 0:f4e71504732d | 57 | * the motor moves in the reverse direction. |
piniels | 0:f4e71504732d | 58 | */ |
piniels | 0:f4e71504732d | 59 | void step(int numberOfSteps); |
piniels | 0:f4e71504732d | 60 | |
piniels | 0:f4e71504732d | 61 | /** Run at a specific speed until a startStop becomes false. |
piniels | 0:f4e71504732d | 62 | * |
piniels | 0:f4e71504732d | 63 | * @param startStop If true the stepper motor will run at the specified speed. If false stepper motor will stop |
piniels | 0:f4e71504732d | 64 | */ |
piniels | 0:f4e71504732d | 65 | void runStepperAtSpeed(bool startStop, int whatSpeed, int direction); |
piniels | 0:f4e71504732d | 66 | |
piniels | 0:f4e71504732d | 67 | /** Call back member for timer tick |
piniels | 0:f4e71504732d | 68 | * |
piniels | 0:f4e71504732d | 69 | */ |
piniels | 0:f4e71504732d | 70 | void runMotor(void); |
piniels | 0:f4e71504732d | 71 | |
piniels | 0:f4e71504732d | 72 | private: |
piniels | 0:f4e71504732d | 73 | void stepMotor(int this_step); |
piniels | 0:f4e71504732d | 74 | |
piniels | 0:f4e71504732d | 75 | int direction; // Direction of rotation |
piniels | 0:f4e71504732d | 76 | int speed; // Speed in RPMs |
piniels | 0:f4e71504732d | 77 | unsigned long step_delay; // delay between steps, in ms, based on speed |
piniels | 0:f4e71504732d | 78 | int number_of_steps; // total number of steps this motor can take |
piniels | 0:f4e71504732d | 79 | int step_number; // which step the motor is on |
piniels | 0:f4e71504732d | 80 | PinName digiPvm; // store the common pin for PVM when DC motor and digital pin when stepper motor |
piniels | 0:f4e71504732d | 81 | |
piniels | 0:f4e71504732d | 82 | DigitalOut _BRAKEA; |
piniels | 0:f4e71504732d | 83 | DigitalOut _BRAKEB; |
piniels | 0:f4e71504732d | 84 | DigitalOut _DIRA; |
piniels | 0:f4e71504732d | 85 | DigitalOut _DIRB; |
piniels | 0:f4e71504732d | 86 | DigitalOut _ENA_A; |
piniels | 0:f4e71504732d | 87 | DigitalOut _ENA_B; |
piniels | 0:f4e71504732d | 88 | long last_step_time; // time stamp in ms of when the last step was taken |
piniels | 0:f4e71504732d | 89 | |
piniels | 0:f4e71504732d | 90 | }; |
piniels | 0:f4e71504732d | 91 | |
piniels | 0:f4e71504732d | 92 | |
piniels | 0:f4e71504732d | 93 | #endif |