Cycle Testing
Dependencies: Stepper mbed HCSR04 millis
main.cpp@10:4c3a8f835bef, 2019-01-21 (annotated)
- Committer:
- vamgehealthcare11
- Date:
- Mon Jan 21 23:38:28 2019 +0000
- Revision:
- 10:4c3a8f835bef
- Parent:
- 9:5263aacbda53
update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rschimpf78 | 2:e8f8095464cf | 1 | #include "mbed.h" |
rschimpf78 | 2:e8f8095464cf | 2 | #include "Stepper.h" |
rschimpf78 | 2:e8f8095464cf | 3 | #include "string" |
rschimpf78 | 3:fd2ca631ab44 | 4 | #include "hcsr04.h" |
rschimpf78 | 5:e958d61e0564 | 5 | #include "millis.h" |
rschimpf78 | 6:6ca8a0ef7d4a | 6 | |
rschimpf78 | 3:fd2ca631ab44 | 7 | Serial pc(USBTX, USBRX); |
vamgehealthcare11 | 9:5263aacbda53 | 8 | HCSR04 heightsensor(PTD2,PTD3); |
rschimpf78 | 6:6ca8a0ef7d4a | 9 | HCSR04 pathsensor(PTC2,PTA2); //(trig,echo) |
rschimpf78 | 3:fd2ca631ab44 | 10 | DigitalOut in1(PTC12); |
rschimpf78 | 3:fd2ca631ab44 | 11 | DigitalOut in2(D7); |
rschimpf78 | 6:6ca8a0ef7d4a | 12 | DigitalIn magnetSwitch(D10); |
vamgehealthcare11 | 9:5263aacbda53 | 13 | DigitalIn homeSwitch(D9); |
vamgehealthcare11 | 9:5263aacbda53 | 14 | Stepper mot(D3,D4); |
rschimpf78 | 2:e8f8095464cf | 15 | DigitalOut en(D2); |
rschimpf78 | 0:88834eed3de0 | 16 | |
vamgehealthcare11 | 9:5263aacbda53 | 17 | int main(){ |
vamgehealthcare11 | 10:4c3a8f835bef | 18 | |
vamgehealthcare11 | 10:4c3a8f835bef | 19 | |
vamgehealthcare11 | 9:5263aacbda53 | 20 | en = 1; |
vamgehealthcare11 | 9:5263aacbda53 | 21 | in1 = 1; |
vamgehealthcare11 | 9:5263aacbda53 | 22 | in2 = 1; |
vamgehealthcare11 | 9:5263aacbda53 | 23 | magnetSwitch.mode(PullUp); |
vamgehealthcare11 | 9:5263aacbda53 | 24 | wait (.01); |
vamgehealthcare11 | 9:5263aacbda53 | 25 | homeSwitch.mode(PullUp); |
vamgehealthcare11 | 9:5263aacbda53 | 26 | wait (0.01); |
vamgehealthcare11 | 9:5263aacbda53 | 27 | |
vamgehealthcare11 | 9:5263aacbda53 | 28 | while(1){ |
vamgehealthcare11 | 10:4c3a8f835bef | 29 | |
vamgehealthcare11 | 10:4c3a8f835bef | 30 | LOOPY: |
vamgehealthcare11 | 10:4c3a8f835bef | 31 | |
vamgehealthcare11 | 10:4c3a8f835bef | 32 | for ( int command = 1; command < 4; command++){ |
rschimpf78 | 0:88834eed3de0 | 33 | |
vamgehealthcare11 | 9:5263aacbda53 | 34 | if (command==1){ |
rschimpf78 | 3:fd2ca631ab44 | 35 | |
vamgehealthcare11 | 9:5263aacbda53 | 36 | int plates = 0; |
vamgehealthcare11 | 9:5263aacbda53 | 37 | unsigned int dist; |
vamgehealthcare11 | 9:5263aacbda53 | 38 | int platedist; |
rschimpf78 | 3:fd2ca631ab44 | 39 | |
vamgehealthcare11 | 10:4c3a8f835bef | 40 | plates = (rand() % 16) + 1; |
rschimpf78 | 3:fd2ca631ab44 | 41 | |
vamgehealthcare11 | 9:5263aacbda53 | 42 | if (magnetSwitch == 1){ |
vamgehealthcare11 | 9:5263aacbda53 | 43 | pc.printf("\nNot in Home Position"); |
vamgehealthcare11 | 10:4c3a8f835bef | 44 | exit(1); |
vamgehealthcare11 | 9:5263aacbda53 | 45 | } |
vamgehealthcare11 | 9:5263aacbda53 | 46 | else{ |
vamgehealthcare11 | 9:5263aacbda53 | 47 | wait(1); |
vamgehealthcare11 | 9:5263aacbda53 | 48 | in1=1; |
vamgehealthcare11 | 9:5263aacbda53 | 49 | in2=0; |
vamgehealthcare11 | 9:5263aacbda53 | 50 | wait_ms(900); |
vamgehealthcare11 | 9:5263aacbda53 | 51 | in1=1; |
vamgehealthcare11 | 9:5263aacbda53 | 52 | in2=1; |
vamgehealthcare11 | 9:5263aacbda53 | 53 | } |
vamgehealthcare11 | 10:4c3a8f835bef | 54 | pc.printf("\nRequested Plates:%ld",plates); |
vamgehealthcare11 | 10:4c3a8f835bef | 55 | platedist = (468-24*plates); |
vamgehealthcare11 | 9:5263aacbda53 | 56 | millisStart(); |
vamgehealthcare11 | 9:5263aacbda53 | 57 | long durationSM = 0; |
vamgehealthcare11 | 9:5263aacbda53 | 58 | long starttimeSM = millis(); |
vamgehealthcare11 | 9:5263aacbda53 | 59 | |
vamgehealthcare11 | 10:4c3a8f835bef | 60 | SENSORREAD: |
vamgehealthcare11 | 10:4c3a8f835bef | 61 | heightsensor.start(); |
vamgehealthcare11 | 10:4c3a8f835bef | 62 | wait_ms(250); |
vamgehealthcare11 | 10:4c3a8f835bef | 63 | dist = heightsensor.get_dist_cm(); |
vamgehealthcare11 | 10:4c3a8f835bef | 64 | |
vamgehealthcare11 | 10:4c3a8f835bef | 65 | |
vamgehealthcare11 | 10:4c3a8f835bef | 66 | while ((dist < platedist-2 && durationSM < 60000 || dist > platedist+2 && durationSM < 60000)){ |
vamgehealthcare11 | 10:4c3a8f835bef | 67 | |
vamgehealthcare11 | 9:5263aacbda53 | 68 | |
vamgehealthcare11 | 9:5263aacbda53 | 69 | heightsensor.start(); |
vamgehealthcare11 | 9:5263aacbda53 | 70 | long currenttimeSM = millis(); |
vamgehealthcare11 | 9:5263aacbda53 | 71 | durationSM = currenttimeSM-starttimeSM; |
vamgehealthcare11 | 9:5263aacbda53 | 72 | en = 0; |
vamgehealthcare11 | 9:5263aacbda53 | 73 | mot.setSpeed(500); |
vamgehealthcare11 | 10:4c3a8f835bef | 74 | dist = heightsensor.get_dist_cm(); |
vamgehealthcare11 | 10:4c3a8f835bef | 75 | |
vamgehealthcare11 | 10:4c3a8f835bef | 76 | if(dist >= 480 || dist <= 40 ){ |
vamgehealthcare11 | 10:4c3a8f835bef | 77 | pc.printf("\nBad Read at :%ld", durationSM); |
vamgehealthcare11 | 10:4c3a8f835bef | 78 | goto SENSORREAD; |
vamgehealthcare11 | 10:4c3a8f835bef | 79 | } |
vamgehealthcare11 | 10:4c3a8f835bef | 80 | |
vamgehealthcare11 | 10:4c3a8f835bef | 81 | if(dist<platedist-2){ //397 403 |
vamgehealthcare11 | 9:5263aacbda53 | 82 | mot.rotate(1); |
vamgehealthcare11 | 9:5263aacbda53 | 83 | wait(0.5); |
vamgehealthcare11 | 9:5263aacbda53 | 84 | } |
vamgehealthcare11 | 9:5263aacbda53 | 85 | if(dist>platedist+2){ |
vamgehealthcare11 | 9:5263aacbda53 | 86 | mot.rotate(0); |
vamgehealthcare11 | 9:5263aacbda53 | 87 | wait(0.5); |
vamgehealthcare11 | 9:5263aacbda53 | 88 | } |
vamgehealthcare11 | 9:5263aacbda53 | 89 | mot.stop(); |
rschimpf78 | 3:fd2ca631ab44 | 90 | } |
vamgehealthcare11 | 9:5263aacbda53 | 91 | |
vamgehealthcare11 | 10:4c3a8f835bef | 92 | pc.printf("\nSelector Height:%ld", dist); |
vamgehealthcare11 | 10:4c3a8f835bef | 93 | pc.printf("\nSelector Motor Duration:%ld", durationSM); |
vamgehealthcare11 | 9:5263aacbda53 | 94 | mot.stop(); |
vamgehealthcare11 | 9:5263aacbda53 | 95 | en = 1; |
vamgehealthcare11 | 9:5263aacbda53 | 96 | wait(1); |
rschimpf78 | 3:fd2ca631ab44 | 97 | |
vamgehealthcare11 | 10:4c3a8f835bef | 98 | if (dist >= 480 || dist <= 40){ |
vamgehealthcare11 | 10:4c3a8f835bef | 99 | pc.printf("\nSelector Distance out of Range"); |
vamgehealthcare11 | 10:4c3a8f835bef | 100 | exit(1); |
rschimpf78 | 3:fd2ca631ab44 | 101 | } |
vamgehealthcare11 | 10:4c3a8f835bef | 102 | else if (durationSM >= 60000){ |
vamgehealthcare11 | 9:5263aacbda53 | 103 | pc.printf("\nSelector Motor Time Out"); |
vamgehealthcare11 | 10:4c3a8f835bef | 104 | exit(1); |
rschimpf78 | 3:fd2ca631ab44 | 105 | } |
vamgehealthcare11 | 9:5263aacbda53 | 106 | else{ |
vamgehealthcare11 | 9:5263aacbda53 | 107 | in1=1; |
vamgehealthcare11 | 9:5263aacbda53 | 108 | in2=0; |
vamgehealthcare11 | 10:4c3a8f835bef | 109 | wait(56); |
vamgehealthcare11 | 9:5263aacbda53 | 110 | in1=1; |
vamgehealthcare11 | 9:5263aacbda53 | 111 | in2=1; |
vamgehealthcare11 | 10:4c3a8f835bef | 112 | pc.printf("\nSending Finished"); |
vamgehealthcare11 | 9:5263aacbda53 | 113 | } |
vamgehealthcare11 | 9:5263aacbda53 | 114 | } |
vamgehealthcare11 | 9:5263aacbda53 | 115 | |
vamgehealthcare11 | 9:5263aacbda53 | 116 | else if (command==2){ |
vamgehealthcare11 | 9:5263aacbda53 | 117 | |
vamgehealthcare11 | 9:5263aacbda53 | 118 | unsigned int pathdist; |
vamgehealthcare11 | 9:5263aacbda53 | 119 | int path_sheets; |
vamgehealthcare11 | 9:5263aacbda53 | 120 | int detectionTOL = 12; |
vamgehealthcare11 | 9:5263aacbda53 | 121 | int sens_pos = 131; |
vamgehealthcare11 | 9:5263aacbda53 | 122 | int sheet_th = 25; |
vamgehealthcare11 | 9:5263aacbda53 | 123 | pathsensor.start(); |
vamgehealthcare11 | 9:5263aacbda53 | 124 | wait_ms(500); |
vamgehealthcare11 | 9:5263aacbda53 | 125 | pathdist=pathsensor.get_dist_cm(); |
rschimpf78 | 3:fd2ca631ab44 | 126 | |
vamgehealthcare11 | 9:5263aacbda53 | 127 | if (pathdist >= sens_pos-detectionTOL && pathdist <= sens_pos+detectionTOL){ //110,130 |
vamgehealthcare11 | 9:5263aacbda53 | 128 | path_sheets = 16; |
vamgehealthcare11 | 9:5263aacbda53 | 129 | } |
vamgehealthcare11 | 9:5263aacbda53 | 130 | else if (pathdist >= sens_pos-detectionTOL+sheet_th && pathdist <= sens_pos+detectionTOL+sheet_th){ //152,168 a: |
vamgehealthcare11 | 9:5263aacbda53 | 131 | path_sheets = 15; |
vamgehealthcare11 | 9:5263aacbda53 | 132 | } |
vamgehealthcare11 | 9:5263aacbda53 | 133 | else if (pathdist >= sens_pos-detectionTOL+2*sheet_th && pathdist <= sens_pos+detectionTOL+2*sheet_th){ //179-191 |
vamgehealthcare11 | 9:5263aacbda53 | 134 | path_sheets = 14; |
vamgehealthcare11 | 9:5263aacbda53 | 135 | } |
vamgehealthcare11 | 9:5263aacbda53 | 136 | else if (pathdist >= sens_pos-detectionTOL+3*sheet_th && pathdist <= sens_pos+detectionTOL+3*sheet_th){ //205-217 |
vamgehealthcare11 | 9:5263aacbda53 | 137 | path_sheets = 13; |
vamgehealthcare11 | 9:5263aacbda53 | 138 | } |
vamgehealthcare11 | 9:5263aacbda53 | 139 | else if (pathdist >= sens_pos-detectionTOL+4*sheet_th && pathdist <= sens_pos+detectionTOL+4*sheet_th){ //231-243 |
vamgehealthcare11 | 9:5263aacbda53 | 140 | path_sheets = 12; |
vamgehealthcare11 | 9:5263aacbda53 | 141 | } |
vamgehealthcare11 | 9:5263aacbda53 | 142 | else if (pathdist >= sens_pos-detectionTOL+5*sheet_th && pathdist <= sens_pos+detectionTOL+5*sheet_th){ //257-269 |
vamgehealthcare11 | 9:5263aacbda53 | 143 | path_sheets = 11; |
vamgehealthcare11 | 9:5263aacbda53 | 144 | } |
vamgehealthcare11 | 9:5263aacbda53 | 145 | else if (pathdist >= sens_pos-detectionTOL+6*sheet_th && pathdist <= sens_pos+detectionTOL+6*sheet_th){ //283-295 |
rschimpf78 | 3:fd2ca631ab44 | 146 | path_sheets = 10; |
rschimpf78 | 3:fd2ca631ab44 | 147 | |
rschimpf78 | 3:fd2ca631ab44 | 148 | } |
vamgehealthcare11 | 9:5263aacbda53 | 149 | else if (pathdist >= sens_pos-detectionTOL+7*sheet_th && pathdist <= sens_pos+detectionTOL+7*sheet_th){ //309-321 |
rschimpf78 | 3:fd2ca631ab44 | 150 | path_sheets = 9; |
rschimpf78 | 3:fd2ca631ab44 | 151 | |
rschimpf78 | 3:fd2ca631ab44 | 152 | } |
vamgehealthcare11 | 9:5263aacbda53 | 153 | else if (pathdist >= sens_pos-detectionTOL+8*sheet_th && pathdist <= sens_pos+detectionTOL+8*sheet_th){ //323-333 |
rschimpf78 | 3:fd2ca631ab44 | 154 | path_sheets = 8; |
rschimpf78 | 3:fd2ca631ab44 | 155 | |
rschimpf78 | 3:fd2ca631ab44 | 156 | } |
vamgehealthcare11 | 9:5263aacbda53 | 157 | else if (pathdist >= sens_pos-detectionTOL+9*sheet_th && pathdist <= sens_pos+detectionTOL+9*sheet_th){ //349-359 |
rschimpf78 | 3:fd2ca631ab44 | 158 | path_sheets = 7; |
rschimpf78 | 3:fd2ca631ab44 | 159 | |
rschimpf78 | 3:fd2ca631ab44 | 160 | } |
vamgehealthcare11 | 9:5263aacbda53 | 161 | else if (pathdist >= sens_pos-detectionTOL+10*sheet_th && pathdist <= sens_pos+detectionTOL+10*sheet_th){ |
rschimpf78 | 3:fd2ca631ab44 | 162 | path_sheets = 6; |
rschimpf78 | 3:fd2ca631ab44 | 163 | |
rschimpf78 | 3:fd2ca631ab44 | 164 | } |
vamgehealthcare11 | 9:5263aacbda53 | 165 | else if (pathdist >= sens_pos-detectionTOL+11*sheet_th && pathdist <= sens_pos+detectionTOL+11*sheet_th){ |
rschimpf78 | 3:fd2ca631ab44 | 166 | path_sheets = 5; |
rschimpf78 | 3:fd2ca631ab44 | 167 | |
rschimpf78 | 3:fd2ca631ab44 | 168 | } |
vamgehealthcare11 | 9:5263aacbda53 | 169 | else if (pathdist >= sens_pos-detectionTOL+12*sheet_th && pathdist <= sens_pos+detectionTOL+12*sheet_th){ |
rschimpf78 | 3:fd2ca631ab44 | 170 | path_sheets = 4; |
rschimpf78 | 3:fd2ca631ab44 | 171 | |
rschimpf78 | 3:fd2ca631ab44 | 172 | } |
vamgehealthcare11 | 9:5263aacbda53 | 173 | else if (pathdist >= sens_pos-detectionTOL+13*sheet_th && pathdist <= sens_pos+detectionTOL+13*sheet_th){ //468,478 |
rschimpf78 | 3:fd2ca631ab44 | 174 | path_sheets = 3; |
rschimpf78 | 3:fd2ca631ab44 | 175 | |
rschimpf78 | 3:fd2ca631ab44 | 176 | } |
vamgehealthcare11 | 9:5263aacbda53 | 177 | else if (pathdist >= sens_pos-detectionTOL+14*sheet_th && pathdist <= sens_pos+detectionTOL+14*sheet_th){ //494,504 |
rschimpf78 | 3:fd2ca631ab44 | 178 | path_sheets = 2; |
rschimpf78 | 3:fd2ca631ab44 | 179 | |
rschimpf78 | 3:fd2ca631ab44 | 180 | } |
vamgehealthcare11 | 9:5263aacbda53 | 181 | else if (pathdist >= sens_pos-10-detectionTOL+15*sheet_th && pathdist <= sens_pos-10+detectionTOL+15*sheet_th){//greater than 509 less 517 |
rschimpf78 | 3:fd2ca631ab44 | 182 | path_sheets = 1; |
rschimpf78 | 3:fd2ca631ab44 | 183 | |
rschimpf78 | 3:fd2ca631ab44 | 184 | } |
vamgehealthcare11 | 9:5263aacbda53 | 185 | else if (pathdist > sens_pos-detectionTOL+16*sheet_th){ //greater than 534 |
rschimpf78 | 3:fd2ca631ab44 | 186 | path_sheets = 0; |
vamgehealthcare11 | 9:5263aacbda53 | 187 | } |
vamgehealthcare11 | 9:5263aacbda53 | 188 | else { |
vamgehealthcare11 | 9:5263aacbda53 | 189 | pc.printf("\nBeam Path Detector distance out of range : %ld", pathdist); |
vamgehealthcare11 | 9:5263aacbda53 | 190 | wait(2); |
vamgehealthcare11 | 10:4c3a8f835bef | 191 | path_sheets = 111; |
vamgehealthcare11 | 9:5263aacbda53 | 192 | } |
vamgehealthcare11 | 10:4c3a8f835bef | 193 | pc.printf("\nDetected Plates: %ld", path_sheets); |
vamgehealthcare11 | 9:5263aacbda53 | 194 | wait(2); |
rschimpf78 | 3:fd2ca631ab44 | 195 | } |
vamgehealthcare11 | 9:5263aacbda53 | 196 | |
vamgehealthcare11 | 9:5263aacbda53 | 197 | else if (command==3){ |
vamgehealthcare11 | 9:5263aacbda53 | 198 | |
vamgehealthcare11 | 9:5263aacbda53 | 199 | millisStart(); |
vamgehealthcare11 | 9:5263aacbda53 | 200 | long Duration = 0; |
vamgehealthcare11 | 9:5263aacbda53 | 201 | long StartTime = millis(); |
rschimpf78 | 5:e958d61e0564 | 202 | |
vamgehealthcare11 | 10:4c3a8f835bef | 203 | while(magnetSwitch == 1 && homeSwitch == 1 && Duration <= 52000 ){ |
vamgehealthcare11 | 9:5263aacbda53 | 204 | in1=0; |
vamgehealthcare11 | 9:5263aacbda53 | 205 | in2=1; |
vamgehealthcare11 | 9:5263aacbda53 | 206 | long CurrentTime = millis(); |
vamgehealthcare11 | 9:5263aacbda53 | 207 | Duration = CurrentTime-StartTime; |
vamgehealthcare11 | 9:5263aacbda53 | 208 | } |
vamgehealthcare11 | 9:5263aacbda53 | 209 | |
vamgehealthcare11 | 9:5263aacbda53 | 210 | in1=1; |
vamgehealthcare11 | 9:5263aacbda53 | 211 | in2=1; |
vamgehealthcare11 | 9:5263aacbda53 | 212 | |
vamgehealthcare11 | 10:4c3a8f835bef | 213 | if (Duration > 52000){ |
vamgehealthcare11 | 10:4c3a8f835bef | 214 | pc.printf("\nRetraction Time Out Error: %d\r\n", Duration); |
vamgehealthcare11 | 10:4c3a8f835bef | 215 | exit(1); |
vamgehealthcare11 | 9:5263aacbda53 | 216 | } |
vamgehealthcare11 | 9:5263aacbda53 | 217 | else if (homeSwitch == 0){ |
vamgehealthcare11 | 9:5263aacbda53 | 218 | pc.printf("\nMagnet Switch Broken"); |
vamgehealthcare11 | 10:4c3a8f835bef | 219 | exit(1); |
vamgehealthcare11 | 9:5263aacbda53 | 220 | } |
vamgehealthcare11 | 9:5263aacbda53 | 221 | else{ |
vamgehealthcare11 | 10:4c3a8f835bef | 222 | pc.printf("\nRetraction Complete"); |
vamgehealthcare11 | 10:4c3a8f835bef | 223 | goto LOOPY; |
vamgehealthcare11 | 9:5263aacbda53 | 224 | } |
vamgehealthcare11 | 9:5263aacbda53 | 225 | } |
vamgehealthcare11 | 9:5263aacbda53 | 226 | |
vamgehealthcare11 | 9:5263aacbda53 | 227 | else { |
vamgehealthcare11 | 9:5263aacbda53 | 228 | pc.printf("\nInvalid Command"); |
rschimpf78 | 3:fd2ca631ab44 | 229 | } |
rschimpf78 | 3:fd2ca631ab44 | 230 | } |
vamgehealthcare11 | 10:4c3a8f835bef | 231 | } |
vamgehealthcare11 | 10:4c3a8f835bef | 232 | } |