Denver trai project

Dependencies:   mbed TextLCD

Committer:
carlosperales95
Date:
Thu Jun 14 10:32:54 2018 +0000
Revision:
37:bb15bea420a3
Parent:
35:cfcfeccb959e
Child:
39:9cce8f2c50b6
Comment fix and delete of unused function

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 //mbed DCC Model Train Demo
mglmx 1:0ab26889af9b 11
carlosperales95 7:e2b8461d4f05 12 /******PINS AND DECLARATIONS*******/
carlosperales95 7:e2b8461d4f05 13
carlosperales95 25:a42a1ed4d8e9 14 //------PINS
mglmx 21:e6f1649add39 15
mglmx 21:e6f1649add39 16 //SWITCHES p5 - p8
mglmx 21:e6f1649add39 17 DigitalIn switch1(p5);
mglmx 21:e6f1649add39 18 DigitalIn switch2(p6);
mglmx 21:e6f1649add39 19 DigitalIn switch3(p7);
mglmx 21:e6f1649add39 20 DigitalIn switch4(p8);
carlosperales95 7:e2b8461d4f05 21
carlosperales95 10:2088b1935a93 22 //RAIL SENSORS - INT0,INT1
carlosperales95 7:e2b8461d4f05 23 //INT0 - p9
mglmx 18:aa43bb62e60f 24 InterruptIn int0(p9);
carlosperales95 7:e2b8461d4f05 25 //INT1 - p10
mglmx 18:aa43bb62e60f 26 InterruptIn int1(p10);
carlosperales95 7:e2b8461d4f05 27
carlosperales95 7:e2b8461d4f05 28 ///p11
carlosperales95 7:e2b8461d4f05 29 ///p12
carlosperales95 12:e914ca5cd44b 30
carlosperales95 7:e2b8461d4f05 31 //M0 - p13
mglmx 35:cfcfeccb959e 32 DigitalIn d21stat(p13); //Sensor right of the station
carlosperales95 7:e2b8461d4f05 33 //M1 - p14
mglmx 35:cfcfeccb959e 34 DigitalIn d22stat(p14); //Sensor left of the station
carlosperales95 7:e2b8461d4f05 35 //M2 - p15
carlosperales95 29:559eb2164488 36 DigitalIn station(p15); //Sensor in the middle of the station
carlosperales95 12:e914ca5cd44b 37
carlosperales95 7:e2b8461d4f05 38 //p16
carlosperales95 12:e914ca5cd44b 39 //p17
carlosperales95 12:e914ca5cd44b 40
carlosperales95 11:021210c59a95 41 //BUZZER - p18
carlosperales95 11:021210c59a95 42 DigitalOut buzz(p18); // buzz=0 doesn't beep, buzz=1 beeps
carlosperales95 7:e2b8461d4f05 43
carlosperales95 7:e2b8461d4f05 44 //POTENTIOMETER - p19
carlosperales95 13:dbf1ead12cee 45 AnalogIn pot(p19); //Gives float value pot.read(). Convert analog input to V with f*3.3
carlosperales95 7:e2b8461d4f05 46
carlosperales95 7:e2b8461d4f05 47 //DAT - p20
mglmx 3:fe7010b693a0 48 DigitalOut Track(p20); //Digital output bit used to drive track power via H-bridge
carlosperales95 7:e2b8461d4f05 49
carlosperales95 7:e2b8461d4f05 50 //LCD SCREEN - p21, p22, p23, p24, p25, p26
carlosperales95 11:021210c59a95 51 TextLCD lcd(p22,p21,p23,p24,p25,p26); // RS, E, A4, A5, A6, A7 // ldc.cls() to clear and printf(String up to 16char)
carlosperales95 7:e2b8461d4f05 52
carlosperales95 7:e2b8461d4f05 53 ///p27
carlosperales95 7:e2b8461d4f05 54 ///p28
mglmx 33:24ce12dec157 55 I2C i2c(p28,p27);
carlosperales95 7:e2b8461d4f05 56
carlosperales95 7:e2b8461d4f05 57 //LED1 - p29
mglmx 3:fe7010b693a0 58 DigitalOut redled(p29);
carlosperales95 7:e2b8461d4f05 59 //LED2 - p30
mglmx 3:fe7010b693a0 60 DigitalOut greenled(p30);
carlosperales95 7:e2b8461d4f05 61
carlosperales95 24:1d71dd8778c4 62 //MBED LEDS
mglmx 18:aa43bb62e60f 63 DigitalOut led1(LED1);
mglmx 18:aa43bb62e60f 64 DigitalOut led2(LED2);
mglmx 18:aa43bb62e60f 65 DigitalOut led3(LED3);
mglmx 16:2a2da0e67793 66
mglmx 16:2a2da0e67793 67 //MCP
mglmx 16:2a2da0e67793 68 MCP23017 *mcp;
carlosperales95 12:e914ca5cd44b 69
carlosperales95 24:1d71dd8778c4 70
carlosperales95 25:a42a1ed4d8e9 71 //------GLOBAL VARS
carlosperales95 25:a42a1ed4d8e9 72
mglmx 34:c9ab2a987734 73 #define D0 0
mglmx 34:c9ab2a987734 74 #define D1 1
mglmx 34:c9ab2a987734 75 #define D2 2
mglmx 34:c9ab2a987734 76 #define D3 3
mglmx 34:c9ab2a987734 77 #define D4 4
mglmx 34:c9ab2a987734 78 #define D5 5
mglmx 34:c9ab2a987734 79 #define D6 6
mglmx 34:c9ab2a987734 80 #define D7 7
mglmx 34:c9ab2a987734 81 #define D8 8
mglmx 34:c9ab2a987734 82 #define D9 9
mglmx 34:c9ab2a987734 83 #define D10 10
mglmx 34:c9ab2a987734 84 #define D11 11
mglmx 34:c9ab2a987734 85 #define D12 12
mglmx 34:c9ab2a987734 86 #define D13 13
mglmx 34:c9ab2a987734 87 #define D21 14
mglmx 34:c9ab2a987734 88 #define D22 15
mglmx 34:c9ab2a987734 89
carlosperales95 25:a42a1ed4d8e9 90
carlosperales95 25:a42a1ed4d8e9 91 //.....DCC TRAIN COMMAND VARS
carlosperales95 25:a42a1ed4d8e9 92
carlosperales95 25:a42a1ed4d8e9 93 //typical out of box default engine DCC address is 3 (at least for Bachmann trains)
carlosperales95 25:a42a1ed4d8e9 94 //Note: A DCC controller can reprogram the address whenever needed
mglmx 32:e5b732fb8e65 95 const unsigned int DCCaddressDR = 0x01; //Address for train 1 DARK-RED
mglmx 32:e5b732fb8e65 96 const unsigned int DCCaddressLR = 0x03; //Address for train 3 LIGHT-RED
carlosperales95 25:a42a1ed4d8e9 97
carlosperales95 25:a42a1ed4d8e9 98 //01DCSSSS for speed, D is direction (fwd=1 and rev=0), C is speed(SSSSC) LSB
carlosperales95 25:a42a1ed4d8e9 99 const unsigned int DCCinst_forward = 0x68; //forward half speed
mglmx 33:24ce12dec157 100 const unsigned int DCCinst_forward_slow = 0x66; //forward half speed
carlosperales95 25:a42a1ed4d8e9 101 const unsigned int DCCinst_reverse = 0x48; //reverse half speed
carlosperales95 25:a42a1ed4d8e9 102 const unsigned int DCCinst_stop = 0x50; //stop the train
carlosperales95 25:a42a1ed4d8e9 103
carlosperales95 25:a42a1ed4d8e9 104 //100DDDDD for basic headlight functions
carlosperales95 25:a42a1ed4d8e9 105 const unsigned int DCC_func_lighton = 0x90; //F0 turns on headlight function
carlosperales95 25:a42a1ed4d8e9 106 const unsigned int DCC_func_dimlight = 0x91; //F0 + F1 dims headlight
carlosperales95 25:a42a1ed4d8e9 107
carlosperales95 25:a42a1ed4d8e9 108
carlosperales95 25:a42a1ed4d8e9 109 //.....SWITCH COMMAND VARS
carlosperales95 25:a42a1ed4d8e9 110
carlosperales95 25:a42a1ed4d8e9 111 const unsigned int SWBaddress = 0x06; //Address for switch box
carlosperales95 25:a42a1ed4d8e9 112
carlosperales95 25:a42a1ed4d8e9 113 //100DDDDD where DDDDD is the switch command and 100 is constant:
carlosperales95 25:a42a1ed4d8e9 114
carlosperales95 25:a42a1ed4d8e9 115 //00001(F1 active)-00010(F2 active)-00100(F3 active)-01000(F4 active)
carlosperales95 25:a42a1ed4d8e9 116 //Example - 111111 0 00000101 0 10000000 0 10000101 1 - idle
carlosperales95 25:a42a1ed4d8e9 117
carlosperales95 25:a42a1ed4d8e9 118 const unsigned int SWBidle = 0x80; //IDLE - Flip last activated SW.
carlosperales95 25:a42a1ed4d8e9 119 const unsigned int SWBflip_1 = 0x81; //Flip SW1
carlosperales95 25:a42a1ed4d8e9 120 const unsigned int SWBflip_2 = 0x82; //Flip SW2
carlosperales95 25:a42a1ed4d8e9 121 const unsigned int SWBflip_3 = 0x84; //Flip SW3
carlosperales95 25:a42a1ed4d8e9 122 const unsigned int SWBflip_4 = 0x88; //Flip SW4
carlosperales95 25:a42a1ed4d8e9 123
carlosperales95 31:7637861f4dc7 124 //Arrays that will keep track of the position and direction of th trains.
carlosperales95 31:7637861f4dc7 125 //[pos_trains] -1 = not in track / any other number = sensor where the train is at (sensors 1-16)
carlosperales95 31:7637861f4dc7 126 //[dir_trains] -1 = left / 1 = right
carlosperales95 27:f51397917a52 127 int pos_trains [2] = {-1,-1};
carlosperales95 31:7637861f4dc7 128 int dir_trains [2] = {-1,-1};
carlosperales95 31:7637861f4dc7 129
carlosperales95 31:7637861f4dc7 130
carlosperales95 25:a42a1ed4d8e9 131
carlosperales95 25:a42a1ed4d8e9 132
carlosperales95 24:1d71dd8778c4 133
carlosperales95 11:021210c59a95 134 //**************** FUNCTIONS FOR DENVER TRAIN ****************//
carlosperales95 10:2088b1935a93 135
mglmx 35:cfcfeccb959e 136 class Position{
mglmx 35:cfcfeccb959e 137 private:
mglmx 35:cfcfeccb959e 138 int position;
mglmx 35:cfcfeccb959e 139 vector <int> previous_cw;
mglmx 35:cfcfeccb959e 140 vector <int> previous_ccw;
mglmx 35:cfcfeccb959e 141 public:
mglmx 35:cfcfeccb959e 142 Position(int p){
mglmx 35:cfcfeccb959e 143 position = p;
mglmx 35:cfcfeccb959e 144 }
mglmx 35:cfcfeccb959e 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
mglmx 35:cfcfeccb959e 163 Position d0(D0);
mglmx 35:cfcfeccb959e 164 Position d1(D1);
mglmx 35:cfcfeccb959e 165 Position d2(D2);
mglmx 35:cfcfeccb959e 166 Position d3(D3);
mglmx 35:cfcfeccb959e 167 Position d4(D4);
mglmx 35:cfcfeccb959e 168 Position d5(D5);
mglmx 35:cfcfeccb959e 169 Position d6(D6);
mglmx 35:cfcfeccb959e 170 Position d7(D7);
mglmx 35:cfcfeccb959e 171 Position d8(D8);
mglmx 35:cfcfeccb959e 172 Position d9(D9);
mglmx 35:cfcfeccb959e 173 Position d10(D10);
mglmx 35:cfcfeccb959e 174 Position d11(D11);
mglmx 35:cfcfeccb959e 175 Position d12(D12);
mglmx 35:cfcfeccb959e 176 Position d13(D13);
mglmx 35:cfcfeccb959e 177 Position d21(D21);
mglmx 35:cfcfeccb959e 178 Position d22(D22);
mglmx 35:cfcfeccb959e 179
mglmx 35:cfcfeccb959e 180 vector<Position> positions;
mglmx 35:cfcfeccb959e 181
carlosperales95 37:bb15bea420a3 182
mglmx 35:cfcfeccb959e 183 //Starting position and orientation of the trains
mglmx 35:cfcfeccb959e 184 int DR_train_pos = D4;
mglmx 35:cfcfeccb959e 185 bool DR_cw = true;
mglmx 35:cfcfeccb959e 186
mglmx 35:cfcfeccb959e 187 int LR_train_pos= D10;
mglmx 35:cfcfeccb959e 188 bool LR_cw = true;
mglmx 35:cfcfeccb959e 189
mglmx 18:aa43bb62e60f 190
mglmx 22:e4153ca757dd 191 /**
carlosperales95 25:a42a1ed4d8e9 192 *Activates the buzzer for 0.5 seconds.
mglmx 22:e4153ca757dd 193 **/
mglmx 22:e4153ca757dd 194 void doBuzz(){
carlosperales95 25:a42a1ed4d8e9 195
mglmx 22:e4153ca757dd 196 buzz = 1;
mglmx 22:e4153ca757dd 197 wait(0.5);
mglmx 22:e4153ca757dd 198 buzz = 0;
mglmx 22:e4153ca757dd 199 }
mglmx 18:aa43bb62e60f 200
mglmx 35:cfcfeccb959e 201 void init_positions(){
mglmx 35:cfcfeccb959e 202 //Initialize array with positions
mglmx 35:cfcfeccb959e 203 positions.push_back(d0);
mglmx 35:cfcfeccb959e 204 positions.push_back(d1);
mglmx 35:cfcfeccb959e 205 positions.push_back(d2);
mglmx 35:cfcfeccb959e 206 positions.push_back(d3);
mglmx 35:cfcfeccb959e 207 positions.push_back(d4);
mglmx 35:cfcfeccb959e 208 positions.push_back(d5);
mglmx 35:cfcfeccb959e 209 positions.push_back(d6);
mglmx 35:cfcfeccb959e 210 positions.push_back(d7);
mglmx 35:cfcfeccb959e 211 positions.push_back(d8);
mglmx 35:cfcfeccb959e 212 positions.push_back(d9);
mglmx 35:cfcfeccb959e 213 positions.push_back(d10);
mglmx 35:cfcfeccb959e 214 positions.push_back(d11);
mglmx 35:cfcfeccb959e 215 positions.push_back(d12);
mglmx 35:cfcfeccb959e 216 positions.push_back(d13);
mglmx 35:cfcfeccb959e 217 positions.push_back(d21);
mglmx 35:cfcfeccb959e 218 positions.push_back(d22);
mglmx 35:cfcfeccb959e 219
mglmx 35:cfcfeccb959e 220
mglmx 35:cfcfeccb959e 221 d0.add_prev_cw(D1);
mglmx 35:cfcfeccb959e 222 d0.add_prev_ccw(D13);
mglmx 35:cfcfeccb959e 223
mglmx 35:cfcfeccb959e 224 d1.add_prev_cw(D22);
mglmx 35:cfcfeccb959e 225 d1.add_prev_ccw(D0);
mglmx 35:cfcfeccb959e 226
mglmx 35:cfcfeccb959e 227 d22.add_prev_cw(D2);
mglmx 35:cfcfeccb959e 228 d22.add_prev_ccw(D1);
mglmx 35:cfcfeccb959e 229
mglmx 35:cfcfeccb959e 230 d2.add_prev_cw(D21);
mglmx 35:cfcfeccb959e 231 d2.add_prev_ccw(D22);
mglmx 35:cfcfeccb959e 232
mglmx 35:cfcfeccb959e 233 d21.add_prev_cw(D3);
mglmx 35:cfcfeccb959e 234 d21.add_prev_cw(D4);
mglmx 35:cfcfeccb959e 235 d21.add_prev_ccw(D2);
mglmx 35:cfcfeccb959e 236
mglmx 35:cfcfeccb959e 237 d3.add_prev_cw(D9);
mglmx 35:cfcfeccb959e 238 d3.add_prev_ccw(D21);
mglmx 35:cfcfeccb959e 239
mglmx 35:cfcfeccb959e 240 d4.add_prev_cw(D6);
mglmx 35:cfcfeccb959e 241 d4.add_prev_ccw(D21);
mglmx 35:cfcfeccb959e 242
mglmx 35:cfcfeccb959e 243 d5.add_prev_cw(D6);
mglmx 35:cfcfeccb959e 244 d5.add_prev_ccw(D11);
mglmx 35:cfcfeccb959e 245
mglmx 35:cfcfeccb959e 246 d6.add_prev_cw(D7);
mglmx 35:cfcfeccb959e 247 d6.add_prev_ccw(D4);
mglmx 35:cfcfeccb959e 248 d6.add_prev_ccw(D5);
mglmx 35:cfcfeccb959e 249
mglmx 35:cfcfeccb959e 250 d7.add_prev_cw(D8);
mglmx 35:cfcfeccb959e 251 d7.add_prev_ccw(D6);
mglmx 35:cfcfeccb959e 252
mglmx 35:cfcfeccb959e 253 d8.add_prev_cw(D7);
mglmx 35:cfcfeccb959e 254 d8.add_prev_ccw(D9);
mglmx 35:cfcfeccb959e 255 d8.add_prev_ccw(D10);
mglmx 35:cfcfeccb959e 256
mglmx 35:cfcfeccb959e 257 d9.add_prev_cw(D3);
mglmx 35:cfcfeccb959e 258 d9.add_prev_ccw(D8);
mglmx 35:cfcfeccb959e 259
mglmx 35:cfcfeccb959e 260 d10.add_prev_cw(D12);
mglmx 35:cfcfeccb959e 261 d10.add_prev_ccw(D8);
mglmx 35:cfcfeccb959e 262
mglmx 35:cfcfeccb959e 263 d11.add_prev_cw(D12);
mglmx 35:cfcfeccb959e 264 d11.add_prev_ccw(D5);
mglmx 35:cfcfeccb959e 265
mglmx 35:cfcfeccb959e 266 d12.add_prev_cw(D13);
mglmx 35:cfcfeccb959e 267 d12.add_prev_ccw(D10);
mglmx 35:cfcfeccb959e 268 d12.add_prev_ccw(D11);
mglmx 35:cfcfeccb959e 269
mglmx 35:cfcfeccb959e 270 d13.add_prev_cw(D0);
mglmx 35:cfcfeccb959e 271 d13.add_prev_ccw(D12);
mglmx 35:cfcfeccb959e 272 }
carlosperales95 24:1d71dd8778c4 273
mglmx 22:e4153ca757dd 274 /**
carlosperales95 24:1d71dd8778c4 275 *
carlosperales95 28:71bd4c83c05f 276 *Here we initialize the mcp that will be used to manage the interrupts.
carlosperales95 25:a42a1ed4d8e9 277 *
carlosperales95 24:1d71dd8778c4 278 **/
mglmx 18:aa43bb62e60f 279 void initialize_mcp(){
mglmx 33:24ce12dec157 280 mcp = new MCP23017(i2c,0x40); //Connect to SCL - p28 and SDA - p27 and MPC I2C address 0x40
mglmx 18:aa43bb62e60f 281
mglmx 33:24ce12dec157 282 mcp->_write(IODIRA, (unsigned char )0xff);
mglmx 33:24ce12dec157 283 mcp->_write(IODIRB, (unsigned char )0xff);
mglmx 33:24ce12dec157 284 mcp->_write(IPOLA, (unsigned char )0x00);
mglmx 33:24ce12dec157 285 mcp->_write(IPOLB, (unsigned char )0x00);
mglmx 33:24ce12dec157 286 mcp->_write(DEFVALA, (unsigned char )0xff);
mglmx 33:24ce12dec157 287 mcp->_write(DEFVALB, (unsigned char )0xff);
mglmx 33:24ce12dec157 288 mcp->_write(INTCONA, (unsigned char )0xff);
mglmx 33:24ce12dec157 289 mcp->_write(INTCONB, (unsigned char )0xff);
mglmx 33:24ce12dec157 290 mcp->_write(IOCONA, (unsigned char )0x2);
mglmx 33:24ce12dec157 291 mcp->_write(IOCONB, (unsigned char )0x2);
mglmx 33:24ce12dec157 292 mcp->_write(GPPUA, (unsigned char )0xff);
mglmx 33:24ce12dec157 293 mcp->_write(GPPUB, (unsigned char )0xff);
mglmx 33:24ce12dec157 294
mglmx 18:aa43bb62e60f 295 }
mglmx 18:aa43bb62e60f 296
carlosperales95 24:1d71dd8778c4 297 /**
carlosperales95 24:1d71dd8778c4 298 *
carlosperales95 37:bb15bea420a3 299 *Returns the number of the sensor where the train was detected.
carlosperales95 37:bb15bea420a3 300 *
carlosperales95 37:bb15bea420a3 301 **/
mglmx 34:c9ab2a987734 302 int get_sensor(unsigned int number,int interrupt){
mglmx 34:c9ab2a987734 303 int sensor = -1;
mglmx 34:c9ab2a987734 304
carlosperales95 37:bb15bea420a3 305 for(int i=0; i<8; i++){
mglmx 34:c9ab2a987734 306
mglmx 34:c9ab2a987734 307 if(~number & 1<<i){
mglmx 34:c9ab2a987734 308 sensor = i;
mglmx 34:c9ab2a987734 309 }
mglmx 34:c9ab2a987734 310 }
mglmx 34:c9ab2a987734 311
mglmx 34:c9ab2a987734 312 if(interrupt == 1){
carlosperales95 37:bb15bea420a3 313 sensor+= 8; // Sensors caught by interreupt1 are identified from 8 to 15.
mglmx 34:c9ab2a987734 314 }
mglmx 34:c9ab2a987734 315 return sensor;
mglmx 34:c9ab2a987734 316 }
mglmx 34:c9ab2a987734 317
mglmx 35:cfcfeccb959e 318 void update_train_pos(int sensor){
mglmx 35:cfcfeccb959e 319 bool found_DR = false;
mglmx 35:cfcfeccb959e 320 bool found_LR = false;
mglmx 35:cfcfeccb959e 321
carlosperales95 37:bb15bea420a3 322 Position pos = positions[sensor];
carlosperales95 37:bb15bea420a3 323 for(int i = 0; i<pos.get_prev_cw().size();i++){
mglmx 35:cfcfeccb959e 324 int prev = pos.get_prev_cw()[i];
carlosperales95 37:bb15bea420a3 325
mglmx 35:cfcfeccb959e 326 if(DR_train_pos == prev){
mglmx 35:cfcfeccb959e 327 found_DR = true;
mglmx 35:cfcfeccb959e 328 DR_train_pos = prev; //We update the position of the train
mglmx 35:cfcfeccb959e 329 }
carlosperales95 37:bb15bea420a3 330
mglmx 35:cfcfeccb959e 331 if(LR_train_pos == prev){
mglmx 35:cfcfeccb959e 332 found_LR = true;
mglmx 35:cfcfeccb959e 333 LR_train_pos = prev; //We update the position of the train
mglmx 35:cfcfeccb959e 334 }
mglmx 35:cfcfeccb959e 335 }
mglmx 35:cfcfeccb959e 336
mglmx 35:cfcfeccb959e 337 if(found_DR){
mglmx 35:cfcfeccb959e 338 lcd.cls();
mglmx 35:cfcfeccb959e 339 lcd.printf("DR is at D%d",DR_train_pos);
mglmx 35:cfcfeccb959e 340 }
mglmx 35:cfcfeccb959e 341 if(found_LR){
carlosperales95 37:bb15bea420a3 342 lcd.cls();
mglmx 35:cfcfeccb959e 343 lcd.printf("LR is at D%d",LR_train_pos);
mglmx 35:cfcfeccb959e 344 }
mglmx 35:cfcfeccb959e 345 if(!found_DR && !found_LR){
mglmx 35:cfcfeccb959e 346 lcd.cls();
mglmx 35:cfcfeccb959e 347 lcd.printf("No train before :(");
mglmx 35:cfcfeccb959e 348 }
mglmx 35:cfcfeccb959e 349 }
mglmx 35:cfcfeccb959e 350
carlosperales95 37:bb15bea420a3 351
mglmx 34:c9ab2a987734 352 /**
mglmx 34:c9ab2a987734 353 *
carlosperales95 25:a42a1ed4d8e9 354 *Method to catch interrupts 0
carlosperales95 25:a42a1ed4d8e9 355 *
carlosperales95 24:1d71dd8778c4 356 **/
mglmx 33:24ce12dec157 357 void on_int0_change(){
carlosperales95 25:a42a1ed4d8e9 358
mglmx 33:24ce12dec157 359 wait_us(2000);
mglmx 33:24ce12dec157 360 int sensor_data = mcp->_read(INTCAPA);
mglmx 34:c9ab2a987734 361 int sensor = get_sensor(sensor_data,0);
mglmx 17:0a657e338356 362 lcd.cls();
mglmx 34:c9ab2a987734 363 lcd.printf("int0 0x%x \n Sensor: %d",sensor_data,sensor);
mglmx 22:e4153ca757dd 364
mglmx 35:cfcfeccb959e 365 update_train_pos(sensor);
mglmx 16:2a2da0e67793 366 }
mglmx 16:2a2da0e67793 367
mglmx 35:cfcfeccb959e 368
mglmx 35:cfcfeccb959e 369
carlosperales95 24:1d71dd8778c4 370 /**
carlosperales95 24:1d71dd8778c4 371 *
carlosperales95 25:a42a1ed4d8e9 372 *Method to catch interrupts 1
carlosperales95 25:a42a1ed4d8e9 373 *
carlosperales95 24:1d71dd8778c4 374 **/
mglmx 33:24ce12dec157 375 void on_int1_change(){
carlosperales95 25:a42a1ed4d8e9 376
mglmx 33:24ce12dec157 377 wait_us(2000);
mglmx 33:24ce12dec157 378 int sensor_data = mcp->_read(INTCAPB);
mglmx 34:c9ab2a987734 379 int sensor = get_sensor(sensor_data,1);
mglmx 33:24ce12dec157 380 lcd.cls();
mglmx 34:c9ab2a987734 381 lcd.printf("int1 0x%x \n Sensor: %d",sensor_data,sensor);
mglmx 35:cfcfeccb959e 382
mglmx 35:cfcfeccb959e 383 update_train_pos(sensor);
mglmx 16:2a2da0e67793 384 }
mglmx 26:5c966a0a3e8e 385
mglmx 26:5c966a0a3e8e 386
mglmx 33:24ce12dec157 387 void init() { // Clear current interrupts
mglmx 33:24ce12dec157 388 mcp->_read(GPIOA);
mglmx 33:24ce12dec157 389 mcp->_read(GPIOB); // Register callbacks
mglmx 33:24ce12dec157 390 int0.fall(&on_int0_change);
mglmx 33:24ce12dec157 391 int1.fall(&on_int1_change); // Enable interrupts on MCP
mglmx 33:24ce12dec157 392 mcp->_write(GPINTENA, (unsigned char )0xff);
mglmx 33:24ce12dec157 393 mcp->_write(GPINTENB, (unsigned char )0xff); // Ready to go!
mglmx 33:24ce12dec157 394 }
mglmx 33:24ce12dec157 395
mglmx 33:24ce12dec157 396
carlosperales95 24:1d71dd8778c4 397 /**
carlosperales95 24:1d71dd8778c4 398 *
carlosperales95 25:a42a1ed4d8e9 399 *Method to send DCC commands to train and switches.
carlosperales95 25:a42a1ed4d8e9 400 *
carlosperales95 25:a42a1ed4d8e9 401 *@address - (HEX)Address where the commands will be sent
carlosperales95 25:a42a1ed4d8e9 402 *@inst - (HEX)Number of instruction that will be commanded
carlosperales95 25:a42a1ed4d8e9 403 *@repeat_count - Number of times the command will be sent
carlosperales95 25:a42a1ed4d8e9 404 *
carlosperales95 24:1d71dd8778c4 405 **/
mglmx 1:0ab26889af9b 406 void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count)
mglmx 1:0ab26889af9b 407 {
mglmx 1:0ab26889af9b 408 unsigned __int64 command = 0x0000000000000000; // __int64 is the 64-bit integer type
mglmx 1:0ab26889af9b 409 unsigned __int64 temp_command = 0x0000000000000000;
mglmx 1:0ab26889af9b 410 unsigned __int64 prefix = 0x3FFF; // 14 "1" bits needed at start
mglmx 1:0ab26889af9b 411 unsigned int error = 0x00; //error byte
carlosperales95 24:1d71dd8778c4 412
mglmx 1:0ab26889af9b 413 //calculate error detection byte with xor
mglmx 1:0ab26889af9b 414 error = address ^ inst;
carlosperales95 24:1d71dd8778c4 415
mglmx 1:0ab26889af9b 416 //combine packet bits in basic DCC format
mglmx 1:0ab26889af9b 417 command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01;
mglmx 1:0ab26889af9b 418 //printf("\n\r %llx \n\r",command);
carlosperales95 24:1d71dd8778c4 419
mglmx 1:0ab26889af9b 420 int i=0;
carlosperales95 24:1d71dd8778c4 421 //repeat DCC command lots of times
mglmx 1:0ab26889af9b 422 while(i < repeat_count) {
carlosperales95 24:1d71dd8778c4 423
mglmx 1:0ab26889af9b 424 temp_command = command;
carlosperales95 24:1d71dd8778c4 425 //loops through packet bits encoding and sending out digital pulses for a DCC command
mglmx 1:0ab26889af9b 426 for (int j=0; j<64; j++) {
carlosperales95 24:1d71dd8778c4 427
carlosperales95 24:1d71dd8778c4 428 if((temp_command&0x8000000000000000)==0) {
carlosperales95 24:1d71dd8778c4 429 //test packet bit
mglmx 1:0ab26889af9b 430 //send data for a "0" bit
mglmx 1:0ab26889af9b 431 Track=0;
mglmx 1:0ab26889af9b 432 wait_us(100);
mglmx 1:0ab26889af9b 433 Track=1;
mglmx 1:0ab26889af9b 434 wait_us(100);
mglmx 1:0ab26889af9b 435 //printf("0011");
carlosperales95 24:1d71dd8778c4 436
carlosperales95 24:1d71dd8778c4 437 }else{
carlosperales95 24:1d71dd8778c4 438
mglmx 1:0ab26889af9b 439 //send data for a "1"bit
mglmx 1:0ab26889af9b 440 Track=0;
mglmx 1:0ab26889af9b 441 wait_us(58);
mglmx 1:0ab26889af9b 442 Track=1;
mglmx 1:0ab26889af9b 443 wait_us(58);
mglmx 1:0ab26889af9b 444 //printf("01");
mglmx 1:0ab26889af9b 445 }
mglmx 1:0ab26889af9b 446 // next bit in packet
mglmx 1:0ab26889af9b 447 temp_command = temp_command<<1;
mglmx 1:0ab26889af9b 448 }
mglmx 1:0ab26889af9b 449 i++;
mglmx 0:4d06a6a8e785 450 }
mglmx 0:4d06a6a8e785 451 }
carlosperales95 11:021210c59a95 452
carlosperales95 24:1d71dd8778c4 453
carlosperales95 24:1d71dd8778c4 454 /**
carlosperales95 24:1d71dd8778c4 455 *
carlosperales95 25:a42a1ed4d8e9 456 *Method to flip the switches
carlosperales95 25:a42a1ed4d8e9 457 *
carlosperales95 25:a42a1ed4d8e9 458 *@switchId - (1-4)The ID of the switch we want to flip
carlosperales95 25:a42a1ed4d8e9 459 *@times - The number of times we want to send the command
mglmx 30:293ee760d357 460 *@activate - True if the switch is going to be activated. False if it needs to go back to rest position.
carlosperales95 25:a42a1ed4d8e9 461 *
carlosperales95 24:1d71dd8778c4 462 **/
mglmx 30:293ee760d357 463 void flipSwitch(int switchId, int times, bool activate=true){
mglmx 21:e6f1649add39 464
carlosperales95 25:a42a1ed4d8e9 465 unsigned int SWBflip = SWBidle; //IDLE - Flip last activated SW.
mglmx 21:e6f1649add39 466
mglmx 21:e6f1649add39 467 switch(switchId){
mglmx 21:e6f1649add39 468 case 1:
carlosperales95 25:a42a1ed4d8e9 469 SWBflip = SWBflip_1; //FLIP SW1
mglmx 21:e6f1649add39 470 break;
mglmx 21:e6f1649add39 471 case 2:
carlosperales95 25:a42a1ed4d8e9 472 SWBflip = SWBflip_2; //FLIP SW2
mglmx 21:e6f1649add39 473 break;
mglmx 21:e6f1649add39 474 case 3:
carlosperales95 25:a42a1ed4d8e9 475 SWBflip = SWBflip_3; //FLIP SW3
mglmx 21:e6f1649add39 476 break;
mglmx 21:e6f1649add39 477 case 4:
carlosperales95 25:a42a1ed4d8e9 478 SWBflip = SWBflip_4; //FLIP SW4
mglmx 21:e6f1649add39 479 break;
mglmx 21:e6f1649add39 480 default:
carlosperales95 24:1d71dd8778c4 481 break;
mglmx 21:e6f1649add39 482 }
carlosperales95 11:021210c59a95 483
mglmx 21:e6f1649add39 484 //Security measure not to burn the switch.
mglmx 30:293ee760d357 485 if(times <=5){
mglmx 30:293ee760d357 486 DCC_send_command(SWBaddress,SWBflip,times); //Activating switch
mglmx 30:293ee760d357 487 if(!activate){
mglmx 30:293ee760d357 488 DCC_send_command(SWBaddress,SWBidle,times); //Sending IDLE to flip back.
mglmx 30:293ee760d357 489 }
mglmx 30:293ee760d357 490 }
mglmx 21:e6f1649add39 491
mglmx 21:e6f1649add39 492 }
mglmx 21:e6f1649add39 493
mglmx 22:e4153ca757dd 494
carlosperales95 24:1d71dd8778c4 495 /**
carlosperales95 24:1d71dd8778c4 496 *
carlosperales95 29:559eb2164488 497 *Checks if any of the switches of the box has been activated.
carlosperales95 29:559eb2164488 498 *Calls necessary function and displays LCD text.
carlosperales95 25:a42a1ed4d8e9 499 *
carlosperales95 24:1d71dd8778c4 500 **/
mglmx 21:e6f1649add39 501 void checkSwitch(){
carlosperales95 24:1d71dd8778c4 502
mglmx 22:e4153ca757dd 503 if(switch1 == 1){
carlosperales95 24:1d71dd8778c4 504
mglmx 22:e4153ca757dd 505 lcd.cls();
mglmx 22:e4153ca757dd 506 lcd.printf("Switch 1 ON - SW1");
carlosperales95 25:a42a1ed4d8e9 507 flipSwitch(1,5);
carlosperales95 24:1d71dd8778c4 508
mglmx 22:e4153ca757dd 509 }else if(switch2 == 1){
carlosperales95 24:1d71dd8778c4 510
carlosperales95 24:1d71dd8778c4 511 lcd.cls();
carlosperales95 24:1d71dd8778c4 512 lcd.printf("Switch 2 ON - SW2");
carlosperales95 24:1d71dd8778c4 513 flipSwitch(2,5);
carlosperales95 24:1d71dd8778c4 514
carlosperales95 24:1d71dd8778c4 515 }else if(switch3 == 0){
carlosperales95 24:1d71dd8778c4 516
carlosperales95 24:1d71dd8778c4 517 lcd.cls();
carlosperales95 24:1d71dd8778c4 518 lcd.printf("Switch 3 ON - SW3");
carlosperales95 24:1d71dd8778c4 519 flipSwitch(3,5);
carlosperales95 24:1d71dd8778c4 520
carlosperales95 24:1d71dd8778c4 521 }else if(switch4 == 0){
carlosperales95 24:1d71dd8778c4 522
carlosperales95 24:1d71dd8778c4 523 lcd.cls();
carlosperales95 24:1d71dd8778c4 524 lcd.printf("Switch 4 ON - IDLE");
carlosperales95 24:1d71dd8778c4 525 flipSwitch(0,5);
carlosperales95 24:1d71dd8778c4 526 }
mglmx 22:e4153ca757dd 527 }
mglmx 22:e4153ca757dd 528
carlosperales95 24:1d71dd8778c4 529
carlosperales95 24:1d71dd8778c4 530
carlosperales95 11:021210c59a95 531 //**************** MAIN PROGRAM FOR DENVER TRAIN ****************//
carlosperales95 11:021210c59a95 532
mglmx 1:0ab26889af9b 533 int main()
mglmx 1:0ab26889af9b 534 {
carlosperales95 25:a42a1ed4d8e9 535 //RISE FOR INTERRUPTS?? NOT WORKING ATM
carlosperales95 25:a42a1ed4d8e9 536 //int0.rise(&interrupt0);
carlosperales95 25:a42a1ed4d8e9 537 //int1.rise(&interrupt1);
carlosperales95 25:a42a1ed4d8e9 538
carlosperales95 25:a42a1ed4d8e9 539 //Read and display potentiometer
carlosperales95 25:a42a1ed4d8e9 540 //float f = pot.read();
carlosperales95 25:a42a1ed4d8e9 541 //float vin = f * 3.3;
carlosperales95 25:a42a1ed4d8e9 542 //lcd.printf("vin: %.4f",vin);
carlosperales95 25:a42a1ed4d8e9 543
carlosperales95 25:a42a1ed4d8e9 544 //0xFFFC //1111111111111100
carlosperales95 25:a42a1ed4d8e9 545
carlosperales95 25:a42a1ed4d8e9 546
carlosperales95 25:a42a1ed4d8e9 547 //Led routine to start main program
mglmx 2:f580707c44fa 548 led1 = 1;
mglmx 22:e4153ca757dd 549 wait(0.2);
mglmx 2:f580707c44fa 550 led1 = 0;
mglmx 22:e4153ca757dd 551 wait(0.2);
mglmx 2:f580707c44fa 552 led1 = 1;
mglmx 16:2a2da0e67793 553
carlosperales95 25:a42a1ed4d8e9 554 initialize_mcp(); //mcp initialization for interrupts before train running
mglmx 33:24ce12dec157 555 init();
mglmx 35:cfcfeccb959e 556 init_positions();
mglmx 16:2a2da0e67793 557
carlosperales95 25:a42a1ed4d8e9 558 //Train light routine to start running
mglmx 32:e5b732fb8e65 559 DCC_send_command(DCCaddressDR,DCC_func_lighton,200); // turn light on full
mglmx 32:e5b732fb8e65 560 DCC_send_command(DCCaddressDR,DCC_func_dimlight,400); //dim light
mglmx 32:e5b732fb8e65 561 DCC_send_command(DCCaddressDR,DCC_func_lighton,200); //light full again
mglmx 22:e4153ca757dd 562
carlosperales95 25:a42a1ed4d8e9 563 //LED3 Shows start of route + LCD notif
mglmx 22:e4153ca757dd 564 led3 = 1; // Entering the while
mglmx 22:e4153ca757dd 565 lcd.cls();
mglmx 22:e4153ca757dd 566 lcd.printf("Ready to start");
carlosperales95 19:ff21ba3a4dc5 567
carlosperales95 14:7bb998edd819 568 //Demo for stopping at the station
mglmx 1:0ab26889af9b 569 while(1) {
mglmx 4:50879dfb82d5 570
carlosperales95 25:a42a1ed4d8e9 571 checkSwitch(); //Checks for switch commands everytime.
carlosperales95 25:a42a1ed4d8e9 572
mglmx 33:24ce12dec157 573 if(1==0){
mglmx 33:24ce12dec157 574 //if(station == 1){ //If train is on the sensor at the middle of the station it stops and displays LCD text.
carlosperales95 14:7bb998edd819 575
mglmx 4:50879dfb82d5 576 lcd.cls();
carlosperales95 29:559eb2164488 577 lcd.printf("All aboard\n mind the gap");
mglmx 32:e5b732fb8e65 578 DCC_send_command(DCCaddressDR,DCCinst_stop,400);
carlosperales95 29:559eb2164488 579 lcd.cls();
mglmx 4:50879dfb82d5 580
mglmx 22:e4153ca757dd 581 }else{
mglmx 34:c9ab2a987734 582 DCC_send_command(DCCaddressDR,DCCinst_forward,1); // Forward half speed train addres DARK-RED
mglmx 32:e5b732fb8e65 583 DCC_send_command(DCCaddressLR,DCCinst_forward,1); // Forward half speed train address LIGHT-RED
carlosperales95 25:a42a1ed4d8e9 584 }
mglmx 1:0ab26889af9b 585 }
mglmx 3:fe7010b693a0 586 }