Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Control_Code_LookUp MCP23017 MODSERIAL mbed
Fork of Control_Code_LookUp by
main.cpp
00001 // MBED SCRIPT FOR CONTROLLING THE TEMPERATURE CONTROLLED TEST FIXTURE 00002 // AUTHOR: JUSTIN RODENBURG 00003 // DATE: SEPTEMBER 2017 00004 00005 //PLAN TO HAVE 18 CHANNELS PER MBED 00006 //HARDWARE: 00007 //2 X VALVE DRIVER BOARD 00008 //1 X MBED 00009 //1 X ADC MUX (16 CHN) 00010 00011 //FORMAT FOR CELL DATA: CHANNEL.TEMPERATURE.STATUS.E 1.23.1.E, 1.23.1.E 00012 00013 #include "mbed.h" 00014 #include "MCP23017.h" 00015 #include "MODSERIAL.h" 00016 #include <string> 00017 00018 #ifdef DEBUG 00019 #define printf(fmt, ...) (0) 00020 #endif 00021 00022 00023 DigitalOut rLed(LED1); 00024 DigitalOut gLed(LED2); 00025 00026 //DIGITAL CONTROL PINS 00027 DigitalOut valve(PTB8); 00028 DigitalOut heater(PTB9); 00029 00030 //Init. I2C for CE Board 00031 MCP23017 mcp = MCP23017(PTC9, PTC8, 0x40); 00032 00033 //GLOBAL 00034 // pullUp resistance 00035 int rUp = 10000; 00036 //LUT size 00037 int sizeLUT = 34; 00038 00039 //LOOK UP TABLE 00040 const struct LUT { 00041 int adc; 00042 int temp; 00043 }; 00044 00045 LUT thermLUT[] = { 00046 {62349,-40}, 00047 {61393,-35}, 00048 {60223,-30}, 00049 {58818,-25}, 00050 {57159,-20}, 00051 {55240,-15}, 00052 {53054,-10}, 00053 {50605,-5}, 00054 {47928,0}, 00055 {45069,5}, 00056 {42068,10}, 00057 {38975,15}, 00058 {35856,20}, 00059 {32768,25}, 00060 {29753,30}, 00061 {26867,35}, 00062 {24147,40}, 00063 {21602,45}, 00064 {19257,50}, 00065 {17116,55}, 00066 {15178,60}, 00067 {13465,65}, 00068 {11941,70}, 00069 {10579,75}, 00070 {9374,80}, 00071 {8309,85}, 00072 {7375,90}, 00073 {6548,95}, 00074 {5817,100}, 00075 {5179,105}, 00076 {4618,110}, 00077 {4127,115}, 00078 {3686,120}, 00079 {3304,125} 00080 }; 00081 00082 //ANALOG PINS 00083 AnalogIn ain0(A0); 00084 AnalogIn ain1(A1); 00085 AnalogIn ain2(A2); 00086 AnalogIn ain3(A3); 00087 AnalogIn ain4(A4); 00088 AnalogIn ain5(A5); 00089 00090 MODSERIAL pc(USBTX, USBRX); 00091 00092 volatile int bufA_index; 00093 const int numChnls = 15; 00094 float chTemps[numChnls]; 00095 float chGoalTemps[numChnls]; 00096 int chStatus[numChnls]; 00097 int numSamples = 5; 00098 volatile bool dataRecieved = false; 00099 int chnlSel; 00100 char rxBuf[50]; 00101 int tempup=1; 00102 00103 #define RX_SOF 0x7B 00104 #define RX_EOF 0x7D 00105 00106 void rxInterrupt(MODSERIAL_IRQ_INFO *info){ 00107 gLed = 0; 00108 wait(2); 00109 gLed = 1; 00110 dataRecieved = true; 00111 } 00112 00113 typedef struct { 00114 unsigned char SOF_flag; 00115 unsigned char data_len; 00116 unsigned char chanDelim; 00117 unsigned char chanID; 00118 unsigned char tempDelim; 00119 unsigned char setTemp; 00120 unsigned char chanStatDelim; 00121 unsigned char chanStat; 00122 } HOST_PACKET; 00123 00124 void parseRXData(){ 00125 int pCount = 0; //count data collected 00126 int i = 0; 00127 int chnl; 00128 00129 HOST_PACKET *RxPkt; 00130 00131 chTemps[0] = 1; 00132 00133 string data = ""; 00134 00135 RxPkt = (HOST_PACKET *)(rxBuf); 00136 00137 00138 pc.printf("fl = %02x\n", RxPkt->SOF_flag); 00139 pc.printf("len = %02x\n", RxPkt->data_len); 00140 pc.printf("ch = %02x\n", RxPkt->chanID); 00141 pc.printf("temp = %02x\n", RxPkt->setTemp); 00142 pc.printf("chanStat = %02x\n", RxPkt->chanStat); 00143 00144 tempup = 1; 00145 00146 // Exit if the packet does not contain correct header 00147 if (RxPkt->SOF_flag != RX_SOF) 00148 return; 00149 00150 while(pCount < 3){ 00151 if(rxBuf[i] != '.'){ 00152 data += rxBuf[i]; 00153 } 00154 else{ 00155 pCount++; 00156 if(pCount == 1){ //get channel 00157 if((atoi(data.c_str()) < 0) || (atoi(data.c_str()) > 15)){ 00158 //check if channel is out of array index 00159 rLed = 0; 00160 } 00161 else{ 00162 chnl = atoi(data.c_str()); 00163 chnl = chnl-1; //fix for array indexing 00164 chnlSel = chnl; 00165 //pc.printf("CHANNEL: %i \r\n", chnl); 00166 } 00167 } 00168 else if(pCount == 2){ //get channel temperature 00169 chGoalTemps[chnl] = atoi(data.c_str()); 00170 //pc.printf("TEMPERATURE: %i \r\n", atoi(data.c_str())); 00171 } 00172 else if(pCount == 3){ //get channel status 00173 chStatus[chnl] = atoi(data.c_str()); 00174 //pc.printf("STATUS: %i \r\n", atoi(data.c_str())); 00175 } 00176 data = ""; 00177 } 00178 i++; 00179 } 00180 //wait(30); 00181 } 00182 00183 float getTemp(float ADC_val){ 00184 int i = 0; 00185 while((i < sizeLUT) && (thermLUT[i].adc > ADC_val)){ 00186 i++; 00187 } 00188 00189 float a = float(thermLUT[i-1].temp - thermLUT[i].temp); 00190 float b = float(thermLUT[i-1].adc - thermLUT[i].adc); 00191 00192 float m = a/b; 00193 float y = (m*(ADC_val - thermLUT[i-1].adc))+thermLUT[i-1].temp; 00194 00195 return y; 00196 } 00197 00198 //16-bit ADC --> 2^16 = 65536 00199 void readTemps(){ 00200 float a_0 = 0.0; 00201 00202 #if 0 00203 for(int i=0; i<numSamples; i++){ 00204 a_0 += ain0.read_u16(); 00205 } 00206 00207 a_0 = a_0/numSamples; 00208 00209 //printf("ADC VAL: %f \r\n", a_0); 00210 00211 chTemps[0] = getTemp(a_0); 00212 #else 00213 a_0 = .2; 00214 if (chTemps[0] >= 6) 00215 tempup = 0; 00216 if (chTemps[0] <= 0) 00217 tempup = 1; 00218 00219 if (tempup) 00220 chTemps[0] += a_0; 00221 else 00222 chTemps[0] -= a_0; 00223 #endif 00224 00225 //send current temperature of selected channel to GUI for display 00226 pc.printf("%f \r\n", chTemps[chnlSel]); 00227 00228 wait(1.5); 00229 00230 //printf("TEMPERATURE (C): %f \r\n", chTemps[0]); 00231 00232 //printf("TEMPERATURE (C): 15 \r\n"); 00233 00234 //printf("TEMPERATURE (C): 15 \r\n"); 00235 } 00236 00237 00238 00239 00240 int main() { 00241 //printf("Main Entered.\n"); 00242 //turn off LEDs 00243 rLed = 1; 00244 gLed = 1; 00245 00246 //configure MCP23017 00247 mcp.config(0,0,0); 00248 00249 // 00250 pc.baud(9600); 00251 //pc.autoDetectChar(RX_EOF); 00252 pc.autoDetectChar('\n'); 00253 pc.attach(&rxInterrupt, MODSERIAL::RxAutoDetect); 00254 00255 while(1) { 00256 //read temperatures 00257 readTemps(); 00258 00259 //buffer for noise 00260 double hyst = 0.2; 00261 00262 //check if we recieved data/need to update TCTF data 00263 if(dataRecieved){ 00264 dataRecieved = false; 00265 pc.printf("Count = %d\n", pc.rxBufferGetCount()); 00266 pc.move(rxBuf, 50); 00267 parseRXData(); 00268 pc.rxBufferFlush(); 00269 00270 } 00271 00272 //Control loop: 00273 //Loop through all channels and control 00274 //chiller = 18C 00275 for(int i=0; i<=numChnls; i++){ 00276 //control chiller 00277 if(chStatus[i] == 1){ 00278 if(chTemps[i] > ((chGoalTemps[i])+hyst)){ 00279 //Turn on chiller 00280 //printf("CHILLER ON \r\n"); 00281 mcp.config(0,0,0); 00282 mcp.write_bit(1, 8); 00283 //printf("HEATER OFF \r\n"); 00284 mcp.write_bit(0, 9); 00285 } 00286 else if (chTemps[i] < ((chGoalTemps[i])-hyst)){ 00287 //turn off chiller 00288 //printf("CHILLER OFF \r\n"); 00289 mcp.config(0,0,0); 00290 mcp.write_bit(0, 8); 00291 //printf("HEATER ON \r\n"); 00292 mcp.write_bit(1, 9); 00293 } 00294 else{ 00295 //turn off chiller 00296 //printf("CHILLER OFF \r\n"); 00297 mcp.config(0,0,0); 00298 mcp.write_bit(0, 8); 00299 //printf("HEATER OFF \r\n"); 00300 mcp.write_bit(0, 9); 00301 } 00302 00303 00304 } 00305 wait(0); 00306 } 00307 } 00308 }
Generated on Sun Jul 17 2022 10:31:18 by
1.7.2
