TCTF Control Code V1

Dependencies:   MCP23017 MODSERIAL mbed

Files at this revision

API Documentation at this revision

Comitter:
jrodenburg
Date:
Thu Jan 11 22:51:35 2018 +0000
Parent:
0:11aaf2483a3c
Commit message:
File

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Sep 24 03:24:14 2017 +0000
+++ b/main.cpp	Thu Jan 11 22:51:35 2018 +0000
@@ -8,13 +8,17 @@
 //1 X MBED
 //1 X ADC MUX (16 CHN)
 
-//FORMAT FOR CELL DATA: CHANNEL.TEMPERATURE.STATUS.E 2.23.1.E
+//FORMAT FOR CELL DATA: CHANNEL.TEMPERATURE.STATUS.E 1.23.1.E, 1.23.1.E
 
 #include "mbed.h"
 #include "MCP23017.h"
 #include "MODSERIAL.h"
 #include <string>
 
+#ifdef DEBUG
+#define printf(fmt, ...) (0)
+#endif
+
 
 DigitalOut rLed(LED1);
 DigitalOut gLed(LED2);
@@ -89,9 +93,10 @@
 const int numChnls = 15;
 float chTemps[numChnls];
 float chGoalTemps[numChnls];  
-float chStatus[numChnls];
+int chStatus[numChnls];
 int numSamples = 5;
 volatile bool dataRecieved = false;
+int chnlSel;
 char rxBuf[50];
 
 void rxInterrupt(MODSERIAL_IRQ_INFO *info){
@@ -104,38 +109,43 @@
 void parseRXData(){
     int pCount = 0; //count data collected
     int i = 0;
-    int chnl = 0;
+    int chnl;
     
     string data = "";
+
+    //pc.printf("buff1 = <%s> \r\n", rxBuf);
     
     while(pCount < 3){
         if(rxBuf[i] != '.'){
             data += rxBuf[i];
         }
         else{
-            data = "";
             pCount++; 
             if(pCount == 1){ //get channel
-                if((pCount < 0) || (pCount > 15)){
+                if((atoi(data.c_str()) < 0) || (atoi(data.c_str()) > 15)){
                     //check if channel is out of array index
                     rLed = 0; 
                 }
                 else{
                     chnl = atoi(data.c_str());
-                    pc.printf("CHANNEL: %i \r\n", chnl);
+                    chnl = chnl-1;  //fix for array indexing
+                    chnlSel = chnl;
+                    //pc.printf("CHANNEL: %i \r\n", chnl);
                 }
             }
             else if(pCount == 2){ //get channel temperature
                 chGoalTemps[chnl] = atoi(data.c_str());
-                pc.printf("TEMPERATURE: %i \r\n", chTemps[chnl]);
+                //pc.printf("TEMPERATURE: %i \r\n", atoi(data.c_str()));
             }
             else if(pCount == 3){ //get channel status
-                chGoalTemps[chnl] = atoi(data.c_str());
-                pc.printf("STATUS: %i \r\n", chTemps[chnl]); 
-            }   
+                chStatus[chnl] = atoi(data.c_str());
+                //pc.printf("STATUS: %i \r\n", atoi(data.c_str())); 
+            } 
+            data = "";  
         }
         i++;
     }
+    //wait(30);
 }
 
 float getTemp(float ADC_val){ 
@@ -163,20 +173,27 @@
     
     a_0 = a_0/numSamples;
     
-    printf("ADC VAL: %f \r\n", a_0);
+    //printf("ADC VAL: %f \r\n", a_0);
 
     chTemps[0] = getTemp(a_0); 
     
-    printf("TEMPERATURE (C): %f \r\n", chTemps[0]);
+    //send current temperature of selected channel to GUI for display
+    pc.printf("%f \r\n", chTemps[chnlSel]);
+    
+    wait(1.5);
     
-    printf("TEMPERATURE (C): 15 \r\n");
+    //printf("TEMPERATURE (C): %f \r\n", chTemps[0]);
+    
+    //printf("TEMPERATURE (C): 15 \r\n");
+    
+    //printf("TEMPERATURE (C): 15 \r\n");
 }
 
 
     
 
 int main() {
-    printf("Main Entered.\n");
+    //printf("Main Entered.\n");
     //turn off LEDs
     rLed = 1;
     gLed = 1;
@@ -194,12 +211,12 @@
         readTemps();
         
         //buffer for noise
-        double hyst = 0.5;
+        double hyst = 0.2;
         
         //check if we recieved data/need to update TCTF data
         if(dataRecieved){
             dataRecieved = false;
-            pc.move(rxBuf, 100);
+            pc.move(rxBuf, 50);
             pc.rxBufferFlush();
             parseRXData();
         }
@@ -209,32 +226,35 @@
         //chiller = 18C
         for(int i=0; i<=numChnls; i++){
             //control chiller
-            if(chTemps[i] > ((chGoalTemps[i])+hyst)){
-                //Turn on chiller
-                printf("CHILLER ON \r\n");
-                mcp.config(0,0,0);
-                mcp.write_bit(1, 8); 
-                printf("HEATER OFF \r\n");
-                mcp.write_bit(0, 9);
+            if(chStatus[i] == 1){
+                if(chTemps[i] > ((chGoalTemps[i])+hyst)){
+                    //Turn on chiller
+                    //printf("CHILLER ON \r\n");
+                    mcp.config(0,0,0);
+                    mcp.write_bit(1, 8); 
+                    //printf("HEATER OFF \r\n");
+                    mcp.write_bit(0, 9);
+                }
+                else if (chTemps[i] < ((chGoalTemps[i])-hyst)){
+                    //turn off chiller
+                    //printf("CHILLER OFF \r\n");
+                    mcp.config(0,0,0);
+                    mcp.write_bit(0, 8); 
+                    //printf("HEATER ON  \r\n");
+                    mcp.write_bit(1, 9);
+                }
+                else{
+                    //turn off chiller
+                    //printf("CHILLER OFF \r\n");
+                    mcp.config(0,0,0);
+                    mcp.write_bit(0, 8); 
+                    //printf("HEATER OFF \r\n");
+                    mcp.write_bit(0, 9);
+                }
+                
+                
             }
-            else if (chTemps[i] < ((chGoalTemps[i])-hyst)){
-                //turn off chiller
-                printf("CHILLER OFF \r\n");
-                mcp.config(0,0,0);
-                mcp.write_bit(0, 8); 
-                printf("HEATER ON \r\n");
-                mcp.write_bit(1, 9);
-            }
-            else{
-                //turn off chiller
-                printf("CHILLER OFF \r\n");
-                mcp.config(0,0,0);
-                mcp.write_bit(0, 8); 
-                printf("HEATER OFF \r\n");
-                mcp.write_bit(0, 9);
-            }
-            printf("\r\n");
-            wait(5);
+            wait(0);
         }
     }
 }