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:
- 26:f5e71308a13e
- Parent:
- 25:90f7a34c253a
- Child:
- 27:9f9cd0df9a79
--- a/main.cpp Wed Jun 20 09:41:20 2018 +0000
+++ b/main.cpp Wed Jun 20 10:05:45 2018 +0000
@@ -36,22 +36,18 @@
Train movement
*----------------------------------------------------------------------------*/
//move backwards/reverse
-const unsigned int DCCinst_reverse = 0x48; //reverse speed
//speed dial forward
-const unsigned int DCCinst_step2 = 0x72; //step 2
-const unsigned int DCCinst_step4 = 0x73; //step 4
+const unsigned int slow_speed = 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_step28 = 0x7F; //step 28 Full speed
+const unsigned int normal_speed = 0x78; //step 13 1/2 speed
+
const unsigned int DCCinst_switch1 = 0x81; //Activate switch1
const unsigned int DCCinst_switch2 = 0x82; //Activate switch2
const unsigned int DCCinst_switch3 = 0x84; //Activate switch3
const unsigned int DCCinst_switch4 = 0x88; //Activate switch4
-const unsigned int DCCinst_deactive_switch = 0x80; //Deactivate switches
//stop
-const unsigned int DCCinst_stop = 0x40; //forward and stop 01100000
+const unsigned int stop = 0x40; //forward and stop 01100000
static const int cRight[] = {6,7,8};
vector<int> C_right (cRight, cRight + sizeof(cRight) / sizeof(cRight[0]) );
@@ -59,6 +55,15 @@
static const int cLeft[] = {0,1,2,12,13};
vector<int> C_left (cLeft, cLeft + sizeof(cLeft) / sizeof(cLeft[0]) );
+static const int junction39[] = {3,9};
+vector<int> junction_39 (junction39, junction39 + sizeof(junction39) / sizeof(junction39[0]) );
+
+static const int junction511[] = {5,11};
+vector<int> junction_511 (junction511, junction511 + sizeof(junction511) / sizeof(junction511[0]) );
+
+int redStartVal = 12;
+int silverStartVal = 2;
+
/*----------------------------------------------------------------------------
Function definitions
*----------------------------------------------------------------------------*/
@@ -72,8 +77,8 @@
Train& assignTrain(int pos);
//Trains
-Train redTrain(DCCaddress_red, DCCinst_step13, 12);
-Train silverTrain(DCCaddress_silver,DCCinst_step13, 2);
+Train redTrain(DCCaddress_red, normal_speed, redStartVal);
+Train silverTrain(DCCaddress_silver,normal_speed, silverStartVal);
//Switches
Switch switch1(DCCaddress_switch,DCCinst_switch1);
Switch switch2(DCCaddress_switch,DCCinst_switch2);
@@ -82,23 +87,6 @@
//Detectors
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
*----------------------------------------------------------------------------*/
@@ -113,13 +101,11 @@
if(!readSwitch(sw3)){ //Change speed
lcd.printf("Go forward");
- redTrain.changeSpeed(DCCinst_step13);
- silverTrain.changeSpeed(DCCinst_step13);
+ redTrain.changeSpeed(normal_speed);
+ silverTrain.changeSpeed(normal_speed);
}
if(redTrain.checkStop()){ //check if Train stopped
- lcd.cls();
- lcd.printf("in stop");
executeCase(redTrain.getPosition());
}
else if(silverTrain.checkStop()){
@@ -202,17 +188,17 @@
switch(pos){
case 0:{
lcd.printf("at 0");
- currTrain->changeSpeed(DCCinst_step13);
+ currTrain->changeSpeed(normal_speed);
break;}
case 1:{
lcd.printf("at 1");
//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
+ currTrain->changeSpeed(slow_speed);//Slow down
lcd.printf("slow down");
}*/
//else
- currTrain->changeSpeed(DCCinst_step13);
+ currTrain->changeSpeed(normal_speed);
break;}
case 2:{
@@ -229,9 +215,7 @@
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);
+ dect.makeReservation(junction_39);
}
else { //0.75 chance to disable switch2, reserve 4
switch2.switchOff();
@@ -242,9 +226,7 @@
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);
+ dect.makeReservation(junction_39);
}else{ //IF 3 reserved
lcd.cls();
@@ -253,7 +235,7 @@
vector<int> detectors(4);
dect.makeReservation(detectors);
}
- currTrain->changeSpeed(DCCinst_step13); //Go forward
+ currTrain->changeSpeed(normal_speed); //Go forward
break;}
case 3:{
@@ -263,7 +245,7 @@
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
+ currTrain->changeSpeed(normal_speed); //go forward
break;}
case 4:{
lcd.printf("At 4");
@@ -277,7 +259,7 @@
}
else{
dect.makeReservation(C_right); //else reserve 6,7,8 and go forward
- currTrain->changeSpeed(DCCinst_step13);
+ currTrain->changeSpeed(normal_speed);
}
dect.showReservation();
@@ -289,7 +271,7 @@
currTrain->changeDirection(); //change direction
switch3.switchOn(); //Enable switch3
dect.clearReservation(C_right); //free nr 6,7,8
- currTrain->changeSpeed(DCCinst_step13); //go forward
+ currTrain->changeSpeed(normal_speed); //go forward
break;}
case 6:{
@@ -302,7 +284,7 @@
int arr [3] = {4,5,11};
vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
dect.clearReservation(detectors); //free nr 4,5,11
- currTrain->changeSpeed(DCCinst_step13); //Move forward
+ currTrain->changeSpeed(normal_speed); //Move forward
}
else{
if(dect.checkReservation(5)){ //IF 5 is reserved
@@ -310,19 +292,17 @@
}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);
- currTrain->changeSpeed(DCCinst_step13);
+ dect.makeReservation(junction_511);
+ currTrain->changeSpeed(normal_speed);
}
- currTrain->changeSpeed(DCCinst_step13); //Go forward
+ currTrain->changeSpeed(normal_speed); //Go forward
}
break;}
case 7:{
lcd.printf("At 7");
dect.showReservation();
wait(2);
- currTrain->changeSpeed(DCCinst_step13);
+ currTrain->changeSpeed(normal_speed);
break;}
case 8:{
lcd.printf("At 8");
@@ -331,16 +311,14 @@
if(currTrain->isClockwise()) //IF C goes towards 7
{
switch4.switchOff(); //Disable switch4
- 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
+ dect.clearReservation(junction_39); //Free 3,9,10
+ currTrain->changeSpeed(normal_speed); //Move forward
}
else{ //IF CC
switch4.switchOff(); //Disable switch4, reserve 10
vector<int> detectors(10);
dect.makeReservation(detectors);
- currTrain->changeSpeed(DCCinst_step13); //Go forward
+ currTrain->changeSpeed(normal_speed); //Go forward
}
break;}
case 9:{
@@ -351,7 +329,7 @@
if(!dect.checkReservation(8)){
switch4.switchOn(); //Enable switch4
dect.makeReservation(C_right); //Reserve 6,7,8
- currTrain->changeSpeed(DCCinst_step13); //Go forward
+ currTrain->changeSpeed(normal_speed); //Go forward
}
else{
@@ -373,7 +351,7 @@
else{
lcd.printf("12 not reserved");
dect.makeReservation(C_left);
- currTrain->changeSpeed(DCCinst_step13); //go forward
+ currTrain->changeSpeed(normal_speed); //go forward
}
break;}
@@ -386,7 +364,7 @@
switch1.switchOn(); //Enable switch1
dect.makeReservation(C_left); //Reserve 0,1,2, 12, 13
- currTrain->changeSpeed(DCCinst_step13); //go forward
+ currTrain->changeSpeed(normal_speed); //go forward
}
else{
currTrain->Stop();
@@ -402,23 +380,24 @@
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
+ currTrain->changeSpeed(normal_speed); //Move forward
break;}
case 13: {
lcd.printf("at 13");
- currTrain->changeSpeed(DCCinst_step13);
+ currTrain->changeSpeed(normal_speed);
break;}
case 14:{
lcd.printf("at 21");
- currTrain->changeSpeed(DCCinst_step13);
+ currTrain->changeSpeed(normal_speed);
break;}
case 15:{
lcd.printf("at 22");
- currTrain->changeSpeed(DCCinst_step13);
+ currTrain->changeSpeed(normal_speed);
break;}
default: {
lcd.printf("Not in a valid case");
+ currTrain->Stop();
break; }
}
@@ -494,8 +473,8 @@
}
void init_trains(){
- redTrain.changeSpeed(DCCinst_stop);
- silverTrain.changeSpeed(DCCinst_stop);
+ redTrain.changeSpeed(stop);
+ silverTrain.changeSpeed(stop);
vector<int> res;
res.push_back(2);
