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: MCP23017 MODSERIAL mbed
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 00102 void rxInterrupt(MODSERIAL_IRQ_INFO *info){ 00103 gLed = 0; 00104 wait(2); 00105 gLed = 1; 00106 dataRecieved = true; 00107 } 00108 00109 void parseRXData(){ 00110 int pCount = 0; //count data collected 00111 int i = 0; 00112 int chnl; 00113 00114 string data = ""; 00115 00116 //pc.printf("buff1 = <%s> \r\n", rxBuf); 00117 00118 while(pCount < 3){ 00119 if(rxBuf[i] != '.'){ 00120 data += rxBuf[i]; 00121 } 00122 else{ 00123 pCount++; 00124 if(pCount == 1){ //get channel 00125 if((atoi(data.c_str()) < 0) || (atoi(data.c_str()) > 15)){ 00126 //check if channel is out of array index 00127 rLed = 0; 00128 } 00129 else{ 00130 chnl = atoi(data.c_str()); 00131 chnl = chnl-1; //fix for array indexing 00132 chnlSel = chnl; 00133 //pc.printf("CHANNEL: %i \r\n", chnl); 00134 } 00135 } 00136 else if(pCount == 2){ //get channel temperature 00137 chGoalTemps[chnl] = atoi(data.c_str()); 00138 //pc.printf("TEMPERATURE: %i \r\n", atoi(data.c_str())); 00139 } 00140 else if(pCount == 3){ //get channel status 00141 chStatus[chnl] = atoi(data.c_str()); 00142 //pc.printf("STATUS: %i \r\n", atoi(data.c_str())); 00143 } 00144 data = ""; 00145 } 00146 i++; 00147 } 00148 //wait(30); 00149 } 00150 00151 float getTemp(float ADC_val){ 00152 int i = 0; 00153 while((i < sizeLUT) && (thermLUT[i].adc > ADC_val)){ 00154 i++; 00155 } 00156 00157 float a = float(thermLUT[i-1].temp - thermLUT[i].temp); 00158 float b = float(thermLUT[i-1].adc - thermLUT[i].adc); 00159 00160 float m = a/b; 00161 float y = (m*(ADC_val - thermLUT[i-1].adc))+thermLUT[i-1].temp; 00162 00163 return y; 00164 } 00165 00166 //16-bit ADC --> 2^16 = 65536 00167 void readTemps(){ 00168 float a_0 = 0.0; 00169 00170 for(int i=0; i<numSamples; i++){ 00171 a_0 += ain0.read_u16(); 00172 } 00173 00174 a_0 = a_0/numSamples; 00175 00176 //printf("ADC VAL: %f \r\n", a_0); 00177 00178 chTemps[0] = getTemp(a_0); 00179 00180 //send current temperature of selected channel to GUI for display 00181 pc.printf("%f \r\n", chTemps[chnlSel]); 00182 00183 wait(1.5); 00184 00185 //printf("TEMPERATURE (C): %f \r\n", chTemps[0]); 00186 00187 //printf("TEMPERATURE (C): 15 \r\n"); 00188 00189 //printf("TEMPERATURE (C): 15 \r\n"); 00190 } 00191 00192 00193 00194 00195 int main() { 00196 //printf("Main Entered.\n"); 00197 //turn off LEDs 00198 rLed = 1; 00199 gLed = 1; 00200 00201 //configure MCP23017 00202 mcp.config(0,0,0); 00203 00204 // 00205 pc.baud(9600); 00206 pc.autoDetectChar('E'); 00207 pc.attach(&rxInterrupt, MODSERIAL::RxAutoDetect); 00208 00209 while(1) { 00210 //read temperatures 00211 readTemps(); 00212 00213 //buffer for noise 00214 double hyst = 0.2; 00215 00216 //check if we recieved data/need to update TCTF data 00217 if(dataRecieved){ 00218 dataRecieved = false; 00219 pc.move(rxBuf, 50); 00220 pc.rxBufferFlush(); 00221 parseRXData(); 00222 } 00223 00224 //Control loop: 00225 //Loop through all channels and control 00226 //chiller = 18C 00227 for(int i=0; i<=numChnls; i++){ 00228 //control chiller 00229 if(chStatus[i] == 1){ 00230 if(chTemps[i] > ((chGoalTemps[i])+hyst)){ 00231 //Turn on chiller 00232 //printf("CHILLER ON \r\n"); 00233 mcp.config(0,0,0); 00234 mcp.write_bit(1, 8); 00235 //printf("HEATER OFF \r\n"); 00236 mcp.write_bit(0, 9); 00237 } 00238 else if (chTemps[i] < ((chGoalTemps[i])-hyst)){ 00239 //turn off chiller 00240 //printf("CHILLER OFF \r\n"); 00241 mcp.config(0,0,0); 00242 mcp.write_bit(0, 8); 00243 //printf("HEATER ON \r\n"); 00244 mcp.write_bit(1, 9); 00245 } 00246 else{ 00247 //turn off chiller 00248 //printf("CHILLER OFF \r\n"); 00249 mcp.config(0,0,0); 00250 mcp.write_bit(0, 8); 00251 //printf("HEATER OFF \r\n"); 00252 mcp.write_bit(0, 9); 00253 } 00254 00255 00256 } 00257 wait(0); 00258 } 00259 } 00260 }
Generated on Tue Jul 26 2022 00:42:49 by
1.7.2