Control Code with I/O and ADC working

Dependencies:   MODSERIAL mbed

Files at this revision

API Documentation at this revision

Comitter:
jrodenburg
Date:
Tue May 08 17:08:37 2018 +0000
Parent:
12:1cada1fe4743
Child:
14:69cd53434783
Commit message:
Working code with revised cooling (if within 5 degrees will cool until near set point), has run on a row and is functional

Changed in this revision

LTC2487/LTC2487.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/LTC2487/LTC2487.cpp	Mon May 07 03:38:15 2018 +0000
+++ b/LTC2487/LTC2487.cpp	Tue May 08 17:08:37 2018 +0000
@@ -42,6 +42,7 @@
     char ADC_data_rx[3];
     char ADC_config[1];
     ADC_config[0] = 0b10001000;//0x82; //0b10000010
+    char cmd[2];
         
     //select channel to read
     switch (chnl){
@@ -59,13 +60,18 @@
             break;
     } 
     
-    //send message to select channel
+    cmd[0] = ADC_channel[0];
+    cmd[1] = ADC_config[0];
+    
+    i2c.write((addrI2C<<1)|(I2C_WRITE), cmd, 2);
+    
+    /*//send message to select channel
     i2c.write((addrI2C<<1)|(I2C_WRITE), ADC_channel, 1);
     //must wait, otherwise breaks...
     wait(0.08);
     //send configuration (1 gain, autocalibration)
     i2c.write((addrI2C<<1)|(I2C_WRITE), ADC_config, 1);
-    //must wait, otherwise breaks...
+    //must wait, otherwise breaks...*/
     wait(0.08);
     //Read data from selected channel --> 24bits --> 23bit=SIGN 22bit=MSB 21-7bits=DATA 5-0bits=JUNK
     i2c.read((addrI2C<<1)|(I2C_READ), ADC_data_rx, 3);
--- a/main.cpp	Mon May 07 03:38:15 2018 +0000
+++ b/main.cpp	Tue May 08 17:08:37 2018 +0000
@@ -17,7 +17,7 @@
 #define MAX_TEMP         65
 #define TEMP_MARGIN      20
 #define HYST_LOW         0.3
-#define HYST_HIGH        1.5
+#define HYST_HIGH        1
 #define SAMPLES          5
 #define I2C_Freq         2000
 #define VALVE            1
@@ -662,15 +662,23 @@
         if(DEBUG3)
             pc.printf("DBG: PROGRAM STARTED \r\n");
 
+        /*float time_sec = t.read();
+        
+        if(time_sec >= 60){
+            t.reset();
+        }
+        
+        pc.printf("Time: %f \r\n", time_sec);*/
+
         for(int chnl = 0; chnl < CHN_COUNT; chnl++){
             float currentTempFront = get_temp(chnl, FRONT_THERM);
-            wait(0.08);
-            float currentTempBack = get_temp(chnl, BACK_THERM);
+            //wait(0.08);
+            
             //float valveCurrent = get_valve_current(chnl, VALVE_FET_AMP);
             //float heaterCurrent = get_heater_current(chnl, HEAT_FET_AMP);
-            float currentTemp = currentTempFront;
+            //float currentTemp = currentTempFront;
             //currentTemp = currentTempFront;
-            currentTemp = currentTempBack;
+            
 
             //check if we received data/need to update TCTF data
             if(dataReceived){
@@ -681,10 +689,15 @@
                 pc.rxBufferFlush();
             }
 
+            float currentTempBack = get_temp(chnl, BACK_THERM);
+            float currentTemp = currentTempBack;
+
             if (chnl == chanSel_SendTemp){
                 //Send temp readings for selected chan to GUI
                 sendTempReadings(currChan, currentTemp);
             }
+            
+            
 
             //CONTROL LOOP:
             if(chnlStatus[chnl].status == 1){
@@ -695,7 +708,8 @@
                 if(((currentTempFront >= MAX_TEMP) && (currentTempBack >= MAX_TEMP)) ||
                    ((currentTempFront == 0) || (currentTempBack == 0))||
                     (abs(currentTempBack - currentTempFront) >= TEMP_MARGIN)||
-                   ((currentTempFront <= MIN_TEMP) && (currentTempBack <= MIN_TEMP))){
+                   ((currentTempFront <= MIN_TEMP) && (currentTempBack <= MIN_TEMP))||
+                    (chnlStatus[chnl].error == 1)){
                     status_led(chnl, STATUS_BAD);
                     sendError(chnl, 1);
                     chnlStatus[chnl].error = 1;
@@ -704,8 +718,18 @@
                 if(chnlStatus[chnl].error == 0){
                     if(currentTemp > ((chnlStatus[chnl].setTemp)+HYST_HIGH)){
                         if(DEBUG3) pc.printf("DBG: [%d] Chiller ON \r\n", chnl);
-                        //Turn chiller on
-                        turn_valve_on(chnl);
+                        //check if the temp. diff. is small and can't wait for an entire period (~4.18) to turn valve off
+                        if(abs(currentTemp - (chnlStatus[chnl].setTemp)) < 5){
+                            turn_valve_on(chnl);
+                            while(currentTemp > ((chnlStatus[chnl].setTemp)+HYST_HIGH)){
+                                currentTemp = get_temp(chnl, BACK_THERM);
+                            }
+                            turn_valve_off(chnl);
+                        }
+                        else{
+                            //Turn chiller on
+                            turn_valve_on(chnl);    
+                        }
                     }
                     else if (currentTemp < ((chnlStatus[chnl].setTemp)-HYST_LOW)){
                         if(DEBUG3) pc.printf("DBG: [%d] Heater ON \r\n", chnl);
@@ -723,6 +747,14 @@
                     }
                 }
             }
+            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);
+            }
         }
     }
 }
\ No newline at end of file