Working on rewriting how we acquire data from LTC chip and sending all temp data over serial for python logging

Dependencies:   MODSERIAL mbed

Fork of TCTF_Control_Main by Rivian Irvine Team

Revision:
7:8a5e65e63e2a
Parent:
6:c980535393ed
Child:
8:dbf8bd4815f8
--- a/main.cpp	Wed Mar 07 18:50:10 2018 +0000
+++ b/main.cpp	Tue Mar 20 19:22:55 2018 +0000
@@ -15,8 +15,8 @@
 #define CHN_COUNT       8
 #define MIN_TEMP        15
 #define MAX_TEMP        60
-#define TEMP_MARGIN     2
-#define HYST            0.5
+#define TEMP_MARGIN     5
+#define HYST            0.3
 #define SAMPLES         5
 #define I2C_Freq        2000
 #define VALVE           1
@@ -27,65 +27,68 @@
 #define FRONT_THERM     0
 #define BACK_THERM      1
 #define HEAT_FET_AMP    2
-#define VALV_FET_AMP    3
-#define FET_ON_CURRENT  1.12  
+#define VALVE_FET_AMP    3
+#define FET_ON_CURRENT  1.12
+#define ROOM_TEMP       22 
+ 
 
 
 //TCTF CHANNEL DATA
 struct CHNL_DATA{
    bool status;
    float setTemp;
+   bool error;
 };
 
 CHNL_DATA chnlStatus[] = {
-    {1, 15},
-    {1, 20},
-    {1, 25},
-    {1, 30},
-    {1, 35},
-    {1, 0},
-    {1, 40},
-    {1, 45},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},
-    {0, NULL},  
+    {1, 16, 0},
+    {1, 56, 0},
+    {1, 31, 0},
+    {1, 30, 0},
+    {1, 15, 0},
+    {1, 0, 0},
+    {1, 40, 0},
+    {1, 45, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},
+    {0, NULL, 0},  
 };
 
 
@@ -196,16 +199,30 @@
 DigitalOut myled(LED2);
 
 //I2C FOR MCP23008 (I/O Control)
-MCP23008 io_control(PTC9, PTC8, 0x10, 100000); //sda, scl
+MCP23008 io_control(PTC9, PTC8, 0x10, 400000); //sda, scl
 
 //I2C FOR LTC2487 (ADC Control)
-LTC2487 ltc2487(PTC11, PTC10, 0x23, 100000); //sda, scl
+LTC2487 ltc2487(PTC11, PTC10, 0x23, 200000); //sda, scl
 
 //GLOBAL VARIABLES
 volatile bool dataRecieved = false; //used to check if data has been recieved
 char rxBuf[50];
 int chnlSel;
 
