Denver / Mbed 2 deprecated denver_train_proj

Dependencies:   mbed TextLCD

Committer:
mglmx
Date:
Mon Jun 18 13:51:39 2018 +0000
Revision:
44:94870081aece
Parent:
43:346a1f4144cd
Parent:
42:b445252a772a
Child:
45:d589318238bf
Merged (replaced) with previous commit. Sensors should be detected(not tested) and NAC and AFC implemented(needs to add same logic for LR train)

Who changed what in which revision?

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