Control Code with I/O and ADC working

Dependencies:   MODSERIAL mbed

Revision:
2:bd118a724f03
Parent:
1:0182b86f9bd4
Child:
3:0c9476da0cad
--- a/main.cpp	Mon Feb 05 06:41:23 2018 +0000
+++ b/main.cpp	Thu Feb 08 23:10:52 2018 +0000
@@ -6,6 +6,7 @@
 #include "MODSERIAL.h"
 #include "MCP23008.h"
 #include "LTC2487.h"
+#include <string>
 
 //DEFINITIVE VARIABLES
 #define DEBUG         1
@@ -25,8 +26,67 @@
 #define HEAT_FET_AMP  2
 #define VALV_FET_AMP  3
 
+
+//TCTF CHANNEL DATA
+struct CHNL_DATA{
+   bool status;
+   float setTemp;
+};
+
+CHNL_DATA channelLUT[] = {
+    {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},
+    {0, NULL},
+    {0, NULL},
+    {0, NULL},
+    {0, NULL},
+    {0, NULL},
+    {0, NULL},
+    {0, NULL},
+    {0, NULL},  
+};
+
+
 //I2C AADRESS LOOK UP TABLE, CREATED IN EXCEL SHEET (COUNT, TEMP)
-const struct I2C_ADDR_LUT{
+struct I2C_ADDR_LUT{
    int adc;
    int io;
 };
@@ -83,7 +143,7 @@
 };
 
 //THERMISTOR LOOK UP TABLE, CREATED IN EXCEL SHEET (COUNT, TEMP)
-const struct THERM_LUT{
+struct THERM_LUT{
    int adc;
    int temp;
 };
@@ -125,35 +185,83 @@
     {2284,125}
 };
 
+//SERIAL COMMUNICATION SETUP
+MODSERIAL pc(USBTX, USBRX);
 
 //DEFINE PINS
 DigitalOut myled(LED2);
 
 //I2C FOR MCP23008 (I/O Control)
-//I2C i2c_IO(PTC9, PTC8); //sda, scl
-MCP23008 io_control(PTC9, PTC8, 0x10, 100000);
-MODSERIAL pc(USBTX, USBRX);
+MCP23008 io_control(PTC9, PTC8, 0x10, 100000); //sda, scl
 
 //I2C FOR LTC2487 (ADC Control)
-//I2C i2c_ADC(PTC11, PTC10); //sda, scl
-LTC2487 ltc2487(PTC11, PTC10, 0x23, 100000);
+LTC2487 ltc2487(PTC11, PTC10, 0x23, 100000); //sda, scl
 
 //GLOBAL VARIABLES
 //channel status variables: init. to off and 0 degrees
 float chTemps[CHN_COUNT] = {};
 float chGoalTemps[CHN_COUNT] = {};  
 int chStatus[CHN_COUNT] = {};
-
+volatile bool dataRecieved = false; //used to check if data has been recieved
+char rxBuf[50];
+int chnlSel;
 
-/* Function: set_io_status
+/* Function: rxInterrupt
    **************************************************************
-   Description: Update i/o channels status for this TCTF channel
-   Recieves: chn: the channel of the fixture to read temp. from
+   Description: serial rx interupt handler 
+   Recieves: N/A
+   Returns: N/A     
+*/
+
+void rxInterrupt(MODSERIAL_IRQ_INFO *info){
+    dataRecieved = true;
+    if(DEBUG) pc.printf("DATA RECIEVED \r\n");
+}
+
+/* Function: parseRXData
+   **************************************************************
+   Description: The parse recieved data into 
+   Recieves: chn: The channel we want to put into the channel
    Returns: N/A     
 */
 
