PROJ515 / Mbed 2 deprecated CoilingRigV9

Dependencies:   mbed mbed-rtos TextLCD

Committer:
thomasmorris
Date:
Mon Feb 11 16:58:07 2019 +0000
Revision:
14:63998be3d43c
Parent:
12:d9c133b360b0
Child:
16:9f98ec0ededb
Working stepper needs fixing more;

Who changed what in which revision?

UserRevisionLine numberNew 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 12:d9c133b360b0 12
thomasmorris 12:d9c133b360b0 13 void STEPPER_MOTOR::Rotate_Steps(int Steps)
thomasmorris 12:d9c133b360b0 14 {
thomasmorris 12:d9c133b360b0 15 #define STIME 1000
thomasmorris 12:d9c133b360b0 16
thomasmorris 12:d9c133b360b0 17 Steps = Steps*50;
thomasmorris 12:d9c133b360b0 18 //int correctionfactor;
thomasmorris 12:d9c133b360b0 19 //int timeofturn=correctinfactor*speed;
thomasmorris 12:d9c133b360b0 20
thomasmorris 12:d9c133b360b0 21 int mystep=0;
thomasmorris 14:63998be3d43c 22 //printf("START!!! step value is=%d\n\r",mystep);
thomasmorris 12:d9c133b360b0 23 for(int x =0 ; x <= Steps; x++)
thomasmorris 12:d9c133b360b0 24 {
thomasmorris 14:63998be3d43c 25 //printf("FOR LOOP! step value is=%d %d\n\r",mystep,x); // debugging feedback
thomasmorris 12:d9c133b360b0 26 // phase pulse order
thomasmorris 12:d9c133b360b0 27 this->pin1 = 0;
thomasmorris 12:d9c133b360b0 28 this->pin2 = 1;
thomasmorris 12:d9c133b360b0 29 this->pin3 = 0;
thomasmorris 12:d9c133b360b0 30 this->pin4 = 1;
thomasmorris 12:d9c133b360b0 31 mystep=1;
thomasmorris 12:d9c133b360b0 32 wait_us(1000);
thomasmorris 12:d9c133b360b0 33
thomasmorris 14:63998be3d43c 34 //printf("FOR LOOP! step value is=%d %d\n\r",mystep,x);
thomasmorris 12:d9c133b360b0 35 this->pin1 = 0;
thomasmorris 12:d9c133b360b0 36 this->pin2 = 1;
thomasmorris 12:d9c133b360b0 37 this->pin3 = 1;
thomasmorris 12:d9c133b360b0 38 this->pin4 = 0;
thomasmorris 12:d9c133b360b0 39 mystep=2;
thomasmorris 12:d9c133b360b0 40 wait_us(1000);
thomasmorris 12:d9c133b360b0 41
thomasmorris 14:63998be3d43c 42 //printf("FOR LOOP! step value is=%d %d\n\r",mystep,x);
thomasmorris 12:d9c133b360b0 43 this->pin1 = 1;
thomasmorris 12:d9c133b360b0 44 this->pin2 = 0;
thomasmorris 12:d9c133b360b0 45 this->pin3 = 1;
thomasmorris 12:d9c133b360b0 46 this->pin4 = 0;
thomasmorris 12:d9c133b360b0 47 mystep=3;
thomasmorris 12:d9c133b360b0 48 wait_us(1000);
thomasmorris 12:d9c133b360b0 49
thomasmorris 14:63998be3d43c 50 // printf("FOR LOOP! step value is=%d %d\n\r",mystep,x);
thomasmorris 12:d9c133b360b0 51 this->pin1 = 1;
thomasmorris 12:d9c133b360b0 52 this->pin2 = 0;
thomasmorris 12:d9c133b360b0 53 this->pin3 = 0;
thomasmorris 12:d9c133b360b0 54 this->pin4 = 1;
thomasmorris 12:d9c133b360b0 55 mystep=4;
thomasmorris 14:63998be3d43c 56 wait_us(1000);
thomasmorris 12:d9c133b360b0 57
thomasmorris 12:d9c133b360b0 58 /* printf("FOR LOOP! step value is=%d %d\n\r",mystep,x);
thomasmorris 12:d9c133b360b0 59 this->pin1 = 0;
thomasmorris 12:d9c133b360b0 60 this->pin2 = 1;
thomasmorris 12:d9c133b360b0 61 this->pin3 = 0;
thomasmorris 12:d9c133b360b0 62 this->pin4 = 0;
thomasmorris 12:d9c133b360b0 63 mystep=5;
thomasmorris 12:d9c133b360b0 64 wait_ms(0.6);
thomasmorris 12:d9c133b360b0 65 printf("FOR LOOP! step value is=%d %d\n\r",mystep,x);
thomasmorris 12:d9c133b360b0 66 this->pin1 = 1;
thomasmorris 12:d9c133b360b0 67 this->pin2 = 1;
thomasmorris 12:d9c133b360b0 68 this->pin3 = 0;
thomasmorris 12:d9c133b360b0 69 this->pin4 = 0;
thomasmorris 12:d9c133b360b0 70 mystep=6;
thomasmorris 12:d9c133b360b0 71 wait_ms(0.6);
thomasmorris 12:d9c133b360b0 72 printf("FOR LOOP! step value is=%d %d\n\r",mystep,x);
thomasmorris 12:d9c133b360b0 73 this->pin1 = 1;
thomasmorris 12:d9c133b360b0 74 this->pin2 = 0;
thomasmorris 12:d9c133b360b0 75 this->pin3 = 0;
thomasmorris 12:d9c133b360b0 76 this->pin4 = 0;
thomasmorris 12:d9c133b360b0 77 mystep=7;
thomasmorris 12:d9c133b360b0 78 wait_ms(0.6);
thomasmorris 12:d9c133b360b0 79
thomasmorris 12:d9c133b360b0 80 this->pin1 = 1;
thomasmorris 12:d9c133b360b0 81 this->pin2 = 0;
thomasmorris 12:d9c133b360b0 82 this->pin3 = 0;
thomasmorris 12:d9c133b360b0 83 this->pin4 = 1;
thomasmorris 12:d9c133b360b0 84 mystep=0;
thomasmorris 12:d9c133b360b0 85 wait_ms(0.6);
thomasmorris 12:d9c133b360b0 86
thomasmorris 12:d9c133b360b0 87 this->pin1 = 0;
thomasmorris 12:d9c133b360b0 88 this->pin2 = 0;
thomasmorris 12:d9c133b360b0 89 this->pin3 = 0;
thomasmorris 12:d9c133b360b0 90 this->pin4 = 0;
thomasmorris 12:d9c133b360b0 91 mystep=0;
thomasmorris 12:d9c133b360b0 92 wait_ms(0.6);*/
thomasmorris 12:d9c133b360b0 93 }
thomasmorris 14:63998be3d43c 94 this->pin1 = 0;
thomasmorris 14:63998be3d43c 95 this->pin2 = 0;
thomasmorris 14:63998be3d43c 96 this->pin3 = 0;
thomasmorris 14:63998be3d43c 97 this->pin4 = 0;
thomasmorris 12:d9c133b360b0 98 }
thomasmorris 12:d9c133b360b0 99
thomasmorris 12:d9c133b360b0 100 /*
thomasmorris 12:d9c133b360b0 101 switch(mystep){
thomasmorris 12:d9c133b360b0 102 case 0:{
thomasmorris 12:d9c133b360b0 103
thomasmorris 12:d9c133b360b0 104 break;
thomasmorris 12:d9c133b360b0 105 case 1:{
thomasmorris 12:d9c133b360b0 106
thomasmorris 12:d9c133b360b0 107 break;
thomasmorris 12:d9c133b360b0 108 case 2:
thomasmorris 12:d9c133b360b0 109
thomasmorris 12:d9c133b360b0 110 break;
thomasmorris 12:d9c133b360b0 111 case 3:
thomasmorris 12:d9c133b360b0 112
thomasmorris 12:d9c133b360b0 113 break;
thomasmorris 12:d9c133b360b0 114 case 4:
thomasmorris 12:d9c133b360b0 115
thomasmorris 12:d9c133b360b0 116 break;
thomasmorris 12:d9c133b360b0 117 case 5:
thomasmorris 12:d9c133b360b0 118
thomasmorris 12:d9c133b360b0 119 break;
thomasmorris 12:d9c133b360b0 120 case 6:
thomasmorris 12:d9c133b360b0 121
thomasmorris 12:d9c133b360b0 122 break;
thomasmorris 12:d9c133b360b0 123 case 7:
thomasmorris 12:d9c133b360b0 124
thomasmorris 12:d9c133b360b0 125 break;
thomasmorris 12:d9c133b360b0 126 default:
thomasmorris 12:d9c133b360b0 127
thomasmorris 12:d9c133b360b0 128 break;
thomasmorris 12:d9c133b360b0 129 }
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 //wait_ms(1);
thomasmorris 12:d9c133b360b0 144
thomasmorris 12:d9c133b360b0 145
thomasmorris 12:d9c133b360b0 146 void STEPPER_MOTOR::Permanent_Rotate_clock_wise()
thomasmorris 12:d9c133b360b0 147 {
thomasmorris 12:d9c133b360b0 148 switch(_step){
thomasmorris 12:d9c133b360b0 149 case 0:
thomasmorris 12:d9c133b360b0 150 pin1 = 0;
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 = 0;
thomasmorris 12:d9c133b360b0 157 pin2 = 0;
thomasmorris 12:d9c133b360b0 158 pin3 = 1;
thomasmorris 12:d9c133b360b0 159 pin4 = 1;
thomasmorris 12:d9c133b360b0 160 break;
thomasmorris 12:d9c133b360b0 161 case 2:
thomasmorris 12:d9c133b360b0 162 pin1 = 0;
thomasmorris 12:d9c133b360b0 163 pin2 = 0;
thomasmorris 12:d9c133b360b0 164 pin3 = 1;
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 = 1;
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 = 0;
thomasmorris 12:d9c133b360b0 177 pin4 = 0;
thomasmorris 12:d9c133b360b0 178 break;
thomasmorris 12:d9c133b360b0 179 case 5:
thomasmorris 12:d9c133b360b0 180 pin1 = 1;
thomasmorris 12:d9c133b360b0 181 pin2 = 1;
thomasmorris 12:d9c133b360b0 182 pin3 = 0;
thomasmorris 12:d9c133b360b0 183 pin4 = 0;
thomasmorris 12:d9c133b360b0 184 break;
thomasmorris 12:d9c133b360b0 185 case 6:
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 7:
thomasmorris 12:d9c133b360b0 192 pin1 = 1;
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 }
thomasmorris 12:d9c133b360b0 216
thomasmorris 12:d9c133b360b0 217 void STEPPER_MOTOR::Permanent_Rotate_anti_clock_wise()
thomasmorris 12:d9c133b360b0 218 {
thomasmorris 12:d9c133b360b0 219
thomasmorris 12:d9c133b360b0 220 //Rotate
thomasmorris 12:d9c133b360b0 221 switch(_step){
thomasmorris 12:d9c133b360b0 222 case 0:
thomasmorris 12:d9c133b360b0 223 pin1 = 1;
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 case 1:
thomasmorris 12:d9c133b360b0 229 pin1 = 1;
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 case 2:
thomasmorris 12:d9c133b360b0 235 pin1 = 1;
thomasmorris 12:d9c133b360b0 236 pin2 = 1;
thomasmorris 12:d9c133b360b0 237 pin3 = 0;
thomasmorris 12:d9c133b360b0 238 pin4 = 0;
thomasmorris 12:d9c133b360b0 239 break;
thomasmorris 12:d9c133b360b0 240 case 3:
thomasmorris 12:d9c133b360b0 241 pin1 = 0;
thomasmorris 12:d9c133b360b0 242 pin2 = 1;
thomasmorris 12:d9c133b360b0 243 pin3 = 0;
thomasmorris 12:d9c133b360b0 244 pin4 = 0;
thomasmorris 12:d9c133b360b0 245 break;
thomasmorris 12:d9c133b360b0 246 case 4:
thomasmorris 12:d9c133b360b0 247 pin1 = 0;
thomasmorris 12:d9c133b360b0 248 pin2 = 1;
thomasmorris 12:d9c133b360b0 249 pin3 = 1;
thomasmorris 12:d9c133b360b0 250 pin4 = 0;
thomasmorris 12:d9c133b360b0 251 break;
thomasmorris 12:d9c133b360b0 252 case 5:
thomasmorris 12:d9c133b360b0 253 pin1 = 0;
thomasmorris 12:d9c133b360b0 254 pin2 = 0;
thomasmorris 12:d9c133b360b0 255 pin3 = 1;
thomasmorris 12:d9c133b360b0 256 pin4 = 0;
thomasmorris 12:d9c133b360b0 257 break;
thomasmorris 12:d9c133b360b0 258 case 6:
thomasmorris 12:d9c133b360b0 259 pin1 = 0;
thomasmorris 12:d9c133b360b0 260 pin2 = 0;
thomasmorris 12:d9c133b360b0 261 pin3 = 1;
thomasmorris 12:d9c133b360b0 262 pin4 = 1;
thomasmorris 12:d9c133b360b0 263 break;
thomasmorris 12:d9c133b360b0 264 case 7:
thomasmorris 12:d9c133b360b0 265 pin1 = 0;
thomasmorris 12:d9c133b360b0 266 pin2 = 0;
thomasmorris 12:d9c133b360b0 267 pin3 = 0;
thomasmorris 12:d9c133b360b0 268 pin4 = 1;
thomasmorris 12:d9c133b360b0 269 break;
thomasmorris 12:d9c133b360b0 270 default:
thomasmorris 12:d9c133b360b0 271 pin1 = 0;
thomasmorris 12:d9c133b360b0 272 pin2 = 0;
thomasmorris 12:d9c133b360b0 273 pin3 = 0;
thomasmorris 12:d9c133b360b0 274 pin4 = 0;
thomasmorris 12:d9c133b360b0 275 break;
thomasmorris 12:d9c133b360b0 276 }
thomasmorris 12:d9c133b360b0 277 if(_dir){
thomasmorris 12:d9c133b360b0 278 _step++;
thomasmorris 12:d9c133b360b0 279 }else{
thomasmorris 12:d9c133b360b0 280 _step--;
thomasmorris 12:d9c133b360b0 281 }
thomasmorris 12:d9c133b360b0 282 if(_step>7){
thomasmorris 12:d9c133b360b0 283 _step=0;
thomasmorris 12:d9c133b360b0 284 }
thomasmorris 12:d9c133b360b0 285 if(_step<0){
thomasmorris 12:d9c133b360b0 286 _step=7;
thomasmorris 12:d9c133b360b0 287 }
thomasmorris 12:d9c133b360b0 288 //wait_ms(1);
thomasmorris 12:d9c133b360b0 289 }
thomasmorris 12:d9c133b360b0 290