Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 23:bb57966cb776
- Parent:
- 22:c024b20a0e2d
- Child:
- 24:418711ed8c52
--- a/main.cpp Wed Jun 13 09:06:55 2018 +0000
+++ b/main.cpp Wed Jun 13 20:50:26 2018 +0000
@@ -13,31 +13,14 @@
*----------------------------------------------------------------------------*/
DigitalOut Track(p20); // train track
-DigitalOut myled1(LED1);
-DigitalOut myled2(LED2);
-DigitalOut myled3(LED3);
-DigitalOut myled4(LED4);
-DigitalOut externalLed1(p15);
-DigitalOut externalLed2(p16);
-DigitalOut externalLed3(p17);
-DigitalOut externalLed4(p18);
-
+DigitalOut myled1(LED1), myled2(LED2), myled3(LED3), myled4(LED4);
+DigitalOut externalLed1(p15), externalLed2(p16), externalLed3(p17), externalLed4(p18);
TextLCD lcd(p22, p21, p23, p24, p25, p26); // lcd
-//AnalogIn Ain(p20); // pot. met.
-/* Train detectors d2, d21, d22 (trainstation) */
-// detect_21(p17);
-//DigitalIn detect_22(p16);
-//DigitalIn detect_2(p15);
-
-DigitalIn sw1(p29);
-DigitalIn sw2(p30);
-DigitalIn sw3(p11);
-DigitalIn sw4(p12);
+DigitalIn sw1(p29), sw2(p30), sw3(p11), sw4(p12);
//InterruptIn sw1(p5);
-InterruptIn int0(p13);
-InterruptIn int1(p14);
+InterruptIn int0(p13), int1(p14);
I2C i2c(p28, p27);
MCP23017 *mcp;
@@ -69,68 +52,76 @@
//stop
const unsigned int DCCinst_stop = 0x40; //forward and stop 01100000
-//detectors addresses
-const unsigned int detector_0 = 0xFFFE;
-const unsigned int detector_0_1 = 0xFFFC;
-const unsigned int detector_1 = 0xFFFD;
-
/*----------------------------------------------------------------------------
Function definitions
*----------------------------------------------------------------------------*/
-bool readDetector(DigitalIn detector);
bool readSwitch(DigitalIn theSwitch);
-//void initialize_mcp();
-void on_int0_change();
-void on_int1_change();
+int convertHextoDec(int pos, int intnr);
+bool checkDetector(int inter);
void init_mcp();
-void init();
-
-
+void init_interrupt();
+Train& assignTrain(int pos);
//Trains
-Train redTrain(DCCaddress_red, DCCinst_step13, 2);
-Train silverTrain(DCCaddress_silver,DCCinst_step13, 12);
+Train redTrain(DCCaddress_red, DCCinst_step13, 12);
+Train silverTrain(DCCaddress_silver,DCCinst_step13, 2);
//Switches
Switch switch1(DCCaddress_switch,DCCinst_switch1);
//Switch switch2(DCCaddress_switch,DCCinst_switch2);
Switch switch3(DCCaddress_switch,DCCinst_switch3);
Switch switch4(DCCaddress_switch,DCCinst_switch4);
//Detectors
-Detector dect;
+Detector dect(false);
+// Detectors address
+/*
+d0 = 0xfe //254
+d1 = 0xfd //253
+d2 = fb //251
+d3 = f7 //247
+d4 = ef //239
+d5 = df //223
+d6 = bf //191
+d7 = 7f //255
+d8 = 0xfe //254
+d9 = fd
+d10 = fb
+d11 = f7
+d12 = ef
+d13 = df
+*/
/*----------------------------------------------------------------------------
Main
*----------------------------------------------------------------------------*/
int main() {
- /*sw1.rise(&testInterupt2);
- sw1.fall(&testInterupt);
- inter0.rise(&riseFunction);
- inter1.rise(&riseFunction);*/
lcd.printf("Start journey");
init_mcp();
- init();
- /*int res[2] = {2,4};
+ init_interrupt();
+
+
+ vector<int> res;
+ res.push_back(2);
+ res.push_back(4);
dect.reserveDetector(res); //Run trains counterclockwise and reserve the
- int res2[2] = {12,13};
+ vector<int> res2;
+ res2.push_back(12);
+ res2.push_back(6);
dect.reserveDetector(res2);//detectors in front
- */
+ redTrain.Stop();
+ silverTrain.Stop();
+
while(1){
-
- if(readSwitch(sw1)){ //Control switches
- /*switch1.switchOn();
+ redTrain.sendCommand();
+ silverTrain.sendCommand();
+ /*if(readSwitch(sw1)){ //Control switches
+ switch1.switchOn();
lcd.printf("Switch 1 on");
//switch2.switchOn(); // Need to turn it off immediately!!!
switch3.switchOn();
- switch4.switchOn();*/
+ switch4.switchOn();
- // Clear current interrupts
- mcp->_read(GPIOA);
- mcp->_read(GPIOB);
- // Register callbacks
- int0.fall(&on_int0_change);
- int1.fall(&on_int1_change);
- }
+ }*/
/*else{
//DCC_send_command(DCCaddress_switch,DCCinst_deactive_switch,10);
switch1.switchOff();
@@ -139,9 +130,9 @@
switch4.switchOff();
lcd.printf("Switch 1 off");
- }
+ }*/
- if(readSwitch(sw2)){ //run train
+ /*if(readSwitch(sw2)){ //run train
redTrain.goForward();
silverTrain.goForward();
}
@@ -149,49 +140,95 @@
{
redTrain.Stop();
silverTrain.Stop();
- }
+ }*/
if(!readSwitch(sw3)){ //Change speed
- if(silverTrain.getSpeed() == DCCinst_step2)
- silverTrain.changeSpeed(DCCinst_step13);
- else
- silverTrain.changeSpeed(DCCinst_step2);
+ lcd.printf("Go forward");
+ redTrain.changeSpeed(DCCinst_step13);
+ silverTrain.changeSpeed(DCCinst_step13);
}
- } */
-
- /*lcd.printf("Start the journey");
- time_t tstart, tend;
- initialize_mcp();
- int data = mcp->readRegister(0x12);
- lcd.printf("%d", data);
- wait(2);
- */
+
+ //TODO: Check if train is not moving and then call function checkdetector
+
}
}
/*----------------------------------------------------------------------------
Functions
*----------------------------------------------------------------------------*/
-
+int convertHextoDec(int pos, int intnr){
+ int newPos;
+ switch(pos){
+ case 0xfe:
+ newPos = 0;
+ break;
+ case 0xfd:
+ newPos = 1;
+ break;
+ case 0xfb:
+ newPos = 2;
+ break;
+ case 0xf7:
+ newPos = 3;
+ break;
+ case 0xef:
+ newPos = 4;
+ break;
+ case 0xdf:
+ newPos = 5;
+ break;
+ case 0xbf:
+ newPos = 6;
+ break;
+ case 0x7f:
+ newPos = 7;
+ break;
+ default:
+ return -1;
+ }
+
+ if(intnr == 1)
+ newPos += 8;
+ return newPos;
+}
-bool testInterupt(){
- int pos = 0;// mcp->readRegister(0x12);
- lcd.cls();
+Train& assignTrain(int pos){
+
//Check which train got the interupt
- Train *temptrain;
if(redTrain.checkInterupt(pos)){
redTrain.setPosition(pos);
- temptrain = &redTrain;
+ //lcd.cls();
+ //lcd.printf("Red train!");
+ return redTrain;
}
else if(silverTrain.checkInterupt(pos)){
silverTrain.setPosition(pos);
- temptrain = &silverTrain;
+ //lcd.cls();
+ //lcd.printf("Silver train!");
+ return silverTrain;
}
- else{
- //WE HAVE A PROBLEM
- lcd.printf("WRONG DETECTOR DETECTED");
- return false;
+
+}
+
+bool checkDetector(int inter){
+ lcd.cls();
+ int pos;
+ wait_us(100);
+ if (inter == 0){
+ //Read from first one
+ pos = mcp->_read(INTCAPA);
+ pos = convertHextoDec(pos, 0);
}
+ else if (inter == 1){
+ //Read from second one
+ pos = mcp->_read(INTCAPB);
+ pos = convertHextoDec(pos, 1);
+ }
+ else
+ //PROBLEM
+ lcd.printf("Fail to detect int");
+
+ Train currTrain = assignTrain(pos);
switch(pos){
case 0:
@@ -221,19 +258,48 @@
//free 0,1,2,12,13
//Go forward
case 4:
- //IF CC : goes towards 6
- //free nr 0,1,2,12, 13
- //Turn of switch3
- //Check if 6 is reserved
- //IF yes STOP
- //else reserve 6,7,8 and go forward
- //IF Clock goes towards 2
- //free nr 5,6, 7, 8
- //Turn of switch2
- //Check if 2 is reserved
- //IF yes STOP
- //else reserve 12,13,0,1,2 go forward
-
+ lcd.printf("At 4");
+ if(currTrain.isClockwise()) //IF Clock goes towards 2
+ {
+ int arr [4] = {5,6,7,8};
+ vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+ dect.clearReservation(detectors); //free nr 5,6, 7, 8
+ //switch2.switchOff(); //Turn of switch2
+ if(dect.checkReservation(2)){ //Check if 2 is reserved
+ currTrain.Stop(); //IF yes STOP
+ }
+ else{
+ int arr [5] = {0,1,2,12,13};//else reserve 12,13,0,1,2
+ vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+ dect.reserveDetector(detectors);
+ currTrain.changeSpeed(DCCinst_step13); //go forward
+ }
+ }
+ else{ //IF CC :goes towards 6
+ int arr [5] = {0,1,2,12,13};
+ vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+ dect.clearReservation(detectors); //free nr 0,1,2,12,13
+ switch3.switchOn(); //Turn on switch3
+
+ if(dect.checkReservation(6)){ //Check if 6 is reserved
+ lcd.printf("stop");
+ currTrain.Stop(); //IF yes STOP
+ lcd.cls();
+ lcd.printf("Currtrain: ");
+ lcd.printf("%d", currTrain.getSpeed());
+ lcd.printf("\n Redtrain: ");
+ lcd.printf("%d", redTrain.getSpeed());
+
+ }
+ else{
+ int arr [3] = {6,7,8};
+ vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+ dect.reserveDetector(detectors); //else reserve 6,7,8 and go forward
+ currTrain.changeSpeed(DCCinst_step13);
+ }
+
+ }
+ break;
case 5:
//Clock: Goes from 5->6
//Enable switch3
@@ -321,36 +387,19 @@
//Disable switch1,reserve 10 and go
case 13:
- lcd.printf("In case ");
- lcd.printf("%d", pos);
+ //lcd.printf("In case ");
+ //lcd.printf("%d", pos);
break;
default:
lcd.printf("Not in a valid case");
break;
}
+ mcp->_read(GPIOA);
+ mcp->_read(GPIOB);
return true;
}
-/*
-void testInterupt2(){
- lcd.cls();
- myled = 0;
- lcd.printf("In interupt2 function");
- lcd.printf("%d", sw1.read());
- myled4 = 1;
-}
-
-void riseFunction(){
- lcd.printf("In rise function");
- lcd.printf("%d", inter1.read());
-}
-
-void fallFunction(){
- lcd.printf("In fall function");
- lcd.printf("%d", inter1.read());
-}*/
-
bool readSwitch(DigitalIn theSwitch){
int val = theSwitch.read();
//lcd.printf("%d", val);
@@ -360,18 +409,6 @@
return false;
}
-bool readDetector(DigitalIn detector){
- /*int val = detect.read();
- if(val == 1)
- {
- lcd.cls();
- lcd.printf("Detect: ");
- lcd.printf("%d", val);
- return true;
- }*/
- return false;
-}
-
void init_mcp() {
// Initialisation of MCP registers,documentation on registers is availableatNiels/Jacco/Natalia
mcp = new MCP23017(i2c, 0x40);
@@ -388,37 +425,22 @@
mcp->_write(GPPUA, (unsigned char )0xff);
mcp->_write(GPPUB, (unsigned char )0xff);
}
+void passZero(){
+ checkDetector(0);
+}
-void init() {
+void passOne(){
+ checkDetector(1);
+}
+
+void init_interrupt() {
// Clear current interrupts
mcp->_read(GPIOA);
mcp->_read(GPIOB);
// Register callbacks
- int0.fall(&on_int0_change);
- int1.fall(&on_int1_change);
+ int0.fall(&passZero);
+ int1.fall(&passOne);
// Enable interrupts on MCP
mcp->_write(GPINTENA, (unsigned char )0xff);
mcp->_write(GPINTENB, (unsigned char )0xff);// Ready to go!
}
-
-void on_int0_change() {
- lcd.printf("In int0)");
- // In your code, you might want to move this logic out of the interrupt routine
- // (for instance, by setting a flag and checking this flagin your main-loop)
- wait_us(200000);
- int sensor_data = mcp->_read(INTCAPA);
- // The only thing we do with the interrupt signal is printing it
- lcd.cls();
- lcd.printf("GOT INT0 (0x%x)\n", sensor_data);
-}
-
-void on_int1_change() {
- lcd.printf("In int1)");
- // In your code, you might want to move this logic out of the interrupt routine
- // (for instance, by setting a flag and checking this flagin your main-loop)
- wait_us(200000);
- int sensor_data = mcp->_read(INTCAPB);
- // The only thing we do with the interrupt signal is printing it
- lcd.cls();
- lcd.printf("GOT INT0 (1) (0x%x)\n", sensor_data);
-}
\ No newline at end of file
