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