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