![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Control Code with I/O and ADC working
Diff: main.cpp
- Revision:
- 9:8cd14861dc63
- Parent:
- 8:dbf8bd4815f8
- Child:
- 11:fe6c5aac5d89
--- a/main.cpp Wed Mar 28 01:25:07 2018 +0000 +++ b/main.cpp Mon Apr 02 00:00:04 2018 +0000 @@ -9,8 +9,8 @@ //DEFINITIVE VARIABLES #define DEBUG 0 -#define DEBUG1 1 -#define DEBUG2 1 +#define DEBUG1 0 +#define DEBUG2 0 #define DEBUG3 1 #define CHN_COUNT 8 #define MIN_TEMP 15 @@ -30,8 +30,8 @@ #define HEAT_FET_AMP 2 #define VALVE_FET_AMP 3 #define FET_ON_CURRENT 1.12 -#define ROOM_TEMP 22 - +#define ROOM_TEMP 22 + // Defines for use with Serial communication #pragma pack (1) #define RX_SOF 0x7B @@ -44,12 +44,14 @@ #define CMD_RESPONSE 0xD0 #define CMD_DATA 0xD1 + unsigned int chanSel_SendTemp = 0; unsigned int chanSel_SetTemp = 0; // Default to chan 0 int currChan = 0; bool newTempSet = false; + //*********************************************** // Serial Rx Packet Format //*********************************************** @@ -93,22 +95,6 @@ }; CHNL_DATA chnlStatus[] = { - /*TOP{1, 61, 0}, - {1, 61, 0}, - {1, 51, 0}, - {1, 51, 0}, - {1, 61, 0}, - {1, 61, 0}, - {1, 51, 0}, - {1, 51, 0},*/ - {1, 42, 0}, - {1, 41, 0}, - {1, 31, 0}, - {1, 31, 0}, - {1, 41, 0}, - {1, 41, 0}, - {1, 31, 0}, - {1, 31, 0}, {0, NULL, 0}, {0, NULL, 0}, {0, NULL, 0}, @@ -149,7 +135,14 @@ {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}, }; @@ -207,7 +200,7 @@ {0x6F, 0x5C}, {0x20, 0x1B}, {0x50, 0x6B}, - {0x40, 0x7B}, + {0x40, 0x7B}, }; //THERMISTOR LOOK UP TABLE, CREATED IN EXCEL SHEET (COUNT, TEMP) @@ -255,18 +248,19 @@ //SERIAL COMMUNICATION SETUP MODSERIAL pc(USBTX, USBRX); +MODSERIAL chain(PTA2, PTA1); //DEFINE PINS DigitalOut myled(LED2); //I2C FOR MCP23008 (I/O Control) -MCP23008 io_control(PTC9, PTC8, 0x10, 400000); //sda, scl +MCP23008 io_control(PTC9, PTC8, 0x10, 100000); //sda, scl //I2C FOR LTC2487 (ADC Control) -LTC2487 ltc2487(PTC11, PTC10, 0x23, 200000); //sda, scl +LTC2487 ltc2487(PTC11, PTC10, 0x23, 100000); //sda, scl //GLOBAL VARIABLES -volatile bool dataReceived = false; //used to check if data has been recieved +volatile bool dataReceived = false; //used to check if data has been received volatile bool dataTxReady = false; char rxBuf[50]; int chnlSel; @@ -318,9 +312,9 @@ /* Function: parseRXData ************************************************************** - Description: The parse recieved data into - Recieves: N/A - Returns: N/A + Description: The parse received data into + Receives: chn: The channel we want to put into the channel + Returns: N/A */ void parseRXData() @@ -385,12 +379,10 @@ } } - - /* Function: get_temp ************************************************************** Description: Retrieve data from thermistor - Recieves: chn: the channel of the fixture to read temp. from + Receives: chn: the channel of the fixture to read temp. from port: the I/O channel to read Returns: the temperature of the fixture (front or back) */ @@ -398,37 +390,34 @@ float get_temp(int chn, int port){ myled = 1; ltc2487.setAddress(addrLUT[chn].adc); - + float ADC_val = ltc2487.readOutput(port); //(65536*1.334)/2.5 - + int i = 0; - + while((i < sizeLUT) && (thermLUT[i].adc > ADC_val)){ - i++; + i++; } //find the temp. above therm temp - - //Point slope formula extrapolation: + + //Point slope formula extrapolation: // y1 = m (x1-x0)+ y0 , y = temp. value, x = adc value // y1 = thermLUT[i-1].temp y0 = thermLUT[i].temp // x1 = thermLUT[i-1].adc x0 =thermLUT[i].adc float a = float(thermLUT[i-1].temp - thermLUT[i].temp); //slope of temp between points where therm temp is between (Tmax - Tmin) float b = float(thermLUT[i-1].adc - thermLUT[i].adc); //slope of adc between points where therm adc is between (Amax - Amin) - + float m = a/b; float y = (m*(ADC_val-thermLUT[i].adc))+thermLUT[i].temp; - - if(DEBUG2) pc.printf("CHNL: %i PORT: %i ADC VAL: %f TEMP: %f \r\n", chn, port, ADC_val, y); - - //if(chn == chnlSel) - pc.printf("%f \r\n", y); - - return y; + + if(DEBUG2) pc.printf("DBG: CHAN: %i PORT: %i ADC VAL: %f TEMP: %f \r\n", chn, port, ADC_val, y); + + return y; } /* Function: get_heater_current ************************************************************** Description: Retrieve current into heater control MOSFET - Recieves: chn: the channel of the fixture to read current from + Receives: chn: the channel of the fixture to read current from Returns: the current into the heater control MOSFET */ @@ -439,7 +428,7 @@ /* Function: get_valve_current ************************************************************** Description: Retrieve current into valve control MOSFET - Recieves: chn: the channel of the fixture to read current from + Receives: chn: the channel of the fixture to read current from Returns: the current into the valve control MOSFET */ @@ -450,7 +439,7 @@ /* Function: turn_valve_on ************************************************************** Description: Turn valve on and green status LED on - Recieves: chn: the channel of the fixture + Receives: chn: the channel of the fixture Returns: N/A */ @@ -463,7 +452,7 @@ /* Function: turn_valve_off ************************************************************** Description: Turn valve off and green status LED on - Recieves: chn: the channel of the fixture + Receives: chn: the channel of the fixture Returns: N/A */ @@ -476,7 +465,7 @@ /* Function: turn_heater_on ************************************************************** Description: Turn heater on and green status LED on - Recieves: chn: the channel of the fixture + Receives: chn: the channel of the fixture Returns: N/A */ @@ -489,7 +478,7 @@ /* Function: turn_heater_off ************************************************************** Description: Turn heater off and green status LED on - Recieves: chn: the channel of the fixture + Receives: chn: the channel of the fixture Returns: N/A */ @@ -502,7 +491,7 @@ /* Function: status_led ************************************************************** Description: Turn status LED on (turns on green or red) - Recieves: chn: the channel of the fixture + Receives: chn: the channel of the fixture status: the status of channel (good (1) or bad (0)) Returns: N/A */ @@ -521,7 +510,7 @@ /* Function: test_mcp23008 ************************************************************** Description: Test each output of the MCP23009 - Recieves: N/A + Receives: N/A Returns: N/A */ @@ -542,7 +531,7 @@ /* Function: test_ltc2487 ************************************************************** Description: Test the reading from LTC2487 - Recieves: N/A + Receives: N/A Returns: N/A */ @@ -568,38 +557,38 @@ void error_check(int chn, float frontTemp, float backTemp, float valveFet, float heaterFet){ float setTemp = chnlStatus[chn].setTemp; - + //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 + //ERROR 1: Heater FET stuck on } if(valveFet <= FET_ON_CURRENT){ - //ERROR 2: 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 + //ERROR 2: Heater FET stuck off } else if(valveFet >= FET_ON_CURRENT){ - //ERROR 3: Chiller FET stuck on + //ERROR 3: Chiller FET stuck on } - } + } } //*************************************************************** @@ -625,15 +614,13 @@ } - /*************************************************************/ /* MAIN FUNCTION */ /*************************************************************/ +int main() { -int main() { - - Timer t; + Timer t; // Setup serial port // Look for RX_EOF @@ -646,16 +633,19 @@ gLed = 1; t.start(); - + while(1) { - if(DEBUG3) pc.printf("THE PROGRAM STARTED \r\n"); - + + //if(DEBUG3) + // pc.printf("DBG: PROGRAM STARTED \r\n"); + for(int chnl = 0; chnl < CHN_COUNT; chnl++){ 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; + if(currentTempFront >= currentTempBack){ currentTemp = currentTempFront; } @@ -668,7 +658,7 @@ //check if we received data/need to update TCTF data if(dataReceived){ dataReceived = false; - //pc.printf("DBG: %d bytes Rx'd\n", pc.rxBufferGetCount()); + pc.printf("DBG: %d bytes Rx'd\n", pc.rxBufferGetCount()); pc.move(rxBuf, 50); parseRXData(); pc.rxBufferFlush(); @@ -679,10 +669,10 @@ // Send temp readings for selected chan to GUI sendTempReadings(currChan, currentTemp); } - + //CONTROL LOOP: if(chnlStatus[chnl].status == 1){ - if(DEBUG3) pc.printf("TEMP: %f \r\n", currentTempFront); + if(DEBUG3) pc.printf("DBG: [%d] Temp: %f \r\n", chnl, currentTempFront); //Error check on fixture //error_check(chnl, currentTempFront, currentTempBack, valveCurrent, heaterCurrent); @@ -691,26 +681,26 @@ } if(chnlStatus[chnl].error == 0){ - if((currentTemp > ((chnlStatus[chnl].setTemp)+HYST_HIGH))){ - if(DEBUG3) pc.printf("CHILLER ON \r\n"); + 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); + turn_valve_on(chnl); } else if (currentTemp < ((chnlStatus[chnl].setTemp)-HYST_LOW)){ - if(DEBUG3) pc.printf("HEATER ON \r\n"); + if(DEBUG3) pc.printf("DBG: [%d] Heater ON \r\n", chnl); //Turn heater on turn_heater_on(chnl); } else{ - if(DEBUG3) pc.printf("ALL OFF \r\n"); + if(DEBUG3) pc.printf("DBG: [%d] All OFF \r\n", chnl); //turn off chiller turn_valve_off(chnl); - //turn off heater + //turn off heater turn_heater_off(chnl); //turn on green LED status light - status_led(chnl, 1); - } - } + status_led(chnl, 1); + } + } } } }