Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 74:dbbc528f2b18
- Parent:
- 73:1bf7f6a38fde
- Child:
- 75:bae748a7905f
--- a/main.cpp Wed Jun 27 10:02:27 2018 +0000 +++ b/main.cpp Wed Jun 27 11:11:28 2018 +0000 @@ -71,11 +71,9 @@ MCP23017 *mcp; - -//------GLOBAL VARS +//------DEFINITIONS -//......SENSOR POSITION VARS - +//......SENSOR DEFS //Definition of D sensors, will be interpreted as ints for the program's logic #define D0 0 #define D1 1 @@ -94,6 +92,7 @@ #define D21 14 #define D22 15 +//......SPEED DEFS //Definition of the different train speeds, will be interpreted as ints for the program's logic #define STOP 0 #define SLOW 1 @@ -103,66 +102,42 @@ #define R_MEDIUM 5 +//--------DCC SEND COMMANDS -/** -* -*Position class. -* -*@position - -*@previous_cw - -*@previous_ccw - -* -*Position(int) - -* -*get_pos() - -*get_prev_cw() - -*get_ccw() - -*add_prev_cw() - -*add_ccw() - -* -**/ -class Position{ - private: - int position; - vector <int> previous_cw; - vector <int> previous_ccw; - public: - Position(int p){ - position = p; - } - - int get_pos(){ - return position; - } - - vector<int> get_next_cw(){ - return previous_ccw; - } - - vector<int> get_next_ccw(){ - return previous_cw; - } +//01DCSSSS for speed, D is direction (fwd=1 and rev=0), C is speed(SSSSC) LSB +const unsigned int DCCinst_forward = 0x68; //forward half speed +const unsigned int DCCinst_forward_slow = 0x66; //forward slow speed (step 9) +const unsigned int DCCinst_forward_fast = 0x6C; //Forward fast speed (step 22) +const unsigned int DCCinst_forward_full = 0x6F; //Forward full speed +const unsigned int DCCinst_reverse = 0x48; //reverse half speed +const unsigned int DCCinst_stop = 0x50; //stop the train - vector <int> get_prev_cw(){ - return previous_cw; - } - - vector <int> get_prev_ccw(){ - return previous_ccw; - } - - void add_prev_cw(int pos){ - previous_cw.push_back(pos); - }; - - void add_prev_ccw(int pos){ - previous_ccw.push_back(pos); - }; -}; +//100DDDDD for basic headlight functions +const unsigned int DCC_func_lighton = 0x90; //F0 turns on headlight function +const unsigned int DCC_func_dimlight = 0x91; //F0 + F1 dims headlight -//Creating a vector with all the positions. -vector<Position> positions; +//.....SWITCH COMMAND VARS + +const unsigned int SWBaddress = 0x06; //Address for switch box + +//100DDDDD where DDDDD is the switch command and 100 is constant: + +//00001(F1 active)-00010(F2 active)-00100(F3 active)-01000(F4 active) +//Example - 111111 0 00000101 0 10000000 0 10000101 1 - idle +const unsigned int SWBidle = 0x80; //IDLE - Flip last activated SW. +const unsigned int SWBflip_1 = 0x81; //Flip SW1 +const unsigned int SWBflip_2 = 0x82; //Flip SW2 +const unsigned int SWBflip_3 = 0x84; //Flip SW3 +const unsigned int SWBflip_4 = 0x88; //Flip SW4 + + +//.....DCC TRAIN COMMAND VARS + +//typical out of box default engine DCC address is 3 (at least for Bachmann trains) +//Note: A DCC controller can reprogram the address whenever needed +const unsigned int DCCaddressDR = 0x01; //Address for train 1 DARK-RED +const unsigned int DCCaddressLR = 0x03; //Address for train 3 LIGHT-RED /** @@ -220,6 +195,76 @@ } } + + +//------CLASSES + +//......POSITION CLASS + +/** +* +*Position class. +* +*@position - +*@previous_cw - +*@previous_ccw - +* +*Position(int) - +* +*get_pos() - +*get_prev_cw() - +*get_ccw() - +*add_prev_cw() - +*add_ccw() - +* +**/ +class Position{ + + private: + + int position; + vector <int> previous_cw; + vector <int> previous_ccw; + public: + + Position(int p){ + position = p; + } + + int get_pos(){ + return position; + } + + vector<int> get_next_cw(){ + return previous_ccw; + } + + vector<int> get_next_ccw(){ + return previous_cw; + } + + vector <int> get_prev_cw(){ + return previous_cw; + } + + vector <int> get_prev_ccw(){ + return previous_ccw; + } + + void add_prev_cw(int pos){ + previous_cw.push_back(pos); + }; + + void add_prev_ccw(int pos){ + previous_ccw.push_back(pos); + }; +}; + +//......INITS USED FOR CLASS TRAIN + +//Creating a vector with all the positions. +vector<Position> positions; + /** *Defining areas for train detection and collision logic. *area_A_arr/area_B_arr - Arrays that hold the Dsensors for each area, used to initialize the vectors. @@ -232,6 +277,7 @@ const vector<int> area_B(area_B_arr,area_B_arr + sizeof(area_B_arr) / sizeof(int)); +//......TRAIN CLASS /** * @@ -289,6 +335,18 @@ } } + vector<int> get_previous_sensors(){ + + //Checking direction + if(going_cw){ + + return position->get_next_ccw(); + }else{ + + return position->get_next_cw(); + } + } + void set_speed(int s){ speed = s; } @@ -388,6 +446,10 @@ }; +//------GLOBAL VARS AND INITS + +//......POSITIONS INIT + //Creation of all the positions. One for every sensor on the table - Position name(mapping) Position d0(D0); Position d1(D1); @@ -407,40 +469,7 @@ Position d22(D22); -//01DCSSSS for speed, D is direction (fwd=1 and rev=0), C is speed(SSSSC) LSB -const unsigned int DCCinst_forward = 0x68; //forward half speed -const unsigned int DCCinst_forward_slow = 0x66; //forward slow speed (step 9) -const unsigned int DCCinst_forward_fast = 0x6C; //Forward fast speed (step 22) -const unsigned int DCCinst_forward_full = 0x6F; //Forward full speed -const unsigned int DCCinst_reverse = 0x48; //reverse half speed -const unsigned int DCCinst_stop = 0x50; //stop the train - -//100DDDDD for basic headlight functions -const unsigned int DCC_func_lighton = 0x90; //F0 turns on headlight function -const unsigned int DCC_func_dimlight = 0x91; //F0 + F1 dims headlight - - -//.....SWITCH COMMAND VARS - -const unsigned int SWBaddress = 0x06; //Address for switch box - -//100DDDDD where DDDDD is the switch command and 100 is constant: - -//00001(F1 active)-00010(F2 active)-00100(F3 active)-01000(F4 active) -//Example - 111111 0 00000101 0 10000000 0 10000101 1 - idle -const unsigned int SWBidle = 0x80; //IDLE - Flip last activated SW. -const unsigned int SWBflip_1 = 0x81; //Flip SW1 -const unsigned int SWBflip_2 = 0x82; //Flip SW2 -const unsigned int SWBflip_3 = 0x84; //Flip SW3 -const unsigned int SWBflip_4 = 0x88; //Flip SW4 - - -//.....DCC TRAIN COMMAND VARS - -//typical out of box default engine DCC address is 3 (at least for Bachmann trains) -//Note: A DCC controller can reprogram the address whenever needed -const unsigned int DCCaddressDR = 0x01; //Address for train 1 DARK-RED -const unsigned int DCCaddressLR = 0x03; //Address for train 3 LIGHT-RED +//......TRAINS INIT /** *Creation of 2 Train objects. @@ -450,10 +479,15 @@ Train DR_train(DCCaddressDR,MEDIUM); Train LR_train(DCCaddressLR,MEDIUM); + +//......FLAGS INIT + //possibility of an array having {dr_train, lr_train}? for reuse and modularity of functions int speedcheck = 0; +bool station_stop = false; + //**************** FUNCTIONS FOR DENVER TRAIN ****************// @@ -967,13 +1001,49 @@ /** * +* +* +**/ +void stay_at_distance(Train* train_front, Train* train_back){ + + led1 = 0; + led2 = 0; + led3 = 0; + + led1 = 1; + for(int i=0; i<train_back->get_next_sensors().size(); i++){ + + led2 = 1; + for(int j=0; j<train_front->get_previous_sensors().size(); j++){ + + while(train_back->get_next_sensors()[i] == train_front->get_previous_sensors()[j]){ + + led3 = 1; + //stop back train + train_back->set_speed(STOP); + } + } + } + train_back->set_speed(MEDIUM); + led1 = 0; + led2 = 0; + led3 = 0; +} + + + +/** +* *The method check_position will check if any of the trains is in any of the areas. *It will go through all the area vectors (A,B) and call the function in_vector to check inside the vectors. * **/ void check_position(){ - - if(check_NAC()){NAC_action();} + + stay_at_distance(&DR_train,&LR_train); + stay_at_distance(&LR_train,&DR_train); + + if(check_NAC()){ NAC_action(); } check_AFC(&DR_train,&LR_train); check_AFC(&LR_train,&DR_train); @@ -1079,7 +1149,13 @@ LR_train.set_goes_cw(true); //If train goes ccw and passes D9 or D3 we change orientation } } - LR_train.set_position(sensor); + LR_train.set_position(sensor); + + if(sensor == D2 && station_stop){ + + DR_train.set_speed(STOP); + LR_train.set_speed(STOP); + } } } } @@ -1142,7 +1218,14 @@ //lcd.cls(); //lcd.printf("TRAIN NOW WILL STOP AT STATION"); - }else if(switch2 == 1){ + station_stop = true; + }else{ + + station_stop = false; + LR_train.set_speed(MEDIUM); + DR_train.set_speed(MEDIUM); + } + if(switch2 == 1){ //lcd.cls(); //lcd.printf("SPEEDCHECKMODE"); @@ -1173,8 +1256,6 @@ } - - /** * * Returns a sensor number depending on how many times switch3 flips. @@ -1385,6 +1466,7 @@ //Demo for stopping at the station while(1) { + checkSwitch(); //Checks for switch commands everytime. switch_toSpeed(); lcd.cls();