VAM
Dependencies: Stepper mbed HCSR04 millis
main.cpp@6:6ca8a0ef7d4a, 2019-01-18 (annotated)
- Committer:
- rschimpf78
- Date:
- Fri Jan 18 00:05:40 2019 +0000
- Revision:
- 6:6ca8a0ef7d4a
- Parent:
- 5:e958d61e0564
- Child:
- 7:2c70830e0f36
update2019;
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 | 6:6ca8a0ef7d4a | 248 | while(magnetSwitch == 1 && Duration <= 48000 ) |
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 | 6:6ca8a0ef7d4a | 264 | wait(56); |
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 | 0:88834eed3de0 | 273 | } |
rschimpf78 | 3:fd2ca631ab44 | 274 | |
rschimpf78 | 3:fd2ca631ab44 | 275 | void send_error_check() |
rschimpf78 | 3:fd2ca631ab44 | 276 | { |
rschimpf78 | 3:fd2ca631ab44 | 277 | if (plates <= 0 || plates >= 17) |
rschimpf78 | 2:e8f8095464cf | 278 | { |
rschimpf78 | 3:fd2ca631ab44 | 279 | pc.printf("\nERROR: Invalid Range |"); |
rschimpf78 | 2:e8f8095464cf | 280 | } |
rschimpf78 | 6:6ca8a0ef7d4a | 281 | else if(magnetSwitch == 1) |
rschimpf78 | 3:fd2ca631ab44 | 282 | { |
rschimpf78 | 3:fd2ca631ab44 | 283 | pc.printf("\nERROR: Not at Home Position |"); |
rschimpf78 | 3:fd2ca631ab44 | 284 | } |
rschimpf78 | 3:fd2ca631ab44 | 285 | else |
rschimpf78 | 3:fd2ca631ab44 | 286 | { |
rschimpf78 | 3:fd2ca631ab44 | 287 | plate_selection(); |
rschimpf78 | 3:fd2ca631ab44 | 288 | send(); |
rschimpf78 | 3:fd2ca631ab44 | 289 | } |
rschimpf78 | 3:fd2ca631ab44 | 290 | } |
rschimpf78 | 3:fd2ca631ab44 | 291 | void select_error_check() |
rschimpf78 | 3:fd2ca631ab44 | 292 | { |
rschimpf78 | 6:6ca8a0ef7d4a | 293 | if (magnetSwitch == 1) |
rschimpf78 | 3:fd2ca631ab44 | 294 | { |
rschimpf78 | 3:fd2ca631ab44 | 295 | pc.printf("\nERROR: Not at Home Position |"); |
rschimpf78 | 3:fd2ca631ab44 | 296 | } |
rschimpf78 | 3:fd2ca631ab44 | 297 | else |
rschimpf78 | 3:fd2ca631ab44 | 298 | { |
rschimpf78 | 3:fd2ca631ab44 | 299 | plate_selection(); |
rschimpf78 | 3:fd2ca631ab44 | 300 | } |
rschimpf78 | 3:fd2ca631ab44 | 301 | } |
rschimpf78 | 3:fd2ca631ab44 | 302 | void retract_error_check() |
rschimpf78 | 3:fd2ca631ab44 | 303 | { |
rschimpf78 | 6:6ca8a0ef7d4a | 304 | if (magnetSwitch == 0) |
rschimpf78 | 3:fd2ca631ab44 | 305 | { |
rschimpf78 | 3:fd2ca631ab44 | 306 | pc.printf("\nERROR: Already at Home Position |"); |
rschimpf78 | 0:88834eed3de0 | 307 | } |
rschimpf78 | 2:e8f8095464cf | 308 | else |
rschimpf78 | 2:e8f8095464cf | 309 | { |
rschimpf78 | 3:fd2ca631ab44 | 310 | retract(); |
rschimpf78 | 2:e8f8095464cf | 311 | } |
rschimpf78 | 2:e8f8095464cf | 312 | } |
rschimpf78 | 0:88834eed3de0 | 313 | |
rschimpf78 | 0:88834eed3de0 | 314 | int main() |
rschimpf78 | 2:e8f8095464cf | 315 | { |
rschimpf78 | 2:e8f8095464cf | 316 | in1=1; |
rschimpf78 | 2:e8f8095464cf | 317 | in2=1; |
rschimpf78 | 2:e8f8095464cf | 318 | en = 1; |
rschimpf78 | 2:e8f8095464cf | 319 | wait(0.1); |
rschimpf78 | 0:88834eed3de0 | 320 | internalpullups(); |
rschimpf78 | 3:fd2ca631ab44 | 321 | wait(0.1); |
rschimpf78 | 2:e8f8095464cf | 322 | char command[15] = {0}; |
rschimpf78 | 3:fd2ca631ab44 | 323 | 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 | 324 | |
rschimpf78 | 2:e8f8095464cf | 325 | while(1) |
rschimpf78 | 0:88834eed3de0 | 326 | { |
rschimpf78 | 2:e8f8095464cf | 327 | pc.printf("\nRequests: "); |
rschimpf78 | 2:e8f8095464cf | 328 | pc.scanf("%s",&command); |
rschimpf78 | 2:e8f8095464cf | 329 | |
rschimpf78 | 3:fd2ca631ab44 | 330 | if (strcmp (command, "s") == 0) |
rschimpf78 | 2:e8f8095464cf | 331 | { |
rschimpf78 | 2:e8f8095464cf | 332 | pc.scanf(" %d",&plates); |
rschimpf78 | 2:e8f8095464cf | 333 | pc.printf("\nresponse recieved |"); |
rschimpf78 | 3:fd2ca631ab44 | 334 | send_error_check(); |
rschimpf78 | 3:fd2ca631ab44 | 335 | beam(); |
rschimpf78 | 3:fd2ca631ab44 | 336 | platedetect(); |
rschimpf78 | 0:88834eed3de0 | 337 | } |
rschimpf78 | 3:fd2ca631ab44 | 338 | else if (strcmp (command, "r") == 0) |
rschimpf78 | 2:e8f8095464cf | 339 | { |
rschimpf78 | 2:e8f8095464cf | 340 | pc.printf("\nresponse recieved |"); |
rschimpf78 | 3:fd2ca631ab44 | 341 | retract_error_check(); |
rschimpf78 | 2:e8f8095464cf | 342 | } |
rschimpf78 | 3:fd2ca631ab44 | 343 | else if (strcmp (command, "c") == 0) |
rschimpf78 | 3:fd2ca631ab44 | 344 | { |
rschimpf78 | 3:fd2ca631ab44 | 345 | pc.scanf(" %d",&plates); |
rschimpf78 | 2:e8f8095464cf | 346 | pc.printf("\nresponse recieved |"); |
rschimpf78 | 3:fd2ca631ab44 | 347 | select_error_check(); |
rschimpf78 | 0:88834eed3de0 | 348 | } |
rschimpf78 | 3:fd2ca631ab44 | 349 | else if (strcmp (command, "b") == 0) |
rschimpf78 | 3:fd2ca631ab44 | 350 | { |
rschimpf78 | 2:e8f8095464cf | 351 | pc.printf("\nresponse recieved |"); |
rschimpf78 | 3:fd2ca631ab44 | 352 | beam(); |
rschimpf78 | 3:fd2ca631ab44 | 353 | platedetect(); |
rschimpf78 | 0:88834eed3de0 | 354 | } |
rschimpf78 | 2:e8f8095464cf | 355 | else |
rschimpf78 | 2:e8f8095464cf | 356 | { |
rschimpf78 | 3:fd2ca631ab44 | 357 | pc.printf("\nERROR: Invalid Command"); |
rschimpf78 | 2:e8f8095464cf | 358 | } |
rschimpf78 | 0:88834eed3de0 | 359 | } |
rschimpf78 | 2:e8f8095464cf | 360 | } |