Lucas Citolin / Mbed OS Autoline

Files at this revision

API Documentation at this revision

Comitter:
citolin
Date:
Sun Sep 16 17:28:48 2018 +0000
Parent:
1:cca3a4e419dd
Commit message:
Added a mutex in the state memory space. Also added a example for power supply station.

Changed in this revision

Autoline/Autoline.cpp Show annotated file Show diff for this revision Revisions of this file
Autoline/Autoline.h Show annotated file Show diff for this revision Revisions of this file
--- a/Autoline/Autoline.cpp	Sun Sep 16 16:36:04 2018 +0000
+++ b/Autoline/Autoline.cpp	Sun Sep 16 17:28:48 2018 +0000
@@ -20,6 +20,13 @@
 
 Autoline::~Autoline() { }
 
+// It needs a mutex because it's a variable controlled over two threads (ethernet thread and main thread)
+void Autoline::set_state(short int newState) {
+    _mutex.lock();
+    state = newState;
+    _mutex.unlock();    
+}
+
 void Autoline::print_queue() {
     pc.printf("[AUTOLINE - QUEUE] Queue size: %d\n",queue.size());
     for(int i=0;i<queue.size();i++)
@@ -102,8 +109,14 @@
             
             // Read the input I/Os (bitset from 0 to 4)
             std::string cmmd1 = read_ios_in_range(0,4);
+            cmmd1 = "{1;" + cmmd1 + "}";
             
             pc.printf("CMMD1 - %s\n", cmmd1);
+            
+            
+            // Send through the network
+            connection.send((char*)cmmd1.c_str(),cmmd1.size());
+            
             break;
         }
         
@@ -136,9 +149,14 @@
             //pc.printf("CMMD 4\n");
             
             std::string cmmd4 = read_ios_in_range(5,9);
+            cmmd4 = "{4;" + cmmd4 + "}";
             
             // Read the output I/Os (bitset from 5 to 9)
             pc.printf("CMMD4 - %s\n", cmmd4);
+            
+            // Send through the network
+            connection.send((char*)cmmd4.c_str(),cmmd4.size());
+            
             break;
         }
         
@@ -146,7 +164,7 @@
             pc.printf("CMMD 5\n");
             // Enter MAINTENANCE MODE.
             
-            state = MAINTENANCE_MODE;
+            set_state(MAINTENANCE_MODE);
             // code here ...
             
             break;
@@ -155,7 +173,7 @@
             pc.printf("CMMD 6\n");
             // Enter AUTOMATIC MODE.
             
-            state = AUTOMATIC_MODE;
+            set_state(AUTOMATIC_MODE);
             // code here ...
             
             break;
@@ -181,7 +199,7 @@
         case CMMD_P: {
             // CMMD P - { P X } X - Enter pause mode or not. (1 or 0). 1 for pause mode 0 to leave pause mode.   
             
-            state = PAUSED_MODE;
+            set_state(PAUSED_MODE);
             // code here ...
             
             break;
@@ -190,8 +208,21 @@
         case CMMD_E: {
             pc.printf("CMMD E\n");
             // CMMD E - { E } - IHM wants to know if CLP is on emergency mode or not.
+
+            // Suggestion done by setting the general state as emergency or not.
+            char isEmergencyOn = '0';
+            if(state==EMERGENCY_MODE)
+                isEmergencyOn = '1';
             
-            // code here ...    
+            char cmmde[5];
+            memset(cmmde,'\0',sizeof(cmmde));
+            cmmde[0] = '{';
+            cmmde[1] = 'E';
+            cmmde[2] = isEmergencyOn;
+            cmmde[3] = '}';
+            
+            connection.send(cmmde,sizeof(cmmde));
+            
             break;
         }
 
@@ -211,8 +242,39 @@
 
 // -- MAIN --
 void Autoline::run() {
-    // Put the main code in here
+    // General state machine
+    switch(state){
+        
+        case MAINTENANCE_MODE:{
+            // code here ...    
+        }    
+        
+        case AUTOMATIC_MODE: {
+            // code here ...
+            
+            // Put all the station's code in here...
+            //input_elevator();
+            supply_station();    
+            //hipot_wait_station();
+            //hipot_station();
+            //pf_station();
+            //ate_wait_station();
+            //ate1_station();
+            //ate2_station();
+            //eprom_station();
+            //remove_station();
+            //output_elevator();
+        }
+        
+        case EMERGENCY_MODE: {
+            // code here ...    
+        }
+        
+        case PAUSED_MODE: {
+            // code here ...    
+        }
     
+    }
     supply_station();
     
     // Put all the stations code in here
--- a/Autoline/Autoline.h	Sun Sep 16 16:36:04 2018 +0000
+++ b/Autoline/Autoline.h	Sun Sep 16 17:28:48 2018 +0000
@@ -60,10 +60,12 @@
     
     std::string read_ios_in_range(int start, int end);
     
+    // General functions
+    void set_state(short int newState);
+    
     // CMMD's functions
     void read_sensors();
     
-    
     // Stations
     void input_elevator();
     void supply_station();
@@ -76,7 +78,8 @@
     void eprom_station();
     void remove_station();
     void output_elevator();
-    
+  
+    Mutex _mutex;  
 };