Control Code with I/O and ADC working

Dependencies:   MODSERIAL mbed

Revision:
11:fe6c5aac5d89
Parent:
9:8cd14861dc63
--- a/main.cpp	Mon Apr 02 00:00:26 2018 +0000
+++ b/main.cpp	Tue Apr 10 18:57:57 2018 +0000
@@ -14,8 +14,8 @@
 #define DEBUG3           1
 #define CHN_COUNT        8
 #define MIN_TEMP         15
-#define MAX_TEMP         60
-#define TEMP_MARGIN      5
+#define MAX_TEMP         45
+#define TEMP_MARGIN      10
 #define HYST_LOW         0.3
 #define HYST_HIGH        1
 #define SAMPLES          5
@@ -23,7 +23,7 @@
 #define VALVE            1
 #define HEATER           2
 #define STATUS_GOOD      3
-#define STATUS_BAD       4
+#define STATUS_BAD       0
 #define sizeLUT          34
 #define FRONT_THERM      0
 #define BACK_THERM       1
@@ -43,6 +43,7 @@
 #define SELECT_CHANNEL          0xB1
 #define CMD_RESPONSE            0xD0
 #define CMD_DATA                0xD1
+#define ERROR_DATA              0xD2
 
 
 unsigned int chanSel_SendTemp = 0;
@@ -95,14 +96,14 @@
 };
 
 CHNL_DATA chnlStatus[] = {
-    {0, NULL, 0},
-    {0, NULL, 0},
-    {0, NULL, 0},
-    {0, NULL, 0},
-    {0, NULL, 0},
-    {0, NULL, 0},
-    {0, NULL, 0},
-    {0, NULL, 0},
+    {1, 31, 0},
+    {1, 31, 0},
+    {1, 31, 0},
+    {1, 31, 0},
+    {1, 31, 0},
+    {1, 31, 0},
+    {1, 31, 0},
+    {1, 31, 0},
     {0, NULL, 0},
     {0, NULL, 0},
     {0, NULL, 0},
@@ -248,7 +249,6 @@
 
 //SERIAL COMMUNICATION SETUP
 MODSERIAL pc(USBTX, USBRX);
-MODSERIAL chain(PTA2, PTA1);
 
 //DEFINE PINS
 DigitalOut myled(LED2);
@@ -355,6 +355,7 @@
                     chanSel_SetTemp = pRxPkt->chanID;
                     chnlStatus[pRxPkt->chanID].status = pRxPkt->chanStat;
                     chnlStatus[pRxPkt->chanID].setTemp = pRxPkt->setTemp;
+                    chnlStatus[pRxPkt->chanID].error = 0;
                     newTempSet = true;
                 }
             }
@@ -613,6 +614,28 @@
     pc.printf("\n");
 }
 
+//***************************************************************
+// Build packet with errors to send to GUI
+//***************************************************************
+void sendError (int chan, float error)
+{
+    RESPONSE_CMD response;
+    unsigned char *ptr = (unsigned char *)&response;
+    int i;
+
+    response.SOF_flag = TX_SOF;
+    response.cmd_type = ERROR_DATA;
+    response.len = 9;
+    response.Delim = DELIMETER;
+    response.data = (float)error;
+    response.EOF_flag = TX_EOF;
+
+    // Send response to GUI
+    for (i=0; i < response.len; i++, ptr++)
+        pc.printf("%02x", *ptr);
+    pc.printf("\n");
+}
+
 
 /*************************************************************/
 /*                       MAIN FUNCTION                       */
@@ -631,7 +654,7 @@
     myled = 1;
     rLed = 0;
     gLed = 1;
-    
+
     t.start();
 
     while(1) {
@@ -641,20 +664,21 @@
 
         for(int chnl = 0; chnl < CHN_COUNT; chnl++){
             float currentTempFront = get_temp(chnl, FRONT_THERM);
+            //wait(0.1);
             float currentTempBack = get_temp(chnl, BACK_THERM);
             float valveCurrent = get_valve_current(chnl, VALVE_FET_AMP);
             float heaterCurrent = get_heater_current(chnl, HEAT_FET_AMP);
             float currentTemp = currentTempFront;
-            
+
             if(currentTempFront >=  currentTempBack){
                 currentTemp = currentTempFront;
             }
             else{
                 currentTemp = currentTempBack;
-            }    
-                
+            }
+
             //float currentTemp = (currentTempFront + currentTempBack)/2;
-            
+
             //check if we received data/need to update TCTF data
             if(dataReceived){
                 dataReceived = false;
@@ -664,7 +688,7 @@
                 pc.rxBufferFlush();
 
             }
-            
+
             if (chnl == chanSel_SendTemp){
                 // Send temp readings for selected chan to GUI
                 sendTempReadings(currChan, currentTemp);
@@ -672,12 +696,16 @@
 
             //CONTROL LOOP:
             if(chnlStatus[chnl].status == 1){
-                if(DEBUG3) pc.printf("DBG: [%d] Temp: %f \r\n", chnl, currentTempFront);
+                if(DEBUG3) pc.printf("DBG: [%d] Temp: %f %f\r\n", chnl, currentTempFront, currentTempBack);
 
                 //Error check on fixture
                 //error_check(chnl, currentTempFront, currentTempBack, valveCurrent, heaterCurrent);
-                if((currentTempFront >= 70) || (currentTempBack >= 70)){
-                    status_led(chnl, STATUS_BAD);        
+                if(((currentTempFront >= MAX_TEMP) && (currentTempBack >= MAX_TEMP)) || 
+                   ((currentTempFront == 0) && (currentTempBack == 0))||
+                    (abs(currentTempBack - currentTempFront) >= TEMP_MARGIN)){
+                    status_led(chnl, STATUS_BAD);
+                    sendError(chnl, 1);
+                    chnlStatus[chnl].error = 1;
                 }
 
                 if(chnlStatus[chnl].error == 0){