Testing 1 blue pill
Dependencies: mbed mbed-rtos TextLCD
Stepper_Motor/STEPPER_MOTOR.cpp@30:a35f0ab97a65, 2019-04-04 (annotated)
- Committer:
- thomasmorris
- Date:
- Thu Apr 04 10:52:22 2019 +0000
- Revision:
- 30:a35f0ab97a65
- Parent:
- 29:f3b1387c81f1
Testing 1;
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 | 30:a35f0ab97a65 | 84 | this->pin4 = 1; |
thomasmorris | 29:f3b1387c81f1 | 85 | } |
thomasmorris | 25:9751619fa030 | 86 | Thread::wait(loop_wait_time); |
thomasmorris | 23:07a368f2cdb1 | 87 | _Steps_Done = _Steps_Done +1; |
thomasmorris | 23:07a368f2cdb1 | 88 | } |
thomasmorris | 23:07a368f2cdb1 | 89 | else if (_Pause_Code == 1) |
thomasmorris | 23:07a368f2cdb1 | 90 | { |
thomasmorris | 23:07a368f2cdb1 | 91 | printf("Stepper motor code paused\n"); |
thomasmorris | 23:07a368f2cdb1 | 92 | while(_Pause_Code == 1) |
thomasmorris | 23:07a368f2cdb1 | 93 | { |
thomasmorris | 23:07a368f2cdb1 | 94 | Thread::wait(1); |
thomasmorris | 23:07a368f2cdb1 | 95 | } |
thomasmorris | 23:07a368f2cdb1 | 96 | } |
thomasmorris | 23:07a368f2cdb1 | 97 | }//End of for loop |
thomasmorris | 23:07a368f2cdb1 | 98 | this->pin1 = 0; |
thomasmorris | 23:07a368f2cdb1 | 99 | this->pin2 = 0; |
thomasmorris | 23:07a368f2cdb1 | 100 | this->pin3 = 0; |
thomasmorris | 23:07a368f2cdb1 | 101 | this->pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 102 | } |
thomasmorris | 12:d9c133b360b0 | 103 | void STEPPER_MOTOR::Permanent_Rotate_clock_wise() |
thomasmorris | 12:d9c133b360b0 | 104 | { |
thomasmorris | 12:d9c133b360b0 | 105 | switch(_step){ |
thomasmorris | 12:d9c133b360b0 | 106 | case 0: |
thomasmorris | 12:d9c133b360b0 | 107 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 108 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 109 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 110 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 111 | break; |
thomasmorris | 12:d9c133b360b0 | 112 | case 1: |
thomasmorris | 12:d9c133b360b0 | 113 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 114 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 115 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 116 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 117 | break; |
thomasmorris | 12:d9c133b360b0 | 118 | case 2: |
thomasmorris | 12:d9c133b360b0 | 119 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 120 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 121 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 122 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 123 | break; |
thomasmorris | 12:d9c133b360b0 | 124 | case 3: |
thomasmorris | 12:d9c133b360b0 | 125 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 126 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 127 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 128 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 129 | break; |
thomasmorris | 12:d9c133b360b0 | 130 | case 4: |
thomasmorris | 12:d9c133b360b0 | 131 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 132 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 133 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 134 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 135 | break; |
thomasmorris | 12:d9c133b360b0 | 136 | case 5: |
thomasmorris | 12:d9c133b360b0 | 137 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 138 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 139 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 140 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 141 | break; |
thomasmorris | 12:d9c133b360b0 | 142 | case 6: |
thomasmorris | 12:d9c133b360b0 | 143 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 144 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 145 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 146 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 147 | break; |
thomasmorris | 12:d9c133b360b0 | 148 | case 7: |
thomasmorris | 12:d9c133b360b0 | 149 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 150 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 151 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 152 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 153 | break; |
thomasmorris | 12:d9c133b360b0 | 154 | default: |
thomasmorris | 12:d9c133b360b0 | 155 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 156 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 157 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 158 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 159 | break; |
thomasmorris | 12:d9c133b360b0 | 160 | } |
thomasmorris | 12:d9c133b360b0 | 161 | if(_dir){ |
thomasmorris | 12:d9c133b360b0 | 162 | _step++; |
thomasmorris | 12:d9c133b360b0 | 163 | }else{ |
thomasmorris | 12:d9c133b360b0 | 164 | _step--; |
thomasmorris | 12:d9c133b360b0 | 165 | } |
thomasmorris | 12:d9c133b360b0 | 166 | if(_step>7){ |
thomasmorris | 12:d9c133b360b0 | 167 | _step=0; |
thomasmorris | 12:d9c133b360b0 | 168 | } |
thomasmorris | 12:d9c133b360b0 | 169 | if(_step<0){ |
thomasmorris | 12:d9c133b360b0 | 170 | _step=7; |
thomasmorris | 12:d9c133b360b0 | 171 | } |
thomasmorris | 12:d9c133b360b0 | 172 | } |
thomasmorris | 12:d9c133b360b0 | 173 | |
thomasmorris | 12:d9c133b360b0 | 174 | void STEPPER_MOTOR::Permanent_Rotate_anti_clock_wise() |
thomasmorris | 12:d9c133b360b0 | 175 | { |
thomasmorris | 12:d9c133b360b0 | 176 | |
thomasmorris | 12:d9c133b360b0 | 177 | //Rotate |
thomasmorris | 12:d9c133b360b0 | 178 | switch(_step){ |
thomasmorris | 12:d9c133b360b0 | 179 | case 0: |
thomasmorris | 12:d9c133b360b0 | 180 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 181 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 182 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 183 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 184 | break; |
thomasmorris | 12:d9c133b360b0 | 185 | case 1: |
thomasmorris | 12:d9c133b360b0 | 186 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 187 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 188 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 189 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 190 | break; |
thomasmorris | 12:d9c133b360b0 | 191 | case 2: |
thomasmorris | 12:d9c133b360b0 | 192 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 193 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 194 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 195 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 196 | break; |
thomasmorris | 12:d9c133b360b0 | 197 | case 3: |
thomasmorris | 12:d9c133b360b0 | 198 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 199 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 200 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 201 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 202 | break; |
thomasmorris | 12:d9c133b360b0 | 203 | case 4: |
thomasmorris | 12:d9c133b360b0 | 204 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 205 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 206 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 207 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 208 | break; |
thomasmorris | 12:d9c133b360b0 | 209 | case 5: |
thomasmorris | 12:d9c133b360b0 | 210 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 211 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 212 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 213 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 214 | break; |
thomasmorris | 12:d9c133b360b0 | 215 | case 6: |
thomasmorris | 12:d9c133b360b0 | 216 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 217 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 218 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 219 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 220 | break; |
thomasmorris | 12:d9c133b360b0 | 221 | case 7: |
thomasmorris | 12:d9c133b360b0 | 222 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 223 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 224 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 225 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 226 | break; |
thomasmorris | 12:d9c133b360b0 | 227 | default: |
thomasmorris | 12:d9c133b360b0 | 228 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 229 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 230 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 231 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 232 | break; |
thomasmorris | 12:d9c133b360b0 | 233 | } |
thomasmorris | 12:d9c133b360b0 | 234 | if(_dir){ |
thomasmorris | 12:d9c133b360b0 | 235 | _step++; |
thomasmorris | 12:d9c133b360b0 | 236 | }else{ |
thomasmorris | 12:d9c133b360b0 | 237 | _step--; |
thomasmorris | 12:d9c133b360b0 | 238 | } |
thomasmorris | 12:d9c133b360b0 | 239 | if(_step>7){ |
thomasmorris | 12:d9c133b360b0 | 240 | _step=0; |
thomasmorris | 12:d9c133b360b0 | 241 | } |
thomasmorris | 12:d9c133b360b0 | 242 | if(_step<0){ |
thomasmorris | 12:d9c133b360b0 | 243 | _step=7; |
thomasmorris | 12:d9c133b360b0 | 244 | } |
thomasmorris | 12:d9c133b360b0 | 245 | //wait_ms(1); |
thomasmorris | 12:d9c133b360b0 | 246 | } |
thomasmorris | 12:d9c133b360b0 | 247 |