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