![](/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
main.cpp@1:b8231536af32, 2018-01-11 (annotated)
- Committer:
- jrodenburg
- Date:
- Thu Jan 11 22:51:35 2018 +0000
- Revision:
- 1:b8231536af32
- Parent:
- 0:11aaf2483a3c
- Child:
- 2:68f92f17d445
File
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jrodenburg | 0:11aaf2483a3c | 1 | // MBED SCRIPT FOR CONTROLLING THE TEMPERATURE CONTROLLED TEST FIXTURE |
jrodenburg | 0:11aaf2483a3c | 2 | // AUTHOR: JUSTIN RODENBURG |
jrodenburg | 0:11aaf2483a3c | 3 | // DATE: SEPTEMBER 2017 |
jrodenburg | 0:11aaf2483a3c | 4 | |
jrodenburg | 0:11aaf2483a3c | 5 | //PLAN TO HAVE 18 CHANNELS PER MBED |
jrodenburg | 0:11aaf2483a3c | 6 | //HARDWARE: |
jrodenburg | 0:11aaf2483a3c | 7 | //2 X VALVE DRIVER BOARD |
jrodenburg | 0:11aaf2483a3c | 8 | //1 X MBED |
jrodenburg | 0:11aaf2483a3c | 9 | //1 X ADC MUX (16 CHN) |
jrodenburg | 0:11aaf2483a3c | 10 | |
jrodenburg | 1:b8231536af32 | 11 | //FORMAT FOR CELL DATA: CHANNEL.TEMPERATURE.STATUS.E 1.23.1.E, 1.23.1.E |
jrodenburg | 0:11aaf2483a3c | 12 | |
jrodenburg | 0:11aaf2483a3c | 13 | #include "mbed.h" |
jrodenburg | 0:11aaf2483a3c | 14 | #include "MCP23017.h" |
jrodenburg | 0:11aaf2483a3c | 15 | #include "MODSERIAL.h" |
jrodenburg | 0:11aaf2483a3c | 16 | #include <string> |
jrodenburg | 0:11aaf2483a3c | 17 | |
jrodenburg | 1:b8231536af32 | 18 | #ifdef DEBUG |
jrodenburg | 1:b8231536af32 | 19 | #define printf(fmt, ...) (0) |
jrodenburg | 1:b8231536af32 | 20 | #endif |
jrodenburg | 1:b8231536af32 | 21 | |
jrodenburg | 0:11aaf2483a3c | 22 | |
jrodenburg | 0:11aaf2483a3c | 23 | DigitalOut rLed(LED1); |
jrodenburg | 0:11aaf2483a3c | 24 | DigitalOut gLed(LED2); |
jrodenburg | 0:11aaf2483a3c | 25 | |
jrodenburg | 0:11aaf2483a3c | 26 | //DIGITAL CONTROL PINS |
jrodenburg | 0:11aaf2483a3c | 27 | DigitalOut valve(PTB8); |
jrodenburg | 0:11aaf2483a3c | 28 | DigitalOut heater(PTB9); |
jrodenburg | 0:11aaf2483a3c | 29 | |
jrodenburg | 0:11aaf2483a3c | 30 | //Init. I2C for CE Board |
jrodenburg | 0:11aaf2483a3c | 31 | MCP23017 mcp = MCP23017(PTC9, PTC8, 0x40); |
jrodenburg | 0:11aaf2483a3c | 32 | |
jrodenburg | 0:11aaf2483a3c | 33 | //GLOBAL |
jrodenburg | 0:11aaf2483a3c | 34 | // pullUp resistance |
jrodenburg | 0:11aaf2483a3c | 35 | int rUp = 10000; |
jrodenburg | 0:11aaf2483a3c | 36 | //LUT size |
jrodenburg | 0:11aaf2483a3c | 37 | int sizeLUT = 34; |
jrodenburg | 0:11aaf2483a3c | 38 | |
jrodenburg | 0:11aaf2483a3c | 39 | //LOOK UP TABLE |
jrodenburg | 0:11aaf2483a3c | 40 | const struct LUT { |
jrodenburg | 0:11aaf2483a3c | 41 | int adc; |
jrodenburg | 0:11aaf2483a3c | 42 | int temp; |
jrodenburg | 0:11aaf2483a3c | 43 | }; |
jrodenburg | 0:11aaf2483a3c | 44 | |
jrodenburg | 0:11aaf2483a3c | 45 | LUT thermLUT[] = { |
jrodenburg | 0:11aaf2483a3c | 46 | {62349,-40}, |
jrodenburg | 0:11aaf2483a3c | 47 | {61393,-35}, |
jrodenburg | 0:11aaf2483a3c | 48 | {60223,-30}, |
jrodenburg | 0:11aaf2483a3c | 49 | {58818,-25}, |
jrodenburg | 0:11aaf2483a3c | 50 | {57159,-20}, |
jrodenburg | 0:11aaf2483a3c | 51 | {55240,-15}, |
jrodenburg | 0:11aaf2483a3c | 52 | {53054,-10}, |
jrodenburg | 0:11aaf2483a3c | 53 | {50605,-5}, |
jrodenburg | 0:11aaf2483a3c | 54 | {47928,0}, |
jrodenburg | 0:11aaf2483a3c | 55 | {45069,5}, |
jrodenburg | 0:11aaf2483a3c | 56 | {42068,10}, |
jrodenburg | 0:11aaf2483a3c | 57 | {38975,15}, |
jrodenburg | 0:11aaf2483a3c | 58 | {35856,20}, |
jrodenburg | 0:11aaf2483a3c | 59 | {32768,25}, |
jrodenburg | 0:11aaf2483a3c | 60 | {29753,30}, |
jrodenburg | 0:11aaf2483a3c | 61 | {26867,35}, |
jrodenburg | 0:11aaf2483a3c | 62 | {24147,40}, |
jrodenburg | 0:11aaf2483a3c | 63 | {21602,45}, |
jrodenburg | 0:11aaf2483a3c | 64 | {19257,50}, |
jrodenburg | 0:11aaf2483a3c | 65 | {17116,55}, |
jrodenburg | 0:11aaf2483a3c | 66 | {15178,60}, |
jrodenburg | 0:11aaf2483a3c | 67 | {13465,65}, |
jrodenburg | 0:11aaf2483a3c | 68 | {11941,70}, |
jrodenburg | 0:11aaf2483a3c | 69 | {10579,75}, |
jrodenburg | 0:11aaf2483a3c | 70 | {9374,80}, |
jrodenburg | 0:11aaf2483a3c | 71 | {8309,85}, |
jrodenburg | 0:11aaf2483a3c | 72 | {7375,90}, |
jrodenburg | 0:11aaf2483a3c | 73 | {6548,95}, |
jrodenburg | 0:11aaf2483a3c | 74 | {5817,100}, |
jrodenburg | 0:11aaf2483a3c | 75 | {5179,105}, |
jrodenburg | 0:11aaf2483a3c | 76 | {4618,110}, |
jrodenburg | 0:11aaf2483a3c | 77 | {4127,115}, |
jrodenburg | 0:11aaf2483a3c | 78 | {3686,120}, |
jrodenburg | 0:11aaf2483a3c | 79 | {3304,125} |
jrodenburg | 0:11aaf2483a3c | 80 | }; |
jrodenburg | 0:11aaf2483a3c | 81 | |
jrodenburg | 0:11aaf2483a3c | 82 | //ANALOG PINS |
jrodenburg | 0:11aaf2483a3c | 83 | AnalogIn ain0(A0); |
jrodenburg | 0:11aaf2483a3c | 84 | AnalogIn ain1(A1); |
jrodenburg | 0:11aaf2483a3c | 85 | AnalogIn ain2(A2); |
jrodenburg | 0:11aaf2483a3c | 86 | AnalogIn ain3(A3); |
jrodenburg | 0:11aaf2483a3c | 87 | AnalogIn ain4(A4); |
jrodenburg | 0:11aaf2483a3c | 88 | AnalogIn ain5(A5); |
jrodenburg | 0:11aaf2483a3c | 89 | |
jrodenburg | 0:11aaf2483a3c | 90 | MODSERIAL pc(USBTX, USBRX); |
jrodenburg | 0:11aaf2483a3c | 91 | |
jrodenburg | 0:11aaf2483a3c | 92 | volatile int bufA_index; |
jrodenburg | 0:11aaf2483a3c | 93 | const int numChnls = 15; |
jrodenburg | 0:11aaf2483a3c | 94 | float chTemps[numChnls]; |
jrodenburg | 0:11aaf2483a3c | 95 | float chGoalTemps[numChnls]; |
jrodenburg | 1:b8231536af32 | 96 | int chStatus[numChnls]; |
jrodenburg | 0:11aaf2483a3c | 97 | int numSamples = 5; |
jrodenburg | 0:11aaf2483a3c | 98 | volatile bool dataRecieved = false; |
jrodenburg | 1:b8231536af32 | 99 | int chnlSel; |
jrodenburg | 0:11aaf2483a3c | 100 | char rxBuf[50]; |
jrodenburg | 0:11aaf2483a3c | 101 | |
jrodenburg | 0:11aaf2483a3c | 102 | void rxInterrupt(MODSERIAL_IRQ_INFO *info){ |
jrodenburg | 0:11aaf2483a3c | 103 | gLed = 0; |
jrodenburg | 0:11aaf2483a3c | 104 | wait(2); |
jrodenburg | 0:11aaf2483a3c | 105 | gLed = 1; |
jrodenburg | 0:11aaf2483a3c | 106 | dataRecieved = true; |
jrodenburg | 0:11aaf2483a3c | 107 | } |
jrodenburg | 0:11aaf2483a3c | 108 | |
jrodenburg | 0:11aaf2483a3c | 109 | void parseRXData(){ |
jrodenburg | 0:11aaf2483a3c | 110 | int pCount = 0; //count data collected |
jrodenburg | 0:11aaf2483a3c | 111 | int i = 0; |
jrodenburg | 1:b8231536af32 | 112 | int chnl; |
jrodenburg | 0:11aaf2483a3c | 113 | |
jrodenburg | 0:11aaf2483a3c | 114 | string data = ""; |
jrodenburg | 1:b8231536af32 | 115 | |
jrodenburg | 1:b8231536af32 | 116 | //pc.printf("buff1 = <%s> \r\n", rxBuf); |
jrodenburg | 0:11aaf2483a3c | 117 | |
jrodenburg | 0:11aaf2483a3c | 118 | while(pCount < 3){ |
jrodenburg | 0:11aaf2483a3c | 119 | if(rxBuf[i] != '.'){ |
jrodenburg | 0:11aaf2483a3c | 120 | data += rxBuf[i]; |
jrodenburg | 0:11aaf2483a3c | 121 | } |
jrodenburg | 0:11aaf2483a3c | 122 | else{ |
jrodenburg | 0:11aaf2483a3c | 123 | pCount++; |
jrodenburg | 0:11aaf2483a3c | 124 | if(pCount == 1){ //get channel |
jrodenburg | 1:b8231536af32 | 125 | if((atoi(data.c_str()) < 0) || (atoi(data.c_str()) > 15)){ |
jrodenburg | 0:11aaf2483a3c | 126 | //check if channel is out of array index |
jrodenburg | 0:11aaf2483a3c | 127 | rLed = 0; |
jrodenburg | 0:11aaf2483a3c | 128 | } |
jrodenburg | 0:11aaf2483a3c | 129 | else{ |
jrodenburg | 0:11aaf2483a3c | 130 | chnl = atoi(data.c_str()); |
jrodenburg | 1:b8231536af32 | 131 | chnl = chnl-1; //fix for array indexing |
jrodenburg | 1:b8231536af32 | 132 | chnlSel = chnl; |
jrodenburg | 1:b8231536af32 | 133 | //pc.printf("CHANNEL: %i \r\n", chnl); |
jrodenburg | 0:11aaf2483a3c | 134 | } |
jrodenburg | 0:11aaf2483a3c | 135 | } |
jrodenburg | 0:11aaf2483a3c | 136 | else if(pCount == 2){ //get channel temperature |
jrodenburg | 0:11aaf2483a3c | 137 | chGoalTemps[chnl] = atoi(data.c_str()); |
jrodenburg | 1:b8231536af32 | 138 | //pc.printf("TEMPERATURE: %i \r\n", atoi(data.c_str())); |
jrodenburg | 0:11aaf2483a3c | 139 | } |
jrodenburg | 0:11aaf2483a3c | 140 | else if(pCount == 3){ //get channel status |
jrodenburg | 1:b8231536af32 | 141 | chStatus[chnl] = atoi(data.c_str()); |
jrodenburg | 1:b8231536af32 | 142 | //pc.printf("STATUS: %i \r\n", atoi(data.c_str())); |
jrodenburg | 1:b8231536af32 | 143 | } |
jrodenburg | 1:b8231536af32 | 144 | data = ""; |
jrodenburg | 0:11aaf2483a3c | 145 | } |
jrodenburg | 0:11aaf2483a3c | 146 | i++; |
jrodenburg | 0:11aaf2483a3c | 147 | } |
jrodenburg | 1:b8231536af32 | 148 | //wait(30); |
jrodenburg | 0:11aaf2483a3c | 149 | } |
jrodenburg | 0:11aaf2483a3c | 150 | |
jrodenburg | 0:11aaf2483a3c | 151 | float getTemp(float ADC_val){ |
jrodenburg | 0:11aaf2483a3c | 152 | int i = 0; |
jrodenburg | 0:11aaf2483a3c | 153 | while((i < sizeLUT) && (thermLUT[i].adc > ADC_val)){ |
jrodenburg | 0:11aaf2483a3c | 154 | i++; |
jrodenburg | 0:11aaf2483a3c | 155 | } |
jrodenburg | 0:11aaf2483a3c | 156 | |
jrodenburg | 0:11aaf2483a3c | 157 | float a = float(thermLUT[i-1].temp - thermLUT[i].temp); |
jrodenburg | 0:11aaf2483a3c | 158 | float b = float(thermLUT[i-1].adc - thermLUT[i].adc); |
jrodenburg | 0:11aaf2483a3c | 159 | |
jrodenburg | 0:11aaf2483a3c | 160 | float m = a/b; |
jrodenburg | 0:11aaf2483a3c | 161 | float y = (m*(ADC_val - thermLUT[i-1].adc))+thermLUT[i-1].temp; |
jrodenburg | 0:11aaf2483a3c | 162 | |
jrodenburg | 0:11aaf2483a3c | 163 | return y; |
jrodenburg | 0:11aaf2483a3c | 164 | } |
jrodenburg | 0:11aaf2483a3c | 165 | |
jrodenburg | 0:11aaf2483a3c | 166 | //16-bit ADC --> 2^16 = 65536 |
jrodenburg | 0:11aaf2483a3c | 167 | void readTemps(){ |
jrodenburg | 0:11aaf2483a3c | 168 | float a_0 = 0.0; |
jrodenburg | 0:11aaf2483a3c | 169 | |
jrodenburg | 0:11aaf2483a3c | 170 | for(int i=0; i<numSamples; i++){ |
jrodenburg | 0:11aaf2483a3c | 171 | a_0 += ain0.read_u16(); |
jrodenburg | 0:11aaf2483a3c | 172 | } |
jrodenburg | 0:11aaf2483a3c | 173 | |
jrodenburg | 0:11aaf2483a3c | 174 | a_0 = a_0/numSamples; |
jrodenburg | 0:11aaf2483a3c | 175 | |
jrodenburg | 1:b8231536af32 | 176 | //printf("ADC VAL: %f \r\n", a_0); |
jrodenburg | 0:11aaf2483a3c | 177 | |
jrodenburg | 0:11aaf2483a3c | 178 | chTemps[0] = getTemp(a_0); |
jrodenburg | 0:11aaf2483a3c | 179 | |
jrodenburg | 1:b8231536af32 | 180 | //send current temperature of selected channel to GUI for display |
jrodenburg | 1:b8231536af32 | 181 | pc.printf("%f \r\n", chTemps[chnlSel]); |
jrodenburg | 1:b8231536af32 | 182 | |
jrodenburg | 1:b8231536af32 | 183 | wait(1.5); |
jrodenburg | 0:11aaf2483a3c | 184 | |
jrodenburg | 1:b8231536af32 | 185 | //printf("TEMPERATURE (C): %f \r\n", chTemps[0]); |
jrodenburg | 1:b8231536af32 | 186 | |
jrodenburg | 1:b8231536af32 | 187 | //printf("TEMPERATURE (C): 15 \r\n"); |
jrodenburg | 1:b8231536af32 | 188 | |
jrodenburg | 1:b8231536af32 | 189 | //printf("TEMPERATURE (C): 15 \r\n"); |
jrodenburg | 0:11aaf2483a3c | 190 | } |
jrodenburg | 0:11aaf2483a3c | 191 | |
jrodenburg | 0:11aaf2483a3c | 192 | |
jrodenburg | 0:11aaf2483a3c | 193 | |
jrodenburg | 0:11aaf2483a3c | 194 | |
jrodenburg | 0:11aaf2483a3c | 195 | int main() { |
jrodenburg | 1:b8231536af32 | 196 | //printf("Main Entered.\n"); |
jrodenburg | 0:11aaf2483a3c | 197 | //turn off LEDs |
jrodenburg | 0:11aaf2483a3c | 198 | rLed = 1; |
jrodenburg | 0:11aaf2483a3c | 199 | gLed = 1; |
jrodenburg | 0:11aaf2483a3c | 200 | |
jrodenburg | 0:11aaf2483a3c | 201 | //configure MCP23017 |
jrodenburg | 0:11aaf2483a3c | 202 | mcp.config(0,0,0); |
jrodenburg | 0:11aaf2483a3c | 203 | |
jrodenburg | 0:11aaf2483a3c | 204 | // |
jrodenburg | 0:11aaf2483a3c | 205 | pc.baud(9600); |
jrodenburg | 0:11aaf2483a3c | 206 | pc.autoDetectChar('E'); |
jrodenburg | 0:11aaf2483a3c | 207 | pc.attach(&rxInterrupt, MODSERIAL::RxAutoDetect); |
jrodenburg | 0:11aaf2483a3c | 208 | |
jrodenburg | 0:11aaf2483a3c | 209 | while(1) { |
jrodenburg | 0:11aaf2483a3c | 210 | //read temperatures |
jrodenburg | 0:11aaf2483a3c | 211 | readTemps(); |
jrodenburg | 0:11aaf2483a3c | 212 | |
jrodenburg | 0:11aaf2483a3c | 213 | //buffer for noise |
jrodenburg | 1:b8231536af32 | 214 | double hyst = 0.2; |
jrodenburg | 0:11aaf2483a3c | 215 | |
jrodenburg | 0:11aaf2483a3c | 216 | //check if we recieved data/need to update TCTF data |
jrodenburg | 0:11aaf2483a3c | 217 | if(dataRecieved){ |
jrodenburg | 0:11aaf2483a3c | 218 | dataRecieved = false; |
jrodenburg | 1:b8231536af32 | 219 | pc.move(rxBuf, 50); |
jrodenburg | 0:11aaf2483a3c | 220 | pc.rxBufferFlush(); |
jrodenburg | 0:11aaf2483a3c | 221 | parseRXData(); |
jrodenburg | 0:11aaf2483a3c | 222 | } |
jrodenburg | 0:11aaf2483a3c | 223 | |
jrodenburg | 0:11aaf2483a3c | 224 | //Control loop: |
jrodenburg | 0:11aaf2483a3c | 225 | //Loop through all channels and control |
jrodenburg | 0:11aaf2483a3c | 226 | //chiller = 18C |
jrodenburg | 0:11aaf2483a3c | 227 | for(int i=0; i<=numChnls; i++){ |
jrodenburg | 0:11aaf2483a3c | 228 | //control chiller |
jrodenburg | 1:b8231536af32 | 229 | if(chStatus[i] == 1){ |
jrodenburg | 1:b8231536af32 | 230 | if(chTemps[i] > ((chGoalTemps[i])+hyst)){ |
jrodenburg | 1:b8231536af32 | 231 | //Turn on chiller |
jrodenburg | 1:b8231536af32 | 232 | //printf("CHILLER ON \r\n"); |
jrodenburg | 1:b8231536af32 | 233 | mcp.config(0,0,0); |
jrodenburg | 1:b8231536af32 | 234 | mcp.write_bit(1, 8); |
jrodenburg | 1:b8231536af32 | 235 | //printf("HEATER OFF \r\n"); |
jrodenburg | 1:b8231536af32 | 236 | mcp.write_bit(0, 9); |
jrodenburg | 1:b8231536af32 | 237 | } |
jrodenburg | 1:b8231536af32 | 238 | else if (chTemps[i] < ((chGoalTemps[i])-hyst)){ |
jrodenburg | 1:b8231536af32 | 239 | //turn off chiller |
jrodenburg | 1:b8231536af32 | 240 | //printf("CHILLER OFF \r\n"); |
jrodenburg | 1:b8231536af32 | 241 | mcp.config(0,0,0); |
jrodenburg | 1:b8231536af32 | 242 | mcp.write_bit(0, 8); |
jrodenburg | 1:b8231536af32 | 243 | //printf("HEATER ON \r\n"); |
jrodenburg | 1:b8231536af32 | 244 | mcp.write_bit(1, 9); |
jrodenburg | 1:b8231536af32 | 245 | } |
jrodenburg | 1:b8231536af32 | 246 | else{ |
jrodenburg | 1:b8231536af32 | 247 | //turn off chiller |
jrodenburg | 1:b8231536af32 | 248 | //printf("CHILLER OFF \r\n"); |
jrodenburg | 1:b8231536af32 | 249 | mcp.config(0,0,0); |
jrodenburg | 1:b8231536af32 | 250 | mcp.write_bit(0, 8); |
jrodenburg | 1:b8231536af32 | 251 | //printf("HEATER OFF \r\n"); |
jrodenburg | 1:b8231536af32 | 252 | mcp.write_bit(0, 9); |
jrodenburg | 1:b8231536af32 | 253 | } |
jrodenburg | 1:b8231536af32 | 254 | |
jrodenburg | 1:b8231536af32 | 255 | |
jrodenburg | 0:11aaf2483a3c | 256 | } |
jrodenburg | 1:b8231536af32 | 257 | wait(0); |
jrodenburg | 0:11aaf2483a3c | 258 | } |
jrodenburg | 0:11aaf2483a3c | 259 | } |
jrodenburg | 0:11aaf2483a3c | 260 | } |