Denver trai project

Dependencies:   mbed TextLCD

Committer:
mglmx
Date:
Tue Jun 19 10:22:37 2018 +0000
Revision:
48:553716e13d45
Parent:
47:e992a129ef44
Child:
49:880c0b9c9c64
Train detection should be working.

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