![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Working Rx IRQ
Dependencies: Control_Code_LookUp MCP23017 MODSERIAL mbed
Fork of Control_Code_LookUp by
Diff: main.cpp
- Revision:
- 2:68f92f17d445
- Parent:
- 1:b8231536af32
diff -r b8231536af32 -r 68f92f17d445 main.cpp --- a/main.cpp Thu Jan 11 22:51:35 2018 +0000 +++ b/main.cpp Wed Mar 07 04:39:44 2018 +0000 @@ -3,7 +3,7 @@ // DATE: SEPTEMBER 2017 //PLAN TO HAVE 18 CHANNELS PER MBED -//HARDWARE: +//HARDWARE: //2 X VALVE DRIVER BOARD //1 X MBED //1 X ADC MUX (16 CHN) @@ -32,8 +32,8 @@ //GLOBAL // pullUp resistance -int rUp = 10000; -//LUT size +int rUp = 10000; +//LUT size int sizeLUT = 34; //LOOK UP TABLE @@ -92,12 +92,16 @@ volatile int bufA_index; const int numChnls = 15; float chTemps[numChnls]; -float chGoalTemps[numChnls]; +float chGoalTemps[numChnls]; int chStatus[numChnls]; int numSamples = 5; volatile bool dataRecieved = false; int chnlSel; char rxBuf[50]; +int tempup=1; + +#define RX_SOF 0x7B +#define RX_EOF 0x7D void rxInterrupt(MODSERIAL_IRQ_INFO *info){ gLed = 0; @@ -106,31 +110,59 @@ dataRecieved = true; } +typedef struct { + unsigned char SOF_flag; + unsigned char data_len; + unsigned char chanDelim; + unsigned char chanID; + unsigned char tempDelim; + unsigned char setTemp; + unsigned char chanStatDelim; + unsigned char chanStat; +} HOST_PACKET; + void parseRXData(){ int pCount = 0; //count data collected int i = 0; int chnl; - string data = ""; + HOST_PACKET *RxPkt; - //pc.printf("buff1 = <%s> \r\n", rxBuf); + chTemps[0] = 1; + + string data = ""; + + RxPkt = (HOST_PACKET *)(rxBuf); + + pc.printf("fl = %02x\n", RxPkt->SOF_flag); + pc.printf("len = %02x\n", RxPkt->data_len); + pc.printf("ch = %02x\n", RxPkt->chanID); + pc.printf("temp = %02x\n", RxPkt->setTemp); + pc.printf("chanStat = %02x\n", RxPkt->chanStat); + + tempup = 1; + + // Exit if the packet does not contain correct header + if (RxPkt->SOF_flag != RX_SOF) + return; + while(pCount < 3){ if(rxBuf[i] != '.'){ data += rxBuf[i]; } else{ - pCount++; + 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 - rLed = 0; + rLed = 0; } else{ chnl = atoi(data.c_str()); chnl = chnl-1; //fix for array indexing chnlSel = chnl; - //pc.printf("CHANNEL: %i \r\n", chnl); + //pc.printf("CHANNEL: %i \r\n", chnl); } } else if(pCount == 2){ //get channel temperature @@ -139,88 +171,104 @@ } else if(pCount == 3){ //get channel status chStatus[chnl] = atoi(data.c_str()); - //pc.printf("STATUS: %i \r\n", atoi(data.c_str())); - } - data = ""; + //pc.printf("STATUS: %i \r\n", atoi(data.c_str())); + } + data = ""; } i++; } //wait(30); } -float getTemp(float ADC_val){ - int i = 0; +float getTemp(float ADC_val){ + int i = 0; while((i < sizeLUT) && (thermLUT[i].adc > ADC_val)){ - i++; - } - + i++; + } + float a = float(thermLUT[i-1].temp - thermLUT[i].temp); float b = float(thermLUT[i-1].adc - thermLUT[i].adc); - + float m = a/b; float y = (m*(ADC_val - thermLUT[i-1].adc))+thermLUT[i-1].temp; - + return y; } //16-bit ADC --> 2^16 = 65536 void readTemps(){ float a_0 = 0.0; - + +#if 0 for(int i=0; i<numSamples; i++){ a_0 += ain0.read_u16(); } - + a_0 = a_0/numSamples; - + //printf("ADC VAL: %f \r\n", a_0); - chTemps[0] = getTemp(a_0); - + chTemps[0] = getTemp(a_0); +#else + a_0 = .2; + if (chTemps[0] >= 6) + tempup = 0; + if (chTemps[0] <= 0) + tempup = 1; + + if (tempup) + chTemps[0] += a_0; + else + chTemps[0] -= a_0; +#endif + //send current temperature of selected channel to GUI for display pc.printf("%f \r\n", chTemps[chnlSel]); - + wait(1.5); - + //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"); //turn off LEDs rLed = 1; gLed = 1; - + //configure MCP23017 mcp.config(0,0,0); - + // pc.baud(9600); - pc.autoDetectChar('E'); + //pc.autoDetectChar(RX_EOF); + pc.autoDetectChar('\n'); pc.attach(&rxInterrupt, MODSERIAL::RxAutoDetect); - + while(1) { //read temperatures readTemps(); - + //buffer for noise double hyst = 0.2; - + //check if we recieved data/need to update TCTF data if(dataRecieved){ dataRecieved = false; + pc.printf("Count = %d\n", pc.rxBufferGetCount()); pc.move(rxBuf, 50); - pc.rxBufferFlush(); parseRXData(); + pc.rxBufferFlush(); + } - + //Control loop: //Loop through all channels and control //chiller = 18C @@ -231,7 +279,7 @@ //Turn on chiller //printf("CHILLER ON \r\n"); mcp.config(0,0,0); - mcp.write_bit(1, 8); + mcp.write_bit(1, 8); //printf("HEATER OFF \r\n"); mcp.write_bit(0, 9); } @@ -239,7 +287,7 @@ //turn off chiller //printf("CHILLER OFF \r\n"); mcp.config(0,0,0); - mcp.write_bit(0, 8); + mcp.write_bit(0, 8); //printf("HEATER ON \r\n"); mcp.write_bit(1, 9); } @@ -247,12 +295,12 @@ //turn off chiller //printf("CHILLER OFF \r\n"); mcp.config(0,0,0); - mcp.write_bit(0, 8); + mcp.write_bit(0, 8); //printf("HEATER OFF \r\n"); mcp.write_bit(0, 9); } - - + + } wait(0); }