Testing 1 blue pill
Dependencies: mbed mbed-rtos TextLCD
Stepper_Motor/STEPPER_MOTOR.cpp@29:f3b1387c81f1, 2019-03-13 (annotated)
- Committer:
- thomasmorris
- Date:
- Wed Mar 13 11:34:39 2019 +0000
- Revision:
- 29:f3b1387c81f1
- Parent:
- 28:3193157ebb0c
- Child:
- 30:a35f0ab97a65
Final Coiling Rig;
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 | 28:3193157ebb0c | 3 | int loop_wait_time = 0; |
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 | 23:07a368f2cdb1 | 12 | |
thomasmorris | 23:07a368f2cdb1 | 13 | void STEPPER_MOTOR::Pause_Code() |
thomasmorris | 23:07a368f2cdb1 | 14 | { |
thomasmorris | 23:07a368f2cdb1 | 15 | _Pause_Code = 1; |
thomasmorris | 23:07a368f2cdb1 | 16 | } |
thomasmorris | 23:07a368f2cdb1 | 17 | void STEPPER_MOTOR::Unpause_Code() |
thomasmorris | 23:07a368f2cdb1 | 18 | { |
thomasmorris | 23:07a368f2cdb1 | 19 | _Pause_Code = 0; |
thomasmorris | 23:07a368f2cdb1 | 20 | } |
thomasmorris | 18:3523660f3930 | 21 | float STEPPER_MOTOR::Get_Turns() |
thomasmorris | 18:3523660f3930 | 22 | { |
thomasmorris | 18:3523660f3930 | 23 | return (_Steps_Done /50); |
thomasmorris | 18:3523660f3930 | 24 | } |
thomasmorris | 29:f3b1387c81f1 | 25 | void STEPPER_MOTOR::Rotate_Steps(int Steps,int Function,bool Direction) |
thomasmorris | 25:9751619fa030 | 26 | { |
thomasmorris | 28:3193157ebb0c | 27 | if(Function == 2)//Coiling |
thomasmorris | 25:9751619fa030 | 28 | { |
thomasmorris | 28:3193157ebb0c | 29 | loop_wait_time = 2; |
thomasmorris | 25:9751619fa030 | 30 | } |
thomasmorris | 25:9751619fa030 | 31 | else if(Function == 3) |
thomasmorris | 25:9751619fa030 | 32 | { |
thomasmorris | 28:3193157ebb0c | 33 | loop_wait_time = 2;//Twisting |
thomasmorris | 25:9751619fa030 | 34 | } |
thomasmorris | 29:f3b1387c81f1 | 35 | Steps = Steps*50;//int correctionfactor;//int timeofturn=correctinfactor*speed; |
thomasmorris | 23:07a368f2cdb1 | 36 | _Steps_Done = 0; |
thomasmorris | 29:f3b1387c81f1 | 37 | |
thomasmorris | 23:07a368f2cdb1 | 38 | for(int x =0 ; x <= Steps; x++) |
thomasmorris | 23:07a368f2cdb1 | 39 | { |
thomasmorris | 23:07a368f2cdb1 | 40 | if(_Pause_Code == 0) |
thomasmorris | 23:07a368f2cdb1 | 41 | { |
thomasmorris | 29:f3b1387c81f1 | 42 | if(Direction == 0) |
thomasmorris | 29:f3b1387c81f1 | 43 | { |
thomasmorris | 29:f3b1387c81f1 | 44 | this->pin1 = 0; |
thomasmorris | 29:f3b1387c81f1 | 45 | this->pin2 = 1; |
thomasmorris | 29:f3b1387c81f1 | 46 | this->pin3 = 0; |
thomasmorris | 29:f3b1387c81f1 | 47 | this->pin4 = 1; |
thomasmorris | 29:f3b1387c81f1 | 48 | Thread::wait(loop_wait_time); |
thomasmorris | 29:f3b1387c81f1 | 49 | this->pin1 = 0; |
thomasmorris | 29:f3b1387c81f1 | 50 | this->pin2 = 1; |
thomasmorris | 29:f3b1387c81f1 | 51 | this->pin3 = 1; |
thomasmorris | 29:f3b1387c81f1 | 52 | this->pin4 = 0; |
thomasmorris | 29:f3b1387c81f1 | 53 | Thread::wait(loop_wait_time); |
thomasmorris | 29:f3b1387c81f1 | 54 | this->pin1 = 1; |
thomasmorris | 29:f3b1387c81f1 | 55 | this->pin2 = 0; |
thomasmorris | 29:f3b1387c81f1 | 56 | this->pin3 = 1; |
thomasmorris | 29:f3b1387c81f1 | 57 | this->pin4 = 0; |
thomasmorris | 29:f3b1387c81f1 | 58 | Thread::wait(loop_wait_time); |
thomasmorris | 29:f3b1387c81f1 | 59 | this->pin1 = 1; |
thomasmorris | 29:f3b1387c81f1 | 60 | this->pin2 = 0; |
thomasmorris | 29:f3b1387c81f1 | 61 | this->pin3 = 0; |
thomasmorris | 29:f3b1387c81f1 | 62 | this->pin4 = 1; |
thomasmorris | 29:f3b1387c81f1 | 63 | } |
thomasmorris | 29:f3b1387c81f1 | 64 | else if (Direction == 1) |
thomasmorris | 29:f3b1387c81f1 | 65 | { |
thomasmorris | 29:f3b1387c81f1 | 66 | this->pin1 = 1; |
thomasmorris | 29:f3b1387c81f1 | 67 | this->pin2 = 0; |
thomasmorris | 29:f3b1387c81f1 | 68 | this->pin3 = 0; |
thomasmorris | 29:f3b1387c81f1 | 69 | this->pin4 = 1; |
thomasmorris | 29:f3b1387c81f1 | 70 | Thread::wait(loop_wait_time); |
thomasmorris | 29:f3b1387c81f1 | 71 | this->pin1 = 1; |
thomasmorris | 29:f3b1387c81f1 | 72 | this->pin2 = 0; |
thomasmorris | 29:f3b1387c81f1 | 73 | this->pin3 = 1; |
thomasmorris | 29:f3b1387c81f1 | 74 | this->pin4 = 0; |
thomasmorris | 29:f3b1387c81f1 | 75 | Thread::wait(loop_wait_time); |
thomasmorris | 29:f3b1387c81f1 | 76 | this->pin1 = 0; |
thomasmorris | 29:f3b1387c81f1 | 77 | this->pin2 = 1; |
thomasmorris | 29:f3b1387c81f1 | 78 | this->pin3 = 1; |
thomasmorris | 29:f3b1387c81f1 | 79 | this->pin4 = 0; |
thomasmorris | 29:f3b1387c81f1 | 80 | Thread::wait(loop_wait_time); |
thomasmorris | 29:f3b1387c81f1 | 81 | this->pin1 = 0; |
thomasmorris | 29:f3b1387c81f1 | 82 | this->pin2 = 1; |
thomasmorris | 29:f3b1387c81f1 | 83 | this->pin3 = 0; |
thomasmorris | 29:f3b1387c81f1 | 84 | this->pin4 = 1; |
thomasmorris | 29:f3b1387c81f1 | 85 | Thread::wait(loop_wait_time); |
thomasmorris | 29:f3b1387c81f1 | 86 | } |
thomasmorris | 25:9751619fa030 | 87 | Thread::wait(loop_wait_time); |
thomasmorris | 23:07a368f2cdb1 | 88 | _Steps_Done = _Steps_Done +1; |
thomasmorris | 23:07a368f2cdb1 | 89 | } |
thomasmorris | 23:07a368f2cdb1 | 90 | else if (_Pause_Code == 1) |
thomasmorris | 23:07a368f2cdb1 | 91 | { |
thomasmorris | 23:07a368f2cdb1 | 92 | printf("Stepper motor code paused\n"); |
thomasmorris | 23:07a368f2cdb1 | 93 | while(_Pause_Code == 1) |
thomasmorris | 23:07a368f2cdb1 | 94 | { |
thomasmorris | 23:07a368f2cdb1 | 95 | Thread::wait(1); |
thomasmorris | 23:07a368f2cdb1 | 96 | } |
thomasmorris | 23:07a368f2cdb1 | 97 | } |
thomasmorris | 23:07a368f2cdb1 | 98 | }//End of for loop |
thomasmorris | 23:07a368f2cdb1 | 99 | this->pin1 = 0; |
thomasmorris | 23:07a368f2cdb1 | 100 | this->pin2 = 0; |
thomasmorris | 23:07a368f2cdb1 | 101 | this->pin3 = 0; |
thomasmorris | 23:07a368f2cdb1 | 102 | this->pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 103 | } |
thomasmorris | 12:d9c133b360b0 | 104 | void STEPPER_MOTOR::Permanent_Rotate_clock_wise() |
thomasmorris | 12:d9c133b360b0 | 105 | { |
thomasmorris | 12:d9c133b360b0 | 106 | switch(_step){ |
thomasmorris | 12:d9c133b360b0 | 107 | case 0: |
thomasmorris | 12:d9c133b360b0 | 108 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 109 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 110 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 111 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 112 | break; |
thomasmorris | 12:d9c133b360b0 | 113 | case 1: |
thomasmorris | 12:d9c133b360b0 | 114 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 115 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 116 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 117 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 118 | break; |
thomasmorris | 12:d9c133b360b0 | 119 | case 2: |
thomasmorris | 12:d9c133b360b0 | 120 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 121 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 122 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 123 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 124 | break; |
thomasmorris | 12:d9c133b360b0 | 125 | case 3: |
thomasmorris | 12:d9c133b360b0 | 126 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 127 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 128 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 129 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 130 | break; |
thomasmorris | 12:d9c133b360b0 | 131 | case 4: |
thomasmorris | 12:d9c133b360b0 | 132 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 133 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 134 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 135 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 136 | break; |
thomasmorris | 12:d9c133b360b0 | 137 | case 5: |
thomasmorris | 12:d9c133b360b0 | 138 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 139 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 140 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 141 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 142 | break; |
thomasmorris | 12:d9c133b360b0 | 143 | case 6: |
thomasmorris | 12:d9c133b360b0 | 144 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 145 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 146 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 147 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 148 | break; |
thomasmorris | 12:d9c133b360b0 | 149 | case 7: |
thomasmorris | 12:d9c133b360b0 | 150 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 151 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 152 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 153 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 154 | break; |
thomasmorris | 12:d9c133b360b0 | 155 | default: |
thomasmorris | 12:d9c133b360b0 | 156 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 157 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 158 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 159 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 160 | break; |
thomasmorris | 12:d9c133b360b0 | 161 | } |
thomasmorris | 12:d9c133b360b0 | 162 | if(_dir){ |
thomasmorris | 12:d9c133b360b0 | 163 | _step++; |
thomasmorris | 12:d9c133b360b0 | 164 | }else{ |
thomasmorris | 12:d9c133b360b0 | 165 | _step--; |
thomasmorris | 12:d9c133b360b0 | 166 | } |
thomasmorris | 12:d9c133b360b0 | 167 | if(_step>7){ |
thomasmorris | 12:d9c133b360b0 | 168 | _step=0; |
thomasmorris | 12:d9c133b360b0 | 169 | } |
thomasmorris | 12:d9c133b360b0 | 170 | if(_step<0){ |
thomasmorris | 12:d9c133b360b0 | 171 | _step=7; |
thomasmorris | 12:d9c133b360b0 | 172 | } |
thomasmorris | 12:d9c133b360b0 | 173 | } |
thomasmorris | 12:d9c133b360b0 | 174 | |
thomasmorris | 12:d9c133b360b0 | 175 | void STEPPER_MOTOR::Permanent_Rotate_anti_clock_wise() |
thomasmorris | 12:d9c133b360b0 | 176 | { |
thomasmorris | 12:d9c133b360b0 | 177 | |
thomasmorris | 12:d9c133b360b0 | 178 | //Rotate |
thomasmorris | 12:d9c133b360b0 | 179 | switch(_step){ |
thomasmorris | 12:d9c133b360b0 | 180 | case 0: |
thomasmorris | 12:d9c133b360b0 | 181 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 182 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 183 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 184 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 185 | break; |
thomasmorris | 12:d9c133b360b0 | 186 | case 1: |
thomasmorris | 12:d9c133b360b0 | 187 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 188 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 189 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 190 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 191 | break; |
thomasmorris | 12:d9c133b360b0 | 192 | case 2: |
thomasmorris | 12:d9c133b360b0 | 193 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 194 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 195 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 196 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 197 | break; |
thomasmorris | 12:d9c133b360b0 | 198 | case 3: |
thomasmorris | 12:d9c133b360b0 | 199 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 200 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 201 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 202 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 203 | break; |
thomasmorris | 12:d9c133b360b0 | 204 | case 4: |
thomasmorris | 12:d9c133b360b0 | 205 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 206 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 207 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 208 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 209 | break; |
thomasmorris | 12:d9c133b360b0 | 210 | case 5: |
thomasmorris | 12:d9c133b360b0 | 211 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 212 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 213 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 214 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 215 | break; |
thomasmorris | 12:d9c133b360b0 | 216 | case 6: |
thomasmorris | 12:d9c133b360b0 | 217 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 218 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 219 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 220 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 221 | break; |
thomasmorris | 12:d9c133b360b0 | 222 | case 7: |
thomasmorris | 12:d9c133b360b0 | 223 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 224 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 225 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 226 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 227 | break; |
thomasmorris | 12:d9c133b360b0 | 228 | default: |
thomasmorris | 12:d9c133b360b0 | 229 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 230 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 231 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 232 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 233 | break; |
thomasmorris | 12:d9c133b360b0 | 234 | } |
thomasmorris | 12:d9c133b360b0 | 235 | if(_dir){ |
thomasmorris | 12:d9c133b360b0 | 236 | _step++; |
thomasmorris | 12:d9c133b360b0 | 237 | }else{ |
thomasmorris | 12:d9c133b360b0 | 238 | _step--; |
thomasmorris | 12:d9c133b360b0 | 239 | } |
thomasmorris | 12:d9c133b360b0 | 240 | if(_step>7){ |
thomasmorris | 12:d9c133b360b0 | 241 | _step=0; |
thomasmorris | 12:d9c133b360b0 | 242 | } |
thomasmorris | 12:d9c133b360b0 | 243 | if(_step<0){ |
thomasmorris | 12:d9c133b360b0 | 244 | _step=7; |
thomasmorris | 12:d9c133b360b0 | 245 | } |
thomasmorris | 12:d9c133b360b0 | 246 | //wait_ms(1); |
thomasmorris | 12:d9c133b360b0 | 247 | } |
thomasmorris | 12:d9c133b360b0 | 248 |