Mid level control code

Dependencies:   ros_lib_kinetic

Revision:
16:1e2804a4e5bd
Parent:
15:59471daef4cb
Child:
17:bbaf3e8440ad
--- a/main.cpp	Fri Aug 31 13:54:50 2018 +0000
+++ b/main.cpp	Tue Sep 11 10:10:26 2018 +0000
@@ -15,7 +15,7 @@
 
 // PATH VARIABLES
 double dblVelocity_mmps[N_CHANNELS] = { 0.0 }; // The linear path velocity (not sent to actuator)
-double dblLinearPathCurrentPos_mm[N_CHANNELS]={ 0.0 }; // The current position of the linear path (not sent to actuator)
+double dblLinearPathCurrentPos_mm[N_CHANNELS]= { 0.0 }; // The current position of the linear path (not sent to actuator)
 double dblTargetActPos_mm[N_CHANNELS] = { 0.0 }; // The final target position for the actuator
 
 // These have to be declared in the global scope or we get a stack overflow. No idea why.
@@ -25,6 +25,7 @@
 
 Serial pc(USBTX, USBRX); // tx, rx for usb debugging
 LLComms llcomms;
+HLComms hlcomms(SERVER_PORT);
 
 Thread threadLowLevelSPI(osPriorityRealtime);
 Thread threadSmoothPathPlan(osPriorityNormal);
@@ -35,6 +36,7 @@
 
 Timer timer;
 Ticker PathCalculationTicker;
+Ticker SendPressuresTicker;
 
 void startPathPlan() { // Plan a new linear path after receiving new target data
     semPathPlan.release(); // Uses threadReceiveAndReplan which is below normal priority to ensure consistent transmission to LL
@@ -42,7 +44,6 @@
 
 // This function will be called when a new transmission is received from high level
 void ReceiveAndReplan() {
-    HLComms hlcomms(SERVER_PORT);
     
     int error_code;
     error_code = hlcomms.setup_server();
@@ -181,7 +182,6 @@
         }
         // Finally recalculate all of the velocities based upon this maximum time for synchronization
         // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!MUTEX FOR dblVelocity_mmps and dblTargetActPos_mm??
-        //if( dblMaxRecalculatedTime >= 0.000000001 ) { // isTimeChanged && 
         for (ii = 0; ii< N_CHANNELS; ii++) { // Work out new velocities
             // If sent a negative requested position, do NOT replan that actuator
             if( dblTargetChambLen_mm[ii] < 0.0 ) continue;
@@ -189,7 +189,6 @@
             _dblVelocity_mmps[ii] = (dblTargetActPos_mm[ii] - dblADCCurrentPosition[ii]) / dblMaxRecalculatedTime;
             dblVelocity_mmps[ii] = _dblVelocity_mmps[ii];
         }
-        // !!!!!!!!! IF TIME ISN'T CHANGED THEN VELOCITY ISN'T UPDATED.....................................................................................
         // SEND MESSAGE
         //dblMaxRecalculatedTime = 1.0;
         hlcomms.make_message(&dblMaxRecalculatedTime);
@@ -255,6 +254,42 @@
     } // end while
 }
 
+//short int whichVar = 0;
+void sendPressures() {
+    pc.printf("%f,%f\r\n",dblLinearPathCurrentPos_mm[1],llcomms.ReadADCPressure_bar(1));
+    /*switch(whichVar) {
+        case 0 : pc.printf("%f,",llcomms.ReadADCPressure_bar(???));
+                 break;
+        case 1 : pc.printf("%f,",dblLinearPathCurrentPos_mm[???]);
+                 break;
+        case 2 : pc.printf("%f,",llcomms.ReadADCPressure_bar(???));
+                 break;
+        case 3 : pc.printf("%f,",dblLinearPathCurrentPos_mm[???]);
+                 break;
+        case 4 : pc.printf("%f,",llcomms.ReadADCPressure_bar(???));
+                 break;
+        case 5 : pc.printf("%f,",dblLinearPathCurrentPos_mm[???]);
+                 break;
+        case 6 : pc.printf("%f,",llcomms.ReadADCPressure_bar(???));
+                 break;
+        case 7 : pc.printf("%f,",dblLinearPathCurrentPos_mm[???]);
+                 break;
+        case 0 : pc.printf("%f,",llcomms.ReadADCPressure_bar(???));
+                 break;
+        case 1 : pc.printf("%f,",dblLinearPathCurrentPos_mm[???]);
+                 break;
+        case 0 : pc.printf("%f,",llcomms.ReadADCPressure_bar(???));
+                 break;
+        case 1 : pc.printf("%f,",dblLinearPathCurrentPos_mm[???]);
+                 break;
+    }
+    if( whichVar < 8 ) {
+        whichVar++;
+    } else {
+        whichVar = 0;
+    }*/
+}
+
 int main() {
     pc.baud(BAUD_RATE);
     printf("Sup bruvva! I'll be your mid-level controller for today.\r\n");
@@ -268,6 +303,7 @@
     threadSmoothPathPlan.start(CalculateSmoothPath); // Start planning thread
 
     PathCalculationTicker.attach(&startPathPlan, PATH_SAMPLE_TIME_S); // Set up planning thread to recur at fixed intervals
+    SendPressuresTicker.attach(&sendPressures, 0.1); // Set up planning thread to recur at fixed intervals
     
     Thread::wait(1);
     while(1) {