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:
- 25:90f7a34c253a
- Parent:
- 24:418711ed8c52
- Child:
- 26:f5e71308a13e
diff -r 418711ed8c52 -r 90f7a34c253a main.cpp
--- a/main.cpp Thu Jun 14 14:47:16 2018 +0000
+++ b/main.cpp Wed Jun 20 09:41:20 2018 +0000
@@ -19,11 +19,12 @@
DigitalIn sw1(p29), sw2(p30), sw3(p11), sw4(p12);
-//InterruptIn sw1(p5);
InterruptIn int0(p13), int1(p14);
I2C i2c(p28, p27);
MCP23017 *mcp;
+//DigitalOut en(p6);
+
/*----------------------------------------------------------------------------
Addresses
*----------------------------------------------------------------------------*/
@@ -42,7 +43,7 @@
const unsigned int DCCinst_step4 = 0x73; //step 4
//const unsigned int DCCinst_step6 = 0x68; //step 6 1/4 speed
const unsigned int DCCinst_step13 = 0x78; //step 13 1/2 speed
-//const unsigned int DCCinst_step20 = 0x75; //step 20 3/4 speed
+const unsigned int DCCinst_step20 = 0x75; //step 20 3/4 speed
//const unsigned int DCCinst_step28 = 0x7F; //step 28 Full speed
const unsigned int DCCinst_switch1 = 0x81; //Activate switch1
const unsigned int DCCinst_switch2 = 0x82; //Activate switch2
@@ -52,6 +53,12 @@
//stop
const unsigned int DCCinst_stop = 0x40; //forward and stop 01100000
+static const int cRight[] = {6,7,8};
+vector<int> C_right (cRight, cRight + sizeof(cRight) / sizeof(cRight[0]) );
+
+static const int cLeft[] = {0,1,2,12,13};
+vector<int> C_left (cLeft, cLeft + sizeof(cLeft) / sizeof(cLeft[0]) );
+
/*----------------------------------------------------------------------------
Function definitions
*----------------------------------------------------------------------------*/
@@ -100,7 +107,6 @@
init_mcp();
init_interrupt();
init_trains();
- wait(2);
while(1){
redTrain.sendCommand();
silverTrain.sendCommand();
@@ -114,13 +120,21 @@
if(redTrain.checkStop()){ //check if Train stopped
lcd.cls();
lcd.printf("in stop");
- wait(2);
executeCase(redTrain.getPosition());
}
else if(silverTrain.checkStop()){
executeCase(silverTrain.getPosition());
- }
+ }
+
+ if(redTrain.checkStop() && silverTrain.checkStop()) {
+ break;
+ }
}
+
+ lcd.printf("Shutting down...");
+ // en = 0; TO Do : emergency shutdown!!
+ //wait(3);
+ return 0;
}
/*----------------------------------------------------------------------------
@@ -167,15 +181,18 @@
//Check which train got the interupt
if(redTrain.checkInterupt(pos)){
redTrain.setPosition(pos);
- //lcd.cls();
- //lcd.printf("Red train!");
return redTrain;
}
else if(silverTrain.checkInterupt(pos)){
silverTrain.setPosition(pos);
- //lcd.cls();
- lcd.printf("Silver train!");
return silverTrain;
+ }else{
+ lcd.cls();
+ lcd.printf("NO TRAIN ASSIGNED");
+ silverTrain.Stop();
+ redTrain.Stop();
+ switch2.switchOff();
+
}
}
@@ -183,155 +200,102 @@
Train* currTrain = &assignTrain(pos);
switch(pos){
- case 0:
+ case 0:{
lcd.printf("at 0");
- break;
- case 1:
+ currTrain->changeSpeed(DCCinst_step13);
+ break;}
+ case 1:{
lcd.printf("at 1");
- currTrain->changeSpeed(DCCinst_step4);//Slow down
- lcd.printf("slow down");
- break;
- case 2:
+ //TO DO: Check which train is going and only slow down if it started from 2
+ /*if(!currTrain->isClockwise()){
+ currTrain->changeSpeed(DCCinst_step4);//Slow down
+ lcd.printf("slow down");
+ }*/
+ //else
+ currTrain->changeSpeed(DCCinst_step13);
+
+ break;}
+ case 2:{
/*
- Stop everytime at 2
-
-
-
+ Stop everytime at 2
*/
-
- lcd.printf("at 2");
- if(currTrain->isClockwise()) //IF C goes towards 1
- {
- switch2.switchOff(); //Disable switch4
- int arr [3] = {3,4,9};
- vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
- dect.clearReservation(detectors); //Free 3,4,9
- currTrain->changeSpeed(DCCinst_step13); //Move forward
- }
- else{ //IF CC
- if(!dect.checkReservation(4) && !dect.checkReservation(3)){ //Check if 4 or 3 is reserved
- int v1 = rand() % 100; //IF NOT randomize which way to go
- if (v1 < 25){ //0.25 chance to enable the switch2, reserve 3,9
- switch2.switchOn();
- int arr [2] = {3,9};
- vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
- dect.makeReservation(detectors);
- }
- else { //0.75 chance to disable switch2, reserve 4
- switch2.switchOff();
- vector<int> detectors(4);
- dect.makeReservation(detectors);
- }
-
- }else if(dect.checkReservation(10)){ //IF 4 is reserved
- switch2.switchOn(); //Enable switch2, reserve 3,9
+ lcd.printf("at 2");
+ dect.showReservation();
+ wait(2);
+
+ if(!dect.checkReservation(4) && !dect.checkReservation(3)){ //Check if 4 or 3 is reserved
+ lcd.cls();
+ lcd.printf("none is reserved");
+ int v1 = rand() % 100; //IF NOT randomize which way to go
+ if (v1 < 75){ //0.25 chance to enable the switch2, reserve 3,9
+ switch2.switchOn();
int arr [2] = {3,9};
vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
dect.makeReservation(detectors);
-
- }else{ //IF 3 reserved
- switch2.switchOff(); //Disable switch2, reserve 4
+ }
+ else { //0.75 chance to disable switch2, reserve 4
+ switch2.switchOff();
vector<int> detectors(4);
dect.makeReservation(detectors);
- }
-
- currTrain->changeSpeed(DCCinst_step13); //Go forward
- }
- break;
- case 3:
+ }
+ }else if(dect.checkReservation(4)){ //IF 4 is reserved
+ switch2.switchOn(); //Enable switch2, reserve 3,9
+ lcd.cls();
+ lcd.printf("4 is reserved");
+ int arr [2] = {3,9};
+ vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+ dect.makeReservation(detectors);
+
+ }else{ //IF 3 reserved
+ lcd.cls();
+ lcd.printf("3 is reserved");
+ switch2.switchOff(); //Disable switch2, reserve 4
+ vector<int> detectors(4);
+ dect.makeReservation(detectors);
+ }
+ currTrain->changeSpeed(DCCinst_step13); //Go forward
+
+ break;}
+ case 3:{
lcd.printf("at 3");
- if(currTrain->isClockwise()){ //Clock: Goes from 3 to 2
- if(!dect.checkReservation(2)){
- switch2.switchOn(); //Enable switch2
- int arr [5] = {0,1,2,12,13};
- vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
- dect.makeReservation(detectors); //Reserve 0,1,2, 12, 13
- currTrain->changeSpeed(DCCinst_step13); //go forward
- }
- else{
- currTrain->Stop();
- }
+ dect.showReservation();
+
+ currTrain->changeDirection(); //change direction
+ switch2.switchOff(); //Disable switch2
+ dect.clearReservation(C_left); //free nr 0,1,2,12,13
+ currTrain->changeSpeed(DCCinst_step13); //go forward
+ break;}
+ case 4:{
+ lcd.printf("At 4");
+ dect.showReservation();
+ wait(2);
+ dect.clearReservation(C_left); //free nr 0,1,2,12,13
+ switch3.switchOn(); //Turn on switch3
+ if(dect.checkReservation(6)){ //Check if 6 is reserved
+ lcd.printf("6 is reserved");
+ currTrain->Stop(); //IF yes STOP
}
- else{ //Other Clock: Goes from 3->9
- currTrain->changeDirection(); //change direction
- switch2.switchOff(); //Disable switch2
- 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
- currTrain->changeSpeed(DCCinst_step13); //go forward
+ else{
+ dect.makeReservation(C_right); //else reserve 6,7,8 and go forward
+ currTrain->changeSpeed(DCCinst_step13);
}
- break;
- case 4:
- 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.makeReservation(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
- dect.showReservation();
- wait(2);
- if(dect.checkReservation(6)){ //Check if 6 is reserved
- lcd.printf("stop, 6 is reserved");
- currTrain->Stop(); //IF yes STOP
- lcd.cls();
- lcd.printf("Currtrain: ");
- lcd.printf("%d", currTrain->getSpeed());
- lcd.printf("\n Silver: ");
- lcd.printf("%d", silverTrain.getSpeed());
-
- }
- else{
- int arr [3] = {6,7,8};
- vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
- dect.makeReservation(detectors); //else reserve 6,7,8 and go forward
- currTrain->changeSpeed(DCCinst_step13);
- }
-
- }
- break;
- case 5:
+ dect.showReservation();
+ break; }
+ case 5:{
lcd.printf("at 5");
+ dect.showReservation();
- if(!currTrain->isClockwise()){ //CC: Goes from 5 to 6
- if(!dect.checkReservation(2)){
- switch3.switchOff(); //Disable switch3
- int arr [3] = {6,7,8};
- vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
- dect.makeReservation(detectors); //Reserve 6,7,8
- currTrain->changeSpeed(DCCinst_step13); //go forward
- }
- else{
- currTrain->Stop();
- }
- }
- else{ //Other Clock: Goes from 5->11
- currTrain->changeDirection(); //change direction
- switch3.switchOn(); //Enable switch3
- int arr [3] = {6,7,8};
- vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
- dect.clearReservation(detectors); //free nr 6,7,8
- currTrain->changeSpeed(DCCinst_step13); //go forward
- }
- break;
- case 6:
+ currTrain->changeDirection(); //change direction
+ switch3.switchOn(); //Enable switch3
+ dect.clearReservation(C_right); //free nr 6,7,8
+ currTrain->changeSpeed(DCCinst_step13); //go forward
+
+ break;}
+ case 6:{
lcd.printf("At 6");
+ dect.showReservation();
+ wait(2);
if(!currTrain->isClockwise()) //IF CC goes towards 7
{
switch3.switchOn(); //Enable switch3
@@ -341,222 +305,121 @@
currTrain->changeSpeed(DCCinst_step13); //Move forward
}
else{
- if(!dect.checkReservation(4) && !dect.checkReservation(5)){ //Check if 4 or 5 is reserved
- int v1 = rand() % 100; //IF NOT randomize which way to go
- if (v1 < 25){ //0.25 chance to disable the switch3, reserve 5,11
- switch3.switchOff();
- int arr [2] = {5,11};
- vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
- dect.makeReservation(detectors);
- }
- else { //0.75 chance to Enable switch3, reserve 4
- switch3.switchOn();
- vector<int> detectors(4);
- dect.makeReservation(detectors);
- }
-
- }else if(dect.checkReservation(4)){ //IF 4 is reserved
- switch3.switchOff(); //Disable switch3, reserve 5,11
+ if(dect.checkReservation(5)){ //IF 5 is reserved
+ currTrain->Stop();
+
+ }else{ //else 5 reserved
+ switch3.switchOff(); //Enable switch3, reserve 4
int arr [2] = {5, 11};
vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
dect.makeReservation(detectors);
-
- }else{ //IF 5 reserved
- switch3.switchOn(); //Enable switch3, reserve 4
- vector<int> detectors(4);
- dect.makeReservation(detectors);
+ currTrain->changeSpeed(DCCinst_step13);
}
-
currTrain->changeSpeed(DCCinst_step13); //Go forward
}
- break;
- case 7:
- lcd.printf("At 7yhyh");
- break;
- case 8:
- lcd.printf("At 8");
+ break;}
+ case 7:{
+ lcd.printf("At 7");
+ dect.showReservation();
+ wait(2);
+ currTrain->changeSpeed(DCCinst_step13);
+ break;}
+ case 8:{
+ lcd.printf("At 8");
+ dect.showReservation();
+ wait(2);
if(currTrain->isClockwise()) //IF C goes towards 7
{
switch4.switchOff(); //Disable switch4
- int arr [3] = {3,9,10};
+ int arr [3] = {3,9};
vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
dect.clearReservation(detectors); //Free 3,9,10
currTrain->changeSpeed(DCCinst_step13); //Move forward
}
else{ //IF CC
- if(!dect.checkReservation(9) && !dect.checkReservation(10)){ //Check if 9 or 10 is reserved
- int v1 = rand() % 100; //IF NOT randomize which way to go
- if (v1 < 25){ //0.25 chance to enable the switch4, reserve 3,9
- switch4.switchOn();
- int arr [2] = {3,9};
- vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
- dect.makeReservation(detectors);
- }
- else { //0.75 chance to disable switch4, reserve 10
- switch4.switchOff();
- vector<int> detectors(10);
- dect.makeReservation(detectors);
- }
-
- }else if(dect.checkReservation(10)){ //IF 10 is reserved
- switch4.switchOn(); //Enable switch4, reserve 3,9
- int arr [2] = {3,9};
- vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
- dect.makeReservation(detectors);
-
- }else{ //IF 9 reserved
- switch4.switchOff(); //Disable switch4, reserve 10
- vector<int> detectors(10);
- dect.makeReservation(detectors);
- }
-
+ switch4.switchOff(); //Disable switch4, reserve 10
+ vector<int> detectors(10);
+ dect.makeReservation(detectors);
currTrain->changeSpeed(DCCinst_step13); //Go forward
}
- break;
- case 9:
- lcd.printf("at 9");
- if(currTrain->isClockwise()){ //Clock: Goes from 9 to 8
- if(!dect.checkReservation(8)){
- switch4.switchOn(); //Enable switch4
- int arr [3] = {6,7,8};
- vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
- dect.makeReservation(detectors); //Reserve 6,7,8
- currTrain->changeSpeed(DCCinst_step13); //Go forward
-
- }
- else{
- currTrain->Stop();
- }
- }
- else{ //Other Clock: Goes from 9->3
- currTrain->changeDirection(); //change direction
- switch4.switchOff(); //Disable switch4
- int arr [3] = {6,7,8};
- vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
- dect.clearReservation(detectors); //free nr 6,7,8
- currTrain->changeSpeed(DCCinst_step13); //Go forward
- }
- break;
- case 10:
- lcd.printf("At 10");
- if(!currTrain->isClockwise()) //IF CC goes towards 12
- {
- int arr [4] = {6,7,8,9};
- vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
- dect.clearReservation(detectors); //free nr 6, 7, 8, 9
- switch1.switchOff(); //Turn of switch1
- if(dect.checkReservation(12)){ //Check if 12 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.makeReservation(detectors);
- currTrain->changeSpeed(DCCinst_step13); //go forward
- }
- }
-
- else{ //IF C :goes towards 8
- 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
- switch4.switchOff(); //Turn off switch4
-
- if(dect.checkReservation(8)){ //Check if 8 is reserved
- currTrain->Stop(); //IF yes STOP
- }
- else{
- int arr [3] = {6,7,8};
- vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
- dect.makeReservation(detectors); //else reserve 6,7,8 and go forward
- currTrain->changeSpeed(DCCinst_step13);
- }
- }
- break;
-
- case 11:
- lcd.printf("at 11");
-
- if(currTrain->isClockwise()){ //Clockwise: Goes from 11 to 5
- currTrain->changeDirection(); //change direction
- switch1.switchOff(); //Disable switch1
- int arr [5] = {0,1,2,12,13};
- vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
- dect.clearReservation(detectors); //free 0,1,2,12,13
- currTrain->changeSpeed(DCCinst_step13); //go forward
+ break;}
+ case 9:{
+ lcd.printf("at 9");
+ dect.showReservation();
+ wait(2);
+ dect.showReservation();
+ if(!dect.checkReservation(8)){
+ switch4.switchOn(); //Enable switch4
+ dect.makeReservation(C_right); //Reserve 6,7,8
+ currTrain->changeSpeed(DCCinst_step13); //Go forward
}
- else{ //Other Clock: Goes from 11->12
- if(!dect.checkReservation(8)){
-
- switch1.switchOn(); //Enable switch1
- int arr [5] = {0,1,2, 12,13};
- vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
- dect.makeReservation(detectors); //Reserve 0,1,2, 12, 13
- currTrain->changeSpeed(DCCinst_step13); //go forward
- }
- else{
- currTrain->Stop();
- }
+ else{
+ lcd.printf("from 9->8 : 8 reserved");
+ currTrain->Stop();
+ }
+ break;}
+ case 10:{
+ lcd.printf("At 10");
+ dect.showReservation();
+ wait(2);
+
+ dect.clearReservation(C_right); //free nr 6, 7, 8, 9
+ switch1.switchOff(); //Turn off switch1
+ if(dect.checkReservation(12)){ //Check if 12 is reserved
+ lcd.printf("12resv. STOP");
+ currTrain->Stop(); //IF yes STOP
}
+ else{
+ lcd.printf("12 not reserved");
+ dect.makeReservation(C_left);
+ currTrain->changeSpeed(DCCinst_step13); //go forward
+ }
+ break;}
+
+ case 11:{
+ lcd.printf("at 11");
+ dect.showReservation();
+ wait(2);
+
+ if(!dect.checkReservation(12)){
+
+ switch1.switchOn(); //Enable switch1
+ dect.makeReservation(C_left); //Reserve 0,1,2, 12, 13
+ currTrain->changeSpeed(DCCinst_step13); //go forward
+ }
+ else{
+ currTrain->Stop();
+ }
+
break;
case 12:
lcd.printf("at 12");
- if(!currTrain->isClockwise()) //IF CC goes towards 13
- {
- switch1.switchOff(); //Turn of switch1
- int arr [3] = {5,10,11};
- vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
- dect.clearReservation(detectors); //free nr 5, 10, 11
- currTrain->changeSpeed(DCCinst_step13); //Move forward
- }
- else{ //if C
- if(!dect.checkReservation(10) && !dect.checkReservation(11)){ //Check if 10 or 11 is reserved
- int v1 = rand() % 100; //IF NOT randomize which way to go
- if (v1 < 25){ //0.25 chance to enable the switch1, reserve 5,11
- switch1.switchOn();
- int arr [2] = {5,11};
- vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
- dect.makeReservation(detectors);
- }
- else { //0.75 chance to disable switch1, reserve 10
- switch1.switchOff();
- vector<int> detectors(10);
- dect.makeReservation(detectors);
- }
-
- }else if(dect.checkReservation(10)){ //IF 10 is reserved
- switch1.switchOn(); //Enable switch1, reserve 5,11
- int arr [2] = {5, 11};
- vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
- dect.makeReservation(detectors);
-
- }else{ //IF 11 reserved
- switch1.switchOff(); //Disable switch1, reserve 10
- vector<int> detectors(10);
- dect.makeReservation(detectors);
- }
-
- currTrain->changeSpeed(DCCinst_step13); //Go forward
- }
- break;
-
-
- case 13:
+ dect.showReservation();
+ wait(2);
+
+ switch1.switchOff(); //Turn of switch1
+ int arr2 [3] = {5,10,11};
+ vector<int> detectors2(arr2, arr2 + sizeof(arr2) / sizeof(int));
+ dect.clearReservation(detectors2); //free nr 5, 10, 11
+ currTrain->changeSpeed(DCCinst_step13); //Move forward
+
+ break;}
+ case 13: {
lcd.printf("at 13");
- break;
- case 14:
- lcd.printf("at 21");
- break;
- case 15:
- lcd.printf("at 22");
-
- //lcd.printf("In case ");
- //lcd.printf("%d", pos);
- break;
- default:
+ currTrain->changeSpeed(DCCinst_step13);
+ break;}
+ case 14:{
+ lcd.printf("at 21");
+ currTrain->changeSpeed(DCCinst_step13);
+ break;}
+ case 15:{
+ lcd.printf("at 22");
+ currTrain->changeSpeed(DCCinst_step13);
+ break;}
+ default: {
lcd.printf("Not in a valid case");
- break;
+ break; }
}
mcp->_read(GPIOA);
@@ -581,8 +444,7 @@
//PROBLEM
lcd.printf("Fail to detect int");
- executeCase(pos);
-
+ executeCase(pos);
}
bool readSwitch(DigitalIn theSwitch){
@@ -620,6 +482,7 @@
void init_interrupt() {
// Clear current interrupts
+ //en = 1;
mcp->_read(GPIOA);
mcp->_read(GPIOB);
// Register callbacks
@@ -631,15 +494,16 @@
}
void init_trains(){
- redTrain.changeSpeed(0x40);
- silverTrain.changeSpeed(0x40);
+ redTrain.changeSpeed(DCCinst_stop);
+ silverTrain.changeSpeed(DCCinst_stop);
vector<int> res;
res.push_back(2);
- res.push_back(4);
- dect.makeReservation(res); //Run trains counterclockwise and reserve the
+ //res.push_back(4);
+ dect.makeReservation(res); //Run trains counterclockwise and reserve
vector<int> res2;
res2.push_back(12);
- res2.push_back(13);
+ //res2.push_back(13);
dect.makeReservation(res2);
-}
+ dect.showReservation();
+}
\ No newline at end of file
