Control Code with I/O and ADC working

Dependencies:   MODSERIAL mbed

Revision:
6:c980535393ed
Parent:
5:0f38a0bd4f86
Child:
7:8a5e65e63e2a
--- a/main.cpp	Thu Mar 01 21:13:55 2018 +0000
+++ b/main.cpp	Wed Mar 07 18:50:10 2018 +0000
@@ -3,30 +3,32 @@
 
 #include "mbed.h"
 #include "MODSERIAL.h"
-#include "MCP23008.h"
+#include "MCP23008.h"A
 #include "LTC2487.h"
 #include <string>
 
 //DEFINITIVE VARIABLES
-#define DEBUG         0
-#define DEBUG1        1
-#define DEBUG2        1
-#define DEBUG3        1
-#define CHN_COUNT     8
-#define MIN_TEMP      15
-#define MAX_TEMP      60
-#define HYST          0.2
-#define SAMPLES       5
-#define I2C_Freq      2000
-#define VALVE         1
-#define HEATER        2
-#define STATUS_GOOD   3
-#define STATUS_BAD    4
-#define sizeLUT       34
-#define FRONT_THERM   0
-#define BACK_THERM    1
-#define HEAT_FET_AMP  2
-#define VALV_FET_AMP  3
+#define DEBUG           0
+#define DEBUG1          1
+#define DEBUG2          1
+#define DEBUG3          1
+#define CHN_COUNT       8
+#define MIN_TEMP        15
+#define MAX_TEMP        60
+#define TEMP_MARGIN     2
+#define HYST            0.5
+#define SAMPLES         5
+#define I2C_Freq        2000
+#define VALVE           1
+#define HEATER          2
+#define STATUS_GOOD     3
+#define STATUS_BAD      4
+#define sizeLUT         34
+#define FRONT_THERM     0
+#define BACK_THERM      1
+#define HEAT_FET_AMP    2
+#define VALV_FET_AMP    3
+#define FET_ON_CURRENT  1.12  
 
 
 //TCTF CHANNEL DATA
@@ -36,14 +38,14 @@
 };
 
 CHNL_DATA chnlStatus[] = {
-    {1, 40},
-    {1, 40},
-    {1, 40},
+    {1, 15},
+    {1, 20},
+    {1, 25},
+    {1, 30},
+    {1, 35},
+    {1, 0},
     {1, 40},
-    {1, 40},
-    {1, 40},
-    {1, 40},
-    {1, 40},
+    {1, 45},
     {0, NULL},
     {0, NULL},
     {0, NULL},
@@ -307,8 +309,8 @@
    Returns: the current into the heater control MOSFET
 */
 
-float get_heater_current(int chn){
-    return 0.0;
+float get_heater_current(int chn, int port){
+    return ltc2487.readOutput(port);
 }
 
 /* Function: get_valve_current
@@ -318,8 +320,8 @@
    Returns: the current into the valve control MOSFET
 */
 
-float get_valve_current(int chn){
-    return 0.0;
+float get_valve_current(int chn, int port){
+    return ltc2487.readOutput(port);
 }
 
 /* Function: turn_valve_on
@@ -423,34 +425,61 @@
 void test_ltc2487(int chn){
     get_temp(chn, FRONT_THERM);
     wait(0.1);
-    //get_temp(chn, BACK_THERM);
-    //wait(0.1);
-    //get_temp(chn, VALV_FET_AMP);
-    //wait(0.1);
+    get_temp(chn, BACK_THERM);
+    wait(0.1);
+    get_temp(chn, VALV_FET_AMP);
+    wait(0.1);
     //if(DEBUG1) pc.printf("TEMPERATURE READING: %f \r\n", get_temp(chn));
 }
 
-void test_all_IO(){
-    test_mcp23008(0);
-    test_mcp23008(1);
-    test_mcp23008(2);
-    test_mcp23008(3);
-    test_mcp23008(4);
-    test_mcp23008(5);
-    test_mcp23008(6);
-    test_mcp23008(7);
+/* Function: error_check
+   **************************************************************
+   Description: Checks for any system errors
+   Recieves: frontTemp: 
+             backTemp: 
+             valveFet:
+             heaterFet:
+   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){
+        //ERROR 6: Thermistor reading off
+    }   
+    
+    //CHECK IF HEATER'S STUCK ON OR CELL OVERHEATING
+    if((frontTemp >= (setTemp+TEMP_MARGIN)) || (backTemp >= (setTemp+TEMP_MARGIN))){
+         //ERROR 0
+         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   
+         }
+    }
+    
+    //CHECK IF VALVE STUCK ON OR CELL OVERHEATING
+    if((frontTemp <= (setTemp-TEMP_MARGIN)) || (backTemp <= (setTemp-TEMP_MARGIN))){
+        //ERROR 0
+        status_led(chn, STATUS_BAD);
+        if(heaterFet <= FET_ON_CURRENT){
+        //ERROR 2: Heater FET stuck off   
+        }
+        else if(valveFet >= FET_ON_CURRENT){
+        //ERROR 3: Chiller FET stuck on 
+        }
+    }    
 }
 
-void test_all_ADC(){
-    test_ltc2487(0);
-    test_ltc2487(1);
-    test_ltc2487(2);
-    test_ltc2487(3);
-    test_ltc2487(4);
-    test_ltc2487(5);
-    test_ltc2487(6);
-    test_ltc2487(7);
-}
+
+
+/*************************************************************/
+/*                       MAIN FUNCTION                       */
+/*************************************************************/
 
 
 int main() {
@@ -475,6 +504,7 @@
     
     
     while(1) {
+        //test_all_IO();
         if(DEBUG3) pc.printf("THE PROGRAM STARTED \r\n");
         
         //check if we recieved data/need to update TCTF data
@@ -489,22 +519,19 @@
         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 currentTempBack = get_temp(chnl, BACK_THERM);
+            float currentTemp = (currentTempFront + currentTempBack)/2;
             
             //CONTROL LOOP:
             if(chnlStatus[chnl].status == 1){
                 if(DEBUG3) pc.printf("TEMP: %f \r\n", currentTempFront);
-                if(currentTempFront > ((chnlStatus[chnl].setTemp)+HYST)){
+                if(currentTemp > ((chnlStatus[chnl].setTemp)+HYST)){
                     if(DEBUG3) pc.printf("CHILLER ON \r\n");
-                    //Turn heater off
-                    //turn_heater_off(chnl);
                     //Turn chiller on
                     turn_valve_on(chnl); 
                 }
-                else if (currentTempFront < ((chnlStatus[chnl].setTemp)-HYST)){
+                else if (currentTemp < ((chnlStatus[chnl].setTemp)-HYST)){
                     if(DEBUG3) pc.printf("HEATER ON \r\n");
-                    //Turn chiller off
-                    turn_valve_off(chnl); 
                     //Turn heater on
                     turn_heater_on(chnl);
                 }