New project

Dependencies:   mbed TextLCD

Files at this revision

API Documentation at this revision

Comitter:
jasminealice
Date:
Tue Jun 12 15:02:44 2018 +0000
Parent:
20:32ba0a5f2d02
Child:
22:c024b20a0e2d
Commit message:
Comments for logic

Changed in this revision

Detector.cpp Show annotated file Show diff for this revision Revisions of this file
Detector.h Show annotated file Show diff for this revision Revisions of this file
Switch.cpp Show annotated file Show diff for this revision Revisions of this file
Track.cpp Show annotated file Show diff for this revision Revisions of this file
Track.h Show annotated file Show diff for this revision Revisions of this file
Train.cpp Show annotated file Show diff for this revision Revisions of this file
Train.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Detector.cpp	Tue Jun 12 15:02:44 2018 +0000
@@ -0,0 +1,31 @@
+#include "Detector.h"
+
+Detector::Detector(bool setValue)
+{
+    for(int i = 0; i < 14; i++)
+        detector[i] = setValue;
+}
+
+Detector::~Detector(){
+    //dtor
+}
+
+bool Detector::checkReservation(int number){
+    if(detector[number])
+        return true;
+    return false;               
+}
+
+void Detector::reserveDetector(int area[]){
+
+    for(int i = 0; i<sizeof(area); i++){
+        detector[area[i]] = true;    
+    }   
+}
+
+void Detector::clearReservation(int area[]){
+
+    for(int i = 0; i<sizeof(area); i++){
+        detector[area[i]] = false;    
+    }   
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Detector.h	Tue Jun 12 15:02:44 2018 +0000
@@ -0,0 +1,20 @@
+#ifndef DETECTOR_H
+#define DETECTOR_H
+#include "Detector.h"
+
+class Detector
+{
+    public:
+        Detector(bool setValue = false);
+        ~Detector();
+        bool checkReservation(int number);
+        void reserveDetector(int area[]);
+        void clearReservation(int area[]);
+
+    protected:
+
+    private:
+    bool detector[];
+};
+
+#endif // DETECTOR_H
--- a/Switch.cpp	Mon Jun 11 14:50:15 2018 +0000
+++ b/Switch.cpp	Tue Jun 12 15:02:44 2018 +0000
@@ -8,6 +8,8 @@
     nrPacket = 10;
     if(inst == 0x82)
         nrPacket = 100;
+    switchOff();    
+    
 }
 
 Switch::~Switch()
--- a/Track.cpp	Mon Jun 11 14:50:15 2018 +0000
+++ b/Track.cpp	Tue Jun 12 15:02:44 2018 +0000
@@ -1,5 +1,4 @@
 #include "Track.h"
-#include <iostream>
 
 Track::Track () : trackpin(p20), lcd(p22, p21, p23, p24, p25, p26), myled1(LED1)
 {
@@ -14,6 +13,7 @@
     //dtor
 }
 
+
 void Track::DCC_send_command(int repeat_count)
 {
     unsigned __int64 command = 0x0000000000000000; // __int64 is the 64-bit integer type
--- a/Track.h	Mon Jun 11 14:50:15 2018 +0000
+++ b/Track.h	Tue Jun 12 15:02:44 2018 +0000
@@ -6,6 +6,10 @@
 #include "MCP23017.h"
 #include "DigitalOut.h"
 
+#include <iostream>
+
+using namespace std;
+
 class Track
 {
     public:
@@ -15,7 +19,6 @@
         void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count);
 
     protected:
-
     /*----------------------------------------------------------------------------
     Pin definitions
     *----------------------------------------------------------------------------*/
@@ -23,26 +26,7 @@
     TextLCD lcd; // lcd
     
     DigitalOut myled1;
-    /*DigitalOut myled2(LED2);
-    DigitalOut myled3(LED3);
-    DigitalOut myled4(LED4);
-    DigitalOut externalLed1(p15);
-    DigitalOut externalLed2(p16);
-    DigitalOut externalLed3(p17);
-    DigitalOut externalLed4(p18);
     
-    TextLCD lcd(p22, p21, p23, p24, p25, p26); // lcd
-    
-    DigitalIn sw1(p29);
-    DigitalIn sw2(p30);
-    DigitalIn sw3(p11);
-    DigitalIn sw4(p12);
-    //InterruptIn sw1(p5);
-    InterruptIn inter0(p13);
-    InterruptIn inter1(p14);
-    MCP23017 *mcp;*/
-    
-    //
     unsigned int address;
     unsigned int inst;
 
--- a/Train.cpp	Mon Jun 11 14:50:15 2018 +0000
+++ b/Train.cpp	Tue Jun 12 15:02:44 2018 +0000
@@ -1,12 +1,13 @@
 #include "Train.h"
 
-Train::Train(const unsigned int newaddress, const unsigned int newinst)
+Train::Train(const unsigned int newaddress, const unsigned int newinst, int pos)
 {
     //ctor
     address = newaddress;
     inst = newinst;
-    position = -1;
-    direction = true;
+    position = pos;
+    dirClockwise = false;
+    nrPacket = 20;
 }
 
 Train::~Train()
@@ -19,14 +20,91 @@
     return position;
 }
 
