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