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