Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@26:f5e71308a13e, 2018-06-20 (annotated)
- Committer:
- KwamsC
- Date:
- Wed Jun 20 10:05:45 2018 +0000
- Revision:
- 26:f5e71308a13e
- Parent:
- 25:90f7a34c253a
- Child:
- 27:9f9cd0df9a79
check
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jasminealice | 0:57cbecaac770 | 1 | #include "mbed.h" |
| Kwame |
2:9bb410612929 | 2 | #include "TextLCD.h" |
| jasminealice | 18:f5824ba95892 | 3 | #include "MCP23017.h" |
| jasminealice | 18:f5824ba95892 | 4 | #include "Train.h" |
| jasminealice | 20:32ba0a5f2d02 | 5 | #include "Switch.h" |
| jasminealice | 20:32ba0a5f2d02 | 6 | #include "Track.h" |
| jasminealice | 21:31647d80614f | 7 | #include "Detector.h" |
| KwamsC | 24:418711ed8c52 | 8 | #include <cstdlib> |
| KwamsC | 10:cda57976225f | 9 | |
| jasminealice | 16:55c3c5727f14 | 10 | //Board 1 |
| jasminealice | 16:55c3c5727f14 | 11 | /*---------------------------------------------------------------------------- |
| jasminealice | 16:55c3c5727f14 | 12 | Pin definitions |
| jasminealice | 16:55c3c5727f14 | 13 | *----------------------------------------------------------------------------*/ |
| jasminealice | 17:b7b5f40cebff | 14 | DigitalOut Track(p20); // train track |
| jasminealice | 17:b7b5f40cebff | 15 | |
| jasminealice | 23:bb57966cb776 | 16 | DigitalOut myled1(LED1), myled2(LED2), myled3(LED3), myled4(LED4); |
| jasminealice | 23:bb57966cb776 | 17 | DigitalOut externalLed1(p15), externalLed2(p16), externalLed3(p17), externalLed4(p18); |
| KwamsC | 14:a5fd98a957e6 | 18 | TextLCD lcd(p22, p21, p23, p24, p25, p26); // lcd |
| KwamsC | 14:a5fd98a957e6 | 19 | |
| jasminealice | 23:bb57966cb776 | 20 | DigitalIn sw1(p29), sw2(p30), sw3(p11), sw4(p12); |
| jasminealice | 21:31647d80614f | 21 | |
| jasminealice | 23:bb57966cb776 | 22 | InterruptIn int0(p13), int1(p14); |
| jasminealice | 22:c024b20a0e2d | 23 | I2C i2c(p28, p27); |
| jasminealice | 18:f5824ba95892 | 24 | MCP23017 *mcp; |
| jasminealice | 17:b7b5f40cebff | 25 | |
| jasminealice | 25:90f7a34c253a | 26 | //DigitalOut en(p6); |
| jasminealice | 25:90f7a34c253a | 27 | |
| jasminealice | 17:b7b5f40cebff | 28 | /*---------------------------------------------------------------------------- |
| jasminealice | 17:b7b5f40cebff | 29 | Addresses |
| jasminealice | 17:b7b5f40cebff | 30 | *----------------------------------------------------------------------------*/ |
| jasminealice | 21:31647d80614f | 31 | const unsigned int DCCaddress_silver = 0x01; |
| jasminealice | 21:31647d80614f | 32 | const unsigned int DCCaddress_red = 0x03; |
| jasminealice | 17:b7b5f40cebff | 33 | const unsigned int DCCaddress_switch = 0x06; |
| KwamsC | 14:a5fd98a957e6 | 34 | |
| KwamsC | 14:a5fd98a957e6 | 35 | /*---------------------------------------------------------------------------- |
| KwamsC | 14:a5fd98a957e6 | 36 | Train movement |
| KwamsC | 14:a5fd98a957e6 | 37 | *----------------------------------------------------------------------------*/ |
| KwamsC | 14:a5fd98a957e6 | 38 | //move backwards/reverse |
| KwamsC | 14:a5fd98a957e6 | 39 | |
| jasminealice | 15:8d96f7a06103 | 40 | //speed dial forward |
| KwamsC | 26:f5e71308a13e | 41 | const unsigned int slow_speed = 0x73; //step 4 |
| jasminealice | 18:f5824ba95892 | 42 | //const unsigned int DCCinst_step6 = 0x68; //step 6 1/4 speed |
| KwamsC | 26:f5e71308a13e | 43 | const unsigned int normal_speed = 0x78; //step 13 1/2 speed |
| KwamsC | 26:f5e71308a13e | 44 | |
| jasminealice | 17:b7b5f40cebff | 45 | const unsigned int DCCinst_switch1 = 0x81; //Activate switch1 |
| jasminealice | 17:b7b5f40cebff | 46 | const unsigned int DCCinst_switch2 = 0x82; //Activate switch2 |
| jasminealice | 17:b7b5f40cebff | 47 | const unsigned int DCCinst_switch3 = 0x84; //Activate switch3 |
| jasminealice | 17:b7b5f40cebff | 48 | const unsigned int DCCinst_switch4 = 0x88; //Activate switch4 |
| KwamsC | 14:a5fd98a957e6 | 49 | //stop |
| KwamsC | 26:f5e71308a13e | 50 | const unsigned int stop = 0x40; //forward and stop 01100000 |
| KwamsC | 10:cda57976225f | 51 | |
| jasminealice | 25:90f7a34c253a | 52 | static const int cRight[] = {6,7,8}; |
| jasminealice | 25:90f7a34c253a | 53 | vector<int> C_right (cRight, cRight + sizeof(cRight) / sizeof(cRight[0]) ); |
| jasminealice | 25:90f7a34c253a | 54 | |
| jasminealice | 25:90f7a34c253a | 55 | static const int cLeft[] = {0,1,2,12,13}; |
| jasminealice | 25:90f7a34c253a | 56 | vector<int> C_left (cLeft, cLeft + sizeof(cLeft) / sizeof(cLeft[0]) ); |
| jasminealice | 25:90f7a34c253a | 57 | |
| KwamsC | 26:f5e71308a13e | 58 | static const int junction39[] = {3,9}; |
| KwamsC | 26:f5e71308a13e | 59 | vector<int> junction_39 (junction39, junction39 + sizeof(junction39) / sizeof(junction39[0]) ); |
| KwamsC | 26:f5e71308a13e | 60 | |
| KwamsC | 26:f5e71308a13e | 61 | static const int junction511[] = {5,11}; |
| KwamsC | 26:f5e71308a13e | 62 | vector<int> junction_511 (junction511, junction511 + sizeof(junction511) / sizeof(junction511[0]) ); |
| KwamsC | 26:f5e71308a13e | 63 | |
| KwamsC | 26:f5e71308a13e | 64 | int redStartVal = 12; |
| KwamsC | 26:f5e71308a13e | 65 | int silverStartVal = 2; |
| KwamsC | 26:f5e71308a13e | 66 | |
| jasminealice | 17:b7b5f40cebff | 67 | /*---------------------------------------------------------------------------- |
| jasminealice | 17:b7b5f40cebff | 68 | Function definitions |
| jasminealice | 17:b7b5f40cebff | 69 | *----------------------------------------------------------------------------*/ |
| jasminealice | 17:b7b5f40cebff | 70 | bool readSwitch(DigitalIn theSwitch); |
| jasminealice | 23:bb57966cb776 | 71 | int convertHextoDec(int pos, int intnr); |
| KwamsC | 24:418711ed8c52 | 72 | void checkDetector(int inter); |
| jasminealice | 22:c024b20a0e2d | 73 | void init_mcp(); |
| jasminealice | 23:bb57966cb776 | 74 | void init_interrupt(); |
| KwamsC | 24:418711ed8c52 | 75 | void init_trains(); |
| KwamsC | 24:418711ed8c52 | 76 | void executeCase(int pos); |
| jasminealice | 23:bb57966cb776 | 77 | Train& assignTrain(int pos); |
| KwamsC | 10:cda57976225f | 78 | |
| jasminealice | 21:31647d80614f | 79 | //Trains |
| KwamsC | 26:f5e71308a13e | 80 | Train redTrain(DCCaddress_red, normal_speed, redStartVal); |
| KwamsC | 26:f5e71308a13e | 81 | Train silverTrain(DCCaddress_silver,normal_speed, silverStartVal); |
| jasminealice | 21:31647d80614f | 82 | //Switches |
| jasminealice | 21:31647d80614f | 83 | Switch switch1(DCCaddress_switch,DCCinst_switch1); |
| KwamsC | 24:418711ed8c52 | 84 | Switch switch2(DCCaddress_switch,DCCinst_switch2); |
| jasminealice | 21:31647d80614f | 85 | Switch switch3(DCCaddress_switch,DCCinst_switch3); |
| jasminealice | 21:31647d80614f | 86 | Switch switch4(DCCaddress_switch,DCCinst_switch4); |
| jasminealice | 21:31647d80614f | 87 | //Detectors |
| jasminealice | 23:bb57966cb776 | 88 | Detector dect(false); |
| jasminealice | 21:31647d80614f | 89 | |
| jasminealice | 16:55c3c5727f14 | 90 | /*---------------------------------------------------------------------------- |
| jasminealice | 16:55c3c5727f14 | 91 | Main |
| jasminealice | 16:55c3c5727f14 | 92 | *----------------------------------------------------------------------------*/ |
| jasminealice | 16:55c3c5727f14 | 93 | int main() { |
| jasminealice | 22:c024b20a0e2d | 94 | lcd.printf("Start journey"); |
| jasminealice | 22:c024b20a0e2d | 95 | init_mcp(); |
| jasminealice | 23:bb57966cb776 | 96 | init_interrupt(); |
| KwamsC | 24:418711ed8c52 | 97 | init_trains(); |
| jasminealice | 21:31647d80614f | 98 | while(1){ |
| jasminealice | 23:bb57966cb776 | 99 | redTrain.sendCommand(); |
| jasminealice | 23:bb57966cb776 | 100 | silverTrain.sendCommand(); |
| jasminealice | 22:c024b20a0e2d | 101 | |
| jasminealice | 21:31647d80614f | 102 | if(!readSwitch(sw3)){ //Change speed |
| jasminealice | 23:bb57966cb776 | 103 | lcd.printf("Go forward"); |
| KwamsC | 26:f5e71308a13e | 104 | redTrain.changeSpeed(normal_speed); |
| KwamsC | 26:f5e71308a13e | 105 | silverTrain.changeSpeed(normal_speed); |
| jasminealice | 22:c024b20a0e2d | 106 | } |
| jasminealice | 23:bb57966cb776 | 107 | |
| KwamsC | 24:418711ed8c52 | 108 | if(redTrain.checkStop()){ //check if Train stopped |
| KwamsC | 24:418711ed8c52 | 109 | executeCase(redTrain.getPosition()); |
| KwamsC | 24:418711ed8c52 | 110 | } |
| KwamsC | 24:418711ed8c52 | 111 | else if(silverTrain.checkStop()){ |
| KwamsC | 24:418711ed8c52 | 112 | executeCase(silverTrain.getPosition()); |
| jasminealice | 25:90f7a34c253a | 113 | } |
| jasminealice | 25:90f7a34c253a | 114 | |
| jasminealice | 25:90f7a34c253a | 115 | if(redTrain.checkStop() && silverTrain.checkStop()) { |
| jasminealice | 25:90f7a34c253a | 116 | break; |
| jasminealice | 25:90f7a34c253a | 117 | } |
| jasminealice | 22:c024b20a0e2d | 118 | } |
| jasminealice | 25:90f7a34c253a | 119 | |
| jasminealice | 25:90f7a34c253a | 120 | lcd.printf("Shutting down..."); |
| jasminealice | 25:90f7a34c253a | 121 | // en = 0; TO Do : emergency shutdown!! |
| jasminealice | 25:90f7a34c253a | 122 | //wait(3); |
| jasminealice | 25:90f7a34c253a | 123 | return 0; |
| jasminealice | 16:55c3c5727f14 | 124 | } |
| KwamsC | 10:cda57976225f | 125 | |
| KwamsC | 14:a5fd98a957e6 | 126 | /*---------------------------------------------------------------------------- |
| KwamsC | 14:a5fd98a957e6 | 127 | Functions |
| KwamsC | 14:a5fd98a957e6 | 128 | *----------------------------------------------------------------------------*/ |
| jasminealice | 23:bb57966cb776 | 129 | int convertHextoDec(int pos, int intnr){ |
| jasminealice | 23:bb57966cb776 | 130 | int newPos; |
| jasminealice | 23:bb57966cb776 | 131 | switch(pos){ |
| jasminealice | 23:bb57966cb776 | 132 | case 0xfe: |
| jasminealice | 23:bb57966cb776 | 133 | newPos = 0; |
| jasminealice | 23:bb57966cb776 | 134 | break; |
| jasminealice | 23:bb57966cb776 | 135 | case 0xfd: |
| jasminealice | 23:bb57966cb776 | 136 | newPos = 1; |
| jasminealice | 23:bb57966cb776 | 137 | break; |
| jasminealice | 23:bb57966cb776 | 138 | case 0xfb: |
| jasminealice | 23:bb57966cb776 | 139 | newPos = 2; |
| jasminealice | 23:bb57966cb776 | 140 | break; |
| jasminealice | 23:bb57966cb776 | 141 | case 0xf7: |
| jasminealice | 23:bb57966cb776 | 142 | newPos = 3; |
| jasminealice | 23:bb57966cb776 | 143 | break; |
| jasminealice | 23:bb57966cb776 | 144 | case 0xef: |
| jasminealice | 23:bb57966cb776 | 145 | newPos = 4; |
| jasminealice | 23:bb57966cb776 | 146 | break; |
| jasminealice | 23:bb57966cb776 | 147 | case 0xdf: |
| jasminealice | 23:bb57966cb776 | 148 | newPos = 5; |
| jasminealice | 23:bb57966cb776 | 149 | break; |
| jasminealice | 23:bb57966cb776 | 150 | case 0xbf: |
| jasminealice | 23:bb57966cb776 | 151 | newPos = 6; |
| jasminealice | 23:bb57966cb776 | 152 | break; |
| jasminealice | 23:bb57966cb776 | 153 | case 0x7f: |
| jasminealice | 23:bb57966cb776 | 154 | newPos = 7; |
| jasminealice | 23:bb57966cb776 | 155 | break; |
| jasminealice | 23:bb57966cb776 | 156 | default: |
| jasminealice | 23:bb57966cb776 | 157 | return -1; |
| jasminealice | 23:bb57966cb776 | 158 | } |
| jasminealice | 23:bb57966cb776 | 159 | |
| jasminealice | 23:bb57966cb776 | 160 | if(intnr == 1) |
| jasminealice | 23:bb57966cb776 | 161 | newPos += 8; |
| jasminealice | 23:bb57966cb776 | 162 | return newPos; |
| jasminealice | 23:bb57966cb776 | 163 | } |
| jasminealice | 21:31647d80614f | 164 | |
| jasminealice | 23:bb57966cb776 | 165 | Train& assignTrain(int pos){ |
| jasminealice | 23:bb57966cb776 | 166 | |
| jasminealice | 21:31647d80614f | 167 | //Check which train got the interupt |
| jasminealice | 21:31647d80614f | 168 | if(redTrain.checkInterupt(pos)){ |
| jasminealice | 21:31647d80614f | 169 | redTrain.setPosition(pos); |
| jasminealice | 23:bb57966cb776 | 170 | return redTrain; |
| jasminealice | 21:31647d80614f | 171 | } |
| jasminealice | 21:31647d80614f | 172 | else if(silverTrain.checkInterupt(pos)){ |
| jasminealice | 21:31647d80614f | 173 | silverTrain.setPosition(pos); |
| jasminealice | 23:bb57966cb776 | 174 | return silverTrain; |
| jasminealice | 25:90f7a34c253a | 175 | }else{ |
| jasminealice | 25:90f7a34c253a | 176 | lcd.cls(); |
| jasminealice | 25:90f7a34c253a | 177 | lcd.printf("NO TRAIN ASSIGNED"); |
| jasminealice | 25:90f7a34c253a | 178 | silverTrain.Stop(); |
| jasminealice | 25:90f7a34c253a | 179 | redTrain.Stop(); |
| jasminealice | 25:90f7a34c253a | 180 | switch2.switchOff(); |
| jasminealice | 25:90f7a34c253a | 181 | |
| jasminealice | 21:31647d80614f | 182 | } |
| jasminealice | 23:bb57966cb776 | 183 | } |
| jasminealice | 23:bb57966cb776 | 184 | |
| KwamsC | 24:418711ed8c52 | 185 | void executeCase(int pos){ |
| KwamsC | 24:418711ed8c52 | 186 | Train* currTrain = &assignTrain(pos); |
| KwamsC | 24:418711ed8c52 | 187 | |
| KwamsC | 24:418711ed8c52 | 188 | switch(pos){ |
| jasminealice | 25:90f7a34c253a | 189 | case 0:{ |
| KwamsC | 24:418711ed8c52 | 190 | lcd.printf("at 0"); |
| KwamsC | 26:f5e71308a13e | 191 | currTrain->changeSpeed(normal_speed); |
| jasminealice | 25:90f7a34c253a | 192 | break;} |
| jasminealice | 25:90f7a34c253a | 193 | case 1:{ |
| KwamsC | 24:418711ed8c52 | 194 | lcd.printf("at 1"); |
| jasminealice | 25:90f7a34c253a | 195 | //TO DO: Check which train is going and only slow down if it started from 2 |
| jasminealice | 25:90f7a34c253a | 196 | /*if(!currTrain->isClockwise()){ |
| KwamsC | 26:f5e71308a13e | 197 | currTrain->changeSpeed(slow_speed);//Slow down |
| jasminealice | 25:90f7a34c253a | 198 | lcd.printf("slow down"); |
| jasminealice | 25:90f7a34c253a | 199 | }*/ |
| jasminealice | 25:90f7a34c253a | 200 | //else |
| KwamsC | 26:f5e71308a13e | 201 | currTrain->changeSpeed(normal_speed); |
| jasminealice | 25:90f7a34c253a | 202 | |
| jasminealice | 25:90f7a34c253a | 203 | break;} |
| jasminealice | 25:90f7a34c253a | 204 | case 2:{ |
| KwamsC | 24:418711ed8c52 | 205 | /* |
| jasminealice | 25:90f7a34c253a | 206 | Stop everytime at 2 |
| KwamsC | 24:418711ed8c52 | 207 | */ |
| jasminealice | 25:90f7a34c253a | 208 | lcd.printf("at 2"); |
| jasminealice | 25:90f7a34c253a | 209 | dect.showReservation(); |
| jasminealice | 25:90f7a34c253a | 210 | wait(2); |
| jasminealice | 25:90f7a34c253a | 211 | |
| jasminealice | 25:90f7a34c253a | 212 | if(!dect.checkReservation(4) && !dect.checkReservation(3)){ //Check if 4 or 3 is reserved |
| jasminealice | 25:90f7a34c253a | 213 | lcd.cls(); |
| jasminealice | 25:90f7a34c253a | 214 | lcd.printf("none is reserved"); |
| jasminealice | 25:90f7a34c253a | 215 | int v1 = rand() % 100; //IF NOT randomize which way to go |
| jasminealice | 25:90f7a34c253a | 216 | if (v1 < 75){ //0.25 chance to enable the switch2, reserve 3,9 |
| jasminealice | 25:90f7a34c253a | 217 | switch2.switchOn(); |
| KwamsC | 26:f5e71308a13e | 218 | dect.makeReservation(junction_39); |
| jasminealice | 25:90f7a34c253a | 219 | } |
| jasminealice | 25:90f7a34c253a | 220 | else { //0.75 chance to disable switch2, reserve 4 |
| jasminealice | 25:90f7a34c253a | 221 | switch2.switchOff(); |
| KwamsC | 24:418711ed8c52 | 222 | vector<int> detectors(4); |
| KwamsC | 24:418711ed8c52 | 223 | dect.makeReservation(detectors); |
| jasminealice | 25:90f7a34c253a | 224 | } |
| jasminealice | 25:90f7a34c253a | 225 | }else if(dect.checkReservation(4)){ //IF 4 is reserved |
| jasminealice | 25:90f7a34c253a | 226 | switch2.switchOn(); //Enable switch2, reserve 3,9 |
| jasminealice | 25:90f7a34c253a | 227 | lcd.cls(); |
| jasminealice | 25:90f7a34c253a | 228 | lcd.printf("4 is reserved"); |
| KwamsC | 26:f5e71308a13e | 229 | dect.makeReservation(junction_39); |
| jasminealice | 25:90f7a34c253a | 230 | |
| jasminealice | 25:90f7a34c253a | 231 | }else{ //IF 3 reserved |
| jasminealice | 25:90f7a34c253a | 232 | lcd.cls(); |
| jasminealice | 25:90f7a34c253a | 233 | lcd.printf("3 is reserved"); |
| jasminealice | 25:90f7a34c253a | 234 | switch2.switchOff(); //Disable switch2, reserve 4 |
| jasminealice | 25:90f7a34c253a | 235 | vector<int> detectors(4); |
| jasminealice | 25:90f7a34c253a | 236 | dect.makeReservation(detectors); |
| jasminealice | 25:90f7a34c253a | 237 | } |
| KwamsC | 26:f5e71308a13e | 238 | currTrain->changeSpeed(normal_speed); //Go forward |
| jasminealice | 25:90f7a34c253a | 239 | |
| jasminealice | 25:90f7a34c253a | 240 | break;} |
| jasminealice | 25:90f7a34c253a | 241 | case 3:{ |
| KwamsC | 24:418711ed8c52 | 242 | lcd.printf("at 3"); |
| jasminealice | 25:90f7a34c253a | 243 | dect.showReservation(); |
| jasminealice | 25:90f7a34c253a | 244 | |
| jasminealice | 25:90f7a34c253a | 245 | currTrain->changeDirection(); //change direction |
| jasminealice | 25:90f7a34c253a | 246 | switch2.switchOff(); //Disable switch2 |
| jasminealice | 25:90f7a34c253a | 247 | dect.clearReservation(C_left); //free nr 0,1,2,12,13 |
| KwamsC | 26:f5e71308a13e | 248 | currTrain->changeSpeed(normal_speed); //go forward |
| jasminealice | 25:90f7a34c253a | 249 | break;} |
| jasminealice | 25:90f7a34c253a | 250 | case 4:{ |
| jasminealice | 25:90f7a34c253a | 251 | lcd.printf("At 4"); |
| jasminealice | 25:90f7a34c253a | 252 | dect.showReservation(); |
| jasminealice | 25:90f7a34c253a | 253 | wait(2); |
| jasminealice | 25:90f7a34c253a | 254 | dect.clearReservation(C_left); //free nr 0,1,2,12,13 |
| jasminealice | 25:90f7a34c253a | 255 | switch3.switchOn(); //Turn on switch3 |
| jasminealice | 25:90f7a34c253a | 256 | if(dect.checkReservation(6)){ //Check if 6 is reserved |
| jasminealice | 25:90f7a34c253a | 257 | lcd.printf("6 is reserved"); |
| jasminealice | 25:90f7a34c253a | 258 | currTrain->Stop(); //IF yes STOP |
| KwamsC | 24:418711ed8c52 | 259 | } |
| jasminealice | 25:90f7a34c253a | 260 | else{ |
| jasminealice | 25:90f7a34c253a | 261 | dect.makeReservation(C_right); //else reserve 6,7,8 and go forward |
| KwamsC | 26:f5e71308a13e | 262 | currTrain->changeSpeed(normal_speed); |
| KwamsC | 24:418711ed8c52 | 263 | } |
| KwamsC | 24:418711ed8c52 | 264 | |
| jasminealice | 25:90f7a34c253a | 265 | dect.showReservation(); |
| jasminealice | 25:90f7a34c253a | 266 | break; } |
| jasminealice | 25:90f7a34c253a | 267 | case 5:{ |
| KwamsC | 24:418711ed8c52 | 268 | lcd.printf("at 5"); |
| jasminealice | 25:90f7a34c253a | 269 | dect.showReservation(); |
| KwamsC | 24:418711ed8c52 | 270 | |
| jasminealice | 25:90f7a34c253a | 271 | currTrain->changeDirection(); //change direction |
| jasminealice | 25:90f7a34c253a | 272 | switch3.switchOn(); //Enable switch3 |
| jasminealice | 25:90f7a34c253a | 273 | dect.clearReservation(C_right); //free nr 6,7,8 |
| KwamsC | 26:f5e71308a13e | 274 | currTrain->changeSpeed(normal_speed); //go forward |
| jasminealice | 25:90f7a34c253a | 275 | |
| jasminealice | 25:90f7a34c253a | 276 | break;} |
| jasminealice | 25:90f7a34c253a | 277 | case 6:{ |
| KwamsC | 24:418711ed8c52 | 278 | lcd.printf("At 6"); |
| jasminealice | 25:90f7a34c253a | 279 | dect.showReservation(); |
| jasminealice | 25:90f7a34c253a | 280 | wait(2); |
| KwamsC | 24:418711ed8c52 | 281 | if(!currTrain->isClockwise()) //IF CC goes towards 7 |
| KwamsC | 24:418711ed8c52 | 282 | { |
| KwamsC | 24:418711ed8c52 | 283 | switch3.switchOn(); //Enable switch3 |
| KwamsC | 24:418711ed8c52 | 284 | int arr [3] = {4,5,11}; |
| KwamsC | 24:418711ed8c52 | 285 | vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int)); |
| KwamsC | 24:418711ed8c52 | 286 | dect.clearReservation(detectors); //free nr 4,5,11 |
| KwamsC | 26:f5e71308a13e | 287 | currTrain->changeSpeed(normal_speed); //Move forward |
| KwamsC | 24:418711ed8c52 | 288 | } |
| KwamsC | 24:418711ed8c52 | 289 | else{ |
| jasminealice | 25:90f7a34c253a | 290 | if(dect.checkReservation(5)){ //IF 5 is reserved |
| jasminealice | 25:90f7a34c253a | 291 | currTrain->Stop(); |
| jasminealice | 25:90f7a34c253a | 292 | |
| jasminealice | 25:90f7a34c253a | 293 | }else{ //else 5 reserved |
| jasminealice | 25:90f7a34c253a | 294 | switch3.switchOff(); //Enable switch3, reserve 4 |
| KwamsC | 26:f5e71308a13e | 295 | dect.makeReservation(junction_511); |
| KwamsC | 26:f5e71308a13e | 296 | currTrain->changeSpeed(normal_speed); |
| KwamsC | 24:418711ed8c52 | 297 | } |
| KwamsC | 26:f5e71308a13e | 298 | currTrain->changeSpeed(normal_speed); //Go forward |
| KwamsC | 24:418711ed8c52 | 299 | } |
| jasminealice | 25:90f7a34c253a | 300 | break;} |
| jasminealice | 25:90f7a34c253a | 301 | case 7:{ |
| jasminealice | 25:90f7a34c253a | 302 | lcd.printf("At 7"); |
| jasminealice | 25:90f7a34c253a | 303 | dect.showReservation(); |
| jasminealice | 25:90f7a34c253a | 304 | wait(2); |
| KwamsC | 26:f5e71308a13e | 305 | currTrain->changeSpeed(normal_speed); |
| jasminealice | 25:90f7a34c253a | 306 | break;} |
| jasminealice | 25:90f7a34c253a | 307 | case 8:{ |
| jasminealice | 25:90f7a34c253a | 308 | lcd.printf("At 8"); |
| jasminealice | 25:90f7a34c253a | 309 | dect.showReservation(); |
| jasminealice | 25:90f7a34c253a | 310 | wait(2); |
| KwamsC | 24:418711ed8c52 | 311 | if(currTrain->isClockwise()) //IF C goes towards 7 |
| KwamsC | 24:418711ed8c52 | 312 | { |
| KwamsC | 24:418711ed8c52 | 313 | switch4.switchOff(); //Disable switch4 |
| KwamsC | 26:f5e71308a13e | 314 | dect.clearReservation(junction_39); //Free 3,9,10 |
| KwamsC | 26:f5e71308a13e | 315 | currTrain->changeSpeed(normal_speed); //Move forward |
| KwamsC | 24:418711ed8c52 | 316 | } |
| KwamsC | 24:418711ed8c52 | 317 | else{ //IF CC |
| jasminealice | 25:90f7a34c253a | 318 | switch4.switchOff(); //Disable switch4, reserve 10 |
| jasminealice | 25:90f7a34c253a | 319 | vector<int> detectors(10); |
| jasminealice | 25:90f7a34c253a | 320 | dect.makeReservation(detectors); |
| KwamsC | 26:f5e71308a13e | 321 | currTrain->changeSpeed(normal_speed); //Go forward |
| KwamsC | 24:418711ed8c52 | 322 | } |
| jasminealice | 25:90f7a34c253a | 323 | break;} |
| jasminealice | 25:90f7a34c253a | 324 | case 9:{ |
| jasminealice | 25:90f7a34c253a | 325 | lcd.printf("at 9"); |
| jasminealice | 25:90f7a34c253a | 326 | dect.showReservation(); |
| jasminealice | 25:90f7a34c253a | 327 | wait(2); |
| jasminealice | 25:90f7a34c253a | 328 | dect.showReservation(); |
| jasminealice | 25:90f7a34c253a | 329 | if(!dect.checkReservation(8)){ |
| jasminealice | 25:90f7a34c253a | 330 | switch4.switchOn(); //Enable switch4 |
| jasminealice | 25:90f7a34c253a | 331 | dect.makeReservation(C_right); //Reserve 6,7,8 |
| KwamsC | 26:f5e71308a13e | 332 | currTrain->changeSpeed(normal_speed); //Go forward |
| KwamsC | 24:418711ed8c52 | 333 | |
| KwamsC | 24:418711ed8c52 | 334 | } |
| jasminealice | 25:90f7a34c253a | 335 | else{ |
| jasminealice | 25:90f7a34c253a | 336 | lcd.printf("from 9->8 : 8 reserved"); |
| jasminealice | 25:90f7a34c253a | 337 | currTrain->Stop(); |
| jasminealice | 25:90f7a34c253a | 338 | } |
| jasminealice | 25:90f7a34c253a | 339 | break;} |
| jasminealice | 25:90f7a34c253a | 340 | case 10:{ |
| jasminealice | 25:90f7a34c253a | 341 | lcd.printf("At 10"); |
| jasminealice | 25:90f7a34c253a | 342 | dect.showReservation(); |
| jasminealice | 25:90f7a34c253a | 343 | wait(2); |
| jasminealice | 25:90f7a34c253a | 344 | |
| jasminealice | 25:90f7a34c253a | 345 | dect.clearReservation(C_right); //free nr 6, 7, 8, 9 |
| jasminealice | 25:90f7a34c253a | 346 | switch1.switchOff(); //Turn off switch1 |
| jasminealice | 25:90f7a34c253a | 347 | if(dect.checkReservation(12)){ //Check if 12 is reserved |
| jasminealice | 25:90f7a34c253a | 348 | lcd.printf("12resv. STOP"); |
| jasminealice | 25:90f7a34c253a | 349 | currTrain->Stop(); //IF yes STOP |
| KwamsC | 24:418711ed8c52 | 350 | } |
| jasminealice | 25:90f7a34c253a | 351 | else{ |
| jasminealice | 25:90f7a34c253a | 352 | lcd.printf("12 not reserved"); |
| jasminealice | 25:90f7a34c253a | 353 | dect.makeReservation(C_left); |
| KwamsC | 26:f5e71308a13e | 354 | currTrain->changeSpeed(normal_speed); //go forward |
| jasminealice | 25:90f7a34c253a | 355 | } |
| jasminealice | 25:90f7a34c253a | 356 | break;} |
| jasminealice | 25:90f7a34c253a | 357 | |
| jasminealice | 25:90f7a34c253a | 358 | case 11:{ |
| jasminealice | 25:90f7a34c253a | 359 | lcd.printf("at 11"); |
| jasminealice | 25:90f7a34c253a | 360 | dect.showReservation(); |
| jasminealice | 25:90f7a34c253a | 361 | wait(2); |
| jasminealice | 25:90f7a34c253a | 362 | |
| jasminealice | 25:90f7a34c253a | 363 | if(!dect.checkReservation(12)){ |
| jasminealice | 25:90f7a34c253a | 364 | |
| jasminealice | 25:90f7a34c253a | 365 | switch1.switchOn(); //Enable switch1 |
| jasminealice | 25:90f7a34c253a | 366 | dect.makeReservation(C_left); //Reserve 0,1,2, 12, 13 |
| KwamsC | 26:f5e71308a13e | 367 | currTrain->changeSpeed(normal_speed); //go forward |
| jasminealice | 25:90f7a34c253a | 368 | } |
| jasminealice | 25:90f7a34c253a | 369 | else{ |
| jasminealice | 25:90f7a34c253a | 370 | currTrain->Stop(); |
| jasminealice | 25:90f7a34c253a | 371 | } |
| jasminealice | 25:90f7a34c253a | 372 | |
| KwamsC | 24:418711ed8c52 | 373 | break; |
| KwamsC | 24:418711ed8c52 | 374 | case 12: |
| KwamsC | 24:418711ed8c52 | 375 | lcd.printf("at 12"); |
| jasminealice | 25:90f7a34c253a | 376 | dect.showReservation(); |
| jasminealice | 25:90f7a34c253a | 377 | wait(2); |
| jasminealice | 25:90f7a34c253a | 378 | |
| jasminealice | 25:90f7a34c253a | 379 | switch1.switchOff(); //Turn of switch1 |
| jasminealice | 25:90f7a34c253a | 380 | int arr2 [3] = {5,10,11}; |
| jasminealice | 25:90f7a34c253a | 381 | vector<int> detectors2(arr2, arr2 + sizeof(arr2) / sizeof(int)); |
| jasminealice | 25:90f7a34c253a | 382 | dect.clearReservation(detectors2); //free nr 5, 10, 11 |
| KwamsC | 26:f5e71308a13e | 383 | currTrain->changeSpeed(normal_speed); //Move forward |
| jasminealice | 25:90f7a34c253a | 384 | |
| jasminealice | 25:90f7a34c253a | 385 | break;} |
| jasminealice | 25:90f7a34c253a | 386 | case 13: { |
| KwamsC | 24:418711ed8c52 | 387 | lcd.printf("at 13"); |
| KwamsC | 26:f5e71308a13e | 388 | currTrain->changeSpeed(normal_speed); |
| jasminealice | 25:90f7a34c253a | 389 | break;} |
| jasminealice | 25:90f7a34c253a | 390 | case 14:{ |
| jasminealice | 25:90f7a34c253a | 391 | lcd.printf("at 21"); |
| KwamsC | 26:f5e71308a13e | 392 | currTrain->changeSpeed(normal_speed); |
| jasminealice | 25:90f7a34c253a | 393 | break;} |
| jasminealice | 25:90f7a34c253a | 394 | case 15:{ |
| jasminealice | 25:90f7a34c253a | 395 | lcd.printf("at 22"); |
| KwamsC | 26:f5e71308a13e | 396 | currTrain->changeSpeed(normal_speed); |
| jasminealice | 25:90f7a34c253a | 397 | break;} |
| jasminealice | 25:90f7a34c253a | 398 | default: { |
| KwamsC | 24:418711ed8c52 | 399 | lcd.printf("Not in a valid case"); |
| KwamsC | 26:f5e71308a13e | 400 | currTrain->Stop(); |
| jasminealice | 25:90f7a34c253a | 401 | break; } |
| KwamsC | 24:418711ed8c52 | 402 | } |
| KwamsC | 24:418711ed8c52 | 403 | |
| KwamsC | 24:418711ed8c52 | 404 | mcp->_read(GPIOA); |
| KwamsC | 24:418711ed8c52 | 405 | mcp->_read(GPIOB); |
| KwamsC | 24:418711ed8c52 | 406 | } |
| KwamsC | 24:418711ed8c52 | 407 | |
| KwamsC | 24:418711ed8c52 | 408 | void checkDetector(int inter){ |
| jasminealice | 23:bb57966cb776 | 409 | lcd.cls(); |
| jasminealice | 23:bb57966cb776 | 410 | int pos; |
| jasminealice | 23:bb57966cb776 | 411 | wait_us(100); |
| jasminealice | 23:bb57966cb776 | 412 | if (inter == 0){ |
| jasminealice | 23:bb57966cb776 | 413 | //Read from first one |
| jasminealice | 23:bb57966cb776 | 414 | pos = mcp->_read(INTCAPA); |
| jasminealice | 23:bb57966cb776 | 415 | pos = convertHextoDec(pos, 0); |
| jasminealice | 21:31647d80614f | 416 | } |
| jasminealice | 23:bb57966cb776 | 417 | else if (inter == 1){ |
| jasminealice | 23:bb57966cb776 | 418 | //Read from second one |
| jasminealice | 23:bb57966cb776 | 419 | pos = mcp->_read(INTCAPB); |
| jasminealice | 23:bb57966cb776 | 420 | pos = convertHextoDec(pos, 1); |
| jasminealice | 23:bb57966cb776 | 421 | } |
| jasminealice | 23:bb57966cb776 | 422 | else |
| jasminealice | 23:bb57966cb776 | 423 | //PROBLEM |
| jasminealice | 23:bb57966cb776 | 424 | lcd.printf("Fail to detect int"); |
| KwamsC | 24:418711ed8c52 | 425 | |
| jasminealice | 25:90f7a34c253a | 426 | executeCase(pos); |
| jasminealice | 15:8d96f7a06103 | 427 | } |
| jasminealice | 15:8d96f7a06103 | 428 | |
| jasminealice | 17:b7b5f40cebff | 429 | bool readSwitch(DigitalIn theSwitch){ |
| jasminealice | 17:b7b5f40cebff | 430 | int val = theSwitch.read(); |
| jasminealice | 22:c024b20a0e2d | 431 | //lcd.printf("%d", val); |
| jasminealice | 16:55c3c5727f14 | 432 | if(val == 1) |
| jasminealice | 16:55c3c5727f14 | 433 | return true; |
| jasminealice | 16:55c3c5727f14 | 434 | else |
| jasminealice | 16:55c3c5727f14 | 435 | return false; |
| tanjinamicky | 13:78a2481fd65b | 436 | } |
| tanjinamicky | 13:78a2481fd65b | 437 | |
| jasminealice | 22:c024b20a0e2d | 438 | void init_mcp() { |
| jasminealice | 22:c024b20a0e2d | 439 | // Initialisation of MCP registers,documentation on registers is availableatNiels/Jacco/Natalia |
| jasminealice | 22:c024b20a0e2d | 440 | mcp = new MCP23017(i2c, 0x40); |
| jasminealice | 22:c024b20a0e2d | 441 | mcp->_write(IODIRA, (unsigned char )0xff); |
| jasminealice | 22:c024b20a0e2d | 442 | mcp->_write(IODIRB, (unsigned char )0xff); |
| jasminealice | 22:c024b20a0e2d | 443 | mcp->_write(IPOLA, (unsigned char )0x00); |
| jasminealice | 22:c024b20a0e2d | 444 | mcp->_write(IPOLB, (unsigned char )0x00); |
| jasminealice | 22:c024b20a0e2d | 445 | mcp->_write(DEFVALA, (unsigned char )0xff); |
| jasminealice | 22:c024b20a0e2d | 446 | mcp->_write(DEFVALB, (unsigned char )0xff); |
| jasminealice | 22:c024b20a0e2d | 447 | mcp->_write(INTCONA, (unsigned char )0xff); |
| jasminealice | 22:c024b20a0e2d | 448 | mcp->_write(INTCONB, (unsigned char )0xff); |
| jasminealice | 22:c024b20a0e2d | 449 | mcp->_write(IOCONA, (unsigned char )0x2); |
| jasminealice | 22:c024b20a0e2d | 450 | mcp->_write(IOCONB, (unsigned char )0x2); |
| jasminealice | 22:c024b20a0e2d | 451 | mcp->_write(GPPUA, (unsigned char )0xff); |
| jasminealice | 22:c024b20a0e2d | 452 | mcp->_write(GPPUB, (unsigned char )0xff); |
| jasminealice | 18:f5824ba95892 | 453 | } |
| jasminealice | 23:bb57966cb776 | 454 | void passZero(){ |
| jasminealice | 23:bb57966cb776 | 455 | checkDetector(0); |
| jasminealice | 23:bb57966cb776 | 456 | } |
| jasminealice | 22:c024b20a0e2d | 457 | |
| jasminealice | 23:bb57966cb776 | 458 | void passOne(){ |
| jasminealice | 23:bb57966cb776 | 459 | checkDetector(1); |
| jasminealice | 23:bb57966cb776 | 460 | } |
| jasminealice | 23:bb57966cb776 | 461 | |
| jasminealice | 23:bb57966cb776 | 462 | void init_interrupt() { |
| jasminealice | 22:c024b20a0e2d | 463 | // Clear current interrupts |
| jasminealice | 25:90f7a34c253a | 464 | //en = 1; |
| jasminealice | 22:c024b20a0e2d | 465 | mcp->_read(GPIOA); |
| jasminealice | 22:c024b20a0e2d | 466 | mcp->_read(GPIOB); |
| jasminealice | 22:c024b20a0e2d | 467 | // Register callbacks |
| jasminealice | 23:bb57966cb776 | 468 | int0.fall(&passZero); |
| jasminealice | 23:bb57966cb776 | 469 | int1.fall(&passOne); |
| jasminealice | 22:c024b20a0e2d | 470 | // Enable interrupts on MCP |
| jasminealice | 22:c024b20a0e2d | 471 | mcp->_write(GPINTENA, (unsigned char )0xff); |
| jasminealice | 22:c024b20a0e2d | 472 | mcp->_write(GPINTENB, (unsigned char )0xff);// Ready to go! |
| jasminealice | 22:c024b20a0e2d | 473 | } |
| KwamsC | 24:418711ed8c52 | 474 | |
| KwamsC | 24:418711ed8c52 | 475 | void init_trains(){ |
| KwamsC | 26:f5e71308a13e | 476 | redTrain.changeSpeed(stop); |
| KwamsC | 26:f5e71308a13e | 477 | silverTrain.changeSpeed(stop); |
| KwamsC | 24:418711ed8c52 | 478 | |
| KwamsC | 24:418711ed8c52 | 479 | vector<int> res; |
| KwamsC | 24:418711ed8c52 | 480 | res.push_back(2); |
| jasminealice | 25:90f7a34c253a | 481 | //res.push_back(4); |
| jasminealice | 25:90f7a34c253a | 482 | dect.makeReservation(res); //Run trains counterclockwise and reserve |
| KwamsC | 24:418711ed8c52 | 483 | vector<int> res2; |
| KwamsC | 24:418711ed8c52 | 484 | res2.push_back(12); |
| jasminealice | 25:90f7a34c253a | 485 | //res2.push_back(13); |
| KwamsC | 24:418711ed8c52 | 486 | dect.makeReservation(res2); |
| jasminealice | 25:90f7a34c253a | 487 | dect.showReservation(); |
| jasminealice | 25:90f7a34c253a | 488 | } |
