Class library for a Stepper Motor to be able to Select amount of steps,set the time interval between steps, run stepper motor freely and stop the stepper motor.
Stepper.h@3:82951e628d6a, 2017-03-09 (annotated)
- Committer:
- Armand
- Date:
- Thu Mar 09 06:31:16 2017 +0000
- Revision:
- 3:82951e628d6a
- Parent:
- 2:bfdd7246ac5e
Just added email address
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Armand | 0:b0c267f9cc05 | 1 | /* Stepper Motor Library v1.0 |
Armand | 0:b0c267f9cc05 | 2 | * Copyright (c) 2017 Armand Coetzer |
Armand | 3:82951e628d6a | 3 | * s213293048@nmmu.ac.za |
Armand | 0:b0c267f9cc05 | 4 | * |
Armand | 0:b0c267f9cc05 | 5 | * |
Armand | 0:b0c267f9cc05 | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
Armand | 0:b0c267f9cc05 | 7 | * of this software and associated documentation files (the "Software"), to deal |
Armand | 0:b0c267f9cc05 | 8 | * in the Software without restriction, including without limitation the rights |
Armand | 0:b0c267f9cc05 | 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
Armand | 0:b0c267f9cc05 | 10 | * copies of the Software, and to permit persons to whom the Software is |
Armand | 0:b0c267f9cc05 | 11 | * furnished to do so, subject to the following conditions: |
Armand | 0:b0c267f9cc05 | 12 | * |
Armand | 0:b0c267f9cc05 | 13 | * The above copyright notice and this permission notice shall be included in |
Armand | 0:b0c267f9cc05 | 14 | * all copies or substantial portions of the Software. |
Armand | 0:b0c267f9cc05 | 15 | * |
Armand | 0:b0c267f9cc05 | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Armand | 0:b0c267f9cc05 | 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Armand | 0:b0c267f9cc05 | 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Armand | 0:b0c267f9cc05 | 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Armand | 0:b0c267f9cc05 | 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Armand | 0:b0c267f9cc05 | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
Armand | 0:b0c267f9cc05 | 22 | * THE SOFTWARE. |
Armand | 0:b0c267f9cc05 | 23 | */ |
Armand | 0:b0c267f9cc05 | 24 | |
Armand | 0:b0c267f9cc05 | 25 | #ifndef Stepper_H |
Armand | 0:b0c267f9cc05 | 26 | #define Stepper_H |
Armand | 0:b0c267f9cc05 | 27 | |
Armand | 0:b0c267f9cc05 | 28 | #include "mbed.h" |
Armand | 0:b0c267f9cc05 | 29 | |
Armand | 1:ceb6e16ceeff | 30 | /** Class library for a Stepper Motor to be able to Select amount of steps,set the time interval between steps, run stepper motor freely and stop the stepper motor. |
Armand | 0:b0c267f9cc05 | 31 | * |
Armand | 0:b0c267f9cc05 | 32 | * Example: |
Armand | 0:b0c267f9cc05 | 33 | * @code |
Armand | 0:b0c267f9cc05 | 34 | * #include "mbed.h" //include the mbed APIs and other libraries |
Armand | 0:b0c267f9cc05 | 35 | * #include "Stepper.h" //include the Stepper librariy |
Armand | 0:b0c267f9cc05 | 36 | * |
Armand | 0:b0c267f9cc05 | 37 | * Stepper stepper (PE_4, PE_5, PE_6, PE_7); //Initialize bus pins |
Armand | 0:b0c267f9cc05 | 38 | * |
Armand | 0:b0c267f9cc05 | 39 | * int main() |
Armand | 0:b0c267f9cc05 | 40 | * { |
Armand | 0:b0c267f9cc05 | 41 | * |
Armand | 0:b0c267f9cc05 | 42 | * while(1) //Enter infinite while loop |
Armand | 0:b0c267f9cc05 | 43 | * { |
Armand | 0:b0c267f9cc05 | 44 | * stepper.Interval(0.1); //Choosing time interval between steps(control speed) |
Armand | 2:bfdd7246ac5e | 45 | * stepper.Direction(true); //Choosing direction. true = Clockwise, false = Anticlockwise |
Armand | 0:b0c267f9cc05 | 46 | * stepper.Run(); //Makes the stepper run continuously |
Armand | 2:bfdd7246ac5e | 47 | * wait(5); //mbed's wait function, wait 5 sec. |
Armand | 2:bfdd7246ac5e | 48 | * stepper.Direction(false); |
Armand | 0:b0c267f9cc05 | 49 | * wait(5); |
Armand | 0:b0c267f9cc05 | 50 | * stepper.Stop(); //To make stepper stop |
Armand | 0:b0c267f9cc05 | 51 | * wait(5); |
Armand | 0:b0c267f9cc05 | 52 | * } |
Armand | 0:b0c267f9cc05 | 53 | * } |
Armand | 0:b0c267f9cc05 | 54 | * @endcode |
Armand | 0:b0c267f9cc05 | 55 | */ |
Armand | 0:b0c267f9cc05 | 56 | |
Armand | 0:b0c267f9cc05 | 57 | class Stepper { |
Armand | 0:b0c267f9cc05 | 58 | public: |
Armand | 0:b0c267f9cc05 | 59 | /** Create a Stepper motor object connected to the specified pins. |
Armand | 0:b0c267f9cc05 | 60 | * @param coil1 MCU Output Pin for coil1. |
Armand | 0:b0c267f9cc05 | 61 | * @param coil2 MCU Output Pin for coil2. |
Armand | 0:b0c267f9cc05 | 62 | * @param coil3 MCU Output Pin for coil3. |
Armand | 0:b0c267f9cc05 | 63 | * @param coil4 MCU Output Pin for coil4. |
Armand | 0:b0c267f9cc05 | 64 | */ |
Armand | 0:b0c267f9cc05 | 65 | Stepper(PinName Coil1, PinName Coil2, PinName Coil3, PinName Coil4); |
Armand | 0:b0c267f9cc05 | 66 | |
Armand | 0:b0c267f9cc05 | 67 | /** Makes the stepper run continuously. |
Armand | 0:b0c267f9cc05 | 68 | * @param |
Armand | 0:b0c267f9cc05 | 69 | * None |
Armand | 0:b0c267f9cc05 | 70 | */ |
Armand | 0:b0c267f9cc05 | 71 | void Run(); |
Armand | 0:b0c267f9cc05 | 72 | |
Armand | 0:b0c267f9cc05 | 73 | /** To make stepper stop. |
Armand | 0:b0c267f9cc05 | 74 | * @param |
Armand | 0:b0c267f9cc05 | 75 | * None |
Armand | 0:b0c267f9cc05 | 76 | */ |
Armand | 0:b0c267f9cc05 | 77 | void Stop(); |
Armand | 0:b0c267f9cc05 | 78 | |
Armand | 0:b0c267f9cc05 | 79 | /** Setting direction of the stepper motor, Clockwise or Anticlockwise. |
Armand | 0:b0c267f9cc05 | 80 | * @param |
Armand | 2:bfdd7246ac5e | 81 | * boolean slection to select direction. true = Clockwise, false = Anticlockwise |
Armand | 0:b0c267f9cc05 | 82 | */ |
Armand | 2:bfdd7246ac5e | 83 | void Direction(bool dir); |
Armand | 0:b0c267f9cc05 | 84 | |
Armand | 0:b0c267f9cc05 | 85 | /** Choosing time interval between steps(control speed). |
Armand | 0:b0c267f9cc05 | 86 | * @param |
Armand | 0:b0c267f9cc05 | 87 | * A float value to set the time between steps. |
Armand | 0:b0c267f9cc05 | 88 | * @return |
Armand | 0:b0c267f9cc05 | 89 | * None |
Armand | 0:b0c267f9cc05 | 90 | */ |
Armand | 0:b0c267f9cc05 | 91 | void Interval(float sec); |
Armand | 0:b0c267f9cc05 | 92 | |
Armand | 0:b0c267f9cc05 | 93 | /** Selct amount of steps the stpper motor should turn. |
Armand | 0:b0c267f9cc05 | 94 | * @param |
Armand | 0:b0c267f9cc05 | 95 | * interger value to select the amount of steps. |
Armand | 0:b0c267f9cc05 | 96 | * @return |
Armand | 0:b0c267f9cc05 | 97 | * None |
Armand | 0:b0c267f9cc05 | 98 | */ |
Armand | 0:b0c267f9cc05 | 99 | void Step(int steps); |
Armand | 0:b0c267f9cc05 | 100 | |
Armand | 0:b0c267f9cc05 | 101 | private: |
Armand | 0:b0c267f9cc05 | 102 | |
Armand | 0:b0c267f9cc05 | 103 | void stepper_isr(); |
Armand | 0:b0c267f9cc05 | 104 | |
Armand | 0:b0c267f9cc05 | 105 | BusOut _Coils; |
Armand | 0:b0c267f9cc05 | 106 | Timer timer; |
Armand | 0:b0c267f9cc05 | 107 | Ticker ticker; |
Armand | 0:b0c267f9cc05 | 108 | float secinter; |
Armand | 0:b0c267f9cc05 | 109 | float lastsecinter; |
Armand | 0:b0c267f9cc05 | 110 | uint8_t StepDir; |
Armand | 0:b0c267f9cc05 | 111 | uint8_t stepper_steps[4]; |
Armand | 0:b0c267f9cc05 | 112 | int8_t stepcount; |
Armand | 0:b0c267f9cc05 | 113 | uint8_t Prevtime; |
Armand | 0:b0c267f9cc05 | 114 | uint8_t run; |
Armand | 0:b0c267f9cc05 | 115 | uint8_t stepincr; |
Armand | 0:b0c267f9cc05 | 116 | uint8_t stepping; |
Armand | 0:b0c267f9cc05 | 117 | uint8_t Steps; |
Armand | 0:b0c267f9cc05 | 118 | }; |
Armand | 0:b0c267f9cc05 | 119 | |
Armand | 0:b0c267f9cc05 | 120 | #endif |