Rivian Irvine Team
/
TCTF_Control_Main
Control Code with I/O and ADC working
Diff: main.cpp
- Revision:
- 8:dbf8bd4815f8
- Parent:
- 7:8a5e65e63e2a
- Child:
- 9:8cd14861dc63
- Child:
- 12:1cada1fe4743
diff -r 8a5e65e63e2a -r dbf8bd4815f8 main.cpp --- a/main.cpp Tue Mar 20 19:22:55 2018 +0000 +++ b/main.cpp Wed Mar 28 01:25:07 2018 +0000 @@ -3,35 +3,87 @@ #include "mbed.h" #include "MODSERIAL.h" -#include "MCP23008.h"A +#include "MCP23008.h" #include "LTC2487.h" #include <string> //DEFINITIVE VARIABLES -#define DEBUG 0 -#define DEBUG1 1 -#define DEBUG2 1 -#define DEBUG3 1 -#define CHN_COUNT 8 -#define MIN_TEMP 15 -#define MAX_TEMP 60 -#define TEMP_MARGIN 5 -#define HYST 0.3 -#define SAMPLES 5 -#define I2C_Freq 2000 -#define VALVE 1 -#define HEATER 2 -#define STATUS_GOOD 3 -#define STATUS_BAD 4 -#define sizeLUT 34 -#define FRONT_THERM 0 -#define BACK_THERM 1 -#define HEAT_FET_AMP 2 +#define DEBUG 0 +#define DEBUG1 1 +#define DEBUG2 1 +#define DEBUG3 1 +#define CHN_COUNT 8 +#define MIN_TEMP 15 +#define MAX_TEMP 60 +#define TEMP_MARGIN 5 +#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 4 +#define sizeLUT 34 +#define FRONT_THERM 0 +#define BACK_THERM 1 +#define HEAT_FET_AMP 2 #define VALVE_FET_AMP 3 -#define FET_ON_CURRENT 1.12 -#define ROOM_TEMP 22 +#define FET_ON_CURRENT 1.12 +#define ROOM_TEMP 22 +// Defines for use with Serial communication +#pragma pack (1) +#define RX_SOF 0x7B +#define RX_EOF 0x7D +#define TX_SOF 0x7B +#define TX_EOF 0x7D +#define DELIMETER 0x2E +#define SET_TEMPERATURE 0xB0 +#define SELECT_CHANNEL 0xB1 +#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 +//*********************************************** +typedef struct { + unsigned char SOF_flag; + unsigned char cmd_type; + unsigned char len; + unsigned char Delim; +} HOST_CMD_HEADER; + +typedef struct { + HOST_CMD_HEADER cmd_header; + unsigned char chanID; + unsigned char tempDelim; + float setTemp; + unsigned char chanStatDelim; + unsigned char chanStat; +} SET_TEMPERATURE_CMD; + +typedef struct { + HOST_CMD_HEADER cmd_header; + unsigned char chanIDSel; + unsigned char chanDelim; + unsigned char chanStat; +} SELECT_CHANNEL_CMD; + +typedef struct { + unsigned char SOF_flag; + unsigned char cmd_type; + unsigned char len; + unsigned char Delim; + float data; + unsigned char EOF_flag; +} RESPONSE_CMD; //TCTF CHANNEL DATA struct CHNL_DATA{ @@ -41,14 +93,23 @@ }; CHNL_DATA chnlStatus[] = { - {1, 16, 0}, - {1, 56, 0}, + /*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, 30, 0}, - {1, 15, 0}, - {1, 0, 0}, - {1, 40, 0}, - {1, 45, 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}, {0, NULL, 0}, @@ -205,7 +266,8 @@ LTC2487 ltc2487(PTC11, PTC10, 0x23, 200000); //sda, scl //GLOBAL VARIABLES -volatile bool dataRecieved = false; //used to check if data has been recieved +volatile bool dataReceived = false; //used to check if data has been recieved +volatile bool dataTxReady = false; char rxBuf[50]; int chnlSel; @@ -223,63 +285,103 @@ io_control.writeOutput(0,0,0,0); } + /* Function: rxInterrupt ************************************************************** - Description: serial rx interupt handler - Recieves: N/A - Returns: N/A + Description: serial rx interupt handler + Receives: N/A + Returns: N/A */ -void rxInterrupt(MODSERIAL_IRQ_INFO *info){ - dataRecieved = true; - if(DEBUG) pc.printf("DATA RECIEVED \r\n"); +DigitalOut rLed(LED1); +DigitalOut gLed(LED2); + +//*********************************************** +// Rx Interrupt from serial Host interface +//*********************************************** +void rxInterrupt(MODSERIAL_IRQ_INFO *info) +{ + gLed = 0; + dataReceived = true; + gLed = 1; } + +//*************************************************************** +// Tx Interrupt from serial Host interface +//*************************************************************** +void txInterrupt(MODSERIAL_IRQ_INFO *info) +{ + dataTxReady = true; +} + + /* Function: parseRXData ************************************************************** Description: The parse recieved data into - Recieves: chn: The channel we want to put into the channel + Recieves: N/A Returns: N/A */ -void parseRXData(){ - int pCount = 0; //count data collected - int i = 0; - int chnl; +void parseRXData() +{ + HOST_CMD_HEADER *pRxPktHdr; string data = ""; + unsigned char *ptemp; + int i; + + pRxPktHdr = (HOST_CMD_HEADER *)rxBuf; + +#ifdef DEBUG + pc.printf("DBG: fl = %02x\n", pRxPktHdr->SOF_flag); + pc.printf("DBG: len = %02x\n", pRxPktHdr->len); +#endif + + // Exit if the packet does not contain correct header + // Maybe send NAK? + if ((pRxPktHdr->SOF_flag != RX_SOF) || (pRxPktHdr->Delim != DELIMETER)) + return; - 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); + switch (pRxPktHdr->cmd_type) + { + case SET_TEMPERATURE: + // Process set temp for specified channel + { + SET_TEMPERATURE_CMD *pRxPkt = (SET_TEMPERATURE_CMD *)(rxBuf); +#if 1 //def DEBUG + pc.printf("DBG: ch = %02x\n", pRxPkt->chanID); + pc.printf("DBG: tempSet = %f\n", pRxPkt->setTemp); + pc.printf("DBG: chanStat = %02x\n", pRxPkt->chanStat); +#endif + if ((pRxPkt->tempDelim != DELIMETER) || (pRxPkt->chanStatDelim != DELIMETER)) { + // Send NAK back + pc.printf("DBG: Error\n"); } - else{ - chnl = atoi(data.c_str()); - chnl = chnl-1; //fix for array indexing - chnlSel = chnl; //chnl selected by user in GUI + else { + chanSel_SetTemp = pRxPkt->chanID; + chnlStatus[pRxPkt->chanID].status = pRxPkt->chanStat; + chnlStatus[pRxPkt->chanID].setTemp = pRxPkt->setTemp; + newTempSet = true; } } - else if(pCount == 2){ //get channel temperature - chnlStatus[chnl].setTemp = atoi(data.c_str()); - if(DEBUG) pc.printf("CHANNEL TEMP: %f \r\n", chnlStatus[chnl].setTemp); + break; + + case SELECT_CHANNEL: + // Select channel to send temp data to + { + SELECT_CHANNEL_CMD *pRxPkt = (SELECT_CHANNEL_CMD *)(rxBuf); + + chanSel_SendTemp = pRxPkt->chanIDSel; +#if 1 //def DEBUG + pc.printf("DBG: chanSel = %02x\n", pRxPkt->chanIDSel); + pc.printf("DBG: chanStat = %02x\n", pRxPkt->chanStat); +#endif } - else if(pCount == 3){ //get channel status - //IF CHANNEL IS TURNED OFF, TURN THE STATUS OFF FIRST! - chnlStatus[chnl].status = atoi(data.c_str()); - if(chnlStatus[chnl].status == 0) turnOffChannel(chnl); - if(DEBUG) pc.printf("CHANNEL STATUS: %f \r\n", chnlStatus[chnl].status); - } - data = ""; - } - i++; + break; + + default: + // Error + break; } } @@ -411,7 +513,8 @@ io_control.writeOutput(0,0,1,0); } else{ - io_control.writeOutput(0,0,0,1); + //turn valve on too + io_control.writeOutput(1,0,0,1); } } @@ -448,8 +551,8 @@ wait(0.1); get_temp(chn, BACK_THERM); wait(0.1); - //get_temp(chn, VALV_FET_AMP); - //wait(0.1); + get_temp(chn, VALVE_FET_AMP); + wait(0.1); //if(DEBUG1) pc.printf("TEMPERATURE READING: %f \r\n", get_temp(chn)); } @@ -499,6 +602,28 @@ } } +//*************************************************************** +// Build packet with temperature readings to send to GUI +//*************************************************************** +void sendTempReadings (int chan, float currentTemp) +{ + RESPONSE_CMD response; + unsigned char *ptr = (unsigned char *)&response; + int i; + + response.SOF_flag = TX_SOF; + response.cmd_type = CMD_DATA; + response.len = 9; + response.Delim = DELIMETER; + response.data = (float)currentTemp; + response.EOF_flag = TX_EOF; + + // Send response to GUI + for (i=0; i < response.len; i++, ptr++) + pc.printf("%02x", *ptr); + pc.printf("\n"); +} + /*************************************************************/ @@ -508,50 +633,70 @@ int main() { - //pc.baud(9600); - //pc.autoDetectChar('E'); - //pc.attach(&rxInterrupt, MODSERIAL::RxAutoDetect); + Timer t; + + // Setup serial port + // Look for RX_EOF + pc.baud(9600); + pc.autoDetectChar(RX_EOF); + pc.attach(&rxInterrupt, MODSERIAL::RxAutoDetect); + + myled = 1; + rLed = 0; + gLed = 1; - myled = 1; - - //FRONT_THERM, BACK_THERM, HEAT_FET_AMP, VALV_FET_AMP - + t.start(); while(1) { - //test_all_IO(); if(DEBUG3) 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 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 + currentTempBack)/2; + float currentTemp = currentTempFront; + if(currentTempFront >= currentTempBack){ + currentTemp = currentTempFront; + } + else{ + currentTemp = currentTempBack; + } + + //float currentTemp = (currentTempFront + currentTempBack)/2; + + //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.move(rxBuf, 50); + parseRXData(); + pc.rxBufferFlush(); + + } + + if (chnl == chanSel_SendTemp){ + // 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); //Error check on fixture - error_check(chnl, currentTempFront, currentTempBack, valveCurrent, heaterCurrent); - + //error_check(chnl, currentTempFront, currentTempBack, valveCurrent, heaterCurrent); + if((currentTempFront >= 70) || (currentTempBack >= 70)){ + status_led(chnl, STATUS_BAD); + } if(chnlStatus[chnl].error == 0){ - if((currentTemp > ((chnlStatus[chnl].setTemp)+HYST))){ + if((currentTemp > ((chnlStatus[chnl].setTemp)+HYST_HIGH))){ if(DEBUG3) pc.printf("CHILLER ON \r\n"); //Turn chiller on turn_valve_on(chnl); } - else if (currentTemp < ((chnlStatus[chnl].setTemp)-HYST)){ + else if (currentTemp < ((chnlStatus[chnl].setTemp)-HYST_LOW)){ if(DEBUG3) pc.printf("HEATER ON \r\n"); //Turn heater on turn_heater_on(chnl);