-void Train::goForward(unsigned int forward){
+void Train::goForward(){
     
-    inst = forward;
     DCC_send_command(address, inst, nrPacket);
         
 }
 
-void Train::Stop(unsigned int stop){
-        stop = 0x40;
+void Train::changeSpeed(unsigned int speed){
+    inst = speed;    
+}
+
+void Train::Stop(){
+        unsigned int stop = 0x40;
         DCC_send_command(address, stop, nrPacket);
+}
+
+unsigned int Train::getSpeed(){
+    return inst;
+}
+
+void Train::setPosition(int pos){
+    position = pos;    
+}
+    
+bool Train::checkInterupt(int pos){
+    switch(pos){
+    case 0:
+        if(position == 13 || position == 1)
+            return true;
+        break;
+    case 1: 
+        if(position == 0 || position == 2)
+            return true;
+        break;
+    case 2: 
+        if(position == 1 || position == 3 || position == 4)
+            return true;
+        break;
+    case 3: 
+        if(position == 9 || position == 2)
+            return true;
+        break;
+    case 4: 
+        if(position == 6 || position == 2)
+            return true;
+        break;
+    case 5: 
+        if(position == 11 || position == 6)
+            return true;
+        break;
+    case 6: 
+        if(position == 7 || position == 4 || position == 5)
+            return true;
+        break;
+    case 7: 
+        if(position == 8 || position == 6)
+            return true;
+        break;
+    case 8: 
+        if(position == 7 || position == 9 || position == 10)
+            return true;
+        break;
+    case 9: 
+        if(position == 3 || position == 8)
+            return true;
+        break;
+    case 10: 
+        if(position == 8 || position == 12)
+            return true;
+        break;
+    case 11: 
+        if(position == 5 || position == 12)
+            return true;
+        break;
+    case 12: 
+        if(position == 11 || position == 13 || position == 10)
+            return true;
+        break;
+    case 13: 
+        if(position == 0 || position == 12)
+            return true;
+        break;
+    default: 
+        lcd.cls();
+        lcd.printf("Train not right one");
+        break;
+    }
+    return false;
 }
\ No newline at end of file
--- a/Train.h	Mon Jun 11 14:50:15 2018 +0000
+++ b/Train.h	Tue Jun 12 15:02:44 2018 +0000
@@ -6,19 +6,23 @@
 class Train : public Track
 {
     public:
-        Train(const unsigned int newaddress, const unsigned int newinst);
+        Train(const unsigned int newaddress, const unsigned int newinst, int pos);
         virtual ~Train();
         int getPosition();
-        void goForward(unsigned int);
-        void Stop(unsigned int);
-        const int nrPacket = 20;
+        void goForward();
+        void changeSpeed(unsigned int speed);
+        void Stop();
+        unsigned int getSpeed();
+        bool checkInterupt(int pos);
+        void setPosition(int pos);
+        
 
     protected:
 
     private:
-
+    int nrPacket;
     int position;
-    bool direction;
+    bool dirClockwise;
 };
 
 #endif // TRAIN_H
--- a/main.cpp	Mon Jun 11 14:50:15 2018 +0000
+++ b/main.cpp	Tue Jun 12 15:02:44 2018 +0000
@@ -4,11 +4,9 @@
 #include "Train.h"
 #include "Switch.h"
 #include "Track.h"
-
-#include <ctime>
+#include "Detector.h"
 
 //Board 1
-
 /*----------------------------------------------------------------------------
 Pin definitions
 *----------------------------------------------------------------------------*/
@@ -35,6 +33,7 @@
 DigitalIn sw2(p30);
 DigitalIn sw3(p11);
 DigitalIn sw4(p12);
+
 //InterruptIn sw1(p5);
 InterruptIn inter0(p13);
 InterruptIn inter1(p14);
@@ -43,18 +42,18 @@
 /*----------------------------------------------------------------------------
 Addresses
 *----------------------------------------------------------------------------*/
-const unsigned int DCCaddress_darkRed = 0x01;
-const unsigned int DCCaddress_lightRed = 0x03;
+const unsigned int DCCaddress_silver = 0x01;
+const unsigned int DCCaddress_red = 0x03;
 const unsigned int DCCaddress_switch = 0x06;
 
 /*----------------------------------------------------------------------------
 Train movement
 *----------------------------------------------------------------------------*/
 //move backwards/reverse
-//const unsigned int DCCinst_reverse = 0x48; //reverse speed
+const unsigned int DCCinst_reverse = 0x48; //reverse speed
 
 //speed dial forward
-//const unsigned int DCCinst_step2 = 0x72; //step 2
+const unsigned int DCCinst_step2 = 0x72; //step 2
 //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
@@ -76,13 +75,22 @@
 /*----------------------------------------------------------------------------
 Function definitions
 *----------------------------------------------------------------------------*/
-void readVoltage();
 bool readDetector(DigitalIn detector);
 bool readSwitch(DigitalIn theSwitch);
-//void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count); //send command
 void initialize_mcp();
 
 
+//Trains
+Train redTrain(DCCaddress_red, DCCinst_step13, 2);
+Train silverTrain(DCCaddress_silver,DCCinst_step13, 12);
+//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;
+
 /*----------------------------------------------------------------------------
 Main
 *----------------------------------------------------------------------------*/
@@ -91,18 +99,16 @@
     sw1.fall(&testInterupt);
     inter0.rise(&riseFunction);
     inter1.rise(&riseFunction);*/
-    Train lightRed(DCCaddress_darkRed, DCCinst_step13);
-    //Train darkRed(DCCaddress_darkRed);
-    Switch switch1(DCCaddress_switch,DCCinst_switch1);
-    //Switch switch2(DCCaddress_switch,DCCinst_switch2);
-    Switch switch3(DCCaddress_switch,DCCinst_switch3);
-    Switch switch4(DCCaddress_switch,DCCinst_switch4);
+    
+    initialize_mcp();
+    int res[2] = {2,4};
+    dect.reserveDetector(res); //Run trains counterclockwise and reserve the 
+    int res2[2] = {12,13};
+    dect.reserveDetector(res2);//detectors in front 
 
-    while(1){
-        if(readSwitch(sw1)){
-        //DCC_send_command(DCCaddress_switch,DCCinst_switch3,10); //Make sure to deactivate 2 right after activating it
+    while(1){ 
+        if(readSwitch(sw1)){ //Control switches
         switch1.switchOn();    
-        //lcd.printf("%d",switch1.switchOn());
         lcd.printf("Switch 1 on");
         //switch2.switchOn();  // Need to turn it off immediately!!!
         switch3.switchOn();
@@ -119,15 +125,23 @@
             
         }
             
-        if(readSwitch(sw2)){
-            lightRed.goForward(DCCinst_step13);
+        if(readSwitch(sw2)){ //run train
+            redTrain.goForward();
+            silverTrain.goForward();
         }
         else 
         {
-            lightRed.Stop(DCCinst_stop);
+            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("Start the journey");
     time_t tstart, tend; 
@@ -135,78 +149,172 @@
     int data = mcp->readRegister(0x12);
     lcd.printf("%d", data);
     wait(2);
-    
-    while(1){
-        
-        wait(0.2);
-        if(readSwitch(sw1)){
-            //lcd.cls();
-            //lcd.printf("Forward");
-            tstart = time(0);
-            DCC_send_command(DCCaddress_darkRed,DCCinst_step13,10); // forward half speed train address 3
-            tend = time(0); 
-            lcd.cls();
-            lcd.printf("Time to send command:");
-            lcd.printf("%f", difftime(tend, tstart));
-
-        }else{
-            //lcd.cls();
-            //lcd.printf("Stop");
-            tstart = time(0);
-            DCC_send_command(DCCaddress_darkRed,DCCinst_stop,10); // forward half speed train address 3
-            tend = time(0); 
-            lcd.cls();
-            lcd.printf("Time to send command:");
-            lcd.printf("%f", difftime(tend, tstart));
-        }
-        
-        if(readSwitch(sw2)){
-             
-             DCC_send_command(DCCaddress_switch,DCCinst_switch1,10);
-             myled1 = 1;
-             externalLed1 = 1;
-             DCC_send_command(DCCaddress_switch,DCCinst_deactive_switch,10);
-             myled1 = 0;
-             externalLed1 = 0;
-             
-             DCC_send_command(DCCaddress_switch,DCCinst_switch2,10); //Make sure to deactivate 2 right after activating it
-             myled2 = 1;
-             externalLed2 = 1;
-             DCC_send_command(DCCaddress_switch,DCCinst_deactive_switch,10);
-             myled2 = 0;
-             externalLed2 = 0;
-             
-             DCC_send_command(DCCaddress_switch,DCCinst_switch3,10);
-             myled3 = 1;
-             externalLed3 = 1;
-             DCC_send_command(DCCaddress_switch,DCCinst_deactive_switch,10);
-             myled3 = 0;
-             externalLed3 = 0;
-             
-             DCC_send_command(DCCaddress_switch,DCCinst_switch4,10);
-             myled4 = 1;
-             externalLed4 = 1;
-             DCC_send_command(DCCaddress_switch,DCCinst_deactive_switch,10);
-             myled4 = 0;
-             externalLed4 = 0;
-             
-        }
-    }*/
+    */
 }
 
 /*----------------------------------------------------------------------------
 Functions
 *----------------------------------------------------------------------------*/
 
-/*
-void testInterupt(){
+
+bool testInterupt(){
+    int pos = mcp->readRegister(0x12); 
     lcd.cls();
-    myled = 1;
-    lcd.printf("In interupt function");
-    lcd.printf("%d", sw1.read());
-    myled4 = 0;
+    //Check which train got the interupt
+    Train *temptrain;
+    if(redTrain.checkInterupt(pos)){
+        redTrain.setPosition(pos);
+        temptrain = &redTrain;
+    }
+    else if(silverTrain.checkInterupt(pos)){
+        silverTrain.setPosition(pos);
+        temptrain = &silverTrain;    
+    }
+    else{
+        //WE HAVE A PROBLEM
+        lcd.printf("WRONG DETECTOR DETECTED");
+        return false; 
+    }
+    
+    switch(pos){
+    case 0:
+    case 1:
+    case 2:
+        //IF Clock 2->1
+            //Disable Switch2
+            //Free 3,4,9 
+            //Move forward
+        //IF CC
+            //Check if 4 or 3 is reserved
+                //IF NOT randomize which way to go
+                    //0.3 chance to enable the switch2, reserve 3,9 and go
+                    //0.7 chance to disable switch2, reserve 4 and go
+                //IF 4 is reserved
+                    //Enable switch2, reserve 3,9 and go
+                //IF 3 reserved 
+                    //Disable switch2, reserve 4 and go
+    case 3:
+        //Clock: Goes from 3 to 2
+            //Enable switch2
+            //Reserve 0,1,2, 12, 13
+            //Go forward
+        //Other Clock: Goes from 3->9
+            //disable switch2
+            //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
+        
+    case 5:
+        //Clock: Goes from 5->6
+            //Enable switch3
+            //Reserve 6,7,8
+            //Go forward
+        //Other Clock: Goes from 5->11
+            //disable switch3
+            //free 6,7,8
+            //Go forward
+    case 6:
+        //IF CC
+            //Disable switch3
+            //Free 4,5,11
+            //Move forward
+        //IF Clock
+            //Check if 4 or 5 is reserved
+                //IF NOT randomize which way to go
+                    //0.3 chance to enable the switch3, reserve 5,11 and go
+                    //0.7 chance to disable switch3, reserve 4 and go
+                //IF 4 is reserved
+                    //Enable switch3, reserve 5,11 and go
+                //IF 5 reserved 
+                    //Disable switch3, reserve 4 and go
+                    
+    
+    case 7:
+    case 8:
+        //IF Clock 8->7
+            //Disable switch4
+            //Free 3,9,10
+            //Move forward
+        //IF CC
+            //Check if 9 or 10 is reserved
+                //IF NOT randomize which way to go
+                    //0.3 chance to enable the switch4, reserve 3,9 and go
+                    //0.7 chance to disable switch4, reserve 10 and go
+                //IF 10 is reserved
+                    //Enable switch4, reserve 3,9 and go
+                //IF 9 reserved 
+                    //Disable switch4, reserve 10 and go
+    case 9:
+        //Clock: Goes from 9 to 8
+            //Enable switch4
+            //Reserve 6,7,8
+            //Go forward
+        //Other Clock: Goes from 9->3
+            //disable switch4
+            //free 6,7,8
+            //Go forward
+    case 10:
+        //IF CC
+            //free nr 6,7,8,9
+            //Turn off switch1
+            //Check if 12 is reserved
+            //IF yes STOP
+            //else reserve 12,13,0,1,2 and go forward
+        //IF Clock
+            //Free nr 0,1,2,11, 12, 13
+            //Turn off switch4
+            //Check if 8 is reserved
+            //IF yes STOP
+            //else reserve 6,7,8 go forward
+    case 11:
+        //Clock: Goes from 11 to 12
+            //Enable switch1
+            //Reserve 0,1,2, 12, 13
+            //Go forward
+        //Other Clock: Goes from 11->5
+            //disable switch1
+            //free 0,1,2,12,13
+            //Go forward
+    case 12: 
+        //IF CC: 12->13
+            //Disable switch1
+            //Free 5,10,11
+            //Move forward
+        //IF Clock
+            //Check if 11 or 10 is reserved
+                //IF NOT randomize which way to go
+                    //0.3 chance to enable switch1, reserve 5,11 and go
+                    //0.7 chance to disable switch1, reserve 10 and go
+                //IF 10 is reserved
+                    //Enable switch1, reserve 5,11 and go
+                //IF 11 reserved 
+                    //Disable switch1,reserve 10 and go
+    case 13: 
+        
+        lcd.printf("In case ");
+        lcd.printf("%d", pos);
+        break;
+    default:   
+        lcd.printf("Not in a valid case");
+        break; 
+    }
+    
+    return true;
 }
 
+/*
 void testInterupt2(){
     lcd.cls();
     myled = 0;
@@ -246,13 +354,6 @@
     return false;
 }
 
-void readVoltage(){
-        /*float f = Ain.read(); //Read voltage value
-        float Vin = f *3.3;
-        lcd.printf("%.2f", Vin);
-        wait(0.1);
-        lcd.printf("\n");*/
-}
 void initialize_mcp(){
     mcp = new MCP23017(p28, p27, 0x40);
     mcp->reset();     
@@ -271,42 +372,3 @@
     mcp->writeRegister(0x0c, (unsigned char )0x00);     
     mcp->writeRegister(0x0d, (unsigned char )0x00);
 }
-
-/*
-void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count)
-{
-    unsigned __int64 command = 0x0000000000000000; // __int64 is the 64-bit integer type
-    unsigned __int64 temp_command = 0x0000000000000000;
-    unsigned __int64 prefix = 0x3FFF; // 14 "1" bits needed at start
-    unsigned int error = 0x00; //error byte
-    //calculate error detection byte with xor
-    error = address ^ inst;
-    //combine packet bits in basic DCC format
-    command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01;
-    //printf("\n\r %llx \n\r",command);
-    int i=0;
-//repeat DCC command lots of times
-    while(i < repeat_count) {
-        temp_command = command;
-//loops through packet bits encoding and sending out digital pulses for a DCC command
-        for (int j=0; j<64; j++) {
-            if((temp_command&0x8000000000000000)==0) { //test packet bit
-                //send data for a "0" bit
-                Track=0;
-                wait_us(100);
-                Track=1;
-                wait_us(100);
-            } else {
-                //send data for a "1"bit
-                Track=0;
-                wait_us(58);
-                Track=1;
-                wait_us(58);
-            }
-            // next bit in packet
-            temp_command = temp_command<<1;
-        }
-        i++;
-    }
-}
-*/
\ No newline at end of file