VAM
Dependencies: Stepper mbed HCSR04 millis
main.cpp@8:a7b443aee89b, 2019-01-18 (annotated)
- Committer:
- rschimpf78
- Date:
- Fri Jan 18 21:07:57 2019 +0000
- Revision:
- 8:a7b443aee89b
- Parent:
- 7:2c70830e0f36
- Child:
- 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 | //#include "SDFileSystem.h" |
rschimpf78 | 6:6ca8a0ef7d4a | 7 | |
rschimpf78 | 3:fd2ca631ab44 | 8 | Serial pc(USBTX, USBRX); |
rschimpf78 | 6:6ca8a0ef7d4a | 9 | HCSR04 pathsensor(PTC2,PTA2); //(trig,echo) |
rschimpf78 | 6:6ca8a0ef7d4a | 10 | HCSR04 heightsensor(PTD2,PTD3); |
rschimpf78 | 3:fd2ca631ab44 | 11 | DigitalOut in1(PTC12); |
rschimpf78 | 3:fd2ca631ab44 | 12 | DigitalOut in2(D7); |
rschimpf78 | 6:6ca8a0ef7d4a | 13 | |
rschimpf78 | 6:6ca8a0ef7d4a | 14 | //DigitalIn homeSwitch(D9); |
rschimpf78 | 6:6ca8a0ef7d4a | 15 | DigitalIn magnetSwitch(D10); |
rschimpf78 | 3:fd2ca631ab44 | 16 | Stepper mot(D3,D4); //(D3 to PUL+, D4 to DIR+) |
rschimpf78 | 2:e8f8095464cf | 17 | DigitalOut en(D2); |
rschimpf78 | 0:88834eed3de0 | 18 | |
rschimpf78 | 3:fd2ca631ab44 | 19 | volatile int plates=0; |
rschimpf78 | 3:fd2ca631ab44 | 20 | volatile unsigned int sheets=0; |
rschimpf78 | 3:fd2ca631ab44 | 21 | volatile unsigned int path_sheets=0; |
rschimpf78 | 0:88834eed3de0 | 22 | |
rschimpf78 | 3:fd2ca631ab44 | 23 | void plate_selection() |
rschimpf78 | 0:88834eed3de0 | 24 | { |
rschimpf78 | 3:fd2ca631ab44 | 25 | unsigned int dist; |
rschimpf78 | 3:fd2ca631ab44 | 26 | int er = 5; |
rschimpf78 | 6:6ca8a0ef7d4a | 27 | int fp = 447; //adjust if needed |
rschimpf78 | 3:fd2ca631ab44 | 28 | int lp = 64; //adjust if needed |
rschimpf78 | 3:fd2ca631ab44 | 29 | int tp = 25; |
rschimpf78 | 5:e958d61e0564 | 30 | wait(1); |
rschimpf78 | 5:e958d61e0564 | 31 | in1=1; |
rschimpf78 | 5:e958d61e0564 | 32 | in2=0; |
rschimpf78 | 6:6ca8a0ef7d4a | 33 | wait_ms(850); |
rschimpf78 | 5:e958d61e0564 | 34 | in1=1; |
rschimpf78 | 5:e958d61e0564 | 35 | in2=1; |
rschimpf78 | 5:e958d61e0564 | 36 | wait(1); |
rschimpf78 | 3:fd2ca631ab44 | 37 | while (plates!=sheets) { |
rschimpf78 | 3:fd2ca631ab44 | 38 | heightsensor.start(); |
rschimpf78 | 3:fd2ca631ab44 | 39 | wait_ms(500); |
rschimpf78 | 3:fd2ca631ab44 | 40 | dist=heightsensor.get_dist_cm(); |
rschimpf78 | 3:fd2ca631ab44 | 41 | pc.printf("\nmm:%ld",dist); |
rschimpf78 | 3:fd2ca631ab44 | 42 | |
rschimpf78 | 3:fd2ca631ab44 | 43 | if (dist<=lp-10 || dist >= fp+10){ |
rschimpf78 | 3:fd2ca631ab44 | 44 | mot.stop(); |
rschimpf78 | 3:fd2ca631ab44 | 45 | en = 1; |
rschimpf78 | 3:fd2ca631ab44 | 46 | sheets = plates; |
rschimpf78 | 3:fd2ca631ab44 | 47 | pc.printf("\nERROR: Selector is outside of plate range\nRestart/Reset Program"); |
rschimpf78 | 3:fd2ca631ab44 | 48 | } |
rschimpf78 | 3:fd2ca631ab44 | 49 | else if (plates<sheets){ |
rschimpf78 | 3:fd2ca631ab44 | 50 | en = 0; |
rschimpf78 | 3:fd2ca631ab44 | 51 | mot.setSpeed(600); |
rschimpf78 | 3:fd2ca631ab44 | 52 | mot.rotate(1); |
rschimpf78 | 3:fd2ca631ab44 | 53 | } |
rschimpf78 | 3:fd2ca631ab44 | 54 | else{ |
rschimpf78 | 3:fd2ca631ab44 | 55 | en = 0; |
rschimpf78 | 3:fd2ca631ab44 | 56 | mot.setSpeed(600); |
rschimpf78 | 3:fd2ca631ab44 | 57 | mot.rotate(0); |
rschimpf78 | 3:fd2ca631ab44 | 58 | } |
rschimpf78 | 3:fd2ca631ab44 | 59 | |
rschimpf78 | 3:fd2ca631ab44 | 60 | if (dist<=fp+er && dist>=fp-er){ //450,440 |
rschimpf78 | 3:fd2ca631ab44 | 61 | sheets = 1; |
rschimpf78 | 3:fd2ca631ab44 | 62 | pc.printf("\nSheets selected:%ld",sheets); |
rschimpf78 | 3:fd2ca631ab44 | 63 | } |
rschimpf78 | 3:fd2ca631ab44 | 64 | |
rschimpf78 | 3:fd2ca631ab44 | 65 | if (dist<= fp-tp+er && dist>=fp-tp-er){ //424,414 |
rschimpf78 | 3:fd2ca631ab44 | 66 | sheets = 2; |
rschimpf78 | 3:fd2ca631ab44 | 67 | pc.printf("\nSheets selected:%ld",sheets); |
rschimpf78 | 3:fd2ca631ab44 | 68 | } |
rschimpf78 | 3:fd2ca631ab44 | 69 | |
rschimpf78 | 3:fd2ca631ab44 | 70 | if (dist<= fp-2*tp+er && dist>=fp-2*tp-er){ |
rschimpf78 | 3:fd2ca631ab44 | 71 | sheets = 3; |
rschimpf78 | 3:fd2ca631ab44 | 72 | pc.printf("\nSheets selected:%ld",sheets); |
rschimpf78 | 3:fd2ca631ab44 | 73 | } |
rschimpf78 | 3:fd2ca631ab44 | 74 | |
rschimpf78 | 3:fd2ca631ab44 | 75 | if (dist<= fp-3*tp+er && dist>=fp-3*tp-er){ |
rschimpf78 | 3:fd2ca631ab44 | 76 | sheets = 4; |
rschimpf78 | 3:fd2ca631ab44 | 77 | pc.printf("\nSheets selected:%ld",sheets); |
rschimpf78 | 3:fd2ca631ab44 | 78 | } |
rschimpf78 | 3:fd2ca631ab44 | 79 | if (dist<= fp-4*tp+er && dist>=fp-4*tp-er){ |
rschimpf78 | 3:fd2ca631ab44 | 80 | sheets = 5; |
rschimpf78 | 3:fd2ca631ab44 | 81 | pc.printf("\nSheets selected:%ld",sheets); |
rschimpf78 | 3:fd2ca631ab44 | 82 | } |
rschimpf78 | 3:fd2ca631ab44 | 83 | if (dist<= fp-5*tp+er && dist>=fp-5*tp-er){ |
rschimpf78 | 3:fd2ca631ab44 | 84 | sheets = 6; |
rschimpf78 | 3:fd2ca631ab44 | 85 | pc.printf("\nSheets selected:%ld",sheets); |
rschimpf78 | 3:fd2ca631ab44 | 86 | } |
rschimpf78 | 3:fd2ca631ab44 | 87 | if (dist<= fp-6*tp+er && dist>=fp-6*tp-er){ |
rschimpf78 | 3:fd2ca631ab44 | 88 | sheets = 7; |
rschimpf78 | 3:fd2ca631ab44 | 89 | pc.printf("\nSheets selected:%ld",sheets); |
rschimpf78 | 3:fd2ca631ab44 | 90 | } |
rschimpf78 | 3:fd2ca631ab44 | 91 | if (dist<= fp-7*tp+er && dist>=fp-7*tp-er){ |
rschimpf78 | 3:fd2ca631ab44 | 92 | sheets = 8; |
rschimpf78 | 3:fd2ca631ab44 | 93 | pc.printf("\nSheets selected:%ld",sheets); |
rschimpf78 | 3:fd2ca631ab44 | 94 | } |
rschimpf78 | 3:fd2ca631ab44 | 95 | if (dist<= fp-8*tp+er && dist>=fp-8*tp-er){ |
rschimpf78 | 3:fd2ca631ab44 | 96 | sheets = 9; |
rschimpf78 | 3:fd2ca631ab44 | 97 | pc.printf("\nSheets selected:%ld",sheets); |
rschimpf78 | 3:fd2ca631ab44 | 98 | } |
rschimpf78 | 3:fd2ca631ab44 | 99 | if (dist<= fp-9*tp+er && dist>=fp-9*tp-er){ |
rschimpf78 | 3:fd2ca631ab44 | 100 | sheets = 10; |
rschimpf78 | 3:fd2ca631ab44 | 101 | pc.printf("\nSheets selected:%ld",sheets); |
rschimpf78 | 3:fd2ca631ab44 | 102 | } |
rschimpf78 | 3:fd2ca631ab44 | 103 | if (dist<= fp-10*tp+er && dist>=fp-10*tp-er){ |
rschimpf78 | 3:fd2ca631ab44 | 104 | sheets = 11; |
rschimpf78 | 3:fd2ca631ab44 | 105 | pc.printf("\nSheets selected:%ld",sheets); |
rschimpf78 | 3:fd2ca631ab44 | 106 | } |
rschimpf78 | 3:fd2ca631ab44 | 107 | if (dist<= fp-11*tp+er && dist>=fp-11*tp-er){ |
rschimpf78 | 3:fd2ca631ab44 | 108 | sheets = 12; |
rschimpf78 | 3:fd2ca631ab44 | 109 | pc.printf("\nSheets selected:%ld",sheets); |
rschimpf78 | 3:fd2ca631ab44 | 110 | } |
rschimpf78 | 3:fd2ca631ab44 | 111 | if (dist<= fp-12*tp+er && dist>=fp-12*tp-er){ |
rschimpf78 | 3:fd2ca631ab44 | 112 | sheets = 13; |
rschimpf78 | 3:fd2ca631ab44 | 113 | pc.printf("\nSheets selected:%ld",sheets); |
rschimpf78 | 3:fd2ca631ab44 | 114 | } |
rschimpf78 | 3:fd2ca631ab44 | 115 | if (dist<= fp-13*tp+er && dist>=fp-13*tp-er){ |
rschimpf78 | 3:fd2ca631ab44 | 116 | sheets = 14; |
rschimpf78 | 3:fd2ca631ab44 | 117 | pc.printf("\nSheets selected:%ld",sheets); |
rschimpf78 | 3:fd2ca631ab44 | 118 | } |
rschimpf78 | 3:fd2ca631ab44 | 119 | if (dist<= fp-14*tp+er && dist>=fp-14*tp-er){ |
rschimpf78 | 3:fd2ca631ab44 | 120 | sheets = 15; |
rschimpf78 | 3:fd2ca631ab44 | 121 | pc.printf("\nSheets selected:%ld",sheets); |
rschimpf78 | 3:fd2ca631ab44 | 122 | } |
rschimpf78 | 3:fd2ca631ab44 | 123 | if (dist<= fp-15*tp+er && dist>=fp-15*tp-er){ // 75, 65 |
rschimpf78 | 3:fd2ca631ab44 | 124 | sheets = 16; |
rschimpf78 | 3:fd2ca631ab44 | 125 | pc.printf("\nSheets selected:%ld",sheets); |
rschimpf78 | 3:fd2ca631ab44 | 126 | } |
rschimpf78 | 3:fd2ca631ab44 | 127 | } |
rschimpf78 | 3:fd2ca631ab44 | 128 | mot.stop(); |
rschimpf78 | 3:fd2ca631ab44 | 129 | en = 1; |
rschimpf78 | 3:fd2ca631ab44 | 130 | wait(1); |
rschimpf78 | 3:fd2ca631ab44 | 131 | } |
rschimpf78 | 3:fd2ca631ab44 | 132 | |
rschimpf78 | 3:fd2ca631ab44 | 133 | void beam() |
rschimpf78 | 3:fd2ca631ab44 | 134 | { |
rschimpf78 | 3:fd2ca631ab44 | 135 | |
rschimpf78 | 3:fd2ca631ab44 | 136 | unsigned int path_dist; |
rschimpf78 | 6:6ca8a0ef7d4a | 137 | int err = 12; |
rschimpf78 | 6:6ca8a0ef7d4a | 138 | int sens_pos = 131; //adjust if needed //535 0 plates |
rschimpf78 | 3:fd2ca631ab44 | 139 | int sheet_th = 25; |
rschimpf78 | 3:fd2ca631ab44 | 140 | pathsensor.start(); |
rschimpf78 | 3:fd2ca631ab44 | 141 | wait_ms(500); |
rschimpf78 | 3:fd2ca631ab44 | 142 | path_dist=pathsensor.get_dist_cm(); |
rschimpf78 | 3:fd2ca631ab44 | 143 | pc.printf("\nBeam Path Sensor : %ld",path_dist); |
rschimpf78 | 3:fd2ca631ab44 | 144 | path_sheets = 17; |
rschimpf78 | 3:fd2ca631ab44 | 145 | |
rschimpf78 | 4:1a4022b21dae | 146 | if (path_dist >= sens_pos-err && path_dist <= sens_pos+err){ //110,130 |
rschimpf78 | 3:fd2ca631ab44 | 147 | path_sheets = 16; |
rschimpf78 | 3:fd2ca631ab44 | 148 | |
rschimpf78 | 3:fd2ca631ab44 | 149 | } |
rschimpf78 | 3:fd2ca631ab44 | 150 | if (path_dist >= sens_pos-err+sheet_th && path_dist <= sens_pos+err+sheet_th){ //152,168 a: |
rschimpf78 | 3:fd2ca631ab44 | 151 | path_sheets = 15; |
rschimpf78 | 3:fd2ca631ab44 | 152 | |
rschimpf78 | 3:fd2ca631ab44 | 153 | } |
rschimpf78 | 3:fd2ca631ab44 | 154 | if (path_dist >= sens_pos-err+2*sheet_th && path_dist <= sens_pos+err+2*sheet_th){ //179-191 |
rschimpf78 | 3:fd2ca631ab44 | 155 | path_sheets = 14; |
rschimpf78 | 3:fd2ca631ab44 | 156 | |
rschimpf78 | 3:fd2ca631ab44 | 157 | } |
rschimpf78 | 3:fd2ca631ab44 | 158 | if (path_dist >= sens_pos-err+3*sheet_th && path_dist <= sens_pos+err+3*sheet_th){ //205-217 |
rschimpf78 | 3:fd2ca631ab44 | 159 | path_sheets = 13; |
rschimpf78 | 3:fd2ca631ab44 | 160 | |
rschimpf78 | 3:fd2ca631ab44 | 161 | } |
rschimpf78 | 3:fd2ca631ab44 | 162 | if (path_dist >= sens_pos-err+4*sheet_th && path_dist <= sens_pos+err+4*sheet_th){ //231-243 |
rschimpf78 | 3:fd2ca631ab44 | 163 | path_sheets = 12; |
rschimpf78 | 3:fd2ca631ab44 | 164 | |
rschimpf78 | 3:fd2ca631ab44 | 165 | } |
rschimpf78 | 3:fd2ca631ab44 | 166 | if (path_dist >= sens_pos-err+5*sheet_th && path_dist <= sens_pos+err+5*sheet_th){ //257-269 |
rschimpf78 | 3:fd2ca631ab44 | 167 | path_sheets = 11; |
rschimpf78 | 3:fd2ca631ab44 | 168 | |
rschimpf78 | 3:fd2ca631ab44 | 169 | } |
rschimpf78 | 3:fd2ca631ab44 | 170 | if (path_dist >= sens_pos-err+6*sheet_th && path_dist <= sens_pos+err+6*sheet_th){ //283-295 |
rschimpf78 | 3:fd2ca631ab44 | 171 | path_sheets = 10; |
rschimpf78 | 3:fd2ca631ab44 | 172 | |
rschimpf78 | 3:fd2ca631ab44 | 173 | } |
rschimpf78 | 3:fd2ca631ab44 | 174 | if (path_dist >= sens_pos-err+7*sheet_th && path_dist <= sens_pos+err+7*sheet_th){ //309-321 |
rschimpf78 | 3:fd2ca631ab44 | 175 | path_sheets = 9; |
rschimpf78 | 3:fd2ca631ab44 | 176 | |
rschimpf78 | 3:fd2ca631ab44 | 177 | } |
rschimpf78 | 3:fd2ca631ab44 | 178 | if (path_dist >= sens_pos-err+8*sheet_th && path_dist <= sens_pos+err+8*sheet_th){ //323-333 |
rschimpf78 | 3:fd2ca631ab44 | 179 | path_sheets = 8; |
rschimpf78 | 3:fd2ca631ab44 | 180 | |
rschimpf78 | 3:fd2ca631ab44 | 181 | } |
rschimpf78 | 3:fd2ca631ab44 | 182 | if (path_dist >= sens_pos-err+9*sheet_th && path_dist <= sens_pos+err+9*sheet_th){ //349-359 |
rschimpf78 | 3:fd2ca631ab44 | 183 | path_sheets = 7; |
rschimpf78 | 3:fd2ca631ab44 | 184 | |
rschimpf78 | 3:fd2ca631ab44 | 185 | } |
rschimpf78 | 3:fd2ca631ab44 | 186 | if (path_dist >= sens_pos-err+10*sheet_th && path_dist <= sens_pos+err+10*sheet_th){ |
rschimpf78 | 3:fd2ca631ab44 | 187 | path_sheets = 6; |
rschimpf78 | 3:fd2ca631ab44 | 188 | |
rschimpf78 | 3:fd2ca631ab44 | 189 | } |
rschimpf78 | 3:fd2ca631ab44 | 190 | if (path_dist >= sens_pos-err+11*sheet_th && path_dist <= sens_pos+err+11*sheet_th){ |
rschimpf78 | 3:fd2ca631ab44 | 191 | path_sheets = 5; |
rschimpf78 | 3:fd2ca631ab44 | 192 | |
rschimpf78 | 3:fd2ca631ab44 | 193 | } |
rschimpf78 | 3:fd2ca631ab44 | 194 | if (path_dist >= sens_pos-err+12*sheet_th && path_dist <= sens_pos+err+12*sheet_th){ |
rschimpf78 | 3:fd2ca631ab44 | 195 | path_sheets = 4; |
rschimpf78 | 3:fd2ca631ab44 | 196 | |
rschimpf78 | 3:fd2ca631ab44 | 197 | } |
rschimpf78 | 3:fd2ca631ab44 | 198 | if (path_dist >= sens_pos-err+13*sheet_th && path_dist <= sens_pos+err+13*sheet_th){ //468,478 |
rschimpf78 | 3:fd2ca631ab44 | 199 | path_sheets = 3; |
rschimpf78 | 3:fd2ca631ab44 | 200 | |
rschimpf78 | 3:fd2ca631ab44 | 201 | } |
rschimpf78 | 3:fd2ca631ab44 | 202 | if (path_dist >= sens_pos-err+14*sheet_th && path_dist <= sens_pos+err+14*sheet_th){ //494,504 |
rschimpf78 | 3:fd2ca631ab44 | 203 | path_sheets = 2; |
rschimpf78 | 3:fd2ca631ab44 | 204 | |
rschimpf78 | 3:fd2ca631ab44 | 205 | } |
rschimpf78 | 4:1a4022b21dae | 206 | if (path_dist >= sens_pos-10-err+15*sheet_th && path_dist <= sens_pos-10+err+15*sheet_th){//greater than 509 less 517 |
rschimpf78 | 3:fd2ca631ab44 | 207 | path_sheets = 1; |
rschimpf78 | 3:fd2ca631ab44 | 208 | |
rschimpf78 | 3:fd2ca631ab44 | 209 | } |
rschimpf78 | 4:1a4022b21dae | 210 | if (path_dist > sens_pos-err+16*sheet_th){ //greater than 534 |
rschimpf78 | 3:fd2ca631ab44 | 211 | path_sheets = 0; |
rschimpf78 | 3:fd2ca631ab44 | 212 | } |
rschimpf78 | 3:fd2ca631ab44 | 213 | } |
rschimpf78 | 3:fd2ca631ab44 | 214 | |
rschimpf78 | 3:fd2ca631ab44 | 215 | void platedetect() |
rschimpf78 | 3:fd2ca631ab44 | 216 | { |
rschimpf78 | 6:6ca8a0ef7d4a | 217 | if(magnetSwitch==0 && path_sheets==0) |
rschimpf78 | 2:e8f8095464cf | 218 | { |
rschimpf78 | 3:fd2ca631ab44 | 219 | pc.printf("\nAll plates are in the start position"); |
rschimpf78 | 2:e8f8095464cf | 220 | } |
rschimpf78 | 6:6ca8a0ef7d4a | 221 | else if (magnetSwitch==1 && path_sheets==0) |
rschimpf78 | 2:e8f8095464cf | 222 | { |
rschimpf78 | 3:fd2ca631ab44 | 223 | pc.printf("\nError-Plates are not fully extended"); |
rschimpf78 | 2:e8f8095464cf | 224 | } |
rschimpf78 | 2:e8f8095464cf | 225 | else |
rschimpf78 | 2:e8f8095464cf | 226 | { |
rschimpf78 | 3:fd2ca631ab44 | 227 | if (plates==path_sheets) |
rschimpf78 | 3:fd2ca631ab44 | 228 | { |
rschimpf78 | 3:fd2ca631ab44 | 229 | pc.printf("\nPlates in beam path:%ld",path_sheets); |
rschimpf78 | 3:fd2ca631ab44 | 230 | } |
rschimpf78 | 3:fd2ca631ab44 | 231 | else if (plates != path_sheets && path_sheets <= 16) |
rschimpf78 | 3:fd2ca631ab44 | 232 | { |
rschimpf78 | 3:fd2ca631ab44 | 233 | pc.printf("\nError: %ld",path_sheets); |
rschimpf78 | 3:fd2ca631ab44 | 234 | pc.printf(" Plates in beam path, but user sent %ld",plates); |
rschimpf78 | 3:fd2ca631ab44 | 235 | pc.printf(" Plates"); |
rschimpf78 | 3:fd2ca631ab44 | 236 | } |
rschimpf78 | 3:fd2ca631ab44 | 237 | else{ |
rschimpf78 | 3:fd2ca631ab44 | 238 | pc.printf("\nERROR: Additional objects detected in beam path"); |
rschimpf78 | 3:fd2ca631ab44 | 239 | } |
rschimpf78 | 3:fd2ca631ab44 | 240 | } |
rschimpf78 | 3:fd2ca631ab44 | 241 | } |
rschimpf78 | 3:fd2ca631ab44 | 242 | void retract() |
rschimpf78 | 0:88834eed3de0 | 243 | { |
rschimpf78 | 5:e958d61e0564 | 244 | millisStart(); |
rschimpf78 | 5:e958d61e0564 | 245 | long Duration = 0; |
rschimpf78 | 5:e958d61e0564 | 246 | long StartTime = millis(); |
rschimpf78 | 5:e958d61e0564 | 247 | |
rschimpf78 | 7:2c70830e0f36 | 248 | while(magnetSwitch == 1 && Duration <= 60000 ) |
rschimpf78 | 2:e8f8095464cf | 249 | { |
rschimpf78 | 0:88834eed3de0 | 250 | in1=0; |
rschimpf78 | 2:e8f8095464cf | 251 | in2=1; |
rschimpf78 | 5:e958d61e0564 | 252 | long CurrentTime = millis(); |
rschimpf78 | 5:e958d61e0564 | 253 | Duration = CurrentTime-StartTime; |
rschimpf78 | 5:e958d61e0564 | 254 | pc.printf("Duration = %d\r\n", Duration); |
rschimpf78 | 3:fd2ca631ab44 | 255 | } |
rschimpf78 | 2:e8f8095464cf | 256 | in1=1; |
rschimpf78 | 2:e8f8095464cf | 257 | in2=1; |
rschimpf78 | 0:88834eed3de0 | 258 | } |
rschimpf78 | 0:88834eed3de0 | 259 | |
rschimpf78 | 3:fd2ca631ab44 | 260 | void send() |
rschimpf78 | 3:fd2ca631ab44 | 261 | { |
rschimpf78 | 3:fd2ca631ab44 | 262 | in1=1; |
rschimpf78 | 3:fd2ca631ab44 | 263 | in2=0; |
rschimpf78 | 7:2c70830e0f36 | 264 | wait(58); |
rschimpf78 | 0:88834eed3de0 | 265 | in1=1; |
rschimpf78 | 2:e8f8095464cf | 266 | in2=1; |
rschimpf78 | 0:88834eed3de0 | 267 | } |
rschimpf78 | 3:fd2ca631ab44 | 268 | |
rschimpf78 | 0:88834eed3de0 | 269 | void internalpullups() |
rschimpf78 | 0:88834eed3de0 | 270 | { |
rschimpf78 | 6:6ca8a0ef7d4a | 271 | magnetSwitch.mode(PullUp); |
rschimpf78 | 0:88834eed3de0 | 272 | wait (.01); |
rschimpf78 | 8:a7b443aee89b | 273 | //homeSwitch.mode(PullUp); |
rschimpf78 | 8:a7b443aee89b | 274 | //wait (0.01); |
rschimpf78 | 0:88834eed3de0 | 275 | } |
rschimpf78 | 3:fd2ca631ab44 | 276 | |
rschimpf78 | 3:fd2ca631ab44 | 277 | void send_error_check() |
rschimpf78 | 3:fd2ca631ab44 | 278 | { |
rschimpf78 | 3:fd2ca631ab44 | 279 | if (plates <= 0 || plates >= 17) |
rschimpf78 | 2:e8f8095464cf | 280 | { |
rschimpf78 | 3:fd2ca631ab44 | 281 | pc.printf("\nERROR: Invalid Range |"); |
rschimpf78 | 2:e8f8095464cf | 282 | } |
rschimpf78 | 6:6ca8a0ef7d4a | 283 | else if(magnetSwitch == 1) |
rschimpf78 | 3:fd2ca631ab44 | 284 | { |
rschimpf78 | 3:fd2ca631ab44 | 285 | pc.printf("\nERROR: Not at Home Position |"); |
rschimpf78 | 3:fd2ca631ab44 | 286 | } |
rschimpf78 | 3:fd2ca631ab44 | 287 | else |
rschimpf78 | 3:fd2ca631ab44 | 288 | { |
rschimpf78 | 3:fd2ca631ab44 | 289 | plate_selection(); |
rschimpf78 | 3:fd2ca631ab44 | 290 | send(); |
rschimpf78 | 3:fd2ca631ab44 | 291 | } |
rschimpf78 | 3:fd2ca631ab44 | 292 | } |
rschimpf78 | 3:fd2ca631ab44 | 293 | void select_error_check() |
rschimpf78 | 3:fd2ca631ab44 | 294 | { |
rschimpf78 | 6:6ca8a0ef7d4a | 295 | if (magnetSwitch == 1) |
rschimpf78 | 3:fd2ca631ab44 | 296 | { |
rschimpf78 | 3:fd2ca631ab44 | 297 | pc.printf("\nERROR: Not at Home Position |"); |
rschimpf78 | 3:fd2ca631ab44 | 298 | } |
rschimpf78 | 3:fd2ca631ab44 | 299 | else |
rschimpf78 | 3:fd2ca631ab44 | 300 | { |
rschimpf78 | 3:fd2ca631ab44 | 301 | plate_selection(); |
rschimpf78 | 3:fd2ca631ab44 | 302 | } |
rschimpf78 | 3:fd2ca631ab44 | 303 | } |
rschimpf78 | 3:fd2ca631ab44 | 304 | void retract_error_check() |
rschimpf78 | 3:fd2ca631ab44 | 305 | { |
rschimpf78 | 6:6ca8a0ef7d4a | 306 | if (magnetSwitch == 0) |
rschimpf78 | 3:fd2ca631ab44 | 307 | { |
rschimpf78 | 3:fd2ca631ab44 | 308 | pc.printf("\nERROR: Already at Home Position |"); |
rschimpf78 | 0:88834eed3de0 | 309 | } |
rschimpf78 | 2:e8f8095464cf | 310 | else |
rschimpf78 | 2:e8f8095464cf | 311 | { |
rschimpf78 | 3:fd2ca631ab44 | 312 | retract(); |
rschimpf78 | 2:e8f8095464cf | 313 | } |
rschimpf78 | 2:e8f8095464cf | 314 | } |
rschimpf78 | 0:88834eed3de0 | 315 | |
rschimpf78 | 0:88834eed3de0 | 316 | int main() |
rschimpf78 | 2:e8f8095464cf | 317 | { |
rschimpf78 | 2:e8f8095464cf | 318 | in1=1; |
rschimpf78 | 2:e8f8095464cf | 319 | in2=1; |
rschimpf78 | 2:e8f8095464cf | 320 | en = 1; |
rschimpf78 | 2:e8f8095464cf | 321 | wait(0.1); |
rschimpf78 | 0:88834eed3de0 | 322 | internalpullups(); |
rschimpf78 | 3:fd2ca631ab44 | 323 | wait(0.1); |
rschimpf78 | 2:e8f8095464cf | 324 | char command[15] = {0}; |
rschimpf78 | 3:fd2ca631ab44 | 325 | pc.printf("\nCommands:\n\n\ts [N]\t**moves N PMMA sheets in beam path\n\tr\t\t**removes all PMMA from beam path\n\tc [N]\t**raises bar to \n\tb\t\t**reports number of plates in beam path\n\n"); |
rschimpf78 | 2:e8f8095464cf | 326 | |
rschimpf78 | 2:e8f8095464cf | 327 | while(1) |
rschimpf78 | 0:88834eed3de0 | 328 | { |
rschimpf78 | 2:e8f8095464cf | 329 | pc.printf("\nRequests: "); |
rschimpf78 | 2:e8f8095464cf | 330 | pc.scanf("%s",&command); |
rschimpf78 | 2:e8f8095464cf | 331 | |
rschimpf78 | 3:fd2ca631ab44 | 332 | if (strcmp (command, "s") == 0) |
rschimpf78 | 2:e8f8095464cf | 333 | { |
rschimpf78 | 2:e8f8095464cf | 334 | pc.scanf(" %d",&plates); |
rschimpf78 | 2:e8f8095464cf | 335 | pc.printf("\nresponse recieved |"); |
rschimpf78 | 3:fd2ca631ab44 | 336 | send_error_check(); |
rschimpf78 | 3:fd2ca631ab44 | 337 | beam(); |
rschimpf78 | 3:fd2ca631ab44 | 338 | platedetect(); |
rschimpf78 | 0:88834eed3de0 | 339 | } |
rschimpf78 | 3:fd2ca631ab44 | 340 | else if (strcmp (command, "r") == 0) |
rschimpf78 | 2:e8f8095464cf | 341 | { |
rschimpf78 | 2:e8f8095464cf | 342 | pc.printf("\nresponse recieved |"); |
rschimpf78 | 3:fd2ca631ab44 | 343 | retract_error_check(); |
rschimpf78 | 2:e8f8095464cf | 344 | } |
rschimpf78 | 3:fd2ca631ab44 | 345 | else if (strcmp (command, "c") == 0) |
rschimpf78 | 3:fd2ca631ab44 | 346 | { |
rschimpf78 | 3:fd2ca631ab44 | 347 | pc.scanf(" %d",&plates); |
rschimpf78 | 2:e8f8095464cf | 348 | pc.printf("\nresponse recieved |"); |
rschimpf78 | 3:fd2ca631ab44 | 349 | select_error_check(); |
rschimpf78 | 0:88834eed3de0 | 350 | } |
rschimpf78 | 3:fd2ca631ab44 | 351 | else if (strcmp (command, "b") == 0) |
rschimpf78 | 3:fd2ca631ab44 | 352 | { |
rschimpf78 | 2:e8f8095464cf | 353 | pc.printf("\nresponse recieved |"); |
rschimpf78 | 3:fd2ca631ab44 | 354 | beam(); |
rschimpf78 | 3:fd2ca631ab44 | 355 | platedetect(); |
rschimpf78 | 0:88834eed3de0 | 356 | } |
rschimpf78 | 2:e8f8095464cf | 357 | else |
rschimpf78 | 2:e8f8095464cf | 358 | { |
rschimpf78 | 3:fd2ca631ab44 | 359 | pc.printf("\nERROR: Invalid Command"); |
rschimpf78 | 2:e8f8095464cf | 360 | } |
rschimpf78 | 0:88834eed3de0 | 361 | } |
rschimpf78 | 2:e8f8095464cf | 362 | } |