Testing 1 blue pill
Dependencies: mbed mbed-rtos TextLCD
Stepper_Motor/STEPPER_MOTOR.cpp@19:384642f39496, 2019-02-12 (annotated)
- Committer:
- mwthewsey
- Date:
- Tue Feb 12 11:28:07 2019 +0000
- Revision:
- 19:384642f39496
- Parent:
- 18:3523660f3930
- Child:
- 23:07a368f2cdb1
Removed stepper motor clunking by adding thread waits and priority.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thomasmorris | 12:d9c133b360b0 | 1 | #include "mbed.h" //Include the mbed libraries |
thomasmorris | 12:d9c133b360b0 | 2 | #include "STEPPER_MOTOR.hpp" //Include the header file, this acts like a series of forward declarations |
thomasmorris | 12:d9c133b360b0 | 3 | |
thomasmorris | 12:d9c133b360b0 | 4 | //Constructor |
thomasmorris | 12:d9c133b360b0 | 5 | STEPPER_MOTOR::STEPPER_MOTOR(PinName N1, PinName N2, PinName N3, PinName N4) : pin1(N1),pin2(N2),pin3(N3),pin4(N4) |
thomasmorris | 12:d9c133b360b0 | 6 | { |
thomasmorris | 12:d9c133b360b0 | 7 | _dir = true; |
thomasmorris | 12:d9c133b360b0 | 8 | _step = 0; |
thomasmorris | 12:d9c133b360b0 | 9 | } |
thomasmorris | 12:d9c133b360b0 | 10 | |
thomasmorris | 12:d9c133b360b0 | 11 | STEPPER_MOTOR::~STEPPER_MOTOR(){} //Destructor |
thomasmorris | 18:3523660f3930 | 12 | float STEPPER_MOTOR::Get_Turns() |
thomasmorris | 18:3523660f3930 | 13 | { |
thomasmorris | 18:3523660f3930 | 14 | return (_Steps_Done /50); |
thomasmorris | 18:3523660f3930 | 15 | } |
thomasmorris | 12:d9c133b360b0 | 16 | void STEPPER_MOTOR::Rotate_Steps(int Steps) |
thomasmorris | 12:d9c133b360b0 | 17 | { |
thomasmorris | 12:d9c133b360b0 | 18 | #define STIME 1000 |
thomasmorris | 12:d9c133b360b0 | 19 | |
thomasmorris | 12:d9c133b360b0 | 20 | Steps = Steps*50; |
thomasmorris | 12:d9c133b360b0 | 21 | //int correctionfactor; |
thomasmorris | 12:d9c133b360b0 | 22 | //int timeofturn=correctinfactor*speed; |
thomasmorris | 18:3523660f3930 | 23 | _Steps_Done = 0; |
thomasmorris | 18:3523660f3930 | 24 | //int mystep=0; |
thomasmorris | 18:3523660f3930 | 25 | //printf("START!!! step value is=%d\n\r",mystep); |
thomasmorris | 12:d9c133b360b0 | 26 | for(int x =0 ; x <= Steps; x++) |
thomasmorris | 12:d9c133b360b0 | 27 | { |
thomasmorris | 17:68b3fdabe4c5 | 28 | //printf("FOR LOOP! step value is=%d %d\n\r",mystep,x); |
thomasmorris | 12:d9c133b360b0 | 29 | this->pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 30 | this->pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 31 | this->pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 32 | this->pin4 = 1; |
thomasmorris | 18:3523660f3930 | 33 | //mystep=1; |
mwthewsey | 19:384642f39496 | 34 | Thread::wait(1); |
mwthewsey | 19:384642f39496 | 35 | //wait_us(1000); |
thomasmorris | 14:63998be3d43c | 36 | //printf("FOR LOOP! step value is=%d %d\n\r",mystep,x); |
thomasmorris | 12:d9c133b360b0 | 37 | this->pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 38 | this->pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 39 | this->pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 40 | this->pin4 = 0; |
thomasmorris | 18:3523660f3930 | 41 | //mystep=2; |
mwthewsey | 19:384642f39496 | 42 | Thread::wait(1); |
mwthewsey | 19:384642f39496 | 43 | //wait_us(1000); |
thomasmorris | 14:63998be3d43c | 44 | //printf("FOR LOOP! step value is=%d %d\n\r",mystep,x); |
thomasmorris | 12:d9c133b360b0 | 45 | this->pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 46 | this->pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 47 | this->pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 48 | this->pin4 = 0; |
thomasmorris | 18:3523660f3930 | 49 | //mystep=3; |
mwthewsey | 19:384642f39496 | 50 | Thread::wait(1); |
mwthewsey | 19:384642f39496 | 51 | //wait_us(1000); |
thomasmorris | 17:68b3fdabe4c5 | 52 | //printf("FOR LOOP! step value is=%d %d\n\r",mystep,x); |
thomasmorris | 12:d9c133b360b0 | 53 | this->pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 54 | this->pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 55 | this->pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 56 | this->pin4 = 1; |
thomasmorris | 18:3523660f3930 | 57 | //mystep=4; |
mwthewsey | 19:384642f39496 | 58 | Thread::wait(1); |
mwthewsey | 19:384642f39496 | 59 | //wait_us(1000); |
thomasmorris | 18:3523660f3930 | 60 | |
thomasmorris | 18:3523660f3930 | 61 | _Steps_Done = _Steps_Done +1; |
thomasmorris | 12:d9c133b360b0 | 62 | /* printf("FOR LOOP! step value is=%d %d\n\r",mystep,x); |
thomasmorris | 12:d9c133b360b0 | 63 | this->pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 64 | this->pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 65 | this->pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 66 | this->pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 67 | mystep=5; |
thomasmorris | 12:d9c133b360b0 | 68 | wait_ms(0.6); |
thomasmorris | 12:d9c133b360b0 | 69 | printf("FOR LOOP! step value is=%d %d\n\r",mystep,x); |
thomasmorris | 12:d9c133b360b0 | 70 | this->pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 71 | this->pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 72 | this->pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 73 | this->pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 74 | mystep=6; |
thomasmorris | 12:d9c133b360b0 | 75 | wait_ms(0.6); |
thomasmorris | 12:d9c133b360b0 | 76 | printf("FOR LOOP! step value is=%d %d\n\r",mystep,x); |
thomasmorris | 12:d9c133b360b0 | 77 | this->pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 78 | this->pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 79 | this->pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 80 | this->pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 81 | mystep=7; |
thomasmorris | 12:d9c133b360b0 | 82 | wait_ms(0.6); |
thomasmorris | 12:d9c133b360b0 | 83 | |
thomasmorris | 12:d9c133b360b0 | 84 | this->pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 85 | this->pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 86 | this->pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 87 | this->pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 88 | mystep=0; |
thomasmorris | 12:d9c133b360b0 | 89 | wait_ms(0.6); |
thomasmorris | 12:d9c133b360b0 | 90 | |
thomasmorris | 12:d9c133b360b0 | 91 | this->pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 92 | this->pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 93 | this->pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 94 | this->pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 95 | mystep=0; |
thomasmorris | 12:d9c133b360b0 | 96 | wait_ms(0.6);*/ |
thomasmorris | 12:d9c133b360b0 | 97 | } |
thomasmorris | 17:68b3fdabe4c5 | 98 | this->pin1 = 0; |
thomasmorris | 17:68b3fdabe4c5 | 99 | this->pin2 = 0; |
thomasmorris | 17:68b3fdabe4c5 | 100 | this->pin3 = 0; |
thomasmorris | 17:68b3fdabe4c5 | 101 | this->pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 102 | } |
thomasmorris | 12:d9c133b360b0 | 103 | |
thomasmorris | 12:d9c133b360b0 | 104 | /* |
thomasmorris | 12:d9c133b360b0 | 105 | switch(mystep){ |
thomasmorris | 12:d9c133b360b0 | 106 | case 0:{ |
thomasmorris | 12:d9c133b360b0 | 107 | |
thomasmorris | 12:d9c133b360b0 | 108 | break; |
thomasmorris | 12:d9c133b360b0 | 109 | case 1:{ |
thomasmorris | 12:d9c133b360b0 | 110 | |
thomasmorris | 12:d9c133b360b0 | 111 | break; |
thomasmorris | 12:d9c133b360b0 | 112 | case 2: |
thomasmorris | 12:d9c133b360b0 | 113 | |
thomasmorris | 12:d9c133b360b0 | 114 | break; |
thomasmorris | 12:d9c133b360b0 | 115 | case 3: |
thomasmorris | 12:d9c133b360b0 | 116 | |
thomasmorris | 12:d9c133b360b0 | 117 | break; |
thomasmorris | 12:d9c133b360b0 | 118 | case 4: |
thomasmorris | 12:d9c133b360b0 | 119 | |
thomasmorris | 12:d9c133b360b0 | 120 | break; |
thomasmorris | 12:d9c133b360b0 | 121 | case 5: |
thomasmorris | 12:d9c133b360b0 | 122 | |
thomasmorris | 12:d9c133b360b0 | 123 | break; |
thomasmorris | 12:d9c133b360b0 | 124 | case 6: |
thomasmorris | 12:d9c133b360b0 | 125 | |
thomasmorris | 12:d9c133b360b0 | 126 | break; |
thomasmorris | 12:d9c133b360b0 | 127 | case 7: |
thomasmorris | 12:d9c133b360b0 | 128 | |
thomasmorris | 12:d9c133b360b0 | 129 | break; |
thomasmorris | 12:d9c133b360b0 | 130 | default: |
thomasmorris | 12:d9c133b360b0 | 131 | |
thomasmorris | 12:d9c133b360b0 | 132 | break; |
thomasmorris | 12:d9c133b360b0 | 133 | } |
thomasmorris | 12:d9c133b360b0 | 134 | |
thomasmorris | 12:d9c133b360b0 | 135 | if(_dir){ |
thomasmorris | 12:d9c133b360b0 | 136 | _step++; |
thomasmorris | 12:d9c133b360b0 | 137 | }else{ |
thomasmorris | 12:d9c133b360b0 | 138 | _step--; |
thomasmorris | 12:d9c133b360b0 | 139 | } |
thomasmorris | 12:d9c133b360b0 | 140 | if(_step>7){ |
thomasmorris | 12:d9c133b360b0 | 141 | _step=0; |
thomasmorris | 12:d9c133b360b0 | 142 | } |
thomasmorris | 12:d9c133b360b0 | 143 | if(_step<0){ |
thomasmorris | 12:d9c133b360b0 | 144 | _step=7; |
thomasmorris | 12:d9c133b360b0 | 145 | } |
thomasmorris | 12:d9c133b360b0 | 146 | */ |
thomasmorris | 12:d9c133b360b0 | 147 | //wait_ms(1); |
thomasmorris | 12:d9c133b360b0 | 148 | |
thomasmorris | 12:d9c133b360b0 | 149 | |
thomasmorris | 12:d9c133b360b0 | 150 | void STEPPER_MOTOR::Permanent_Rotate_clock_wise() |
thomasmorris | 12:d9c133b360b0 | 151 | { |
thomasmorris | 12:d9c133b360b0 | 152 | switch(_step){ |
thomasmorris | 12:d9c133b360b0 | 153 | case 0: |
thomasmorris | 12:d9c133b360b0 | 154 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 155 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 156 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 157 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 158 | break; |
thomasmorris | 12:d9c133b360b0 | 159 | case 1: |
thomasmorris | 12:d9c133b360b0 | 160 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 161 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 162 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 163 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 164 | break; |
thomasmorris | 12:d9c133b360b0 | 165 | case 2: |
thomasmorris | 12:d9c133b360b0 | 166 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 167 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 168 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 169 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 170 | break; |
thomasmorris | 12:d9c133b360b0 | 171 | case 3: |
thomasmorris | 12:d9c133b360b0 | 172 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 173 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 174 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 175 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 176 | break; |
thomasmorris | 12:d9c133b360b0 | 177 | case 4: |
thomasmorris | 12:d9c133b360b0 | 178 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 179 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 180 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 181 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 182 | break; |
thomasmorris | 12:d9c133b360b0 | 183 | case 5: |
thomasmorris | 12:d9c133b360b0 | 184 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 185 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 186 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 187 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 188 | break; |
thomasmorris | 12:d9c133b360b0 | 189 | case 6: |
thomasmorris | 12:d9c133b360b0 | 190 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 191 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 192 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 193 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 194 | break; |
thomasmorris | 12:d9c133b360b0 | 195 | case 7: |
thomasmorris | 12:d9c133b360b0 | 196 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 197 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 198 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 199 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 200 | break; |
thomasmorris | 12:d9c133b360b0 | 201 | default: |
thomasmorris | 12:d9c133b360b0 | 202 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 203 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 204 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 205 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 206 | break; |
thomasmorris | 12:d9c133b360b0 | 207 | } |
thomasmorris | 12:d9c133b360b0 | 208 | if(_dir){ |
thomasmorris | 12:d9c133b360b0 | 209 | _step++; |
thomasmorris | 12:d9c133b360b0 | 210 | }else{ |
thomasmorris | 12:d9c133b360b0 | 211 | _step--; |
thomasmorris | 12:d9c133b360b0 | 212 | } |
thomasmorris | 12:d9c133b360b0 | 213 | if(_step>7){ |
thomasmorris | 12:d9c133b360b0 | 214 | _step=0; |
thomasmorris | 12:d9c133b360b0 | 215 | } |
thomasmorris | 12:d9c133b360b0 | 216 | if(_step<0){ |
thomasmorris | 12:d9c133b360b0 | 217 | _step=7; |
thomasmorris | 12:d9c133b360b0 | 218 | } |
thomasmorris | 12:d9c133b360b0 | 219 | } |
thomasmorris | 12:d9c133b360b0 | 220 | |
thomasmorris | 12:d9c133b360b0 | 221 | void STEPPER_MOTOR::Permanent_Rotate_anti_clock_wise() |
thomasmorris | 12:d9c133b360b0 | 222 | { |
thomasmorris | 12:d9c133b360b0 | 223 | |
thomasmorris | 12:d9c133b360b0 | 224 | //Rotate |
thomasmorris | 12:d9c133b360b0 | 225 | switch(_step){ |
thomasmorris | 12:d9c133b360b0 | 226 | case 0: |
thomasmorris | 12:d9c133b360b0 | 227 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 228 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 229 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 230 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 231 | break; |
thomasmorris | 12:d9c133b360b0 | 232 | case 1: |
thomasmorris | 12:d9c133b360b0 | 233 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 234 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 235 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 236 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 237 | break; |
thomasmorris | 12:d9c133b360b0 | 238 | case 2: |
thomasmorris | 12:d9c133b360b0 | 239 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 240 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 241 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 242 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 243 | break; |
thomasmorris | 12:d9c133b360b0 | 244 | case 3: |
thomasmorris | 12:d9c133b360b0 | 245 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 246 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 247 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 248 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 249 | break; |
thomasmorris | 12:d9c133b360b0 | 250 | case 4: |
thomasmorris | 12:d9c133b360b0 | 251 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 252 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 253 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 254 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 255 | break; |
thomasmorris | 12:d9c133b360b0 | 256 | case 5: |
thomasmorris | 12:d9c133b360b0 | 257 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 258 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 259 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 260 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 261 | break; |
thomasmorris | 12:d9c133b360b0 | 262 | case 6: |
thomasmorris | 12:d9c133b360b0 | 263 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 264 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 265 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 266 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 267 | break; |
thomasmorris | 12:d9c133b360b0 | 268 | case 7: |
thomasmorris | 12:d9c133b360b0 | 269 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 270 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 271 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 272 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 273 | break; |
thomasmorris | 12:d9c133b360b0 | 274 | default: |
thomasmorris | 12:d9c133b360b0 | 275 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 276 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 277 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 278 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 279 | break; |
thomasmorris | 12:d9c133b360b0 | 280 | } |
thomasmorris | 12:d9c133b360b0 | 281 | if(_dir){ |
thomasmorris | 12:d9c133b360b0 | 282 | _step++; |
thomasmorris | 12:d9c133b360b0 | 283 | }else{ |
thomasmorris | 12:d9c133b360b0 | 284 | _step--; |
thomasmorris | 12:d9c133b360b0 | 285 | } |
thomasmorris | 12:d9c133b360b0 | 286 | if(_step>7){ |
thomasmorris | 12:d9c133b360b0 | 287 | _step=0; |
thomasmorris | 12:d9c133b360b0 | 288 | } |
thomasmorris | 12:d9c133b360b0 | 289 | if(_step<0){ |
thomasmorris | 12:d9c133b360b0 | 290 | _step=7; |
thomasmorris | 12:d9c133b360b0 | 291 | } |
thomasmorris | 12:d9c133b360b0 | 292 | //wait_ms(1); |
thomasmorris | 12:d9c133b360b0 | 293 | } |
thomasmorris | 12:d9c133b360b0 | 294 |