Stepper motor with 5 states. State machine is used for switching between states.
Dependencies: mbed
Fork of NucleoF103-stepper_motor_with_button by
main.cpp@1:d30c9dbba3f2, 2015-04-26 (annotated)
- Committer:
- sh3ppard
- Date:
- Sun Apr 26 17:14:24 2015 +0000
- Revision:
- 1:d30c9dbba3f2
- Parent:
- 0:a8de23e49ba2
Changed halfStep function
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sh3ppard | 0:a8de23e49ba2 | 1 | /********************************************************************************** |
sh3ppard | 0:a8de23e49ba2 | 2 | * @file main.cpp |
sh3ppard | 0:a8de23e49ba2 | 3 | * @author Vaclav Moravcik |
sh3ppard | 0:a8de23e49ba2 | 4 | * @version V0.1 |
sh3ppard | 0:a8de23e49ba2 | 5 | * @date 22-March-2015 |
sh3ppard | 0:a8de23e49ba2 | 6 | * @brief Controling of stepper motor with state machine. Presing the button changes direction, speed or stops the motor. |
sh3ppard | 0:a8de23e49ba2 | 7 | ***********************************************************************************/ |
sh3ppard | 0:a8de23e49ba2 | 8 | |
sh3ppard | 0:a8de23e49ba2 | 9 | /* Includes ----------------------------------------------------------------------*/ |
sh3ppard | 0:a8de23e49ba2 | 10 | #include "mbed.h" |
sh3ppard | 0:a8de23e49ba2 | 11 | |
sh3ppard | 0:a8de23e49ba2 | 12 | /* Defines -----------------------------------------------------------------------*/ |
sh3ppard | 0:a8de23e49ba2 | 13 | |
sh3ppard | 0:a8de23e49ba2 | 14 | /* Function prototypes -----------------------------------------------------------*/ |
sh3ppard | 0:a8de23e49ba2 | 15 | void goForward(); |
sh3ppard | 0:a8de23e49ba2 | 16 | void goBackward(); |
sh3ppard | 0:a8de23e49ba2 | 17 | void accelerate(); |
sh3ppard | 0:a8de23e49ba2 | 18 | void decelerate(); |
sh3ppard | 0:a8de23e49ba2 | 19 | void stop(); |
sh3ppard | 0:a8de23e49ba2 | 20 | void pressed(); |
sh3ppard | 0:a8de23e49ba2 | 21 | |
sh3ppard | 0:a8de23e49ba2 | 22 | /* Variables ---------------------------------------------------------------------*/ |
sh3ppard | 0:a8de23e49ba2 | 23 | int direction = 0; |
sh3ppard | 0:a8de23e49ba2 | 24 | bool running = true; |
sh3ppard | 0:a8de23e49ba2 | 25 | int state = 1; |
sh3ppard | 0:a8de23e49ba2 | 26 | float stepFreq = 5; |
sh3ppard | 0:a8de23e49ba2 | 27 | int accelerationMode = 0; //0 - no acceleration, 1 - accelerating, 2 - decelerating |
sh3ppard | 1:d30c9dbba3f2 | 28 | int position = 0; |
sh3ppard | 1:d30c9dbba3f2 | 29 | int posArray[4][8] = {1,1,0,0,0,0,0,1, |
sh3ppard | 1:d30c9dbba3f2 | 30 | 0,1,1,1,0,0,0,0, |
sh3ppard | 1:d30c9dbba3f2 | 31 | 0,0,0,1,1,1,0,0, |
sh3ppard | 1:d30c9dbba3f2 | 32 | 0,0,0,0,0,1,1,1}; |
sh3ppard | 0:a8de23e49ba2 | 33 | |
sh3ppard | 0:a8de23e49ba2 | 34 | //mbed - initialization of peripherals |
sh3ppard | 0:a8de23e49ba2 | 35 | DigitalOut my_led(LED1); |
sh3ppard | 0:a8de23e49ba2 | 36 | DigitalOut out1(PB_10); |
sh3ppard | 0:a8de23e49ba2 | 37 | DigitalOut out2(PB_4); |
sh3ppard | 0:a8de23e49ba2 | 38 | DigitalOut out3(PB_5); |
sh3ppard | 0:a8de23e49ba2 | 39 | DigitalOut out4(PB_3); |
sh3ppard | 0:a8de23e49ba2 | 40 | InterruptIn my_button(USER_BUTTON); |
sh3ppard | 0:a8de23e49ba2 | 41 | |
sh3ppard | 0:a8de23e49ba2 | 42 | /* Functions----------------------------------------------------------------------*/ |
sh3ppard | 0:a8de23e49ba2 | 43 | |
sh3ppard | 0:a8de23e49ba2 | 44 | /******************************************************************************* |
sh3ppard | 0:a8de23e49ba2 | 45 | * Function Name : goForward. |
sh3ppard | 0:a8de23e49ba2 | 46 | * Description : Sets variables so the motor revolves forward. |
sh3ppard | 0:a8de23e49ba2 | 47 | * Input : none. |
sh3ppard | 0:a8de23e49ba2 | 48 | * Output : none. |
sh3ppard | 0:a8de23e49ba2 | 49 | * Return : none. |
sh3ppard | 0:a8de23e49ba2 | 50 | *******************************************************************************/ |
sh3ppard | 0:a8de23e49ba2 | 51 | void goForward() { |
sh3ppard | 0:a8de23e49ba2 | 52 | accelerationMode = 0; |
sh3ppard | 0:a8de23e49ba2 | 53 | running = true; |
sh3ppard | 0:a8de23e49ba2 | 54 | direction = 0; |
sh3ppard | 0:a8de23e49ba2 | 55 | } |
sh3ppard | 0:a8de23e49ba2 | 56 | |
sh3ppard | 0:a8de23e49ba2 | 57 | /******************************************************************************* |
sh3ppard | 0:a8de23e49ba2 | 58 | * Function Name : goBackward. |
sh3ppard | 0:a8de23e49ba2 | 59 | * Description : Sets variables so the motor revolves backward. |
sh3ppard | 0:a8de23e49ba2 | 60 | * Input : none. |
sh3ppard | 0:a8de23e49ba2 | 61 | * Output : none. |
sh3ppard | 0:a8de23e49ba2 | 62 | * Return : none. |
sh3ppard | 0:a8de23e49ba2 | 63 | *******************************************************************************/ |
sh3ppard | 0:a8de23e49ba2 | 64 | void goBackward() { |
sh3ppard | 0:a8de23e49ba2 | 65 | accelerationMode = 0; |
sh3ppard | 0:a8de23e49ba2 | 66 | running = true; |
sh3ppard | 0:a8de23e49ba2 | 67 | direction = 1; |
sh3ppard | 0:a8de23e49ba2 | 68 | } |
sh3ppard | 0:a8de23e49ba2 | 69 | |
sh3ppard | 0:a8de23e49ba2 | 70 | /******************************************************************************* |
sh3ppard | 0:a8de23e49ba2 | 71 | * Function Name : stop. |
sh3ppard | 0:a8de23e49ba2 | 72 | * Description : Stops the rotation of the motor. |
sh3ppard | 0:a8de23e49ba2 | 73 | * Input : none. |
sh3ppard | 0:a8de23e49ba2 | 74 | * Output : none. |
sh3ppard | 0:a8de23e49ba2 | 75 | * Return : none. |
sh3ppard | 0:a8de23e49ba2 | 76 | *******************************************************************************/ |
sh3ppard | 0:a8de23e49ba2 | 77 | void stop() { |
sh3ppard | 0:a8de23e49ba2 | 78 | running = false; |
sh3ppard | 0:a8de23e49ba2 | 79 | } |
sh3ppard | 0:a8de23e49ba2 | 80 | |
sh3ppard | 0:a8de23e49ba2 | 81 | /******************************************************************************* |
sh3ppard | 0:a8de23e49ba2 | 82 | * Function Name : accelerate. |
sh3ppard | 0:a8de23e49ba2 | 83 | * Description : Sets accelerationMode so the stepFrequency increases over time. |
sh3ppard | 0:a8de23e49ba2 | 84 | * Input : none. |
sh3ppard | 0:a8de23e49ba2 | 85 | * Output : none. |
sh3ppard | 0:a8de23e49ba2 | 86 | * Return : none. |
sh3ppard | 0:a8de23e49ba2 | 87 | *******************************************************************************/ |
sh3ppard | 0:a8de23e49ba2 | 88 | void accelerate() { |
sh3ppard | 0:a8de23e49ba2 | 89 | accelerationMode = 1; |
sh3ppard | 0:a8de23e49ba2 | 90 | running = true; |
sh3ppard | 0:a8de23e49ba2 | 91 | } |
sh3ppard | 0:a8de23e49ba2 | 92 | |
sh3ppard | 0:a8de23e49ba2 | 93 | /******************************************************************************* |
sh3ppard | 0:a8de23e49ba2 | 94 | * Function Name : decelerate. |
sh3ppard | 0:a8de23e49ba2 | 95 | * Description : Sets accelerationMode so the stepFrequency decreases over time. |
sh3ppard | 0:a8de23e49ba2 | 96 | * Input : none. |
sh3ppard | 0:a8de23e49ba2 | 97 | * Output : none. |
sh3ppard | 0:a8de23e49ba2 | 98 | * Return : none. |
sh3ppard | 0:a8de23e49ba2 | 99 | *******************************************************************************/ |
sh3ppard | 0:a8de23e49ba2 | 100 | void decelerate() { |
sh3ppard | 0:a8de23e49ba2 | 101 | accelerationMode = 2; |
sh3ppard | 0:a8de23e49ba2 | 102 | running = true; |
sh3ppard | 0:a8de23e49ba2 | 103 | } |
sh3ppard | 0:a8de23e49ba2 | 104 | |
sh3ppard | 0:a8de23e49ba2 | 105 | /******************************************************************************* |
sh3ppard | 0:a8de23e49ba2 | 106 | * Function Name : pressed. |
sh3ppard | 0:a8de23e49ba2 | 107 | * Description : When button is pushed the state is switched. |
sh3ppard | 0:a8de23e49ba2 | 108 | * Input : none. |
sh3ppard | 0:a8de23e49ba2 | 109 | * Output : none. |
sh3ppard | 0:a8de23e49ba2 | 110 | * Return : none. |
sh3ppard | 0:a8de23e49ba2 | 111 | *******************************************************************************/ |
sh3ppard | 0:a8de23e49ba2 | 112 | void pressed() { |
sh3ppard | 0:a8de23e49ba2 | 113 | if(state < 5) { //Program goes through the states in circle |
sh3ppard | 0:a8de23e49ba2 | 114 | state++; |
sh3ppard | 0:a8de23e49ba2 | 115 | } |
sh3ppard | 0:a8de23e49ba2 | 116 | else { |
sh3ppard | 0:a8de23e49ba2 | 117 | state = 1; |
sh3ppard | 0:a8de23e49ba2 | 118 | } |
sh3ppard | 0:a8de23e49ba2 | 119 | switch(state){ //Depending on the state the appropriate action is done |
sh3ppard | 0:a8de23e49ba2 | 120 | case 1 : //Stopped |
sh3ppard | 0:a8de23e49ba2 | 121 | goForward(); |
sh3ppard | 0:a8de23e49ba2 | 122 | break; |
sh3ppard | 0:a8de23e49ba2 | 123 | case 2 : //Running forward |
sh3ppard | 0:a8de23e49ba2 | 124 | goBackward(); |
sh3ppard | 0:a8de23e49ba2 | 125 | break; |
sh3ppard | 0:a8de23e49ba2 | 126 | case 3 : //Running backward |
sh3ppard | 0:a8de23e49ba2 | 127 | stop(); |
sh3ppard | 0:a8de23e49ba2 | 128 | break; |
sh3ppard | 0:a8de23e49ba2 | 129 | case 4 : //Accelerating |
sh3ppard | 0:a8de23e49ba2 | 130 | accelerate(); |
sh3ppard | 0:a8de23e49ba2 | 131 | break; |
sh3ppard | 0:a8de23e49ba2 | 132 | case 5 : //Accelerating |
sh3ppard | 0:a8de23e49ba2 | 133 | decelerate(); |
sh3ppard | 0:a8de23e49ba2 | 134 | break; |
sh3ppard | 0:a8de23e49ba2 | 135 | } |
sh3ppard | 0:a8de23e49ba2 | 136 | } |
sh3ppard | 0:a8de23e49ba2 | 137 | |
sh3ppard | 0:a8de23e49ba2 | 138 | /******************************************************************************* |
sh3ppard | 0:a8de23e49ba2 | 139 | * Function Name : increment. |
sh3ppard | 0:a8de23e49ba2 | 140 | * Description : Increments step position and when reaches limit, circles back to start value. |
sh3ppard | 0:a8de23e49ba2 | 141 | * Input : position: current position of motor. direction: decides if function counts up or down. |
sh3ppard | 0:a8de23e49ba2 | 142 | * : upperLimit: limit stated by resolution of stepping (for half-stepping its 8). |
sh3ppard | 0:a8de23e49ba2 | 143 | * Output : position: new incremented position. |
sh3ppard | 0:a8de23e49ba2 | 144 | * Return : |
sh3ppard | 0:a8de23e49ba2 | 145 | *******************************************************************************/ |
sh3ppard | 0:a8de23e49ba2 | 146 | int increment(int position ,int direction, int upperLimit) { |
sh3ppard | 0:a8de23e49ba2 | 147 | if(direction == 0) { //Counting up |
sh3ppard | 0:a8de23e49ba2 | 148 | if(position > upperLimit - 1) { //Check limits |
sh3ppard | 0:a8de23e49ba2 | 149 | position = 1; //If position is equal to upperLimit then come back to 1 |
sh3ppard | 0:a8de23e49ba2 | 150 | } |
sh3ppard | 0:a8de23e49ba2 | 151 | else { |
sh3ppard | 0:a8de23e49ba2 | 152 | position++; |
sh3ppard | 0:a8de23e49ba2 | 153 | } |
sh3ppard | 0:a8de23e49ba2 | 154 | } |
sh3ppard | 0:a8de23e49ba2 | 155 | else { //Counting down |
sh3ppard | 0:a8de23e49ba2 | 156 | if(position < 2) { //Check limits |
sh3ppard | 0:a8de23e49ba2 | 157 | position = upperLimit; //If position is equal to 1 then come back to upperLimit |
sh3ppard | 0:a8de23e49ba2 | 158 | } |
sh3ppard | 0:a8de23e49ba2 | 159 | else { |
sh3ppard | 0:a8de23e49ba2 | 160 | position--; |
sh3ppard | 0:a8de23e49ba2 | 161 | } |
sh3ppard | 0:a8de23e49ba2 | 162 | } |
sh3ppard | 0:a8de23e49ba2 | 163 | return position; |
sh3ppard | 0:a8de23e49ba2 | 164 | } |
sh3ppard | 0:a8de23e49ba2 | 165 | |
sh3ppard | 0:a8de23e49ba2 | 166 | /******************************************************************************* |
sh3ppard | 0:a8de23e49ba2 | 167 | * Function Name : halfStep. |
sh3ppard | 0:a8de23e49ba2 | 168 | * Description : Depending on current position function activates corresponding outputs. |
sh3ppard | 0:a8de23e49ba2 | 169 | * Input : position: current position of motor. |
sh3ppard | 0:a8de23e49ba2 | 170 | * Output : none. |
sh3ppard | 0:a8de23e49ba2 | 171 | * Return : none. |
sh3ppard | 0:a8de23e49ba2 | 172 | *******************************************************************************/ |
sh3ppard | 1:d30c9dbba3f2 | 173 | void halfStep(int position) { //Alternating between one powered coil and two adjacent coils. |
sh3ppard | 1:d30c9dbba3f2 | 174 | out1.write(posArray[0][position]); |
sh3ppard | 1:d30c9dbba3f2 | 175 | out2.write(posArray[1][position]); |
sh3ppard | 1:d30c9dbba3f2 | 176 | out3.write(posArray[2][position]); |
sh3ppard | 1:d30c9dbba3f2 | 177 | out4.write(posArray[3][position]); |
sh3ppard | 0:a8de23e49ba2 | 178 | } |
sh3ppard | 0:a8de23e49ba2 | 179 | |
sh3ppard | 0:a8de23e49ba2 | 180 | /******************************************************************************* |
sh3ppard | 0:a8de23e49ba2 | 181 | * Function Name : main. |
sh3ppard | 0:a8de23e49ba2 | 182 | * Description : Main routine. |
sh3ppard | 0:a8de23e49ba2 | 183 | * Input : None. |
sh3ppard | 0:a8de23e49ba2 | 184 | * Output : None. |
sh3ppard | 0:a8de23e49ba2 | 185 | * Return : None. |
sh3ppard | 0:a8de23e49ba2 | 186 | *******************************************************************************/ |
sh3ppard | 0:a8de23e49ba2 | 187 | int main() { |
sh3ppard | 0:a8de23e49ba2 | 188 | |
sh3ppard | 0:a8de23e49ba2 | 189 | // Set button action |
sh3ppard | 0:a8de23e49ba2 | 190 | my_button.fall(&pressed); //On falling edge function pressed is called |
sh3ppard | 0:a8de23e49ba2 | 191 | |
sh3ppard | 0:a8de23e49ba2 | 192 | while (1) { //Infinite while loop |
sh3ppard | 0:a8de23e49ba2 | 193 | if(running == true) { |
sh3ppard | 0:a8de23e49ba2 | 194 | halfStep(position); |
sh3ppard | 1:d30c9dbba3f2 | 195 | position = increment(position , direction, 7); |
sh3ppard | 1:d30c9dbba3f2 | 196 | my_led = !my_led; //Signaliyation of stepping. |
sh3ppard | 0:a8de23e49ba2 | 197 | } |
sh3ppard | 0:a8de23e49ba2 | 198 | if(accelerationMode == 1 && stepFreq < 500) { |
sh3ppard | 0:a8de23e49ba2 | 199 | wait(0.05); //For better control over acceleration |
sh3ppard | 0:a8de23e49ba2 | 200 | stepFreq = stepFreq + 1; //Increases stepFreq causing faster rotation of the motor |
sh3ppard | 0:a8de23e49ba2 | 201 | } |
sh3ppard | 0:a8de23e49ba2 | 202 | if(accelerationMode == 2 && stepFreq > 1) { |
sh3ppard | 0:a8de23e49ba2 | 203 | wait(0.05); //For better control over deceleration |
sh3ppard | 0:a8de23e49ba2 | 204 | stepFreq = stepFreq - 1; //Decreases stepFreq causing slower rotation of the motor |
sh3ppard | 0:a8de23e49ba2 | 205 | } |
sh3ppard | 0:a8de23e49ba2 | 206 | wait(1/stepFreq); // 1/stepFreq ms wait |
sh3ppard | 0:a8de23e49ba2 | 207 | } |
sh3ppard | 0:a8de23e49ba2 | 208 | } |
sh3ppard | 0:a8de23e49ba2 | 209 |