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