Denver
/
denver_train_proj
Denver trai project
main.cpp@51:badef9fc202f, 2018-06-20 (annotated)
- Committer:
- mglmx
- Date:
- Wed Jun 20 10:08:20 2018 +0000
- Revision:
- 51:badef9fc202f
- Parent:
- 50:ee4398ee44be
- Child:
- 52:c08495446f87
Added fast speed and full speed instructions;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mglmx | 0:4d06a6a8e785 | 1 | #include "mbed.h" |
mglmx | 3:fe7010b693a0 | 2 | #include "TextLCD.h" |
mglmx | 18:aa43bb62e60f | 3 | #include "MCP23017.h" |
mglmx | 22:e4153ca757dd | 4 | #include <string> |
mglmx | 22:e4153ca757dd | 5 | #include <iostream> |
mglmx | 35:cfcfeccb959e | 6 | #include <vector> |
mglmx | 35:cfcfeccb959e | 7 | |
mglmx | 35:cfcfeccb959e | 8 | using namespace std; |
mglmx | 18:aa43bb62e60f | 9 | |
mglmx | 1:0ab26889af9b | 10 | |
carlosperales95 | 7:e2b8461d4f05 | 11 | /******PINS AND DECLARATIONS*******/ |
carlosperales95 | 7:e2b8461d4f05 | 12 | |
carlosperales95 | 25:a42a1ed4d8e9 | 13 | //------PINS |
mglmx | 21:e6f1649add39 | 14 | |
mglmx | 21:e6f1649add39 | 15 | //SWITCHES p5 - p8 |
mglmx | 21:e6f1649add39 | 16 | DigitalIn switch1(p5); |
mglmx | 21:e6f1649add39 | 17 | DigitalIn switch2(p6); |
mglmx | 21:e6f1649add39 | 18 | DigitalIn switch3(p7); |
mglmx | 21:e6f1649add39 | 19 | DigitalIn switch4(p8); |
carlosperales95 | 7:e2b8461d4f05 | 20 | |
carlosperales95 | 10:2088b1935a93 | 21 | //RAIL SENSORS - INT0,INT1 |
carlosperales95 | 7:e2b8461d4f05 | 22 | //INT0 - p9 |
mglmx | 18:aa43bb62e60f | 23 | InterruptIn int0(p9); |
carlosperales95 | 7:e2b8461d4f05 | 24 | //INT1 - p10 |
mglmx | 18:aa43bb62e60f | 25 | InterruptIn int1(p10); |
carlosperales95 | 7:e2b8461d4f05 | 26 | |
carlosperales95 | 7:e2b8461d4f05 | 27 | ///p11 |
carlosperales95 | 7:e2b8461d4f05 | 28 | ///p12 |
carlosperales95 | 12:e914ca5cd44b | 29 | |
carlosperales95 | 7:e2b8461d4f05 | 30 | //M0 - p13 |
mglmx | 35:cfcfeccb959e | 31 | DigitalIn d21stat(p13); //Sensor right of the station |
carlosperales95 | 7:e2b8461d4f05 | 32 | //M1 - p14 |
mglmx | 35:cfcfeccb959e | 33 | DigitalIn d22stat(p14); //Sensor left of the station |
carlosperales95 | 7:e2b8461d4f05 | 34 | //M2 - p15 |
carlosperales95 | 29:559eb2164488 | 35 | DigitalIn station(p15); //Sensor in the middle of the station |
carlosperales95 | 12:e914ca5cd44b | 36 | |
carlosperales95 | 7:e2b8461d4f05 | 37 | //p16 |
carlosperales95 | 50:ee4398ee44be | 38 | //ENABLE - p17 |
carlosperales95 | 50:ee4398ee44be | 39 | DigitalOut enable(p17); |
carlosperales95 | 12:e914ca5cd44b | 40 | |
carlosperales95 | 11:021210c59a95 | 41 | //BUZZER - p18 |
carlosperales95 | 11:021210c59a95 | 42 | DigitalOut buzz(p18); // buzz=0 doesn't beep, buzz=1 beeps |
carlosperales95 | 7:e2b8461d4f05 | 43 | |
carlosperales95 | 7:e2b8461d4f05 | 44 | //POTENTIOMETER - p19 |
carlosperales95 | 13:dbf1ead12cee | 45 | AnalogIn pot(p19); //Gives float value pot.read(). Convert analog input to V with f*3.3 |
carlosperales95 | 7:e2b8461d4f05 | 46 | |
carlosperales95 | 7:e2b8461d4f05 | 47 | //DAT - p20 |
mglmx | 3:fe7010b693a0 | 48 | DigitalOut Track(p20); //Digital output bit used to drive track power via H-bridge |
carlosperales95 | 7:e2b8461d4f05 | 49 | |
carlosperales95 | 7:e2b8461d4f05 | 50 | //LCD SCREEN - p21, p22, p23, p24, p25, p26 |
carlosperales95 | 11:021210c59a95 | 51 | TextLCD lcd(p22,p21,p23,p24,p25,p26); // RS, E, A4, A5, A6, A7 // ldc.cls() to clear and printf(String up to 16char) |
carlosperales95 | 7:e2b8461d4f05 | 52 | |
carlosperales95 | 7:e2b8461d4f05 | 53 | ///p27 |
carlosperales95 | 7:e2b8461d4f05 | 54 | ///p28 |
mglmx | 33:24ce12dec157 | 55 | I2C i2c(p28,p27); |
carlosperales95 | 7:e2b8461d4f05 | 56 | |
carlosperales95 | 7:e2b8461d4f05 | 57 | //LED1 - p29 |
mglmx | 3:fe7010b693a0 | 58 | DigitalOut redled(p29); |
carlosperales95 | 7:e2b8461d4f05 | 59 | //LED2 - p30 |
mglmx | 3:fe7010b693a0 | 60 | DigitalOut greenled(p30); |
carlosperales95 | 7:e2b8461d4f05 | 61 | |
carlosperales95 | 24:1d71dd8778c4 | 62 | //MBED LEDS |
mglmx | 18:aa43bb62e60f | 63 | DigitalOut led1(LED1); |
mglmx | 18:aa43bb62e60f | 64 | DigitalOut led2(LED2); |
mglmx | 18:aa43bb62e60f | 65 | DigitalOut led3(LED3); |
mglmx | 16:2a2da0e67793 | 66 | |
mglmx | 16:2a2da0e67793 | 67 | //MCP |
mglmx | 16:2a2da0e67793 | 68 | MCP23017 *mcp; |
carlosperales95 | 12:e914ca5cd44b | 69 | |
carlosperales95 | 24:1d71dd8778c4 | 70 | |
carlosperales95 | 25:a42a1ed4d8e9 | 71 | //------GLOBAL VARS |
carlosperales95 | 25:a42a1ed4d8e9 | 72 | |
carlosperales95 | 38:b9aba3715682 | 73 | //......SENSOR POSITION VARS |
carlosperales95 | 38:b9aba3715682 | 74 | |
carlosperales95 | 45:d589318238bf | 75 | //Definition of D sensors, will be interpreted as ints for the program's logic |
mglmx | 34:c9ab2a987734 | 76 | #define D0 0 |
mglmx | 34:c9ab2a987734 | 77 | #define D1 1 |
mglmx | 34:c9ab2a987734 | 78 | #define D2 2 |
mglmx | 34:c9ab2a987734 | 79 | #define D3 3 |
mglmx | 34:c9ab2a987734 | 80 | #define D4 4 |
mglmx | 34:c9ab2a987734 | 81 | #define D5 5 |
mglmx | 34:c9ab2a987734 | 82 | #define D6 6 |
mglmx | 34:c9ab2a987734 | 83 | #define D7 7 |
mglmx | 34:c9ab2a987734 | 84 | #define D8 8 |
mglmx | 34:c9ab2a987734 | 85 | #define D9 9 |
mglmx | 34:c9ab2a987734 | 86 | #define D10 10 |
mglmx | 34:c9ab2a987734 | 87 | #define D11 11 |
mglmx | 34:c9ab2a987734 | 88 | #define D12 12 |
mglmx | 34:c9ab2a987734 | 89 | #define D13 13 |
mglmx | 34:c9ab2a987734 | 90 | #define D21 14 |
mglmx | 34:c9ab2a987734 | 91 | #define D22 15 |
mglmx | 34:c9ab2a987734 | 92 | |
mglmx | 41:4fa6aa29d1ed | 93 | |
carlosperales95 | 38:b9aba3715682 | 94 | /** |
carlosperales95 | 38:b9aba3715682 | 95 | * |
carlosperales95 | 38:b9aba3715682 | 96 | *Position class. |
carlosperales95 | 38:b9aba3715682 | 97 | * |
carlosperales95 | 38:b9aba3715682 | 98 | *@position - |
carlosperales95 | 38:b9aba3715682 | 99 | *@previous_cw - |
carlosperales95 | 38:b9aba3715682 | 100 | *@previous_ccw - |
carlosperales95 | 38:b9aba3715682 | 101 | * |
carlosperales95 | 38:b9aba3715682 | 102 | *Position(int) - |
carlosperales95 | 38:b9aba3715682 | 103 | * |
carlosperales95 | 38:b9aba3715682 | 104 | *get_pos() - |
carlosperales95 | 38:b9aba3715682 | 105 | *get_prev_cw() - |
carlosperales95 | 38:b9aba3715682 | 106 | *get_ccw() - |
carlosperales95 | 38:b9aba3715682 | 107 | *add_prev_cw() - |
carlosperales95 | 38:b9aba3715682 | 108 | *add_ccw() - |
carlosperales95 | 38:b9aba3715682 | 109 | * |
carlosperales95 | 38:b9aba3715682 | 110 | **/ |
mglmx | 35:cfcfeccb959e | 111 | class Position{ |
mglmx | 35:cfcfeccb959e | 112 | private: |
mglmx | 35:cfcfeccb959e | 113 | int position; |
mglmx | 35:cfcfeccb959e | 114 | vector <int> previous_cw; |
mglmx | 35:cfcfeccb959e | 115 | vector <int> previous_ccw; |
mglmx | 35:cfcfeccb959e | 116 | public: |
mglmx | 35:cfcfeccb959e | 117 | Position(int p){ |
mglmx | 35:cfcfeccb959e | 118 | position = p; |
mglmx | 35:cfcfeccb959e | 119 | } |
mglmx | 36:9428c72bdd58 | 120 | |
mglmx | 36:9428c72bdd58 | 121 | int get_pos(){ |
mglmx | 36:9428c72bdd58 | 122 | return position; |
mglmx | 36:9428c72bdd58 | 123 | } |
mglmx | 41:4fa6aa29d1ed | 124 | |
mglmx | 41:4fa6aa29d1ed | 125 | vector<int> get_next_cw(){ |
mglmx | 41:4fa6aa29d1ed | 126 | return previous_ccw; |
mglmx | 41:4fa6aa29d1ed | 127 | } |
mglmx | 41:4fa6aa29d1ed | 128 | |
mglmx | 41:4fa6aa29d1ed | 129 | vector<int> get_next_ccw(){ |
mglmx | 41:4fa6aa29d1ed | 130 | return previous_cw; |
mglmx | 41:4fa6aa29d1ed | 131 | } |
carlosperales95 | 25:a42a1ed4d8e9 | 132 | |
mglmx | 35:cfcfeccb959e | 133 | vector <int> get_prev_cw(){ |
mglmx | 35:cfcfeccb959e | 134 | return previous_cw; |
mglmx | 35:cfcfeccb959e | 135 | } |
mglmx | 35:cfcfeccb959e | 136 | |
mglmx | 35:cfcfeccb959e | 137 | vector <int> get_prev_ccw(){ |
mglmx | 35:cfcfeccb959e | 138 | return previous_ccw; |
mglmx | 35:cfcfeccb959e | 139 | } |
mglmx | 35:cfcfeccb959e | 140 | |
mglmx | 35:cfcfeccb959e | 141 | void add_prev_cw(int pos){ |
mglmx | 35:cfcfeccb959e | 142 | previous_cw.push_back(pos); |
mglmx | 35:cfcfeccb959e | 143 | }; |
mglmx | 35:cfcfeccb959e | 144 | |
mglmx | 35:cfcfeccb959e | 145 | void add_prev_ccw(int pos){ |
mglmx | 35:cfcfeccb959e | 146 | previous_ccw.push_back(pos); |
mglmx | 35:cfcfeccb959e | 147 | }; |
mglmx | 35:cfcfeccb959e | 148 | }; |
mglmx | 35:cfcfeccb959e | 149 | |
carlosperales95 | 45:d589318238bf | 150 | |
mglmx | 41:4fa6aa29d1ed | 151 | //Creating a vector with all the positions. |
mglmx | 41:4fa6aa29d1ed | 152 | vector<Position> positions; |
mglmx | 41:4fa6aa29d1ed | 153 | |
carlosperales95 | 45:d589318238bf | 154 | |
carlosperales95 | 45:d589318238bf | 155 | /** |
carlosperales95 | 45:d589318238bf | 156 | * |
carlosperales95 | 45:d589318238bf | 157 | *Train class. |
carlosperales95 | 45:d589318238bf | 158 | * |
carlosperales95 | 45:d589318238bf | 159 | *@position - |
carlosperales95 | 45:d589318238bf | 160 | *@going_cw - |
carlosperales95 | 45:d589318238bf | 161 | * |
carlosperales95 | 45:d589318238bf | 162 | *Train(int, bool) - |
carlosperales95 | 45:d589318238bf | 163 | *Train(bool) - |
carlosperales95 | 45:d589318238bf | 164 | * |
carlosperales95 | 45:d589318238bf | 165 | *Vector get_next_sensors() - |
carlosperales95 | 45:d589318238bf | 166 | *set_position(int) - |
carlosperales95 | 45:d589318238bf | 167 | *set_goes_cw(bool) - |
carlosperales95 | 45:d589318238bf | 168 | *Position get_position() - |
carlosperales95 | 45:d589318238bf | 169 | *Int get_position_number() - |
carlosperales95 | 45:d589318238bf | 170 | *Bool goes_cw() - |
carlosperales95 | 45:d589318238bf | 171 | * |
carlosperales95 | 45:d589318238bf | 172 | **/ |
mglmx | 41:4fa6aa29d1ed | 173 | class Train{ |
carlosperales95 | 45:d589318238bf | 174 | |
mglmx | 41:4fa6aa29d1ed | 175 | private: |
mglmx | 41:4fa6aa29d1ed | 176 | Position *position; |
mglmx | 41:4fa6aa29d1ed | 177 | bool going_cw; |
carlosperales95 | 45:d589318238bf | 178 | |
mglmx | 41:4fa6aa29d1ed | 179 | public: |
mglmx | 41:4fa6aa29d1ed | 180 | Train(int pos, bool cw){ |
carlosperales95 | 45:d589318238bf | 181 | |
mglmx | 41:4fa6aa29d1ed | 182 | position = &positions[pos]; |
mglmx | 41:4fa6aa29d1ed | 183 | going_cw = cw; |
mglmx | 41:4fa6aa29d1ed | 184 | } |
carlosperales95 | 45:d589318238bf | 185 | |
carlosperales95 | 45:d589318238bf | 186 | Train(bool cw){ going_cw = cw; } |
mglmx | 41:4fa6aa29d1ed | 187 | |
mglmx | 41:4fa6aa29d1ed | 188 | vector<int> get_next_sensors(){ |
mglmx | 41:4fa6aa29d1ed | 189 | |
mglmx | 41:4fa6aa29d1ed | 190 | //Checking direction |
mglmx | 41:4fa6aa29d1ed | 191 | if(going_cw){ |
carlosperales95 | 45:d589318238bf | 192 | |
mglmx | 43:346a1f4144cd | 193 | return position->get_next_cw(); |
mglmx | 41:4fa6aa29d1ed | 194 | }else{ |
carlosperales95 | 45:d589318238bf | 195 | |
mglmx | 43:346a1f4144cd | 196 | return position->get_next_ccw(); |
mglmx | 41:4fa6aa29d1ed | 197 | } |
mglmx | 41:4fa6aa29d1ed | 198 | } |
mglmx | 41:4fa6aa29d1ed | 199 | |
mglmx | 41:4fa6aa29d1ed | 200 | void set_position(int pos){ |
carlosperales95 | 45:d589318238bf | 201 | |
mglmx | 41:4fa6aa29d1ed | 202 | position = &positions[pos]; //Taking the new position from the positions vector |
mglmx | 41:4fa6aa29d1ed | 203 | } |
mglmx | 41:4fa6aa29d1ed | 204 | |
mglmx | 41:4fa6aa29d1ed | 205 | void set_goes_cw(bool cw){ |
carlosperales95 | 45:d589318238bf | 206 | |
mglmx | 41:4fa6aa29d1ed | 207 | going_cw = cw; |
mglmx | 41:4fa6aa29d1ed | 208 | } |
mglmx | 41:4fa6aa29d1ed | 209 | |
mglmx | 41:4fa6aa29d1ed | 210 | Position get_position(){ |
carlosperales95 | 45:d589318238bf | 211 | |
mglmx | 41:4fa6aa29d1ed | 212 | return *position; |
mglmx | 41:4fa6aa29d1ed | 213 | } |
mglmx | 41:4fa6aa29d1ed | 214 | |
mglmx | 41:4fa6aa29d1ed | 215 | int get_position_number(){ |
carlosperales95 | 45:d589318238bf | 216 | |
mglmx | 41:4fa6aa29d1ed | 217 | return position->get_pos(); |
mglmx | 41:4fa6aa29d1ed | 218 | } |
mglmx | 41:4fa6aa29d1ed | 219 | |
mglmx | 41:4fa6aa29d1ed | 220 | bool goes_cw(){ |
carlosperales95 | 45:d589318238bf | 221 | |
mglmx | 41:4fa6aa29d1ed | 222 | return going_cw; |
mglmx | 41:4fa6aa29d1ed | 223 | } |
mglmx | 41:4fa6aa29d1ed | 224 | }; |
mglmx | 41:4fa6aa29d1ed | 225 | |
carlosperales95 | 45:d589318238bf | 226 | |
carlosperales95 | 38:b9aba3715682 | 227 | //Creation of all the positions. One for every sensor on the table - Position name(mapping) |
mglmx | 35:cfcfeccb959e | 228 | Position d0(D0); |
mglmx | 35:cfcfeccb959e | 229 | Position d1(D1); |
mglmx | 35:cfcfeccb959e | 230 | Position d2(D2); |
mglmx | 35:cfcfeccb959e | 231 | Position d3(D3); |
mglmx | 35:cfcfeccb959e | 232 | Position d4(D4); |
mglmx | 35:cfcfeccb959e | 233 | Position d5(D5); |
mglmx | 35:cfcfeccb959e | 234 | Position d6(D6); |
mglmx | 35:cfcfeccb959e | 235 | Position d7(D7); |
mglmx | 35:cfcfeccb959e | 236 | Position d8(D8); |
mglmx | 35:cfcfeccb959e | 237 | Position d9(D9); |
mglmx | 35:cfcfeccb959e | 238 | Position d10(D10); |
mglmx | 35:cfcfeccb959e | 239 | Position d11(D11); |
mglmx | 35:cfcfeccb959e | 240 | Position d12(D12); |
mglmx | 35:cfcfeccb959e | 241 | Position d13(D13); |
mglmx | 35:cfcfeccb959e | 242 | Position d21(D21); |
mglmx | 35:cfcfeccb959e | 243 | Position d22(D22); |
mglmx | 35:cfcfeccb959e | 244 | |
carlosperales95 | 45:d589318238bf | 245 | /** |
carlosperales95 | 45:d589318238bf | 246 | *Defining areas for train detection and collision logic. |
carlosperales95 | 45:d589318238bf | 247 | *area_A_arr/area_B_arr - Arrays that hold the Dsensors for each area, used to initialize the vectors. |
carlosperales95 | 45:d589318238bf | 248 | *area_A/area_B - Vectors that hold the different sensors of the corresponding areas of the track. |
carlosperales95 | 45:d589318238bf | 249 | **/ |
mglmx | 43:346a1f4144cd | 250 | int area_A_arr[] = {D21,D2,D22,D1,D0,D13,D12}; |
mglmx | 43:346a1f4144cd | 251 | int area_B_arr[] = {D6,D7,D8}; |
mglmx | 43:346a1f4144cd | 252 | |
mglmx | 43:346a1f4144cd | 253 | const vector<int> area_A(area_A_arr,area_A_arr + sizeof(area_A_arr) / sizeof(int)); |
mglmx | 43:346a1f4144cd | 254 | const vector<int> area_B(area_B_arr,area_B_arr + sizeof(area_B_arr) / sizeof(int)); |
mglmx | 43:346a1f4144cd | 255 | |
mglmx | 41:4fa6aa29d1ed | 256 | |
mglmx | 35:cfcfeccb959e | 257 | |
carlosperales95 | 25:a42a1ed4d8e9 | 258 | //.....DCC TRAIN COMMAND VARS |
carlosperales95 | 25:a42a1ed4d8e9 | 259 | |
carlosperales95 | 25:a42a1ed4d8e9 | 260 | //typical out of box default engine DCC address is 3 (at least for Bachmann trains) |
carlosperales95 | 25:a42a1ed4d8e9 | 261 | //Note: A DCC controller can reprogram the address whenever needed |
mglmx | 32:e5b732fb8e65 | 262 | const unsigned int DCCaddressDR = 0x01; //Address for train 1 DARK-RED |
mglmx | 32:e5b732fb8e65 | 263 | const unsigned int DCCaddressLR = 0x03; //Address for train 3 LIGHT-RED |
carlosperales95 | 25:a42a1ed4d8e9 | 264 | |
carlosperales95 | 25:a42a1ed4d8e9 | 265 | //01DCSSSS for speed, D is direction (fwd=1 and rev=0), C is speed(SSSSC) LSB |
carlosperales95 | 25:a42a1ed4d8e9 | 266 | const unsigned int DCCinst_forward = 0x68; //forward half speed |
mglmx | 51:badef9fc202f | 267 | const unsigned int DCCinst_forward_slow = 0x66; //forward slow speed (step 9) |
mglmx | 51:badef9fc202f | 268 | const unsigned int DCCinst_forward_fast = 0x6C; //Forward fast speed (step 22) |
mglmx | 51:badef9fc202f | 269 | const unsigned int DCCinst_forward_full = 0x6F; //Forward full speed |
carlosperales95 | 25:a42a1ed4d8e9 | 270 | const unsigned int DCCinst_reverse = 0x48; //reverse half speed |
carlosperales95 | 25:a42a1ed4d8e9 | 271 | const unsigned int DCCinst_stop = 0x50; //stop the train |
carlosperales95 | 25:a42a1ed4d8e9 | 272 | |
carlosperales95 | 25:a42a1ed4d8e9 | 273 | //100DDDDD for basic headlight functions |
carlosperales95 | 25:a42a1ed4d8e9 | 274 | const unsigned int DCC_func_lighton = 0x90; //F0 turns on headlight function |
carlosperales95 | 25:a42a1ed4d8e9 | 275 | const unsigned int DCC_func_dimlight = 0x91; //F0 + F1 dims headlight |
carlosperales95 | 25:a42a1ed4d8e9 | 276 | |
carlosperales95 | 25:a42a1ed4d8e9 | 277 | |
carlosperales95 | 25:a42a1ed4d8e9 | 278 | //.....SWITCH COMMAND VARS |
carlosperales95 | 25:a42a1ed4d8e9 | 279 | |
carlosperales95 | 25:a42a1ed4d8e9 | 280 | const unsigned int SWBaddress = 0x06; //Address for switch box |
carlosperales95 | 25:a42a1ed4d8e9 | 281 | |
carlosperales95 | 25:a42a1ed4d8e9 | 282 | //100DDDDD where DDDDD is the switch command and 100 is constant: |
carlosperales95 | 25:a42a1ed4d8e9 | 283 | |
carlosperales95 | 25:a42a1ed4d8e9 | 284 | //00001(F1 active)-00010(F2 active)-00100(F3 active)-01000(F4 active) |
carlosperales95 | 25:a42a1ed4d8e9 | 285 | //Example - 111111 0 00000101 0 10000000 0 10000101 1 - idle |
carlosperales95 | 25:a42a1ed4d8e9 | 286 | const unsigned int SWBidle = 0x80; //IDLE - Flip last activated SW. |
carlosperales95 | 25:a42a1ed4d8e9 | 287 | const unsigned int SWBflip_1 = 0x81; //Flip SW1 |
carlosperales95 | 25:a42a1ed4d8e9 | 288 | const unsigned int SWBflip_2 = 0x82; //Flip SW2 |
carlosperales95 | 25:a42a1ed4d8e9 | 289 | const unsigned int SWBflip_3 = 0x84; //Flip SW3 |
carlosperales95 | 25:a42a1ed4d8e9 | 290 | const unsigned int SWBflip_4 = 0x88; //Flip SW4 |
carlosperales95 | 25:a42a1ed4d8e9 | 291 | |
carlosperales95 | 40:9acc1341456a | 292 | |
carlosperales95 | 45:d589318238bf | 293 | /** |
carlosperales95 | 45:d589318238bf | 294 | *Creation of 2 Train objects. |
carlosperales95 | 45:d589318238bf | 295 | *Using boolean constructor because position initialization will be done after initializing all position vectors. |
carlosperales95 | 45:d589318238bf | 296 | *DR_train = Dark Red train - LR_train = Light Red Train |
carlosperales95 | 45:d589318238bf | 297 | **/ |
mglmx | 43:346a1f4144cd | 298 | Train DR_train(true); //Position and going_cw |
mglmx | 43:346a1f4144cd | 299 | Train LR_train(true); |
mglmx | 43:346a1f4144cd | 300 | |
carlosperales95 | 47:e992a129ef44 | 301 | //possibility of an array having {dr_train, lr_train}? for reuse and modularity of functions |
carlosperales95 | 47:e992a129ef44 | 302 | |
carlosperales95 | 47:e992a129ef44 | 303 | |
carlosperales95 | 45:d589318238bf | 304 | /** |
carlosperales95 | 45:d589318238bf | 305 | *Booleans that will determine if the train should be moving or not. |
carlosperales95 | 45:d589318238bf | 306 | *Booleans will switch to false to stop any of the trains and avoid collisions. |
carlosperales95 | 45:d589318238bf | 307 | *DR_run - Boolean for DR_train / LR_run - Boolean for LR_train |
carlosperales95 | 45:d589318238bf | 308 | **/ |
mglmx | 43:346a1f4144cd | 309 | bool DR_run = true; |
mglmx | 43:346a1f4144cd | 310 | bool LR_run = true; |
mglmx | 18:aa43bb62e60f | 311 | |
carlosperales95 | 38:b9aba3715682 | 312 | |
carlosperales95 | 45:d589318238bf | 313 | |
carlosperales95 | 38:b9aba3715682 | 314 | //**************** FUNCTIONS FOR DENVER TRAIN ****************// |
carlosperales95 | 38:b9aba3715682 | 315 | |
carlosperales95 | 38:b9aba3715682 | 316 | |
mglmx | 22:e4153ca757dd | 317 | /** |
carlosperales95 | 45:d589318238bf | 318 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 319 | *Activates the buzzer for 0.5 seconds. |
carlosperales95 | 45:d589318238bf | 320 | * |
mglmx | 22:e4153ca757dd | 321 | **/ |
mglmx | 22:e4153ca757dd | 322 | void doBuzz(){ |
carlosperales95 | 25:a42a1ed4d8e9 | 323 | |
mglmx | 22:e4153ca757dd | 324 | buzz = 1; |
mglmx | 22:e4153ca757dd | 325 | wait(0.5); |
mglmx | 22:e4153ca757dd | 326 | buzz = 0; |
mglmx | 22:e4153ca757dd | 327 | } |
mglmx | 18:aa43bb62e60f | 328 | |
carlosperales95 | 45:d589318238bf | 329 | |
carlosperales95 | 45:d589318238bf | 330 | /** |
carlosperales95 | 45:d589318238bf | 331 | * |
carlosperales95 | 47:e992a129ef44 | 332 | *Initializes every position's vectors (prev_cw and prev_ccw) with the corresponding sensors. |
carlosperales95 | 47:e992a129ef44 | 333 | *prev_cw - Sensors previous to the current in clockwise sense. |
carlosperales95 | 47:e992a129ef44 | 334 | *prev_ccw - Sensors previous to the current in counter-clockwise sense. |
carlosperales95 | 45:d589318238bf | 335 | * |
carlosperales95 | 45:d589318238bf | 336 | **/ |
mglmx | 35:cfcfeccb959e | 337 | void init_positions(){ |
mglmx | 36:9428c72bdd58 | 338 | |
mglmx | 35:cfcfeccb959e | 339 | d0.add_prev_cw(D1); |
mglmx | 35:cfcfeccb959e | 340 | d0.add_prev_ccw(D13); |
mglmx | 35:cfcfeccb959e | 341 | |
mglmx | 35:cfcfeccb959e | 342 | d1.add_prev_cw(D22); |
mglmx | 35:cfcfeccb959e | 343 | d1.add_prev_ccw(D0); |
mglmx | 35:cfcfeccb959e | 344 | |
mglmx | 35:cfcfeccb959e | 345 | d22.add_prev_cw(D2); |
mglmx | 35:cfcfeccb959e | 346 | d22.add_prev_ccw(D1); |
mglmx | 35:cfcfeccb959e | 347 | |
mglmx | 35:cfcfeccb959e | 348 | d2.add_prev_cw(D21); |
mglmx | 35:cfcfeccb959e | 349 | d2.add_prev_ccw(D22); |
mglmx | 35:cfcfeccb959e | 350 | |
mglmx | 35:cfcfeccb959e | 351 | d21.add_prev_cw(D3); |
mglmx | 35:cfcfeccb959e | 352 | d21.add_prev_cw(D4); |
mglmx | 35:cfcfeccb959e | 353 | d21.add_prev_ccw(D2); |
mglmx | 35:cfcfeccb959e | 354 | |
mglmx | 35:cfcfeccb959e | 355 | d3.add_prev_cw(D9); |
mglmx | 35:cfcfeccb959e | 356 | d3.add_prev_ccw(D21); |
mglmx | 35:cfcfeccb959e | 357 | |
mglmx | 35:cfcfeccb959e | 358 | d4.add_prev_cw(D6); |
mglmx | 35:cfcfeccb959e | 359 | d4.add_prev_ccw(D21); |
mglmx | 35:cfcfeccb959e | 360 | |
mglmx | 35:cfcfeccb959e | 361 | d5.add_prev_cw(D6); |
mglmx | 35:cfcfeccb959e | 362 | d5.add_prev_ccw(D11); |
mglmx | 35:cfcfeccb959e | 363 | |
mglmx | 35:cfcfeccb959e | 364 | d6.add_prev_cw(D7); |
mglmx | 35:cfcfeccb959e | 365 | d6.add_prev_ccw(D4); |
mglmx | 35:cfcfeccb959e | 366 | d6.add_prev_ccw(D5); |
mglmx | 35:cfcfeccb959e | 367 | |
mglmx | 35:cfcfeccb959e | 368 | d7.add_prev_cw(D8); |
mglmx | 35:cfcfeccb959e | 369 | d7.add_prev_ccw(D6); |
mglmx | 35:cfcfeccb959e | 370 | |
mglmx | 36:9428c72bdd58 | 371 | d8.add_prev_cw(D9); |
mglmx | 36:9428c72bdd58 | 372 | d8.add_prev_cw(D10); |
mglmx | 36:9428c72bdd58 | 373 | d8.add_prev_ccw(D7); |
mglmx | 36:9428c72bdd58 | 374 | |
mglmx | 35:cfcfeccb959e | 375 | d9.add_prev_cw(D3); |
mglmx | 35:cfcfeccb959e | 376 | d9.add_prev_ccw(D8); |
mglmx | 35:cfcfeccb959e | 377 | |
mglmx | 35:cfcfeccb959e | 378 | d10.add_prev_cw(D12); |
mglmx | 35:cfcfeccb959e | 379 | d10.add_prev_ccw(D8); |
mglmx | 35:cfcfeccb959e | 380 | |
mglmx | 35:cfcfeccb959e | 381 | d11.add_prev_cw(D12); |
mglmx | 35:cfcfeccb959e | 382 | d11.add_prev_ccw(D5); |
mglmx | 35:cfcfeccb959e | 383 | |
mglmx | 35:cfcfeccb959e | 384 | d12.add_prev_cw(D13); |
mglmx | 35:cfcfeccb959e | 385 | d12.add_prev_ccw(D10); |
mglmx | 35:cfcfeccb959e | 386 | d12.add_prev_ccw(D11); |
mglmx | 35:cfcfeccb959e | 387 | |
mglmx | 35:cfcfeccb959e | 388 | d13.add_prev_cw(D0); |
mglmx | 35:cfcfeccb959e | 389 | d13.add_prev_ccw(D12); |
mglmx | 36:9428c72bdd58 | 390 | |
mglmx | 36:9428c72bdd58 | 391 | //Initialize array with positions |
mglmx | 36:9428c72bdd58 | 392 | positions.push_back(d0); |
mglmx | 36:9428c72bdd58 | 393 | positions.push_back(d1); |
mglmx | 36:9428c72bdd58 | 394 | positions.push_back(d2); |
mglmx | 36:9428c72bdd58 | 395 | positions.push_back(d3); |
mglmx | 36:9428c72bdd58 | 396 | positions.push_back(d4); |
mglmx | 36:9428c72bdd58 | 397 | positions.push_back(d5); |
mglmx | 36:9428c72bdd58 | 398 | positions.push_back(d6); |
mglmx | 36:9428c72bdd58 | 399 | positions.push_back(d7); |
mglmx | 36:9428c72bdd58 | 400 | positions.push_back(d8); |
mglmx | 36:9428c72bdd58 | 401 | positions.push_back(d9); |
mglmx | 36:9428c72bdd58 | 402 | positions.push_back(d10); |
mglmx | 36:9428c72bdd58 | 403 | positions.push_back(d11); |
mglmx | 36:9428c72bdd58 | 404 | positions.push_back(d12); |
mglmx | 36:9428c72bdd58 | 405 | positions.push_back(d13); |
mglmx | 36:9428c72bdd58 | 406 | positions.push_back(d21); |
mglmx | 36:9428c72bdd58 | 407 | positions.push_back(d22); |
mglmx | 35:cfcfeccb959e | 408 | } |
carlosperales95 | 24:1d71dd8778c4 | 409 | |
carlosperales95 | 45:d589318238bf | 410 | |
mglmx | 22:e4153ca757dd | 411 | /** |
carlosperales95 | 24:1d71dd8778c4 | 412 | * |
carlosperales95 | 28:71bd4c83c05f | 413 | *Here we initialize the mcp that will be used to manage the interrupts. |
carlosperales95 | 25:a42a1ed4d8e9 | 414 | * |
carlosperales95 | 24:1d71dd8778c4 | 415 | **/ |
mglmx | 18:aa43bb62e60f | 416 | void initialize_mcp(){ |
mglmx | 33:24ce12dec157 | 417 | mcp = new MCP23017(i2c,0x40); //Connect to SCL - p28 and SDA - p27 and MPC I2C address 0x40 |
mglmx | 18:aa43bb62e60f | 418 | |
mglmx | 33:24ce12dec157 | 419 | mcp->_write(IODIRA, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 420 | mcp->_write(IODIRB, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 421 | mcp->_write(IPOLA, (unsigned char )0x00); |
mglmx | 33:24ce12dec157 | 422 | mcp->_write(IPOLB, (unsigned char )0x00); |
mglmx | 33:24ce12dec157 | 423 | mcp->_write(DEFVALA, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 424 | mcp->_write(DEFVALB, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 425 | mcp->_write(INTCONA, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 426 | mcp->_write(INTCONB, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 427 | mcp->_write(IOCONA, (unsigned char )0x2); |
mglmx | 33:24ce12dec157 | 428 | mcp->_write(IOCONB, (unsigned char )0x2); |
mglmx | 33:24ce12dec157 | 429 | mcp->_write(GPPUA, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 430 | mcp->_write(GPPUB, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 431 | |
mglmx | 18:aa43bb62e60f | 432 | } |
mglmx | 18:aa43bb62e60f | 433 | |
carlosperales95 | 45:d589318238bf | 434 | |
carlosperales95 | 24:1d71dd8778c4 | 435 | /** |
carlosperales95 | 24:1d71dd8778c4 | 436 | * |
carlosperales95 | 37:bb15bea420a3 | 437 | *Returns the number of the sensor where the train was detected. |
carlosperales95 | 37:bb15bea420a3 | 438 | * |
carlosperales95 | 47:e992a129ef44 | 439 | *@number - |
carlosperales95 | 47:e992a129ef44 | 440 | *@interrupt - |
carlosperales95 | 47:e992a129ef44 | 441 | * |
carlosperales95 | 37:bb15bea420a3 | 442 | **/ |
mglmx | 34:c9ab2a987734 | 443 | int get_sensor(unsigned int number,int interrupt){ |
carlosperales95 | 45:d589318238bf | 444 | |
mglmx | 34:c9ab2a987734 | 445 | int sensor = -1; |
mglmx | 34:c9ab2a987734 | 446 | |
carlosperales95 | 37:bb15bea420a3 | 447 | for(int i=0; i<8; i++){ |
mglmx | 34:c9ab2a987734 | 448 | |
carlosperales95 | 45:d589318238bf | 449 | if(~number & 1<<i){ |
carlosperales95 | 45:d589318238bf | 450 | |
mglmx | 34:c9ab2a987734 | 451 | sensor = i; |
mglmx | 34:c9ab2a987734 | 452 | } |
mglmx | 34:c9ab2a987734 | 453 | } |
mglmx | 34:c9ab2a987734 | 454 | |
mglmx | 34:c9ab2a987734 | 455 | if(interrupt == 1){ |
carlosperales95 | 45:d589318238bf | 456 | |
carlosperales95 | 37:bb15bea420a3 | 457 | sensor+= 8; // Sensors caught by interreupt1 are identified from 8 to 15. |
mglmx | 34:c9ab2a987734 | 458 | } |
carlosperales95 | 45:d589318238bf | 459 | |
mglmx | 34:c9ab2a987734 | 460 | return sensor; |
mglmx | 34:c9ab2a987734 | 461 | } |
mglmx | 34:c9ab2a987734 | 462 | |
carlosperales95 | 45:d589318238bf | 463 | |
carlosperales95 | 45:d589318238bf | 464 | /** |
carlosperales95 | 45:d589318238bf | 465 | * |
carlosperales95 | 47:e992a129ef44 | 466 | *Checks if the element exists within the vector. |
carlosperales95 | 45:d589318238bf | 467 | * |
carlosperales95 | 47:e992a129ef44 | 468 | *@v - The vector (of ints) the method will go through. |
carlosperales95 | 47:e992a129ef44 | 469 | *@element - The element the method will look for. |
carlosperales95 | 45:d589318238bf | 470 | * |
carlosperales95 | 45:d589318238bf | 471 | **/ |
mglmx | 43:346a1f4144cd | 472 | bool in_vector(vector<int>v,int element){ |
carlosperales95 | 45:d589318238bf | 473 | |
mglmx | 43:346a1f4144cd | 474 | bool exist = false; |
mglmx | 43:346a1f4144cd | 475 | |
mglmx | 43:346a1f4144cd | 476 | for(int i=0; i< v.size(); i++){ |
carlosperales95 | 45:d589318238bf | 477 | |
mglmx | 43:346a1f4144cd | 478 | if(v[i] == element){ |
carlosperales95 | 45:d589318238bf | 479 | |
mglmx | 43:346a1f4144cd | 480 | exist = true; |
mglmx | 43:346a1f4144cd | 481 | } |
carlosperales95 | 47:e992a129ef44 | 482 | } |
mglmx | 43:346a1f4144cd | 483 | return exist; |
mglmx | 43:346a1f4144cd | 484 | } |
mglmx | 43:346a1f4144cd | 485 | |
carlosperales95 | 45:d589318238bf | 486 | |
carlosperales95 | 42:b445252a772a | 487 | /** |
carlosperales95 | 47:e992a129ef44 | 488 | *This method will checks if there is a non-avoidable frontal collision(NAFC). |
carlosperales95 | 47:e992a129ef44 | 489 | *A NAFC will happen if: |
carlosperales95 | 47:e992a129ef44 | 490 | * |
carlosperales95 | 47:e992a129ef44 | 491 | *Both trains in area A or B with different direction |
carlosperales95 | 47:e992a129ef44 | 492 | *Trains in (D11 and D5) or (D9 and D3) with same direction |
carlosperales95 | 47:e992a129ef44 | 493 | * |
mglmx | 43:346a1f4144cd | 494 | */ |
mglmx | 43:346a1f4144cd | 495 | bool check_NAC(bool DR_in_A, bool DR_in_B,bool LR_in_A,bool LR_in_B){ |
carlosperales95 | 45:d589318238bf | 496 | |
mglmx | 43:346a1f4144cd | 497 | bool NAC = false; |
mglmx | 43:346a1f4144cd | 498 | |
mglmx | 43:346a1f4144cd | 499 | if((DR_in_A && LR_in_A) || (DR_in_B && LR_in_B) ){ //Check if both are in same area |
carlosperales95 | 45:d589318238bf | 500 | |
mglmx | 43:346a1f4144cd | 501 | if(DR_train.goes_cw() ^ LR_train.goes_cw()){ //XOR: They must have different values to be true (Different direction) |
carlosperales95 | 45:d589318238bf | 502 | |
mglmx | 43:346a1f4144cd | 503 | NAC = true; |
mglmx | 43:346a1f4144cd | 504 | } |
mglmx | 43:346a1f4144cd | 505 | }else if((DR_train.get_position_number() == D11) && (LR_train.get_position_number() == D5 )){ //Check if they are in position D11 and D5 |
carlosperales95 | 45:d589318238bf | 506 | |
mglmx | 43:346a1f4144cd | 507 | if(!(DR_train.goes_cw() ^ LR_train.goes_cw())){ // NOT XOR: They must have same values to be true (Same direction) |
carlosperales95 | 45:d589318238bf | 508 | |
mglmx | 43:346a1f4144cd | 509 | NAC = true; |
mglmx | 43:346a1f4144cd | 510 | } |
mglmx | 43:346a1f4144cd | 511 | }else if((DR_train.get_position_number() == D9) && (LR_train.get_position_number() == D3 )){//Check if they are in position D9 and D3 |
carlosperales95 | 45:d589318238bf | 512 | |
mglmx | 43:346a1f4144cd | 513 | if(!(DR_train.goes_cw() ^ LR_train.goes_cw())){ // NOT XOR: They must have same values to be true (Same direction) |
carlosperales95 | 45:d589318238bf | 514 | |
mglmx | 43:346a1f4144cd | 515 | NAC = true; |
mglmx | 43:346a1f4144cd | 516 | } |
mglmx | 43:346a1f4144cd | 517 | } |
carlosperales95 | 45:d589318238bf | 518 | return NAC; |
mglmx | 43:346a1f4144cd | 519 | } |
mglmx | 43:346a1f4144cd | 520 | |
carlosperales95 | 45:d589318238bf | 521 | |
mglmx | 43:346a1f4144cd | 522 | /** |
carlosperales95 | 47:e992a129ef44 | 523 | * |
carlosperales95 | 47:e992a129ef44 | 524 | *CThe function will check if there is an Avoidable Frontal Collision (AFC). |
carlosperales95 | 47:e992a129ef44 | 525 | *AFC will occur if: |
carlosperales95 | 47:e992a129ef44 | 526 | * |
carlosperales95 | 47:e992a129ef44 | 527 | *Train in area A(ccw) and train in D4(cw) |
carlosperales95 | 47:e992a129ef44 | 528 | *Train in area A(cw) and train in D10(ccw) |
carlosperales95 | 47:e992a129ef44 | 529 | *Train in area B(cw) and train in D4(ccw) |
carlosperales95 | 47:e992a129ef44 | 530 | *Train in area B(ccw) and train in D10(ccw) |
carlosperales95 | 47:e992a129ef44 | 531 | * |
carlosperales95 | 46:7a0933676b13 | 532 | **/ |
carlosperales95 | 46:7a0933676b13 | 533 | bool check_AFC(bool DR_in_A, bool DR_in_B,bool LR_in_A,bool LR_in_B){ //TODO - Add same for LR train |
carlosperales95 | 45:d589318238bf | 534 | |
carlosperales95 | 45:d589318238bf | 535 | if( DR_train.get_position_number() == D4){ |
carlosperales95 | 45:d589318238bf | 536 | |
mglmx | 43:346a1f4144cd | 537 | if(DR_train.goes_cw()){ |
carlosperales95 | 45:d589318238bf | 538 | |
mglmx | 43:346a1f4144cd | 539 | if(LR_in_A && !LR_train.goes_cw()){ |
carlosperales95 | 45:d589318238bf | 540 | |
mglmx | 43:346a1f4144cd | 541 | //Activate switch2 |
mglmx | 43:346a1f4144cd | 542 | //DR_train has to stop |
mglmx | 43:346a1f4144cd | 543 | //When LR is at D3 DR continues |
mglmx | 43:346a1f4144cd | 544 | } |
mglmx | 43:346a1f4144cd | 545 | }else{ //DR goes ccw |
carlosperales95 | 45:d589318238bf | 546 | |
mglmx | 43:346a1f4144cd | 547 | if(LR_in_B && LR_train.goes_cw()){ |
carlosperales95 | 45:d589318238bf | 548 | |
mglmx | 43:346a1f4144cd | 549 | //DR_train stops |
mglmx | 43:346a1f4144cd | 550 | //Activate switch3 |
mglmx | 43:346a1f4144cd | 551 | //When LR is at D5 DR continues |
mglmx | 43:346a1f4144cd | 552 | } |
mglmx | 43:346a1f4144cd | 553 | } |
mglmx | 43:346a1f4144cd | 554 | |
mglmx | 43:346a1f4144cd | 555 | }else if(DR_train.get_position_number() == D10){ |
carlosperales95 | 45:d589318238bf | 556 | |
mglmx | 43:346a1f4144cd | 557 | if(DR_train.goes_cw()){ |
carlosperales95 | 45:d589318238bf | 558 | |
mglmx | 43:346a1f4144cd | 559 | if(LR_in_B && !LR_train.goes_cw()){ |
carlosperales95 | 45:d589318238bf | 560 | |
mglmx | 43:346a1f4144cd | 561 | //DR train stops |
mglmx | 43:346a1f4144cd | 562 | //Activate switch4 |
mglmx | 43:346a1f4144cd | 563 | //When LR is at D9 DR continues |
mglmx | 43:346a1f4144cd | 564 | } |
mglmx | 43:346a1f4144cd | 565 | }else{ |
carlosperales95 | 45:d589318238bf | 566 | |
mglmx | 43:346a1f4144cd | 567 | if(LR_in_A && LR_train.goes_cw()){ |
carlosperales95 | 45:d589318238bf | 568 | |
mglmx | 43:346a1f4144cd | 569 | //DR train stops |
mglmx | 43:346a1f4144cd | 570 | //Activate switch1 |
mglmx | 43:346a1f4144cd | 571 | //When LR is at D9 DR continues |
mglmx | 43:346a1f4144cd | 572 | } |
mglmx | 43:346a1f4144cd | 573 | } |
mglmx | 43:346a1f4144cd | 574 | } |
mglmx | 43:346a1f4144cd | 575 | } |
mglmx | 43:346a1f4144cd | 576 | |
carlosperales95 | 45:d589318238bf | 577 | |
carlosperales95 | 45:d589318238bf | 578 | /** |
carlosperales95 | 45:d589318238bf | 579 | * |
carlosperales95 | 47:e992a129ef44 | 580 | *The method check_position will check if any of the trains is in any of the areas. |
carlosperales95 | 47:e992a129ef44 | 581 | *It will go through all the area vectors (A,B) and call the function in_vector to check inside the vectors. |
carlosperales95 | 45:d589318238bf | 582 | * |
carlosperales95 | 45:d589318238bf | 583 | **/ |
mglmx | 43:346a1f4144cd | 584 | void check_position(){ |
carlosperales95 | 45:d589318238bf | 585 | |
mglmx | 43:346a1f4144cd | 586 | bool DR_in_A, DR_in_B, LR_in_A, LR_in_B; |
mglmx | 43:346a1f4144cd | 587 | |
carlosperales95 | 47:e992a129ef44 | 588 | DR_in_A = in_vector(area_A,DR_train.get_position_number()); //Check if DR train is in area A |
carlosperales95 | 47:e992a129ef44 | 589 | DR_in_B = in_vector(area_B,DR_train.get_position_number()); |
carlosperales95 | 47:e992a129ef44 | 590 | LR_in_A = in_vector(area_A,LR_train.get_position_number()); |
carlosperales95 | 47:e992a129ef44 | 591 | LR_in_B = in_vector(area_B,LR_train.get_position_number()); |
mglmx | 43:346a1f4144cd | 592 | |
mglmx | 43:346a1f4144cd | 593 | } |
mglmx | 43:346a1f4144cd | 594 | |
carlosperales95 | 45:d589318238bf | 595 | |
carlosperales95 | 45:d589318238bf | 596 | /** |
carlosperales95 | 45:d589318238bf | 597 | * |
carlosperales95 | 47:e992a129ef44 | 598 | *Description |
carlosperales95 | 45:d589318238bf | 599 | * |
carlosperales95 | 47:e992a129ef44 | 600 | *@sensor - |
carlosperales95 | 45:d589318238bf | 601 | * |
carlosperales95 | 45:d589318238bf | 602 | **/ |
mglmx | 35:cfcfeccb959e | 603 | void update_train_pos(int sensor){ |
mglmx | 41:4fa6aa29d1ed | 604 | |
mglmx | 35:cfcfeccb959e | 605 | bool found_DR = false; |
mglmx | 41:4fa6aa29d1ed | 606 | bool found_LR = false; |
mglmx | 41:4fa6aa29d1ed | 607 | |
mglmx | 49:880c0b9c9c64 | 608 | string DR_dir,LR_dir; |
mglmx | 49:880c0b9c9c64 | 609 | |
mglmx | 49:880c0b9c9c64 | 610 | if(DR_train.goes_cw()){ |
mglmx | 49:880c0b9c9c64 | 611 | DR_dir = "cw"; |
mglmx | 49:880c0b9c9c64 | 612 | }else{ |
mglmx | 49:880c0b9c9c64 | 613 | DR_dir = "ccw"; |
mglmx | 49:880c0b9c9c64 | 614 | } |
mglmx | 49:880c0b9c9c64 | 615 | |
mglmx | 49:880c0b9c9c64 | 616 | if(LR_train.goes_cw()){ |
mglmx | 49:880c0b9c9c64 | 617 | LR_dir = "cw"; |
mglmx | 49:880c0b9c9c64 | 618 | }else{ |
mglmx | 49:880c0b9c9c64 | 619 | LR_dir = "ccw"; |
mglmx | 49:880c0b9c9c64 | 620 | } |
mglmx | 49:880c0b9c9c64 | 621 | |
mglmx | 49:880c0b9c9c64 | 622 | |
mglmx | 41:4fa6aa29d1ed | 623 | lcd.cls(); |
mglmx | 49:880c0b9c9c64 | 624 | lcd.printf("S:D%d DR(",sensor); |
mglmx | 43:346a1f4144cd | 625 | |
mglmx | 43:346a1f4144cd | 626 | //TODO: Do a for to print all next sensors. |
mglmx | 43:346a1f4144cd | 627 | for(int i=0; i<DR_train.get_next_sensors().size(); i++){ |
carlosperales95 | 45:d589318238bf | 628 | |
mglmx | 43:346a1f4144cd | 629 | lcd.printf("%d,",DR_train.get_next_sensors()[i]); |
mglmx | 43:346a1f4144cd | 630 | } |
carlosperales95 | 45:d589318238bf | 631 | |
mglmx | 49:880c0b9c9c64 | 632 | lcd.printf(")%s LR(",DR_dir); |
mglmx | 43:346a1f4144cd | 633 | |
carlosperales95 | 45:d589318238bf | 634 | for(int i=0; i<LR_train.get_next_sensors().size(); i++){ |
carlosperales95 | 45:d589318238bf | 635 | |
mglmx | 43:346a1f4144cd | 636 | lcd.printf("%d,",LR_train.get_next_sensors()[i]); |
mglmx | 43:346a1f4144cd | 637 | } |
carlosperales95 | 45:d589318238bf | 638 | |
mglmx | 49:880c0b9c9c64 | 639 | lcd.printf(")%s",LR_dir); |
mglmx | 43:346a1f4144cd | 640 | wait(0.7); |
mglmx | 41:4fa6aa29d1ed | 641 | |
mglmx | 41:4fa6aa29d1ed | 642 | //Checking next sensors for DR train |
mglmx | 43:346a1f4144cd | 643 | for(int i=0; i<DR_train.get_next_sensors().size(); i++){ |
mglmx | 41:4fa6aa29d1ed | 644 | |
mglmx | 41:4fa6aa29d1ed | 645 | if(DR_train.get_next_sensors()[i] == sensor){ //If the sensor is one expected to visit by the train we update the position |
carlosperales95 | 45:d589318238bf | 646 | |
mglmx | 41:4fa6aa29d1ed | 647 | found_DR = true; |
mglmx | 49:880c0b9c9c64 | 648 | |
carlosperales95 | 37:bb15bea420a3 | 649 | |
mglmx | 41:4fa6aa29d1ed | 650 | if(DR_train.goes_cw()){ |
mglmx | 48:553716e13d45 | 651 | if(DR_train.get_position_number() == D5 || DR_train.get_position_number() == D11){ |
mglmx | 48:553716e13d45 | 652 | |
mglmx | 41:4fa6aa29d1ed | 653 | DR_train.set_goes_cw(false); //If train goes cw and passes D5 or D11 we change orientation |
mglmx | 41:4fa6aa29d1ed | 654 | } |
mglmx | 41:4fa6aa29d1ed | 655 | }else{ |
carlosperales95 | 45:d589318238bf | 656 | |
mglmx | 48:553716e13d45 | 657 | if(DR_train.get_position_number() == D9 || DR_train.get_position_number() == D3){ |
carlosperales95 | 45:d589318238bf | 658 | |
mglmx | 41:4fa6aa29d1ed | 659 | DR_train.set_goes_cw(true); //If train goes ccw and passes D9 or D3 we change orientation |
mglmx | 41:4fa6aa29d1ed | 660 | } |
mglmx | 49:880c0b9c9c64 | 661 | } |
mglmx | 49:880c0b9c9c64 | 662 | |
mglmx | 49:880c0b9c9c64 | 663 | DR_train.set_position(sensor); |
mglmx | 41:4fa6aa29d1ed | 664 | } |
mglmx | 41:4fa6aa29d1ed | 665 | } |
mglmx | 41:4fa6aa29d1ed | 666 | |
mglmx | 41:4fa6aa29d1ed | 667 | //Checking next sensors for LR train |
mglmx | 41:4fa6aa29d1ed | 668 | for(int i=0; i<LR_train.get_next_sensors().size(); i++){ |
mglmx | 43:346a1f4144cd | 669 | |
mglmx | 41:4fa6aa29d1ed | 670 | if(LR_train.get_next_sensors()[i] == sensor){ |
carlosperales95 | 45:d589318238bf | 671 | |
mglmx | 41:4fa6aa29d1ed | 672 | found_LR = true; |
mglmx | 49:880c0b9c9c64 | 673 | |
carlosperales95 | 45:d589318238bf | 674 | if(LR_train.goes_cw()){ |
mglmx | 48:553716e13d45 | 675 | |
mglmx | 49:880c0b9c9c64 | 676 | if(LR_train.get_position_number() == D5 || LR_train.get_position_number() == D11){ |
mglmx | 41:4fa6aa29d1ed | 677 | LR_train.set_goes_cw(false); //If train goes cw and passes D5 or D11 we change orientation |
mglmx | 41:4fa6aa29d1ed | 678 | } |
mglmx | 41:4fa6aa29d1ed | 679 | }else{ |
mglmx | 49:880c0b9c9c64 | 680 | |
mglmx | 49:880c0b9c9c64 | 681 | if(LR_train.get_position_number() == D9 || LR_train.get_position_number() == D3 ){ |
carlosperales95 | 45:d589318238bf | 682 | |
mglmx | 41:4fa6aa29d1ed | 683 | LR_train.set_goes_cw(true); //If train goes ccw and passes D9 or D3 we change orientation |
mglmx | 41:4fa6aa29d1ed | 684 | } |
mglmx | 49:880c0b9c9c64 | 685 | } |
mglmx | 49:880c0b9c9c64 | 686 | LR_train.set_position(sensor); |
mglmx | 41:4fa6aa29d1ed | 687 | } |
mglmx | 41:4fa6aa29d1ed | 688 | } |
mglmx | 41:4fa6aa29d1ed | 689 | |
mglmx | 35:cfcfeccb959e | 690 | if(found_DR){ |
carlosperales95 | 45:d589318238bf | 691 | |
mglmx | 36:9428c72bdd58 | 692 | //doBuzz(); |
mglmx | 35:cfcfeccb959e | 693 | lcd.cls(); |
mglmx | 41:4fa6aa29d1ed | 694 | lcd.printf("DR is at D%d",DR_train.get_position_number()); |
mglmx | 35:cfcfeccb959e | 695 | } |
carlosperales95 | 45:d589318238bf | 696 | |
mglmx | 35:cfcfeccb959e | 697 | if(found_LR){ |
carlosperales95 | 40:9acc1341456a | 698 | |
carlosperales95 | 37:bb15bea420a3 | 699 | lcd.cls(); |
mglmx | 41:4fa6aa29d1ed | 700 | lcd.printf("LR is at D%d",LR_train.get_position_number()); |
mglmx | 35:cfcfeccb959e | 701 | } |
carlosperales95 | 45:d589318238bf | 702 | |
mglmx | 35:cfcfeccb959e | 703 | if(!found_DR && !found_LR){ |
carlosperales95 | 45:d589318238bf | 704 | |
mglmx | 35:cfcfeccb959e | 705 | lcd.cls(); |
mglmx | 35:cfcfeccb959e | 706 | lcd.printf("No train before :("); |
mglmx | 35:cfcfeccb959e | 707 | } |
mglmx | 35:cfcfeccb959e | 708 | } |
mglmx | 35:cfcfeccb959e | 709 | |
carlosperales95 | 37:bb15bea420a3 | 710 | |
mglmx | 34:c9ab2a987734 | 711 | /** |
mglmx | 34:c9ab2a987734 | 712 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 713 | *Method to catch interrupts 0 |
carlosperales95 | 25:a42a1ed4d8e9 | 714 | * |
carlosperales95 | 24:1d71dd8778c4 | 715 | **/ |
mglmx | 33:24ce12dec157 | 716 | void on_int0_change(){ |
carlosperales95 | 25:a42a1ed4d8e9 | 717 | |
mglmx | 33:24ce12dec157 | 718 | wait_us(2000); |
mglmx | 33:24ce12dec157 | 719 | int sensor_data = mcp->_read(INTCAPA); |
mglmx | 34:c9ab2a987734 | 720 | int sensor = get_sensor(sensor_data,0); |
mglmx | 17:0a657e338356 | 721 | lcd.cls(); |
mglmx | 34:c9ab2a987734 | 722 | lcd.printf("int0 0x%x \n Sensor: %d",sensor_data,sensor); |
mglmx | 41:4fa6aa29d1ed | 723 | |
mglmx | 35:cfcfeccb959e | 724 | update_train_pos(sensor); |
mglmx | 16:2a2da0e67793 | 725 | } |
mglmx | 16:2a2da0e67793 | 726 | |
mglmx | 35:cfcfeccb959e | 727 | |
carlosperales95 | 24:1d71dd8778c4 | 728 | /** |
carlosperales95 | 24:1d71dd8778c4 | 729 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 730 | *Method to catch interrupts 1 |
carlosperales95 | 25:a42a1ed4d8e9 | 731 | * |
carlosperales95 | 24:1d71dd8778c4 | 732 | **/ |
mglmx | 33:24ce12dec157 | 733 | void on_int1_change(){ |
carlosperales95 | 25:a42a1ed4d8e9 | 734 | |
mglmx | 33:24ce12dec157 | 735 | wait_us(2000); |
mglmx | 33:24ce12dec157 | 736 | int sensor_data = mcp->_read(INTCAPB); |
mglmx | 34:c9ab2a987734 | 737 | int sensor = get_sensor(sensor_data,1); |
mglmx | 33:24ce12dec157 | 738 | lcd.cls(); |
mglmx | 34:c9ab2a987734 | 739 | lcd.printf("int1 0x%x \n Sensor: %d",sensor_data,sensor); |
mglmx | 35:cfcfeccb959e | 740 | |
mglmx | 35:cfcfeccb959e | 741 | update_train_pos(sensor); |
mglmx | 16:2a2da0e67793 | 742 | } |
mglmx | 26:5c966a0a3e8e | 743 | |
carlosperales95 | 45:d589318238bf | 744 | /** |
carlosperales95 | 45:d589318238bf | 745 | * |
carlosperales95 | 47:e992a129ef44 | 746 | *Clear current interrupts |
carlosperales95 | 45:d589318238bf | 747 | * |
carlosperales95 | 45:d589318238bf | 748 | **/ |
carlosperales95 | 47:e992a129ef44 | 749 | void init() { |
mglmx | 26:5c966a0a3e8e | 750 | |
mglmx | 33:24ce12dec157 | 751 | mcp->_read(GPIOA); |
mglmx | 33:24ce12dec157 | 752 | mcp->_read(GPIOB); // Register callbacks |
mglmx | 33:24ce12dec157 | 753 | int0.fall(&on_int0_change); |
mglmx | 33:24ce12dec157 | 754 | int1.fall(&on_int1_change); // Enable interrupts on MCP |
mglmx | 33:24ce12dec157 | 755 | mcp->_write(GPINTENA, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 756 | mcp->_write(GPINTENB, (unsigned char )0xff); // Ready to go! |
mglmx | 33:24ce12dec157 | 757 | } |
mglmx | 33:24ce12dec157 | 758 | |
mglmx | 33:24ce12dec157 | 759 | |
carlosperales95 | 24:1d71dd8778c4 | 760 | /** |
carlosperales95 | 24:1d71dd8778c4 | 761 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 762 | *Method to send DCC commands to train and switches. |
carlosperales95 | 25:a42a1ed4d8e9 | 763 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 764 | *@address - (HEX)Address where the commands will be sent |
carlosperales95 | 25:a42a1ed4d8e9 | 765 | *@inst - (HEX)Number of instruction that will be commanded |
carlosperales95 | 25:a42a1ed4d8e9 | 766 | *@repeat_count - Number of times the command will be sent |
carlosperales95 | 25:a42a1ed4d8e9 | 767 | * |
carlosperales95 | 24:1d71dd8778c4 | 768 | **/ |
carlosperales95 | 45:d589318238bf | 769 | void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count){ |
carlosperales95 | 45:d589318238bf | 770 | |
mglmx | 1:0ab26889af9b | 771 | unsigned __int64 command = 0x0000000000000000; // __int64 is the 64-bit integer type |
mglmx | 1:0ab26889af9b | 772 | unsigned __int64 temp_command = 0x0000000000000000; |
mglmx | 1:0ab26889af9b | 773 | unsigned __int64 prefix = 0x3FFF; // 14 "1" bits needed at start |
mglmx | 1:0ab26889af9b | 774 | unsigned int error = 0x00; //error byte |
carlosperales95 | 24:1d71dd8778c4 | 775 | |
mglmx | 1:0ab26889af9b | 776 | //calculate error detection byte with xor |
mglmx | 1:0ab26889af9b | 777 | error = address ^ inst; |
carlosperales95 | 24:1d71dd8778c4 | 778 | |
mglmx | 1:0ab26889af9b | 779 | //combine packet bits in basic DCC format |
mglmx | 1:0ab26889af9b | 780 | command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01; |
mglmx | 1:0ab26889af9b | 781 | //printf("\n\r %llx \n\r",command); |
carlosperales95 | 24:1d71dd8778c4 | 782 | |
mglmx | 1:0ab26889af9b | 783 | int i=0; |
carlosperales95 | 24:1d71dd8778c4 | 784 | //repeat DCC command lots of times |
mglmx | 1:0ab26889af9b | 785 | while(i < repeat_count) { |
carlosperales95 | 24:1d71dd8778c4 | 786 | |
mglmx | 1:0ab26889af9b | 787 | temp_command = command; |
carlosperales95 | 24:1d71dd8778c4 | 788 | //loops through packet bits encoding and sending out digital pulses for a DCC command |
mglmx | 1:0ab26889af9b | 789 | for (int j=0; j<64; j++) { |
carlosperales95 | 24:1d71dd8778c4 | 790 | |
carlosperales95 | 24:1d71dd8778c4 | 791 | if((temp_command&0x8000000000000000)==0) { |
carlosperales95 | 24:1d71dd8778c4 | 792 | //test packet bit |
mglmx | 1:0ab26889af9b | 793 | //send data for a "0" bit |
mglmx | 1:0ab26889af9b | 794 | Track=0; |
mglmx | 1:0ab26889af9b | 795 | wait_us(100); |
mglmx | 1:0ab26889af9b | 796 | Track=1; |
mglmx | 1:0ab26889af9b | 797 | wait_us(100); |
mglmx | 1:0ab26889af9b | 798 | //printf("0011"); |
carlosperales95 | 24:1d71dd8778c4 | 799 | }else{ |
carlosperales95 | 24:1d71dd8778c4 | 800 | |
mglmx | 1:0ab26889af9b | 801 | //send data for a "1"bit |
mglmx | 1:0ab26889af9b | 802 | Track=0; |
mglmx | 1:0ab26889af9b | 803 | wait_us(58); |
mglmx | 1:0ab26889af9b | 804 | Track=1; |
mglmx | 1:0ab26889af9b | 805 | wait_us(58); |
mglmx | 1:0ab26889af9b | 806 | //printf("01"); |
mglmx | 1:0ab26889af9b | 807 | } |
mglmx | 1:0ab26889af9b | 808 | // next bit in packet |
mglmx | 1:0ab26889af9b | 809 | temp_command = temp_command<<1; |
mglmx | 1:0ab26889af9b | 810 | } |
mglmx | 1:0ab26889af9b | 811 | i++; |
mglmx | 0:4d06a6a8e785 | 812 | } |
mglmx | 0:4d06a6a8e785 | 813 | } |
carlosperales95 | 11:021210c59a95 | 814 | |
carlosperales95 | 24:1d71dd8778c4 | 815 | |
carlosperales95 | 24:1d71dd8778c4 | 816 | /** |
carlosperales95 | 24:1d71dd8778c4 | 817 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 818 | *Method to flip the switches |
carlosperales95 | 25:a42a1ed4d8e9 | 819 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 820 | *@switchId - (1-4)The ID of the switch we want to flip |
carlosperales95 | 25:a42a1ed4d8e9 | 821 | *@times - The number of times we want to send the command |
mglmx | 30:293ee760d357 | 822 | *@activate - True if the switch is going to be activated. False if it needs to go back to rest position. |
carlosperales95 | 25:a42a1ed4d8e9 | 823 | * |
carlosperales95 | 24:1d71dd8778c4 | 824 | **/ |
mglmx | 30:293ee760d357 | 825 | void flipSwitch(int switchId, int times, bool activate=true){ |
mglmx | 21:e6f1649add39 | 826 | |
carlosperales95 | 25:a42a1ed4d8e9 | 827 | unsigned int SWBflip = SWBidle; //IDLE - Flip last activated SW. |
mglmx | 21:e6f1649add39 | 828 | |
mglmx | 21:e6f1649add39 | 829 | switch(switchId){ |
carlosperales95 | 45:d589318238bf | 830 | |
mglmx | 21:e6f1649add39 | 831 | case 1: |
carlosperales95 | 25:a42a1ed4d8e9 | 832 | SWBflip = SWBflip_1; //FLIP SW1 |
mglmx | 21:e6f1649add39 | 833 | break; |
carlosperales95 | 45:d589318238bf | 834 | |
mglmx | 21:e6f1649add39 | 835 | case 2: |
carlosperales95 | 25:a42a1ed4d8e9 | 836 | SWBflip = SWBflip_2; //FLIP SW2 |
mglmx | 21:e6f1649add39 | 837 | break; |
carlosperales95 | 45:d589318238bf | 838 | |
mglmx | 21:e6f1649add39 | 839 | case 3: |
carlosperales95 | 25:a42a1ed4d8e9 | 840 | SWBflip = SWBflip_3; //FLIP SW3 |
mglmx | 21:e6f1649add39 | 841 | break; |
carlosperales95 | 45:d589318238bf | 842 | |
mglmx | 21:e6f1649add39 | 843 | case 4: |
carlosperales95 | 25:a42a1ed4d8e9 | 844 | SWBflip = SWBflip_4; //FLIP SW4 |
mglmx | 21:e6f1649add39 | 845 | break; |
carlosperales95 | 45:d589318238bf | 846 | |
mglmx | 21:e6f1649add39 | 847 | default: |
carlosperales95 | 24:1d71dd8778c4 | 848 | break; |
mglmx | 21:e6f1649add39 | 849 | } |
carlosperales95 | 11:021210c59a95 | 850 | |
mglmx | 21:e6f1649add39 | 851 | //Security measure not to burn the switch. |
mglmx | 30:293ee760d357 | 852 | if(times <=5){ |
carlosperales95 | 45:d589318238bf | 853 | |
mglmx | 30:293ee760d357 | 854 | DCC_send_command(SWBaddress,SWBflip,times); //Activating switch |
mglmx | 30:293ee760d357 | 855 | if(!activate){ |
carlosperales95 | 45:d589318238bf | 856 | |
mglmx | 30:293ee760d357 | 857 | DCC_send_command(SWBaddress,SWBidle,times); //Sending IDLE to flip back. |
mglmx | 30:293ee760d357 | 858 | } |
mglmx | 30:293ee760d357 | 859 | } |
mglmx | 21:e6f1649add39 | 860 | } |
mglmx | 21:e6f1649add39 | 861 | |
mglmx | 22:e4153ca757dd | 862 | |
carlosperales95 | 24:1d71dd8778c4 | 863 | /** |
carlosperales95 | 24:1d71dd8778c4 | 864 | * |
carlosperales95 | 29:559eb2164488 | 865 | *Checks if any of the switches of the box has been activated. |
carlosperales95 | 29:559eb2164488 | 866 | *Calls necessary function and displays LCD text. |
carlosperales95 | 25:a42a1ed4d8e9 | 867 | * |
carlosperales95 | 24:1d71dd8778c4 | 868 | **/ |
mglmx | 21:e6f1649add39 | 869 | void checkSwitch(){ |
carlosperales95 | 24:1d71dd8778c4 | 870 | |
mglmx | 22:e4153ca757dd | 871 | if(switch1 == 1){ |
carlosperales95 | 24:1d71dd8778c4 | 872 | |
mglmx | 22:e4153ca757dd | 873 | lcd.cls(); |
mglmx | 22:e4153ca757dd | 874 | lcd.printf("Switch 1 ON - SW1"); |
carlosperales95 | 25:a42a1ed4d8e9 | 875 | flipSwitch(1,5); |
mglmx | 22:e4153ca757dd | 876 | }else if(switch2 == 1){ |
carlosperales95 | 24:1d71dd8778c4 | 877 | |
carlosperales95 | 24:1d71dd8778c4 | 878 | lcd.cls(); |
carlosperales95 | 24:1d71dd8778c4 | 879 | lcd.printf("Switch 2 ON - SW2"); |
carlosperales95 | 24:1d71dd8778c4 | 880 | flipSwitch(2,5); |
carlosperales95 | 24:1d71dd8778c4 | 881 | }else if(switch3 == 0){ |
carlosperales95 | 24:1d71dd8778c4 | 882 | |
carlosperales95 | 24:1d71dd8778c4 | 883 | lcd.cls(); |
carlosperales95 | 24:1d71dd8778c4 | 884 | lcd.printf("Switch 3 ON - SW3"); |
carlosperales95 | 45:d589318238bf | 885 | flipSwitch(3,5); |
carlosperales95 | 24:1d71dd8778c4 | 886 | }else if(switch4 == 0){ |
carlosperales95 | 24:1d71dd8778c4 | 887 | |
carlosperales95 | 24:1d71dd8778c4 | 888 | lcd.cls(); |
carlosperales95 | 24:1d71dd8778c4 | 889 | lcd.printf("Switch 4 ON - IDLE"); |
carlosperales95 | 24:1d71dd8778c4 | 890 | flipSwitch(0,5); |
carlosperales95 | 24:1d71dd8778c4 | 891 | } |
mglmx | 22:e4153ca757dd | 892 | } |
mglmx | 22:e4153ca757dd | 893 | |
carlosperales95 | 45:d589318238bf | 894 | |
carlosperales95 | 45:d589318238bf | 895 | /** |
carlosperales95 | 45:d589318238bf | 896 | * |
carlosperales95 | 45:d589318238bf | 897 | * |
carlosperales95 | 45:d589318238bf | 898 | * |
carlosperales95 | 45:d589318238bf | 899 | **/ |
mglmx | 43:346a1f4144cd | 900 | void send_command(){ |
carlosperales95 | 45:d589318238bf | 901 | |
mglmx | 43:346a1f4144cd | 902 | if(DR_run){ |
carlosperales95 | 45:d589318238bf | 903 | |
mglmx | 43:346a1f4144cd | 904 | DCC_send_command(DCCaddressDR,DCCinst_forward,1); // Forward half speed train addres DARK-RED |
mglmx | 43:346a1f4144cd | 905 | }else{ |
carlosperales95 | 45:d589318238bf | 906 | |
mglmx | 43:346a1f4144cd | 907 | DCC_send_command(DCCaddressDR,DCCinst_stop,400); |
mglmx | 43:346a1f4144cd | 908 | } |
mglmx | 43:346a1f4144cd | 909 | |
mglmx | 43:346a1f4144cd | 910 | if(LR_run){ |
carlosperales95 | 45:d589318238bf | 911 | |
mglmx | 43:346a1f4144cd | 912 | DCC_send_command(DCCaddressLR,DCCinst_forward,1); // Forward half speed train addres DARK-RED |
mglmx | 43:346a1f4144cd | 913 | }else{ |
carlosperales95 | 45:d589318238bf | 914 | |
mglmx | 43:346a1f4144cd | 915 | DCC_send_command(DCCaddressLR,DCCinst_stop,400); |
mglmx | 43:346a1f4144cd | 916 | } |
mglmx | 43:346a1f4144cd | 917 | } |
carlosperales95 | 24:1d71dd8778c4 | 918 | |
carlosperales95 | 45:d589318238bf | 919 | |
carlosperales95 | 45:d589318238bf | 920 | |
carlosperales95 | 11:021210c59a95 | 921 | //**************** MAIN PROGRAM FOR DENVER TRAIN ****************// |
carlosperales95 | 11:021210c59a95 | 922 | |
carlosperales95 | 45:d589318238bf | 923 | |
mglmx | 1:0ab26889af9b | 924 | int main() |
mglmx | 1:0ab26889af9b | 925 | { |
carlosperales95 | 25:a42a1ed4d8e9 | 926 | //RISE FOR INTERRUPTS?? NOT WORKING ATM |
carlosperales95 | 25:a42a1ed4d8e9 | 927 | //int0.rise(&interrupt0); |
carlosperales95 | 25:a42a1ed4d8e9 | 928 | //int1.rise(&interrupt1); |
carlosperales95 | 25:a42a1ed4d8e9 | 929 | |
carlosperales95 | 25:a42a1ed4d8e9 | 930 | //Read and display potentiometer |
carlosperales95 | 25:a42a1ed4d8e9 | 931 | //float f = pot.read(); |
carlosperales95 | 25:a42a1ed4d8e9 | 932 | //float vin = f * 3.3; |
carlosperales95 | 25:a42a1ed4d8e9 | 933 | //lcd.printf("vin: %.4f",vin); |
carlosperales95 | 25:a42a1ed4d8e9 | 934 | |
carlosperales95 | 25:a42a1ed4d8e9 | 935 | //0xFFFC //1111111111111100 |
carlosperales95 | 25:a42a1ed4d8e9 | 936 | |
carlosperales95 | 50:ee4398ee44be | 937 | enable = 1; |
carlosperales95 | 25:a42a1ed4d8e9 | 938 | |
carlosperales95 | 25:a42a1ed4d8e9 | 939 | //Led routine to start main program |
mglmx | 2:f580707c44fa | 940 | led1 = 1; |
mglmx | 22:e4153ca757dd | 941 | wait(0.2); |
mglmx | 2:f580707c44fa | 942 | led1 = 0; |
mglmx | 22:e4153ca757dd | 943 | wait(0.2); |
mglmx | 2:f580707c44fa | 944 | led1 = 1; |
mglmx | 16:2a2da0e67793 | 945 | |
carlosperales95 | 25:a42a1ed4d8e9 | 946 | initialize_mcp(); //mcp initialization for interrupts before train running |
mglmx | 33:24ce12dec157 | 947 | init(); |
mglmx | 35:cfcfeccb959e | 948 | init_positions(); |
mglmx | 16:2a2da0e67793 | 949 | |
mglmx | 43:346a1f4144cd | 950 | DR_train.set_position(D4); |
mglmx | 43:346a1f4144cd | 951 | LR_train.set_position(D10); |
mglmx | 43:346a1f4144cd | 952 | |
carlosperales95 | 25:a42a1ed4d8e9 | 953 | //Train light routine to start running |
mglmx | 43:346a1f4144cd | 954 | /* |
mglmx | 32:e5b732fb8e65 | 955 | DCC_send_command(DCCaddressDR,DCC_func_lighton,200); // turn light on full |
mglmx | 32:e5b732fb8e65 | 956 | DCC_send_command(DCCaddressDR,DCC_func_dimlight,400); //dim light |
mglmx | 32:e5b732fb8e65 | 957 | DCC_send_command(DCCaddressDR,DCC_func_lighton,200); //light full again |
mglmx | 43:346a1f4144cd | 958 | */ |
mglmx | 22:e4153ca757dd | 959 | |
carlosperales95 | 25:a42a1ed4d8e9 | 960 | //LED3 Shows start of route + LCD notif |
mglmx | 22:e4153ca757dd | 961 | led3 = 1; // Entering the while |
mglmx | 22:e4153ca757dd | 962 | lcd.cls(); |
mglmx | 22:e4153ca757dd | 963 | lcd.printf("Ready to start"); |
carlosperales95 | 19:ff21ba3a4dc5 | 964 | |
carlosperales95 | 14:7bb998edd819 | 965 | //Demo for stopping at the station |
mglmx | 1:0ab26889af9b | 966 | while(1) { |
mglmx | 4:50879dfb82d5 | 967 | |
carlosperales95 | 25:a42a1ed4d8e9 | 968 | checkSwitch(); //Checks for switch commands everytime. |
carlosperales95 | 25:a42a1ed4d8e9 | 969 | |
mglmx | 33:24ce12dec157 | 970 | if(1==0){ |
mglmx | 33:24ce12dec157 | 971 | //if(station == 1){ //If train is on the sensor at the middle of the station it stops and displays LCD text. |
carlosperales95 | 14:7bb998edd819 | 972 | |
mglmx | 4:50879dfb82d5 | 973 | lcd.cls(); |
carlosperales95 | 29:559eb2164488 | 974 | lcd.printf("All aboard\n mind the gap"); |
mglmx | 32:e5b732fb8e65 | 975 | DCC_send_command(DCCaddressDR,DCCinst_stop,400); |
carlosperales95 | 29:559eb2164488 | 976 | lcd.cls(); |
mglmx | 4:50879dfb82d5 | 977 | |
mglmx | 22:e4153ca757dd | 978 | }else{ |
mglmx | 43:346a1f4144cd | 979 | send_command(); |
carlosperales95 | 25:a42a1ed4d8e9 | 980 | } |
mglmx | 1:0ab26889af9b | 981 | } |
mglmx | 3:fe7010b693a0 | 982 | } |