![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Control Code with I/O and ADC working
Diff: main.cpp
- Revision:
- 16:82d941b1ef21
- Parent:
- 15:74a01aaeb60e
- Child:
- 17:96c064f773ab
--- a/main.cpp Mon Jun 11 21:10:14 2018 +0000 +++ b/main.cpp Wed Jun 20 00:30:48 2018 +0000 @@ -9,30 +9,33 @@ #include <string> //DEFINITIVE VARIABLES -#define DEBUG 0 -#define DEBUG1 0 -#define DEBUG2 0 -#define DEBUG3 1 -#define DEBUG5 0 -#define CHN_COUNT 8 -#define MIN_TEMP 10 -#define MAX_TEMP 65 -#define TEMP_MARGIN 20 -#define HYST_LOW 0.3 -#define HYST_HIGH 1 -#define SAMPLES 5 -#define I2C_Freq 2000 -#define VALVE 1 -#define HEATER 2 -#define STATUS_GOOD 3 -#define STATUS_BAD 0 -#define sizeLUT 34 -#define BACK_THERM 0 -#define FRONT_THERM 1 -#define HEAT_FET_AMP 2 -#define VALVE_FET_AMP 3 -#define FET_ON_CURRENT 1.12 -#define ROOM_TEMP 22 +#define DEBUG 0 +#define DEBUG1 0 +#define DEBUG2 0 +#define DEBUG3 0 +#define DEBUG5 0 +#define UID_PRINT 1 +#define TEMP_PRINT 0 +#define LOOP_TIME 0 +#define CHN_COUNT 8 +#define MIN_TEMP 10 +#define MAX_TEMP 65 +#define TEMP_MARGIN 20 +#define HYST_LOW 0.3 +#define HYST_HIGH 1 +#define SAMPLES 5 +#define I2C_Freq 2000 +#define VALVE 1 +#define HEATER 2 +#define STATUS_GOOD 3 +#define STATUS_BAD 0 +#define sizeLUT 34 +#define BACK_THERM 0 +#define FRONT_THERM 1 +#define HEAT_FET_AMP 2 +#define VALVE_FET_AMP 3 +#define FET_ON_CURRENT 1.12 +#define ROOM_TEMP 22 #define MAX_HEATER_ON_TIME 25 #define MAX_CHILL_TIME 10 //10sec @@ -95,6 +98,15 @@ } RESPONSE_CMD; typedef struct { + unsigned char SOF_flag; + unsigned char cmd_type; + unsigned char len; + unsigned char Delim; + float chTemp[CHN_COUNT]; + unsigned char EOF_flag; +} RESPONSE_TEMP_CMD; + +typedef struct { HOST_CMD_HEADER cmd_header; unsigned char chanIDSel; unsigned char chanDelim; @@ -272,6 +284,14 @@ {2284,125} }; +//TCTF CHANNEL TEMPERATURE +typedef struct{ + float currTempFront; + float currTempBack; +}CHNL_TEMP; + +CHNL_TEMP channelTempData[CHN_COUNT]; + //SERIAL COMMUNICATION SETUP MODSERIAL pc(USBTX, USBRX); @@ -415,21 +435,18 @@ } } - - /* Function: get_temp ************************************************************** - Description: Retrieve data from thermistor - 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) + Description: Convert A/D count to temperature value + Receives: ADC_val: the count from the A/D reading + Returns: the temp. of the A/D reading */ -float get_temp(int chn, int port){ +float get_temp(float ADC_val){ myled = 1; - ltc2487.setAddress(addrLUT[chn].adc); + //ltc2487.setAddress(addrLUT[chn].adc); - float ADC_val = ltc2487.readOutput(port); //(65536*1.334)/2.5 + //float ADC_val = ltc2487.readOutput(port); //(65536*1.334)/2.5 int i = 0; @@ -447,11 +464,44 @@ float m = a/b; float y = (m*(ADC_val-thermLUT[i].adc))+thermLUT[i].temp; - if(DEBUG2) pc.printf("DBG: CHAN: %i PORT: %i ADC VAL: %f TEMP: %f \r\n", chn, port, ADC_val, y); + if(DEBUG3) pc.printf("DBG: ADC VAL: %f TEMP: %f \r\n", ADC_val, y); return y; } +/* Function: get_temp_DATA + ************************************************************** + Description: Read A/D data from LTC2487 and set array with front and back temp. + Receives: N/A + Returns: N/A +*/ + +void get_temp_data(){ + //Write to all 8 channels selecting the front port to read + for(int chnl = 0; chnl < CHN_COUNT; chnl++){ + ltc2487.setAddress(addrLUT[chnl].adc); + ltc2487.writePort(FRONT_THERM); + } + //wait until next clock cycle on LTC + wait(0.08); + for(int chnl = 0; chnl < CHN_COUNT; chnl++){ + ltc2487.setAddress(addrLUT[chnl].adc); + channelTempData[chnl].currTempFront = get_temp(ltc2487.read()); + } + //wait until next clock cycle on LTC + wait(0.08); + for(int chnl = 0; chnl < CHN_COUNT; chnl++){ + ltc2487.setAddress(addrLUT[chnl].adc); + ltc2487.writePort(BACK_THERM); + } + //wait until next clock cycle on LTC + wait(0.08); + for(int chnl = 0; chnl < CHN_COUNT; chnl++){ + ltc2487.setAddress(addrLUT[chnl].adc); + channelTempData[chnl].currTempBack = get_temp(ltc2487.read()); + } +} + /* Function: get_heater_current ************************************************************** Description: Retrieve current into heater control MOSFET @@ -460,7 +510,11 @@ */ float get_heater_current(int chn, int port){ - return ltc2487.readOutput(port); + ltc2487.setAddress(addrLUT[chn].adc); + ltc2487.writePort(HEAT_FET_AMP); + wait(0.08); + ltc2487.read(); + wait(0.08); } /* Function: get_valve_current @@ -471,7 +525,11 @@ */ float get_valve_current(int chn, int port){ - return ltc2487.readOutput(port); + ltc2487.setAddress(addrLUT[chn].adc); + ltc2487.writePort(VALVE_FET_AMP); + wait(0.08); + ltc2487.read(); + wait(0.08); } /* Function: turn_valve_on @@ -573,30 +631,37 @@ Returns: N/A */ -void test_ltc2487(int chn){ - get_temp(chn, FRONT_THERM); - wait(0.1); - get_temp(chn, BACK_THERM); - wait(0.1); - get_temp(chn, VALVE_FET_AMP); - wait(0.1); - //if(DEBUG1) pc.printf("TEMPERATURE READING: %f \r\n", get_temp(chn)); +void test_ltc2487(){ + for(int chnl = 0; chnl < CHN_COUNT; chnl++){ + float frontTemp = channelTempData[chnl].currTempFront; + float backTemp = channelTempData[chnl].currTempBack; + pc.printf("TEMPERATURE READING [%i]:: BACK: %f FRONT: %f \r\n", chnl, backTemp, frontTemp); + } } //*************************************************************** // Build packet with temperature readings to send to GUI //*************************************************************** -void sendTempReadings (int chan, float currentTemp) -{ - RESPONSE_CMD response; +void sendTempReadings(){ + RESPONSE_TEMP_CMD response; unsigned char *ptr = (unsigned char *)&response; int i; response.SOF_flag = TX_SOF; response.cmd_type = TEMP_DATA; - response.len = 9; + response.len = 5+(sizeof(response.chTemp)); response.Delim = DELIMETER; - response.data = (float)currentTemp; + response.chTemp[0] = channelTempData[0].currTempBack; + response.chTemp[1] = channelTempData[1].currTempBack; + response.chTemp[2] = channelTempData[2].currTempBack; + response.chTemp[3] = channelTempData[3].currTempBack; + response.chTemp[4] = channelTempData[4].currTempBack; + response.chTemp[5] = channelTempData[5].currTempBack; + response.chTemp[6] = channelTempData[6].currTempBack; + response.chTemp[7] = channelTempData[7].currTempBack; + /*float *dst = (float *)&response.chTemp[0]; + float *src = (float *)&channelTempData[0]; + memcpy(dst, src, sizeof(channelTempData));*/ response.EOF_flag = TX_EOF; // Send response to GUI @@ -739,12 +804,12 @@ UIDL = (uint32_t)SIM->UIDL; while(1) { - - if(DEBUG3) { + //test_mcp23008(0); + if(UID_PRINT) { pc.printf("DBG: <%f>[UID: %04X%08X%08X] PSTARTED\n", t.read(), UIDMH, UIDML, UIDL); } - //setup timer used to track how long the heater is on + //SETUP TIMER USED TO TRACK HOW LONG THE HEATER IS ON float time_sec = t.read(); if(time_sec >= 60){ @@ -756,11 +821,18 @@ if(time_min >= 60){ time_min = 0; } - - //pc.printf("Time: %f \r\n", time_sec); + + //GET TEMPERATURE DATA + get_temp_data(); + //SEND TEMPERATURE DATA FOR LOGGING + sendTempReadings(); + + time_sec = t.read(); + if(LOOP_TIME) pc.printf("\r TIME: %f s\r\n", time_sec); + + //CONTROL LOOP: LOOPS THROUGH EVERY CHANNEL AND CONTROLS THE FIXTURE IN RESPONSE for(int chnl = 0; chnl < CHN_COUNT; chnl++){ - float currentTempFront = get_temp(chnl, FRONT_THERM); //check if we received data/need to update TCTF data if(dataReceived){ @@ -773,53 +845,32 @@ pc.rxBufferFlush(); } - float currentTempBack = get_temp(chnl, BACK_THERM); + //update channel temperature data + float currentTempFront = channelTempData[chnl].currTempFront; + float currentTempBack = channelTempData[chnl].currTempBack; + + //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); - } - //Error check on fixture error_check(chnl, currentTempFront, currentTempBack, time_min); + if(TEMP_PRINT) pc.printf("DBG: TEMPERATURE: [%d] Temp: F: %f B: %f\r\n", chnl, currentTempFront, currentTempBack); + //CONTROL LOOP: if(chnlStatus[chnl].status == 1){ if(DEBUG) pc.printf("DBG: [%d] Temp: F: %f B: %f\r\n", chnl, currentTempFront, currentTempBack); //main loop for channels if(chnlStatus[chnl].error == 0){ if(currentTemp > ((chnlStatus[chnl].setTemp)+HYST_HIGH)){ - if(DEBUG3) pc.printf("DBG: [%d] Chiller ON \r\n", chnl); + if(DEBUG) pc.printf("DBG: [%d] Chiller ON \r\n", chnl); //reset heater on time chnlStatus[chnl].heater_init_time = 0; - //reset cooling timer - float time_sec = 0; - //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){ - //start timer - t_cool.start(); - //turn chiller on - turn_valve_on(chnl); - //read timer - time_sec = t_cool.read(); - while((currentTemp > ((chnlStatus[chnl].setTemp)+HYST_HIGH)) && (time_sec < MAX_CHILL_TIME)){ - currentTemp = get_temp(chnl, BACK_THERM); - time_sec = t_cool.read(); - if(DEBUG5) pc.printf("DBG: [%d] TIME: %f \r\n", chnl, time_sec); - } - time_sec = 0; - t_cool.stop(); - t_cool.reset(); - turn_valve_off(chnl); - } - else{ - //Turn chiller on - turn_valve_on(chnl); - } + //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); + if(DEBUG) pc.printf("DBG: [%d] Heater ON \r\n", chnl); //establish starting time for heater if(chnlStatus[chnl].heater_init_time == 0){ //check if it is the first time that heater has turned on //make sure time isn't zero, otherwise keeping looping until we are not at 0 @@ -827,12 +878,12 @@ chnlStatus[chnl].heater_init_time = time_min; } } - if(DEBUG5) pc.printf("DBG: TIME ON: %d %d %f %d %d\r\n", chnlStatus[chnl].heater_init_time, time_min, time_sec, init_heat_time, (chnlStatus[chnl].heater_init_time == NULL)); + if(DEBUG2) pc.printf("DBG: TIME ON: %d %d %f %d %d\r\n", chnlStatus[chnl].heater_init_time, time_min, time_sec, init_heat_time, (chnlStatus[chnl].heater_init_time == NULL)); //Turn heater on turn_heater_on(chnl); } else{ - if(DEBUG3) pc.printf("DBG: [%d] All OFF \r\n", chnl); + if(DEBUG) pc.printf("DBG: [%d] All OFF \r\n", chnl); //reset heater on time chnlStatus[chnl].heater_init_time = 0; //turn off chiller @@ -852,7 +903,7 @@ turn_heater_off(chnl); //turn on green LED status light status_led(chnl, 1); - } + } else{ status_led(chnl, STATUS_BAD); }