![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Control Code with I/O and ADC working
main.cpp@5:0f38a0bd4f86, 2018-03-01 (annotated)
- Committer:
- jrodenburg
- Date:
- Thu Mar 01 21:13:55 2018 +0000
- Revision:
- 5:0f38a0bd4f86
- Parent:
- 4:168a446bd0da
- Child:
- 6:c980535393ed
3/1/2018 Code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jrodenburg | 4:168a446bd0da | 1 | // MBED SCRIPT FOR CONTROLLING THE TEMPERATURE CONTROLLED TEST FIXTURE (TCTF) |
jrodenburg | 0:a28a1035c31b | 2 | // DATE: SEPTEMBER 2017 |
jrodenburg | 0:a28a1035c31b | 3 | |
jrodenburg | 0:a28a1035c31b | 4 | #include "mbed.h" |
jrodenburg | 0:a28a1035c31b | 5 | #include "MODSERIAL.h" |
jrodenburg | 0:a28a1035c31b | 6 | #include "MCP23008.h" |
jrodenburg | 0:a28a1035c31b | 7 | #include "LTC2487.h" |
jrodenburg | 2:bd118a724f03 | 8 | #include <string> |
jrodenburg | 0:a28a1035c31b | 9 | |
jrodenburg | 0:a28a1035c31b | 10 | //DEFINITIVE VARIABLES |
jrodenburg | 4:168a446bd0da | 11 | #define DEBUG 0 |
jrodenburg | 4:168a446bd0da | 12 | #define DEBUG1 1 |
jrodenburg | 5:0f38a0bd4f86 | 13 | #define DEBUG2 1 |
jrodenburg | 5:0f38a0bd4f86 | 14 | #define DEBUG3 1 |
jrodenburg | 5:0f38a0bd4f86 | 15 | #define CHN_COUNT 8 |
jrodenburg | 0:a28a1035c31b | 16 | #define MIN_TEMP 15 |
jrodenburg | 0:a28a1035c31b | 17 | #define MAX_TEMP 60 |
jrodenburg | 0:a28a1035c31b | 18 | #define HYST 0.2 |
jrodenburg | 0:a28a1035c31b | 19 | #define SAMPLES 5 |
jrodenburg | 0:a28a1035c31b | 20 | #define I2C_Freq 2000 |
jrodenburg | 1:0182b86f9bd4 | 21 | #define VALVE 1 |
jrodenburg | 1:0182b86f9bd4 | 22 | #define HEATER 2 |
jrodenburg | 1:0182b86f9bd4 | 23 | #define STATUS_GOOD 3 |
jrodenburg | 1:0182b86f9bd4 | 24 | #define STATUS_BAD 4 |
jrodenburg | 0:a28a1035c31b | 25 | #define sizeLUT 34 |
jrodenburg | 0:a28a1035c31b | 26 | #define FRONT_THERM 0 |
jrodenburg | 0:a28a1035c31b | 27 | #define BACK_THERM 1 |
jrodenburg | 0:a28a1035c31b | 28 | #define HEAT_FET_AMP 2 |
jrodenburg | 0:a28a1035c31b | 29 | #define VALV_FET_AMP 3 |
jrodenburg | 0:a28a1035c31b | 30 | |
jrodenburg | 2:bd118a724f03 | 31 | |
jrodenburg | 2:bd118a724f03 | 32 | //TCTF CHANNEL DATA |
jrodenburg | 2:bd118a724f03 | 33 | struct CHNL_DATA{ |
jrodenburg | 2:bd118a724f03 | 34 | bool status; |
jrodenburg | 2:bd118a724f03 | 35 | float setTemp; |
jrodenburg | 2:bd118a724f03 | 36 | }; |
jrodenburg | 2:bd118a724f03 | 37 | |
jrodenburg | 4:168a446bd0da | 38 | CHNL_DATA chnlStatus[] = { |
jrodenburg | 5:0f38a0bd4f86 | 39 | {1, 40}, |
jrodenburg | 5:0f38a0bd4f86 | 40 | {1, 40}, |
jrodenburg | 5:0f38a0bd4f86 | 41 | {1, 40}, |
jrodenburg | 5:0f38a0bd4f86 | 42 | {1, 40}, |
jrodenburg | 5:0f38a0bd4f86 | 43 | {1, 40}, |
jrodenburg | 5:0f38a0bd4f86 | 44 | {1, 40}, |
jrodenburg | 5:0f38a0bd4f86 | 45 | {1, 40}, |
jrodenburg | 5:0f38a0bd4f86 | 46 | {1, 40}, |
jrodenburg | 2:bd118a724f03 | 47 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 48 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 49 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 50 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 51 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 52 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 53 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 54 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 55 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 56 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 57 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 58 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 59 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 60 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 61 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 62 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 63 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 64 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 65 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 66 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 67 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 68 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 69 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 70 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 71 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 72 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 73 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 74 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 75 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 76 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 77 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 78 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 79 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 80 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 81 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 82 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 83 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 84 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 85 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 86 | {0, NULL}, |
jrodenburg | 2:bd118a724f03 | 87 | }; |
jrodenburg | 2:bd118a724f03 | 88 | |
jrodenburg | 2:bd118a724f03 | 89 | |
jrodenburg | 0:a28a1035c31b | 90 | //I2C AADRESS LOOK UP TABLE, CREATED IN EXCEL SHEET (COUNT, TEMP) |
jrodenburg | 2:bd118a724f03 | 91 | struct I2C_ADDR_LUT{ |
jrodenburg | 0:a28a1035c31b | 92 | int adc; |
jrodenburg | 0:a28a1035c31b | 93 | int io; |
jrodenburg | 0:a28a1035c31b | 94 | }; |
jrodenburg | 0:a28a1035c31b | 95 | |
jrodenburg | 0:a28a1035c31b | 96 | I2C_ADDR_LUT addrLUT[] = { |
jrodenburg | 0:a28a1035c31b | 97 | {0x23, 0x10}, |
jrodenburg | 0:a28a1035c31b | 98 | {0x53, 0x60}, |
jrodenburg | 0:a28a1035c31b | 99 | {0x43, 0x70}, |
jrodenburg | 0:a28a1035c31b | 100 | {0x73, 0x40}, |
jrodenburg | 0:a28a1035c31b | 101 | {0x63, 0x50}, |
jrodenburg | 0:a28a1035c31b | 102 | {0x22, 0x11}, |
jrodenburg | 0:a28a1035c31b | 103 | {0x52, 0x61}, |
jrodenburg | 0:a28a1035c31b | 104 | {0x42, 0x71}, |
jrodenburg | 0:a28a1035c31b | 105 | {0x72, 0x41}, |
jrodenburg | 0:a28a1035c31b | 106 | {0x62, 0x51}, |
jrodenburg | 0:a28a1035c31b | 107 | {0x21, 0x12}, |
jrodenburg | 0:a28a1035c31b | 108 | {0x51, 0x62}, |
jrodenburg | 0:a28a1035c31b | 109 | {0x41, 0x72}, |
jrodenburg | 0:a28a1035c31b | 110 | {0x71, 0x42}, |
jrodenburg | 0:a28a1035c31b | 111 | {0x61, 0x52}, |
jrodenburg | 0:a28a1035c31b | 112 | {0x20, 0x13}, |
jrodenburg | 0:a28a1035c31b | 113 | {0x50, 0x63}, |
jrodenburg | 0:a28a1035c31b | 114 | {0x40, 0x73}, |
jrodenburg | 0:a28a1035c31b | 115 | {0x70, 0x43}, |
jrodenburg | 0:a28a1035c31b | 116 | {0x60, 0x53}, |
jrodenburg | 0:a28a1035c31b | 117 | {0x26, 0x14}, |
jrodenburg | 0:a28a1035c31b | 118 | {0x56, 0x64}, |
jrodenburg | 0:a28a1035c31b | 119 | {0x46, 0x74}, |
jrodenburg | 0:a28a1035c31b | 120 | {0x76, 0x44}, |
jrodenburg | 0:a28a1035c31b | 121 | {0x66, 0x54}, |
jrodenburg | 0:a28a1035c31b | 122 | {0x2C, 0x1F}, |
jrodenburg | 0:a28a1035c31b | 123 | {0x5C, 0x6F}, |
jrodenburg | 0:a28a1035c31b | 124 | {0x4C, 0x7F}, |
jrodenburg | 0:a28a1035c31b | 125 | {0x7C, 0x4F}, |
jrodenburg | 0:a28a1035c31b | 126 | {0x6C, 0x5F}, |
jrodenburg | 0:a28a1035c31b | 127 | {0x2D, 0x1E}, |
jrodenburg | 0:a28a1035c31b | 128 | {0x5D, 0x6E}, |
jrodenburg | 0:a28a1035c31b | 129 | {0x4D, 0x7E}, |
jrodenburg | 0:a28a1035c31b | 130 | {0x7D, 0x4E}, |
jrodenburg | 0:a28a1035c31b | 131 | {0x6D, 0x5E}, |
jrodenburg | 0:a28a1035c31b | 132 | {0x2E, 0x1D}, |
jrodenburg | 0:a28a1035c31b | 133 | {0x5E, 0x6D}, |
jrodenburg | 0:a28a1035c31b | 134 | {0x4E, 0x7D}, |
jrodenburg | 0:a28a1035c31b | 135 | {0x7E, 0x4D}, |
jrodenburg | 0:a28a1035c31b | 136 | {0x6E, 0x5D}, |
jrodenburg | 0:a28a1035c31b | 137 | {0x2F, 0x1C}, |
jrodenburg | 0:a28a1035c31b | 138 | {0x5F, 0x6C}, |
jrodenburg | 0:a28a1035c31b | 139 | {0x4F, 0x7C}, |
jrodenburg | 0:a28a1035c31b | 140 | {0x7F, 0x4C}, |
jrodenburg | 0:a28a1035c31b | 141 | {0x6F, 0x5C}, |
jrodenburg | 0:a28a1035c31b | 142 | {0x20, 0x1B}, |
jrodenburg | 0:a28a1035c31b | 143 | {0x50, 0x6B}, |
jrodenburg | 0:a28a1035c31b | 144 | {0x40, 0x7B}, |
jrodenburg | 0:a28a1035c31b | 145 | }; |
jrodenburg | 0:a28a1035c31b | 146 | |
jrodenburg | 0:a28a1035c31b | 147 | //THERMISTOR LOOK UP TABLE, CREATED IN EXCEL SHEET (COUNT, TEMP) |
jrodenburg | 2:bd118a724f03 | 148 | struct THERM_LUT{ |
jrodenburg | 0:a28a1035c31b | 149 | int adc; |
jrodenburg | 0:a28a1035c31b | 150 | int temp; |
jrodenburg | 0:a28a1035c31b | 151 | }; |
jrodenburg | 0:a28a1035c31b | 152 | |
jrodenburg | 0:a28a1035c31b | 153 | THERM_LUT thermLUT[] = { |
jrodenburg | 0:a28a1035c31b | 154 | {113779,-40}, |
jrodenburg | 0:a28a1035c31b | 155 | {109152,-35}, |
jrodenburg | 0:a28a1035c31b | 156 | {103830,-30}, |
jrodenburg | 0:a28a1035c31b | 157 | {97855,-25}, |
jrodenburg | 0:a28a1035c31b | 158 | {91319,-20}, |
jrodenburg | 0:a28a1035c31b | 159 | {84352,-15}, |
jrodenburg | 0:a28a1035c31b | 160 | {77124,-10}, |
jrodenburg | 0:a28a1035c31b | 161 | {69820,-5}, |
jrodenburg | 0:a28a1035c31b | 162 | {62621,0}, |
jrodenburg | 0:a28a1035c31b | 163 | {55693,5}, |
jrodenburg | 0:a28a1035c31b | 164 | {49169,10}, |
jrodenburg | 0:a28a1035c31b | 165 | {43144,15}, |
jrodenburg | 0:a28a1035c31b | 166 | {37669,20}, |
jrodenburg | 0:a28a1035c31b | 167 | {32768,25}, |
jrodenburg | 0:a28a1035c31b | 168 | {28429,30}, |
jrodenburg | 0:a28a1035c31b | 169 | {24622,35}, |
jrodenburg | 0:a28a1035c31b | 170 | {21309,40}, |
jrodenburg | 0:a28a1035c31b | 171 | {18439,45}, |
jrodenburg | 0:a28a1035c31b | 172 | {15962,50}, |
jrodenburg | 0:a28a1035c31b | 173 | {13831,55}, |
jrodenburg | 0:a28a1035c31b | 174 | {12002,60}, |
jrodenburg | 0:a28a1035c31b | 175 | {10428,65}, |
jrodenburg | 0:a28a1035c31b | 176 | {9080,70}, |
jrodenburg | 0:a28a1035c31b | 177 | {7919,75}, |
jrodenburg | 0:a28a1035c31b | 178 | {6923,80}, |
jrodenburg | 0:a28a1035c31b | 179 | {6063,85}, |
jrodenburg | 0:a28a1035c31b | 180 | {5323,90}, |
jrodenburg | 0:a28a1035c31b | 181 | {4685,95}, |
jrodenburg | 0:a28a1035c31b | 182 | {4130,100}, |
jrodenburg | 0:a28a1035c31b | 183 | {3653,105}, |
jrodenburg | 0:a28a1035c31b | 184 | {3234,110}, |
jrodenburg | 0:a28a1035c31b | 185 | {2876,115}, |
jrodenburg | 0:a28a1035c31b | 186 | {2563,120}, |
jrodenburg | 0:a28a1035c31b | 187 | {2284,125} |
jrodenburg | 0:a28a1035c31b | 188 | }; |
jrodenburg | 0:a28a1035c31b | 189 | |
jrodenburg | 2:bd118a724f03 | 190 | //SERIAL COMMUNICATION SETUP |
jrodenburg | 2:bd118a724f03 | 191 | MODSERIAL pc(USBTX, USBRX); |
jrodenburg | 1:0182b86f9bd4 | 192 | |
jrodenburg | 0:a28a1035c31b | 193 | //DEFINE PINS |
jrodenburg | 0:a28a1035c31b | 194 | DigitalOut myled(LED2); |
jrodenburg | 0:a28a1035c31b | 195 | |
jrodenburg | 0:a28a1035c31b | 196 | //I2C FOR MCP23008 (I/O Control) |
jrodenburg | 2:bd118a724f03 | 197 | MCP23008 io_control(PTC9, PTC8, 0x10, 100000); //sda, scl |
jrodenburg | 0:a28a1035c31b | 198 | |
jrodenburg | 0:a28a1035c31b | 199 | //I2C FOR LTC2487 (ADC Control) |
jrodenburg | 2:bd118a724f03 | 200 | LTC2487 ltc2487(PTC11, PTC10, 0x23, 100000); //sda, scl |
jrodenburg | 0:a28a1035c31b | 201 | |
jrodenburg | 0:a28a1035c31b | 202 | //GLOBAL VARIABLES |
jrodenburg | 2:bd118a724f03 | 203 | volatile bool dataRecieved = false; //used to check if data has been recieved |
jrodenburg | 2:bd118a724f03 | 204 | char rxBuf[50]; |
jrodenburg | 2:bd118a724f03 | 205 | int chnlSel; |
jrodenburg | 0:a28a1035c31b | 206 | |
jrodenburg | 2:bd118a724f03 | 207 | /* Function: rxInterrupt |
jrodenburg | 1:0182b86f9bd4 | 208 | ************************************************************** |
jrodenburg | 2:bd118a724f03 | 209 | Description: serial rx interupt handler |
jrodenburg | 2:bd118a724f03 | 210 | Recieves: N/A |
jrodenburg | 2:bd118a724f03 | 211 | Returns: N/A |
jrodenburg | 2:bd118a724f03 | 212 | */ |
jrodenburg | 2:bd118a724f03 | 213 | |
jrodenburg | 2:bd118a724f03 | 214 | void rxInterrupt(MODSERIAL_IRQ_INFO *info){ |
jrodenburg | 2:bd118a724f03 | 215 | dataRecieved = true; |
jrodenburg | 2:bd118a724f03 | 216 | if(DEBUG) pc.printf("DATA RECIEVED \r\n"); |
jrodenburg | 2:bd118a724f03 | 217 | } |
jrodenburg | 2:bd118a724f03 | 218 | |
jrodenburg | 2:bd118a724f03 | 219 | /* Function: parseRXData |
jrodenburg | 2:bd118a724f03 | 220 | ************************************************************** |
jrodenburg | 2:bd118a724f03 | 221 | Description: The parse recieved data into |
jrodenburg | 2:bd118a724f03 | 222 | Recieves: chn: The channel we want to put into the channel |
jrodenburg | 1:0182b86f9bd4 | 223 | Returns: N/A |
jrodenburg | 1:0182b86f9bd4 | 224 | */ |
jrodenburg | 0:a28a1035c31b | 225 | |
jrodenburg | 2:bd118a724f03 | 226 | void parseRXData(){ |
jrodenburg | 2:bd118a724f03 | 227 | int pCount = 0; //count data collected |
jrodenburg | 2:bd118a724f03 | 228 | int i = 0; |
jrodenburg | 2:bd118a724f03 | 229 | int chnl; |
jrodenburg | 2:bd118a724f03 | 230 | string data = ""; |
jrodenburg | 2:bd118a724f03 | 231 | |
jrodenburg | 2:bd118a724f03 | 232 | if(DEBUG) pc.printf("buff1 = <%s> \r\n", rxBuf); |
jrodenburg | 1:0182b86f9bd4 | 233 | |
jrodenburg | 2:bd118a724f03 | 234 | while(pCount < 3){ |
jrodenburg | 2:bd118a724f03 | 235 | if(rxBuf[i] != '.'){ |
jrodenburg | 2:bd118a724f03 | 236 | data += rxBuf[i]; |
jrodenburg | 2:bd118a724f03 | 237 | } |
jrodenburg | 2:bd118a724f03 | 238 | else{ |
jrodenburg | 2:bd118a724f03 | 239 | pCount++; |
jrodenburg | 2:bd118a724f03 | 240 | if(pCount == 1){ //get channel |
jrodenburg | 2:bd118a724f03 | 241 | if((atoi(data.c_str()) < 0) || (atoi(data.c_str()) > 15)){ |
jrodenburg | 2:bd118a724f03 | 242 | //check if channel is out of array index |
jrodenburg | 2:bd118a724f03 | 243 | if(DEBUG) pc.printf("ERROR: INDEX OUT OF RANGE! \r\n", rxBuf); |
jrodenburg | 2:bd118a724f03 | 244 | } |
jrodenburg | 2:bd118a724f03 | 245 | else{ |
jrodenburg | 2:bd118a724f03 | 246 | chnl = atoi(data.c_str()); |
jrodenburg | 2:bd118a724f03 | 247 | chnl = chnl-1; //fix for array indexing |
jrodenburg | 2:bd118a724f03 | 248 | chnlSel = chnl; //chnl selected by user in GUI |
jrodenburg | 2:bd118a724f03 | 249 | } |
jrodenburg | 2:bd118a724f03 | 250 | } |
jrodenburg | 2:bd118a724f03 | 251 | else if(pCount == 2){ //get channel temperature |
jrodenburg | 4:168a446bd0da | 252 | chnlStatus[chnl].setTemp = atoi(data.c_str()); |
jrodenburg | 4:168a446bd0da | 253 | if(DEBUG) pc.printf("CHANNEL TEMP: %f \r\n", chnlStatus[chnl].setTemp); |
jrodenburg | 2:bd118a724f03 | 254 | } |
jrodenburg | 2:bd118a724f03 | 255 | else if(pCount == 3){ //get channel status |
jrodenburg | 4:168a446bd0da | 256 | chnlStatus[chnl].status = atoi(data.c_str()); |
jrodenburg | 4:168a446bd0da | 257 | if(DEBUG) pc.printf("CHANNEL STATUS: %f \r\n", chnlStatus[chnl].status); |
jrodenburg | 2:bd118a724f03 | 258 | } |
jrodenburg | 2:bd118a724f03 | 259 | data = ""; |
jrodenburg | 2:bd118a724f03 | 260 | } |
jrodenburg | 2:bd118a724f03 | 261 | i++; |
jrodenburg | 2:bd118a724f03 | 262 | } |
jrodenburg | 1:0182b86f9bd4 | 263 | } |
jrodenburg | 0:a28a1035c31b | 264 | |
jrodenburg | 0:a28a1035c31b | 265 | /* Function: get_temp |
jrodenburg | 0:a28a1035c31b | 266 | ************************************************************** |
jrodenburg | 0:a28a1035c31b | 267 | Description: Retrieve data from thermistor |
jrodenburg | 0:a28a1035c31b | 268 | Recieves: chn: the channel of the fixture to read temp. from |
jrodenburg | 5:0f38a0bd4f86 | 269 | port: the I/O channel to read |
jrodenburg | 0:a28a1035c31b | 270 | Returns: the temperature of the fixture (front or back) |
jrodenburg | 0:a28a1035c31b | 271 | */ |
jrodenburg | 0:a28a1035c31b | 272 | |
jrodenburg | 5:0f38a0bd4f86 | 273 | float get_temp(int chn, int port){ |
jrodenburg | 1:0182b86f9bd4 | 274 | myled = 1; |
jrodenburg | 0:a28a1035c31b | 275 | ltc2487.setAddress(addrLUT[chn].adc); |
jrodenburg | 0:a28a1035c31b | 276 | |
jrodenburg | 5:0f38a0bd4f86 | 277 | float ADC_val = ltc2487.readOutput(port); //(65536*1.334)/2.5 |
jrodenburg | 0:a28a1035c31b | 278 | |
jrodenburg | 0:a28a1035c31b | 279 | int i = 0; |
jrodenburg | 0:a28a1035c31b | 280 | |
jrodenburg | 0:a28a1035c31b | 281 | while((i < sizeLUT) && (thermLUT[i].adc > ADC_val)){ |
jrodenburg | 0:a28a1035c31b | 282 | i++; |
jrodenburg | 0:a28a1035c31b | 283 | } //find the temp. above therm temp |
jrodenburg | 0:a28a1035c31b | 284 | |
jrodenburg | 0:a28a1035c31b | 285 | //Point slope formula extrapolation: |
jrodenburg | 0:a28a1035c31b | 286 | // y1 = m (x1-x0)+ y0 , y = temp. value, x = adc value |
jrodenburg | 0:a28a1035c31b | 287 | // y1 = thermLUT[i-1].temp y0 = thermLUT[i].temp |
jrodenburg | 0:a28a1035c31b | 288 | // x1 = thermLUT[i-1].adc x0 =thermLUT[i].adc |
jrodenburg | 0:a28a1035c31b | 289 | float a = float(thermLUT[i-1].temp - thermLUT[i].temp); //slope of temp between points where therm temp is between (Tmax - Tmin) |
jrodenburg | 0:a28a1035c31b | 290 | float b = float(thermLUT[i-1].adc - thermLUT[i].adc); //slope of adc between points where therm adc is between (Amax - Amin) |
jrodenburg | 0:a28a1035c31b | 291 | |
jrodenburg | 0:a28a1035c31b | 292 | float m = a/b; |
jrodenburg | 0:a28a1035c31b | 293 | float y = (m*(ADC_val-thermLUT[i].adc))+thermLUT[i].temp; |
jrodenburg | 0:a28a1035c31b | 294 | |
jrodenburg | 5:0f38a0bd4f86 | 295 | if(DEBUG2) pc.printf("CHNL: %i PORT: %i ADC VAL: %f TEMP: %f \r\n", chn, port, ADC_val, y); |
jrodenburg | 1:0182b86f9bd4 | 296 | |
jrodenburg | 4:168a446bd0da | 297 | //if(chn == chnlSel) |
jrodenburg | 4:168a446bd0da | 298 | pc.printf("%f \r\n", y); |
jrodenburg | 4:168a446bd0da | 299 | |
jrodenburg | 0:a28a1035c31b | 300 | return y; |
jrodenburg | 0:a28a1035c31b | 301 | } |
jrodenburg | 0:a28a1035c31b | 302 | |
jrodenburg | 0:a28a1035c31b | 303 | /* Function: get_heater_current |
jrodenburg | 0:a28a1035c31b | 304 | ************************************************************** |
jrodenburg | 0:a28a1035c31b | 305 | Description: Retrieve current into heater control MOSFET |
jrodenburg | 0:a28a1035c31b | 306 | Recieves: chn: the channel of the fixture to read current from |
jrodenburg | 0:a28a1035c31b | 307 | Returns: the current into the heater control MOSFET |
jrodenburg | 0:a28a1035c31b | 308 | */ |
jrodenburg | 0:a28a1035c31b | 309 | |
jrodenburg | 0:a28a1035c31b | 310 | float get_heater_current(int chn){ |
jrodenburg | 1:0182b86f9bd4 | 311 | return 0.0; |
jrodenburg | 0:a28a1035c31b | 312 | } |
jrodenburg | 0:a28a1035c31b | 313 | |
jrodenburg | 0:a28a1035c31b | 314 | /* Function: get_valve_current |
jrodenburg | 0:a28a1035c31b | 315 | ************************************************************** |
jrodenburg | 0:a28a1035c31b | 316 | Description: Retrieve current into valve control MOSFET |
jrodenburg | 0:a28a1035c31b | 317 | Recieves: chn: the channel of the fixture to read current from |
jrodenburg | 0:a28a1035c31b | 318 | Returns: the current into the valve control MOSFET |
jrodenburg | 0:a28a1035c31b | 319 | */ |
jrodenburg | 0:a28a1035c31b | 320 | |
jrodenburg | 0:a28a1035c31b | 321 | float get_valve_current(int chn){ |
jrodenburg | 1:0182b86f9bd4 | 322 | return 0.0; |
jrodenburg | 0:a28a1035c31b | 323 | } |
jrodenburg | 0:a28a1035c31b | 324 | |
jrodenburg | 0:a28a1035c31b | 325 | /* Function: turn_valve_on |
jrodenburg | 0:a28a1035c31b | 326 | ************************************************************** |
jrodenburg | 1:0182b86f9bd4 | 327 | Description: Turn valve on and green status LED on |
jrodenburg | 0:a28a1035c31b | 328 | Recieves: chn: the channel of the fixture |
jrodenburg | 1:0182b86f9bd4 | 329 | Returns: N/A |
jrodenburg | 0:a28a1035c31b | 330 | */ |
jrodenburg | 0:a28a1035c31b | 331 | |
jrodenburg | 1:0182b86f9bd4 | 332 | void turn_valve_on(int chn){ |
jrodenburg | 0:a28a1035c31b | 333 | io_control.setAddress(addrLUT[chn].io); |
jrodenburg | 0:a28a1035c31b | 334 | io_control.init(); |
jrodenburg | 1:0182b86f9bd4 | 335 | io_control.writeOutput(1,0,1,0); |
jrodenburg | 0:a28a1035c31b | 336 | } |
jrodenburg | 0:a28a1035c31b | 337 | |
jrodenburg | 0:a28a1035c31b | 338 | /* Function: turn_valve_off |
jrodenburg | 0:a28a1035c31b | 339 | ************************************************************** |
jrodenburg | 1:0182b86f9bd4 | 340 | Description: Turn valve off and green status LED on |
jrodenburg | 0:a28a1035c31b | 341 | Recieves: chn: the channel of the fixture |
jrodenburg | 1:0182b86f9bd4 | 342 | Returns: N/A |
jrodenburg | 0:a28a1035c31b | 343 | */ |
jrodenburg | 0:a28a1035c31b | 344 | |
jrodenburg | 1:0182b86f9bd4 | 345 | void turn_valve_off(int chn){ |
jrodenburg | 0:a28a1035c31b | 346 | io_control.setAddress(addrLUT[chn].io); |
jrodenburg | 0:a28a1035c31b | 347 | io_control.init(); |
jrodenburg | 1:0182b86f9bd4 | 348 | io_control.writeOutput(0,0,1,0); |
jrodenburg | 0:a28a1035c31b | 349 | } |
jrodenburg | 0:a28a1035c31b | 350 | |
jrodenburg | 0:a28a1035c31b | 351 | /* Function: turn_heater_on |
jrodenburg | 0:a28a1035c31b | 352 | ************************************************************** |
jrodenburg | 1:0182b86f9bd4 | 353 | Description: Turn heater on and green status LED on |
jrodenburg | 0:a28a1035c31b | 354 | Recieves: chn: the channel of the fixture |
jrodenburg | 1:0182b86f9bd4 | 355 | Returns: N/A |
jrodenburg | 0:a28a1035c31b | 356 | */ |
jrodenburg | 0:a28a1035c31b | 357 | |
jrodenburg | 1:0182b86f9bd4 | 358 | void turn_heater_on(int chn){ |
jrodenburg | 0:a28a1035c31b | 359 | io_control.setAddress(addrLUT[chn].io); |
jrodenburg | 0:a28a1035c31b | 360 | io_control.init(); |
jrodenburg | 1:0182b86f9bd4 | 361 | io_control.writeOutput(0,1,1,0); |
jrodenburg | 0:a28a1035c31b | 362 | } |
jrodenburg | 0:a28a1035c31b | 363 | |
jrodenburg | 0:a28a1035c31b | 364 | /* Function: turn_heater_off |
jrodenburg | 0:a28a1035c31b | 365 | ************************************************************** |
jrodenburg | 1:0182b86f9bd4 | 366 | Description: Turn heater off and green status LED on |
jrodenburg | 0:a28a1035c31b | 367 | Recieves: chn: the channel of the fixture |
jrodenburg | 1:0182b86f9bd4 | 368 | Returns: N/A |
jrodenburg | 0:a28a1035c31b | 369 | */ |
jrodenburg | 0:a28a1035c31b | 370 | |
jrodenburg | 1:0182b86f9bd4 | 371 | void turn_heater_off(int chn){ |
jrodenburg | 0:a28a1035c31b | 372 | io_control.setAddress(addrLUT[chn].io); |
jrodenburg | 0:a28a1035c31b | 373 | io_control.init(); |
jrodenburg | 1:0182b86f9bd4 | 374 | io_control.writeOutput(0,0,1,0); |
jrodenburg | 0:a28a1035c31b | 375 | } |
jrodenburg | 0:a28a1035c31b | 376 | |
jrodenburg | 0:a28a1035c31b | 377 | /* Function: status_led |
jrodenburg | 0:a28a1035c31b | 378 | ************************************************************** |
jrodenburg | 0:a28a1035c31b | 379 | Description: Turn status LED on (turns on green or red) |
jrodenburg | 0:a28a1035c31b | 380 | Recieves: chn: the channel of the fixture |
jrodenburg | 0:a28a1035c31b | 381 | status: the status of channel (good (1) or bad (0)) |
jrodenburg | 1:0182b86f9bd4 | 382 | Returns: N/A |
jrodenburg | 0:a28a1035c31b | 383 | */ |
jrodenburg | 0:a28a1035c31b | 384 | |
jrodenburg | 1:0182b86f9bd4 | 385 | void status_led(int chn, int status){ |
jrodenburg | 0:a28a1035c31b | 386 | io_control.setAddress(addrLUT[chn].io); |
jrodenburg | 0:a28a1035c31b | 387 | if(status){ |
jrodenburg | 1:0182b86f9bd4 | 388 | io_control.writeOutput(0,0,1,0); |
jrodenburg | 0:a28a1035c31b | 389 | } |
jrodenburg | 0:a28a1035c31b | 390 | else{ |
jrodenburg | 1:0182b86f9bd4 | 391 | io_control.writeOutput(0,0,0,1); |
jrodenburg | 0:a28a1035c31b | 392 | } |
jrodenburg | 0:a28a1035c31b | 393 | } |
jrodenburg | 0:a28a1035c31b | 394 | |
jrodenburg | 1:0182b86f9bd4 | 395 | /* Function: test_mcp23008 |
jrodenburg | 1:0182b86f9bd4 | 396 | ************************************************************** |
jrodenburg | 1:0182b86f9bd4 | 397 | Description: Test each output of the MCP23009 |
jrodenburg | 1:0182b86f9bd4 | 398 | Recieves: N/A |
jrodenburg | 1:0182b86f9bd4 | 399 | Returns: N/A |
jrodenburg | 1:0182b86f9bd4 | 400 | */ |
jrodenburg | 1:0182b86f9bd4 | 401 | |
jrodenburg | 1:0182b86f9bd4 | 402 | void test_mcp23008(int chn){ |
jrodenburg | 1:0182b86f9bd4 | 403 | turn_valve_on(chn); |
jrodenburg | 1:0182b86f9bd4 | 404 | wait(1); |
jrodenburg | 1:0182b86f9bd4 | 405 | turn_valve_off(chn); |
jrodenburg | 1:0182b86f9bd4 | 406 | wait(1); |
jrodenburg | 1:0182b86f9bd4 | 407 | turn_heater_on(chn); |
jrodenburg | 1:0182b86f9bd4 | 408 | wait(1); |
jrodenburg | 1:0182b86f9bd4 | 409 | turn_heater_off(chn); |
jrodenburg | 1:0182b86f9bd4 | 410 | wait(1); |
jrodenburg | 1:0182b86f9bd4 | 411 | status_led(chn, 0); |
jrodenburg | 1:0182b86f9bd4 | 412 | wait(1); |
jrodenburg | 1:0182b86f9bd4 | 413 | status_led(chn, 1); |
jrodenburg | 1:0182b86f9bd4 | 414 | } |
jrodenburg | 1:0182b86f9bd4 | 415 | |
jrodenburg | 2:bd118a724f03 | 416 | /* Function: test_ltc2487 |
jrodenburg | 2:bd118a724f03 | 417 | ************************************************************** |
jrodenburg | 2:bd118a724f03 | 418 | Description: Test the reading from LTC2487 |
jrodenburg | 2:bd118a724f03 | 419 | Recieves: N/A |
jrodenburg | 2:bd118a724f03 | 420 | Returns: N/A |
jrodenburg | 2:bd118a724f03 | 421 | */ |
jrodenburg | 2:bd118a724f03 | 422 | |
jrodenburg | 2:bd118a724f03 | 423 | void test_ltc2487(int chn){ |
jrodenburg | 5:0f38a0bd4f86 | 424 | get_temp(chn, FRONT_THERM); |
jrodenburg | 5:0f38a0bd4f86 | 425 | wait(0.1); |
jrodenburg | 5:0f38a0bd4f86 | 426 | //get_temp(chn, BACK_THERM); |
jrodenburg | 5:0f38a0bd4f86 | 427 | //wait(0.1); |
jrodenburg | 5:0f38a0bd4f86 | 428 | //get_temp(chn, VALV_FET_AMP); |
jrodenburg | 5:0f38a0bd4f86 | 429 | //wait(0.1); |
jrodenburg | 4:168a446bd0da | 430 | //if(DEBUG1) pc.printf("TEMPERATURE READING: %f \r\n", get_temp(chn)); |
jrodenburg | 2:bd118a724f03 | 431 | } |
jrodenburg | 2:bd118a724f03 | 432 | |
jrodenburg | 5:0f38a0bd4f86 | 433 | void test_all_IO(){ |
jrodenburg | 5:0f38a0bd4f86 | 434 | test_mcp23008(0); |
jrodenburg | 5:0f38a0bd4f86 | 435 | test_mcp23008(1); |
jrodenburg | 5:0f38a0bd4f86 | 436 | test_mcp23008(2); |
jrodenburg | 5:0f38a0bd4f86 | 437 | test_mcp23008(3); |
jrodenburg | 5:0f38a0bd4f86 | 438 | test_mcp23008(4); |
jrodenburg | 5:0f38a0bd4f86 | 439 | test_mcp23008(5); |
jrodenburg | 5:0f38a0bd4f86 | 440 | test_mcp23008(6); |
jrodenburg | 5:0f38a0bd4f86 | 441 | test_mcp23008(7); |
jrodenburg | 5:0f38a0bd4f86 | 442 | } |
jrodenburg | 5:0f38a0bd4f86 | 443 | |
jrodenburg | 5:0f38a0bd4f86 | 444 | void test_all_ADC(){ |
jrodenburg | 5:0f38a0bd4f86 | 445 | test_ltc2487(0); |
jrodenburg | 5:0f38a0bd4f86 | 446 | test_ltc2487(1); |
jrodenburg | 5:0f38a0bd4f86 | 447 | test_ltc2487(2); |
jrodenburg | 5:0f38a0bd4f86 | 448 | test_ltc2487(3); |
jrodenburg | 5:0f38a0bd4f86 | 449 | test_ltc2487(4); |
jrodenburg | 5:0f38a0bd4f86 | 450 | test_ltc2487(5); |
jrodenburg | 5:0f38a0bd4f86 | 451 | test_ltc2487(6); |
jrodenburg | 5:0f38a0bd4f86 | 452 | test_ltc2487(7); |
jrodenburg | 5:0f38a0bd4f86 | 453 | } |
jrodenburg | 0:a28a1035c31b | 454 | |
jrodenburg | 0:a28a1035c31b | 455 | |
jrodenburg | 0:a28a1035c31b | 456 | int main() { |
jrodenburg | 0:a28a1035c31b | 457 | |
jrodenburg | 5:0f38a0bd4f86 | 458 | //pc.baud(9600); |
jrodenburg | 5:0f38a0bd4f86 | 459 | //pc.autoDetectChar('E'); |
jrodenburg | 5:0f38a0bd4f86 | 460 | //pc.attach(&rxInterrupt, MODSERIAL::RxAutoDetect); |
jrodenburg | 4:168a446bd0da | 461 | |
jrodenburg | 0:a28a1035c31b | 462 | myled = 1; |
jrodenburg | 5:0f38a0bd4f86 | 463 | |
jrodenburg | 5:0f38a0bd4f86 | 464 | |
jrodenburg | 4:168a446bd0da | 465 | //test_mcp23008(1); |
jrodenburg | 5:0f38a0bd4f86 | 466 | //FRONT_THERM, BACK_THERM, HEAT_FET_AMP, VALV_FET_AMP |
jrodenburg | 5:0f38a0bd4f86 | 467 | /*while(1){ |
jrodenburg | 5:0f38a0bd4f86 | 468 | myled = 0; |
jrodenburg | 5:0f38a0bd4f86 | 469 | //test_all_ADC(); |
jrodenburg | 5:0f38a0bd4f86 | 470 | //test_ltc2487(0); |
jrodenburg | 5:0f38a0bd4f86 | 471 | test_all_IO(); |
jrodenburg | 5:0f38a0bd4f86 | 472 | wait(1); |
jrodenburg | 5:0f38a0bd4f86 | 473 | myled = 1; |
jrodenburg | 4:168a446bd0da | 474 | }*/ |
jrodenburg | 0:a28a1035c31b | 475 | |
jrodenburg | 5:0f38a0bd4f86 | 476 | |
jrodenburg | 0:a28a1035c31b | 477 | while(1) { |
jrodenburg | 5:0f38a0bd4f86 | 478 | if(DEBUG3) pc.printf("THE PROGRAM STARTED \r\n"); |
jrodenburg | 2:bd118a724f03 | 479 | |
jrodenburg | 2:bd118a724f03 | 480 | //check if we recieved data/need to update TCTF data |
jrodenburg | 2:bd118a724f03 | 481 | if(dataRecieved){ |
jrodenburg | 2:bd118a724f03 | 482 | dataRecieved = false; |
jrodenburg | 2:bd118a724f03 | 483 | pc.autoDetectChar('\n'); |
jrodenburg | 2:bd118a724f03 | 484 | pc.move(rxBuf, 50); |
jrodenburg | 2:bd118a724f03 | 485 | pc.rxBufferFlush(); |
jrodenburg | 2:bd118a724f03 | 486 | parseRXData(); |
jrodenburg | 2:bd118a724f03 | 487 | } |
jrodenburg | 2:bd118a724f03 | 488 | |
jrodenburg | 5:0f38a0bd4f86 | 489 | for(int chnl = 0; chnl < CHN_COUNT; chnl++){ |
jrodenburg | 5:0f38a0bd4f86 | 490 | //wait(2.5); |
jrodenburg | 5:0f38a0bd4f86 | 491 | float currentTempFront = get_temp(chnl, FRONT_THERM); |
jrodenburg | 5:0f38a0bd4f86 | 492 | //float currentTempBack = get_temp(chnl, BACK_THERM); |
jrodenburg | 2:bd118a724f03 | 493 | |
jrodenburg | 2:bd118a724f03 | 494 | //CONTROL LOOP: |
jrodenburg | 4:168a446bd0da | 495 | if(chnlStatus[chnl].status == 1){ |
jrodenburg | 5:0f38a0bd4f86 | 496 | if(DEBUG3) pc.printf("TEMP: %f \r\n", currentTempFront); |
jrodenburg | 5:0f38a0bd4f86 | 497 | if(currentTempFront > ((chnlStatus[chnl].setTemp)+HYST)){ |
jrodenburg | 5:0f38a0bd4f86 | 498 | if(DEBUG3) pc.printf("CHILLER ON \r\n"); |
jrodenburg | 5:0f38a0bd4f86 | 499 | //Turn heater off |
jrodenburg | 5:0f38a0bd4f86 | 500 | //turn_heater_off(chnl); |
jrodenburg | 2:bd118a724f03 | 501 | //Turn chiller on |
jrodenburg | 2:bd118a724f03 | 502 | turn_valve_on(chnl); |
jrodenburg | 2:bd118a724f03 | 503 | } |
jrodenburg | 5:0f38a0bd4f86 | 504 | else if (currentTempFront < ((chnlStatus[chnl].setTemp)-HYST)){ |
jrodenburg | 5:0f38a0bd4f86 | 505 | if(DEBUG3) pc.printf("HEATER ON \r\n"); |
jrodenburg | 2:bd118a724f03 | 506 | //Turn chiller off |
jrodenburg | 2:bd118a724f03 | 507 | turn_valve_off(chnl); |
jrodenburg | 2:bd118a724f03 | 508 | //Turn heater on |
jrodenburg | 2:bd118a724f03 | 509 | turn_heater_on(chnl); |
jrodenburg | 2:bd118a724f03 | 510 | } |
jrodenburg | 2:bd118a724f03 | 511 | else{ |
jrodenburg | 5:0f38a0bd4f86 | 512 | if(DEBUG3) pc.printf("ALL OFF \r\n"); |
jrodenburg | 2:bd118a724f03 | 513 | //turn off chiller |
jrodenburg | 2:bd118a724f03 | 514 | turn_valve_off(chnl); |
jrodenburg | 2:bd118a724f03 | 515 | //turn off heater |
jrodenburg | 2:bd118a724f03 | 516 | turn_heater_off(chnl); |
jrodenburg | 2:bd118a724f03 | 517 | //turn on green LED status light |
jrodenburg | 2:bd118a724f03 | 518 | status_led(chnl, 1); |
jrodenburg | 2:bd118a724f03 | 519 | } |
jrodenburg | 2:bd118a724f03 | 520 | } |
jrodenburg | 2:bd118a724f03 | 521 | } |
jrodenburg | 0:a28a1035c31b | 522 | } |
jrodenburg | 0:a28a1035c31b | 523 | } |