+
+/* Function: turnOffChannel
+   **************************************************************
+   Description: Turns off a channel
+   Recieves: chnl: channel to turn off
+   Returns: N/A
+*/
+
+void turnOffChannel(int chnl){
+    io_control.setAddress(addrLUT[chnl].io);
+    io_control.init();
+    io_control.writeOutput(0,0,0,0);
+}
+
 /* Function: rxInterrupt
    **************************************************************
    Description: serial rx interupt handler 
@@ -255,7 +272,9 @@
                 if(DEBUG) pc.printf("CHANNEL TEMP: %f \r\n", chnlStatus[chnl].setTemp);
             }
             else if(pCount == 3){ //get channel status
+                //IF CHANNEL IS TURNED OFF, TURN THE STATUS OFF FIRST!  
                 chnlStatus[chnl].status = atoi(data.c_str());
+                if(chnlStatus[chnl].status == 0) turnOffChannel(chnl);
                 if(DEBUG) pc.printf("CHANNEL STATUS: %f \r\n", chnlStatus[chnl].status);
             } 
             data = "";  
@@ -264,6 +283,8 @@
     }
 }
 
+
+
 /* Function: get_temp
    **************************************************************
    Description: Retrieve data from thermistor
@@ -403,15 +424,15 @@
 
 void test_mcp23008(int chn){
     turn_valve_on(chn);
-    wait(1);
+    wait(0.5);
     turn_valve_off(chn);
-    wait(1);
+    wait(0.5);
     turn_heater_on(chn);
-    wait(1);
+    wait(0.5);
     turn_heater_off(chn);
-    wait(1);
+    wait(0.5);
     status_led(chn, 0);
-    wait(1);
+    wait(0.5);
     status_led(chn, 1);
 }
 
@@ -427,44 +448,47 @@
     wait(0.1);
     get_temp(chn, BACK_THERM);
     wait(0.1);
-    get_temp(chn, VALV_FET_AMP);
-    wait(0.1);
+    //get_temp(chn, VALV_FET_AMP);
+    //wait(0.1);
     //if(DEBUG1) pc.printf("TEMPERATURE READING: %f \r\n", get_temp(chn));
 }
 
 /* Function: error_check
    **************************************************************
    Description: Checks for any system errors
-   Recieves: frontTemp: 
-             backTemp: 
-             valveFet:
-             heaterFet:
+   Recieves: frontTemp: temp. of front thermistor
+             backTemp: temp. of back thermistor
+             valveFet: current through valve FET
+             heaterFet: current through heater FET
    Returns: N/A
 */
 
 void error_check(int chn, float frontTemp, float backTemp, float valveFet, float heaterFet){
     float setTemp = chnlStatus[chn].setTemp;
     
-    //CHECK IF THERMISTOR READINGS ARE OFF (< 2 DEGREES)
-    if(abs(frontTemp-backTemp) > 2){
+    //CHECK IF THERMISTOR READINGS ARE OFF (> 5 DEGREES)
+    if(abs(frontTemp-backTemp) > 5){
         //ERROR 6: Thermistor reading off
+        chnlStatus[chn].error = 1;
     }   
     
     //CHECK IF HEATER'S STUCK ON OR CELL OVERHEATING
     if((frontTemp >= (setTemp+TEMP_MARGIN)) || (backTemp >= (setTemp+TEMP_MARGIN))){
          //ERROR 0
+         chnlStatus[chn].error = 1;
          status_led(chn, STATUS_BAD);
          if(heaterFet >= FET_ON_CURRENT){
          //ERROR 1: Heater FET stuck on   
          }
          if(valveFet <= FET_ON_CURRENT){
-         //ERROR 1: valve FET stuck off   
+         //ERROR 2: valve FET stuck off   
          }
     }
     
     //CHECK IF VALVE STUCK ON OR CELL OVERHEATING
     if((frontTemp <= (setTemp-TEMP_MARGIN)) || (backTemp <= (setTemp-TEMP_MARGIN))){
         //ERROR 0
+        chnlStatus[chn].error = 1;
         status_led(chn, STATUS_BAD);
         if(heaterFet <= FET_ON_CURRENT){
         //ERROR 2: Heater FET stuck off   
@@ -490,17 +514,7 @@
     
     myled = 1;
     
-    
-    //test_mcp23008(1);
     //FRONT_THERM, BACK_THERM, HEAT_FET_AMP, VALV_FET_AMP
-    /*while(1){
-        myled = 0;
-        //test_all_ADC();
-        //test_ltc2487(0);
-        test_all_IO();
-        wait(1);
-        myled = 1;
-    }*/
     
     
     while(1) {
@@ -517,33 +531,41 @@
         }
         
         for(int chnl = 0; chnl < CHN_COUNT; chnl++){
-            //wait(2.5);
             float currentTempFront = get_temp(chnl, FRONT_THERM);
             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 + currentTempBack)/2;
             
             //CONTROL LOOP:
             if(chnlStatus[chnl].status == 1){
                 if(DEBUG3) pc.printf("TEMP: %f \r\n", currentTempFront);
-                if(currentTemp > ((chnlStatus[chnl].setTemp)+HYST)){
-                    if(DEBUG3) pc.printf("CHILLER ON \r\n");
-                    //Turn chiller on
-                    turn_valve_on(chnl); 
-                }
-                else if (currentTemp < ((chnlStatus[chnl].setTemp)-HYST)){
-                    if(DEBUG3) pc.printf("HEATER ON \r\n");
-                    //Turn heater on
-                    turn_heater_on(chnl);
-                }
-                else{
-                    if(DEBUG3) pc.printf("ALL OFF \r\n");
-                    //turn off chiller
-                    turn_valve_off(chnl);
-                    //turn off heater 
-                    turn_heater_off(chnl);
-                    //turn on green LED status light
-                    status_led(chnl, 1); 
-                }   
+
+                //Error check on fixture
+                error_check(chnl, currentTempFront, currentTempBack, valveCurrent, heaterCurrent);
+                
+
+                if(chnlStatus[chnl].error == 0){
+                    if((currentTemp > ((chnlStatus[chnl].setTemp)+HYST))){
+                        if(DEBUG3) pc.printf("CHILLER ON \r\n");
+                        //Turn chiller on
+                        turn_valve_on(chnl); 
+                    }
+                    else if (currentTemp < ((chnlStatus[chnl].setTemp)-HYST)){
+                        if(DEBUG3) pc.printf("HEATER ON \r\n");
+                        //Turn heater on
+                        turn_heater_on(chnl);
+                    }
+                    else{
+                        if(DEBUG3) pc.printf("ALL OFF \r\n");
+                        //turn off chiller
+                        turn_valve_off(chnl);
+                        //turn off heater 
+                        turn_heater_off(chnl);
+                        //turn on green LED status light
+                        status_led(chnl, 1); 
+                    } 
+                }  
             }
         }
     }