-void set_io_status(int chn){
+void parseRXData(){
+    int pCount = 0; //count data collected
+    int i = 0;
+    int chnl;
+    string data = "";
+
+    if(DEBUG) pc.printf("buff1 = <%s> \r\n", rxBuf);
     
+    while(pCount < 3){
+        if(rxBuf[i] != '.'){
+            data += rxBuf[i];
+        }
+        else{
+            pCount++; 
+            if(pCount == 1){ //get channel
+                if((atoi(data.c_str()) < 0) || (atoi(data.c_str()) > 15)){
+                    //check if channel is out of array index
+                    if(DEBUG) pc.printf("ERROR: INDEX OUT OF RANGE! \r\n", rxBuf);
+                }
+                else{
+                    chnl = atoi(data.c_str());
+                    chnl = chnl-1;  //fix for array indexing
+                    chnlSel = chnl; //chnl selected by user in GUI
+                }
+            }
+            else if(pCount == 2){ //get channel temperature
+                channelLUT[chnl].setTemp = atoi(data.c_str());
+                if(DEBUG) pc.printf("CHANNEL TEMP: %f \r\n", channelLUT[chnl].setTemp);
+            }
+            else if(pCount == 3){ //get channel status
+                channelLUT[chnl].status = atoi(data.c_str());
+                if(DEBUG) pc.printf("CHANNEL STATUS: %f \r\n", channelLUT[chnl].status);
+            } 
+            data = "";  
+        }
+        i++;
+    }
 }
 
 /* Function: get_temp
@@ -185,13 +293,11 @@
     float m = a/b;
     float y = (m*(ADC_val-thermLUT[i].adc))+thermLUT[i].temp;
     
-    pc.printf("ADC VAL: %f TEMP: %f \r\n", ADC_val, y);
+    if(DEBUG) pc.printf("ADC VAL: %f TEMP: %f \r\n", ADC_val, y);
     
     return y;  
-    
 }
 
-
 /* Function: get_heater_current
    **************************************************************
    Description: Retrieve current into heater control MOSFET
@@ -305,22 +411,62 @@
     status_led(chn, 1);
 }
 
+/* Function: test_ltc2487
+   **************************************************************
+   Description: Test the reading from LTC2487
+   Recieves: N/A
+   Returns: N/A
+*/
+
+void test_ltc2487(int chn){
+    if(DEBUG) pc.printf("TEMPERATURE READING: %f \r\n", get_temp(1));
+}
+
 
 
 int main() {
-    //GPIO TEST
-    
-    //ADC TEST
     
     myled = 1;
-    //get_temp(FRONT_THERM);
     
     while(1) {
-        //if(DEBUG) pc.printf("FSTART \r\n");
-        //get_temp(1);
-        test_mcp23008(1);
-        test_mcp23008(0);
-        //wait(0.5);
-       // myled = 0;
+        if(DEBUG) pc.printf("THE PROGRAM STARTED \r\n");
+        
+        //check if we recieved data/need to update TCTF data
+        if(dataRecieved){
+            dataRecieved = false;
+            pc.autoDetectChar('\n');
+            pc.move(rxBuf, 50);
+            pc.rxBufferFlush();
+            parseRXData();
+        }
+        
+        for(int chnl = 0; chnl <= CHN_COUNT; chnl++){
+            float currentTemp = get_temp(chnl);
+            
+            //CONTROL LOOP:
+            if(chStatus[chnl] == 1){
+                if(chTemps[chnl] > ((chGoalTemps[chnl])+HYST)){
+                    //Turn chiller on
+                    turn_valve_on(chnl); 
+                    //Turn heater off
+                    turn_heater_off(chnl);
+                }
+                else if (chTemps[chnl] < ((chGoalTemps[chnl])-HYST)){
+                    //Turn chiller off
+                    turn_valve_off(chnl); 
+                    //Turn heater on
+                    turn_heater_on(chnl);
+                }
+                else{
+                    //turn off chiller
+                    turn_valve_off(chnl);
+                    //turn off heater 
+                    turn_heater_off(chnl);
+                    //turn on green LED status light
+                    status_led(chnl, 1); 
+                }   
+            }
+        }
+        
     }
 }