Control Code with I/O and ADC working

Dependencies:   MODSERIAL mbed

Committer:
jrodenburg
Date:
Wed Mar 07 18:50:10 2018 +0000
Revision:
6:c980535393ed
Parent:
5:0f38a0bd4f86
Child:
7:8a5e65e63e2a
Most recent code, running on test rack.

Who changed what in which revision?

UserRevisionLine numberNew 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 6:c980535393ed 6 #include "MCP23008.h"A
jrodenburg 0:a28a1035c31b 7 #include "LTC2487.h"
jrodenburg 2:bd118a724f03 8 #include <string>
jrodenburg 0:a28a1035c31b 9
jrodenburg 0:a28a1035c31b 10 //DEFINITIVE VARIABLES
jrodenburg 6:c980535393ed 11 #define DEBUG 0
jrodenburg 6:c980535393ed 12 #define DEBUG1 1
jrodenburg 6:c980535393ed 13 #define DEBUG2 1
jrodenburg 6:c980535393ed 14 #define DEBUG3 1
jrodenburg 6:c980535393ed 15 #define CHN_COUNT 8
jrodenburg 6:c980535393ed 16 #define MIN_TEMP 15
jrodenburg 6:c980535393ed 17 #define MAX_TEMP 60
jrodenburg 6:c980535393ed 18 #define TEMP_MARGIN 2
jrodenburg 6:c980535393ed 19 #define HYST 0.5
jrodenburg 6:c980535393ed 20 #define SAMPLES 5
jrodenburg 6:c980535393ed 21 #define I2C_Freq 2000
jrodenburg 6:c980535393ed 22 #define VALVE 1
jrodenburg 6:c980535393ed 23 #define HEATER 2
jrodenburg 6:c980535393ed 24 #define STATUS_GOOD 3
jrodenburg 6:c980535393ed 25 #define STATUS_BAD 4
jrodenburg 6:c980535393ed 26 #define sizeLUT 34
jrodenburg 6:c980535393ed 27 #define FRONT_THERM 0
jrodenburg 6:c980535393ed 28 #define BACK_THERM 1
jrodenburg 6:c980535393ed 29 #define HEAT_FET_AMP 2
jrodenburg 6:c980535393ed 30 #define VALV_FET_AMP 3
jrodenburg 6:c980535393ed 31 #define FET_ON_CURRENT 1.12
jrodenburg 0:a28a1035c31b 32
jrodenburg 2:bd118a724f03 33
jrodenburg 2:bd118a724f03 34 //TCTF CHANNEL DATA
jrodenburg 2:bd118a724f03 35 struct CHNL_DATA{
jrodenburg 2:bd118a724f03 36 bool status;
jrodenburg 2:bd118a724f03 37 float setTemp;
jrodenburg 2:bd118a724f03 38 };
jrodenburg 2:bd118a724f03 39
jrodenburg 4:168a446bd0da 40 CHNL_DATA chnlStatus[] = {
jrodenburg 6:c980535393ed 41 {1, 15},
jrodenburg 6:c980535393ed 42 {1, 20},
jrodenburg 6:c980535393ed 43 {1, 25},
jrodenburg 6:c980535393ed 44 {1, 30},
jrodenburg 6:c980535393ed 45 {1, 35},
jrodenburg 6:c980535393ed 46 {1, 0},
jrodenburg 5:0f38a0bd4f86 47 {1, 40},
jrodenburg 6:c980535393ed 48 {1, 45},
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 {0, NULL},
jrodenburg 2:bd118a724f03 88 {0, NULL},
jrodenburg 2:bd118a724f03 89 };
jrodenburg 2:bd118a724f03 90
jrodenburg 2:bd118a724f03 91
jrodenburg 0:a28a1035c31b 92 //I2C AADRESS LOOK UP TABLE, CREATED IN EXCEL SHEET (COUNT, TEMP)
jrodenburg 2:bd118a724f03 93 struct I2C_ADDR_LUT{
jrodenburg 0:a28a1035c31b 94 int adc;
jrodenburg 0:a28a1035c31b 95 int io;
jrodenburg 0:a28a1035c31b 96 };
jrodenburg 0:a28a1035c31b 97
jrodenburg 0:a28a1035c31b 98 I2C_ADDR_LUT addrLUT[] = {
jrodenburg 0:a28a1035c31b 99 {0x23, 0x10},
jrodenburg 0:a28a1035c31b 100 {0x53, 0x60},
jrodenburg 0:a28a1035c31b 101 {0x43, 0x70},
jrodenburg 0:a28a1035c31b 102 {0x73, 0x40},
jrodenburg 0:a28a1035c31b 103 {0x63, 0x50},
jrodenburg 0:a28a1035c31b 104 {0x22, 0x11},
jrodenburg 0:a28a1035c31b 105 {0x52, 0x61},
jrodenburg 0:a28a1035c31b 106 {0x42, 0x71},
jrodenburg 0:a28a1035c31b 107 {0x72, 0x41},
jrodenburg 0:a28a1035c31b 108 {0x62, 0x51},
jrodenburg 0:a28a1035c31b 109 {0x21, 0x12},
jrodenburg 0:a28a1035c31b 110 {0x51, 0x62},
jrodenburg 0:a28a1035c31b 111 {0x41, 0x72},
jrodenburg 0:a28a1035c31b 112 {0x71, 0x42},
jrodenburg 0:a28a1035c31b 113 {0x61, 0x52},
jrodenburg 0:a28a1035c31b 114 {0x20, 0x13},
jrodenburg 0:a28a1035c31b 115 {0x50, 0x63},
jrodenburg 0:a28a1035c31b 116 {0x40, 0x73},
jrodenburg 0:a28a1035c31b 117 {0x70, 0x43},
jrodenburg 0:a28a1035c31b 118 {0x60, 0x53},
jrodenburg 0:a28a1035c31b 119 {0x26, 0x14},
jrodenburg 0:a28a1035c31b 120 {0x56, 0x64},
jrodenburg 0:a28a1035c31b 121 {0x46, 0x74},
jrodenburg 0:a28a1035c31b 122 {0x76, 0x44},
jrodenburg 0:a28a1035c31b 123 {0x66, 0x54},
jrodenburg 0:a28a1035c31b 124 {0x2C, 0x1F},
jrodenburg 0:a28a1035c31b 125 {0x5C, 0x6F},
jrodenburg 0:a28a1035c31b 126 {0x4C, 0x7F},
jrodenburg 0:a28a1035c31b 127 {0x7C, 0x4F},
jrodenburg 0:a28a1035c31b 128 {0x6C, 0x5F},
jrodenburg 0:a28a1035c31b 129 {0x2D, 0x1E},
jrodenburg 0:a28a1035c31b 130 {0x5D, 0x6E},
jrodenburg 0:a28a1035c31b 131 {0x4D, 0x7E},
jrodenburg 0:a28a1035c31b 132 {0x7D, 0x4E},
jrodenburg 0:a28a1035c31b 133 {0x6D, 0x5E},
jrodenburg 0:a28a1035c31b 134 {0x2E, 0x1D},
jrodenburg 0:a28a1035c31b 135 {0x5E, 0x6D},
jrodenburg 0:a28a1035c31b 136 {0x4E, 0x7D},
jrodenburg 0:a28a1035c31b 137 {0x7E, 0x4D},
jrodenburg 0:a28a1035c31b 138 {0x6E, 0x5D},
jrodenburg 0:a28a1035c31b 139 {0x2F, 0x1C},
jrodenburg 0:a28a1035c31b 140 {0x5F, 0x6C},
jrodenburg 0:a28a1035c31b 141 {0x4F, 0x7C},
jrodenburg 0:a28a1035c31b 142 {0x7F, 0x4C},
jrodenburg 0:a28a1035c31b 143 {0x6F, 0x5C},
jrodenburg 0:a28a1035c31b 144 {0x20, 0x1B},
jrodenburg 0:a28a1035c31b 145 {0x50, 0x6B},
jrodenburg 0:a28a1035c31b 146 {0x40, 0x7B},
jrodenburg 0:a28a1035c31b 147 };
jrodenburg 0:a28a1035c31b 148
jrodenburg 0:a28a1035c31b 149 //THERMISTOR LOOK UP TABLE, CREATED IN EXCEL SHEET (COUNT, TEMP)
jrodenburg 2:bd118a724f03 150 struct THERM_LUT{
jrodenburg 0:a28a1035c31b 151 int adc;
jrodenburg 0:a28a1035c31b 152 int temp;
jrodenburg 0:a28a1035c31b 153 };
jrodenburg 0:a28a1035c31b 154
jrodenburg 0:a28a1035c31b 155 THERM_LUT thermLUT[] = {
jrodenburg 0:a28a1035c31b 156 {113779,-40},
jrodenburg 0:a28a1035c31b 157 {109152,-35},
jrodenburg 0:a28a1035c31b 158 {103830,-30},
jrodenburg 0:a28a1035c31b 159 {97855,-25},
jrodenburg 0:a28a1035c31b 160 {91319,-20},
jrodenburg 0:a28a1035c31b 161 {84352,-15},
jrodenburg 0:a28a1035c31b 162 {77124,-10},
jrodenburg 0:a28a1035c31b 163 {69820,-5},
jrodenburg 0:a28a1035c31b 164 {62621,0},
jrodenburg 0:a28a1035c31b 165 {55693,5},
jrodenburg 0:a28a1035c31b 166 {49169,10},
jrodenburg 0:a28a1035c31b 167 {43144,15},
jrodenburg 0:a28a1035c31b 168 {37669,20},
jrodenburg 0:a28a1035c31b 169 {32768,25},
jrodenburg 0:a28a1035c31b 170 {28429,30},
jrodenburg 0:a28a1035c31b 171 {24622,35},
jrodenburg 0:a28a1035c31b 172 {21309,40},
jrodenburg 0:a28a1035c31b 173 {18439,45},
jrodenburg 0:a28a1035c31b 174 {15962,50},
jrodenburg 0:a28a1035c31b 175 {13831,55},
jrodenburg 0:a28a1035c31b 176 {12002,60},
jrodenburg 0:a28a1035c31b 177 {10428,65},
jrodenburg 0:a28a1035c31b 178 {9080,70},
jrodenburg 0:a28a1035c31b 179 {7919,75},
jrodenburg 0:a28a1035c31b 180 {6923,80},
jrodenburg 0:a28a1035c31b 181 {6063,85},
jrodenburg 0:a28a1035c31b 182 {5323,90},
jrodenburg 0:a28a1035c31b 183 {4685,95},
jrodenburg 0:a28a1035c31b 184 {4130,100},
jrodenburg 0:a28a1035c31b 185 {3653,105},
jrodenburg 0:a28a1035c31b 186 {3234,110},
jrodenburg 0:a28a1035c31b 187 {2876,115},
jrodenburg 0:a28a1035c31b 188 {2563,120},
jrodenburg 0:a28a1035c31b 189 {2284,125}
jrodenburg 0:a28a1035c31b 190 };
jrodenburg 0:a28a1035c31b 191
jrodenburg 2:bd118a724f03 192 //SERIAL COMMUNICATION SETUP
jrodenburg 2:bd118a724f03 193 MODSERIAL pc(USBTX, USBRX);
jrodenburg 1:0182b86f9bd4 194
jrodenburg 0:a28a1035c31b 195 //DEFINE PINS
jrodenburg 0:a28a1035c31b 196 DigitalOut myled(LED2);
jrodenburg 0:a28a1035c31b 197
jrodenburg 0:a28a1035c31b 198 //I2C FOR MCP23008 (I/O Control)
jrodenburg 2:bd118a724f03 199 MCP23008 io_control(PTC9, PTC8, 0x10, 100000); //sda, scl
jrodenburg 0:a28a1035c31b 200
jrodenburg 0:a28a1035c31b 201 //I2C FOR LTC2487 (ADC Control)
jrodenburg 2:bd118a724f03 202 LTC2487 ltc2487(PTC11, PTC10, 0x23, 100000); //sda, scl
jrodenburg 0:a28a1035c31b 203
jrodenburg 0:a28a1035c31b 204 //GLOBAL VARIABLES
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 4:168a446bd0da 254 chnlStatus[chnl].setTemp = atoi(data.c_str());
jrodenburg 4:168a446bd0da 255 if(DEBUG) pc.printf("CHANNEL TEMP: %f \r\n", chnlStatus[chnl].setTemp);
jrodenburg 2:bd118a724f03 256 }
jrodenburg 2:bd118a724f03 257 else if(pCount == 3){ //get channel status
jrodenburg 4:168a446bd0da 258 chnlStatus[chnl].status = atoi(data.c_str());
jrodenburg 4:168a446bd0da 259 if(DEBUG) pc.printf("CHANNEL STATUS: %f \r\n", chnlStatus[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 5:0f38a0bd4f86 271 port: the I/O channel to read
jrodenburg 0:a28a1035c31b 272 Returns: the temperature of the fixture (front or back)
jrodenburg 0:a28a1035c31b 273 */
jrodenburg 0:a28a1035c31b 274
jrodenburg 5:0f38a0bd4f86 275 float get_temp(int chn, int port){
jrodenburg 1:0182b86f9bd4 276 myled = 1;
jrodenburg 0:a28a1035c31b 277 ltc2487.setAddress(addrLUT[chn].adc);
jrodenburg 0:a28a1035c31b 278
jrodenburg 5:0f38a0bd4f86 279 float ADC_val = ltc2487.readOutput(port); //(65536*1.334)/2.5
jrodenburg 0:a28a1035c31b 280
jrodenburg 0:a28a1035c31b 281 int i = 0;
jrodenburg 0:a28a1035c31b 282
jrodenburg 0:a28a1035c31b 283 while((i < sizeLUT) && (thermLUT[i].adc > ADC_val)){
jrodenburg 0:a28a1035c31b 284 i++;
jrodenburg 0:a28a1035c31b 285 } //find the temp. above therm temp
jrodenburg 0:a28a1035c31b 286
jrodenburg 0:a28a1035c31b 287 //Point slope formula extrapolation:
jrodenburg 0:a28a1035c31b 288 // y1 = m (x1-x0)+ y0 , y = temp. value, x = adc value
jrodenburg 0:a28a1035c31b 289 // y1 = thermLUT[i-1].temp y0 = thermLUT[i].temp
jrodenburg 0:a28a1035c31b 290 // x1 = thermLUT[i-1].adc x0 =thermLUT[i].adc
jrodenburg 0:a28a1035c31b 291 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 292 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 293
jrodenburg 0:a28a1035c31b 294 float m = a/b;
jrodenburg 0:a28a1035c31b 295 float y = (m*(ADC_val-thermLUT[i].adc))+thermLUT[i].temp;
jrodenburg 0:a28a1035c31b 296
jrodenburg 5:0f38a0bd4f86 297 if(DEBUG2) pc.printf("CHNL: %i PORT: %i ADC VAL: %f TEMP: %f \r\n", chn, port, ADC_val, y);
jrodenburg 1:0182b86f9bd4 298
jrodenburg 4:168a446bd0da 299 //if(chn == chnlSel)
jrodenburg 4:168a446bd0da 300 pc.printf("%f \r\n", y);
jrodenburg 4:168a446bd0da 301
jrodenburg 0:a28a1035c31b 302 return y;
jrodenburg 0:a28a1035c31b 303 }
jrodenburg 0:a28a1035c31b 304
jrodenburg 0:a28a1035c31b 305 /* Function: get_heater_current
jrodenburg 0:a28a1035c31b 306 **************************************************************
jrodenburg 0:a28a1035c31b 307 Description: Retrieve current into heater control MOSFET
jrodenburg 0:a28a1035c31b 308 Recieves: chn: the channel of the fixture to read current from
jrodenburg 0:a28a1035c31b 309 Returns: the current into the heater control MOSFET
jrodenburg 0:a28a1035c31b 310 */
jrodenburg 0:a28a1035c31b 311
jrodenburg 6:c980535393ed 312 float get_heater_current(int chn, int port){
jrodenburg 6:c980535393ed 313 return ltc2487.readOutput(port);
jrodenburg 0:a28a1035c31b 314 }
jrodenburg 0:a28a1035c31b 315
jrodenburg 0:a28a1035c31b 316 /* Function: get_valve_current
jrodenburg 0:a28a1035c31b 317 **************************************************************
jrodenburg 0:a28a1035c31b 318 Description: Retrieve current into valve control MOSFET
jrodenburg 0:a28a1035c31b 319 Recieves: chn: the channel of the fixture to read current from
jrodenburg 0:a28a1035c31b 320 Returns: the current into the valve control MOSFET
jrodenburg 0:a28a1035c31b 321 */
jrodenburg 0:a28a1035c31b 322
jrodenburg 6:c980535393ed 323 float get_valve_current(int chn, int port){
jrodenburg 6:c980535393ed 324 return ltc2487.readOutput(port);
jrodenburg 0:a28a1035c31b 325 }
jrodenburg 0:a28a1035c31b 326
jrodenburg 0:a28a1035c31b 327 /* Function: turn_valve_on
jrodenburg 0:a28a1035c31b 328 **************************************************************
jrodenburg 1:0182b86f9bd4 329 Description: Turn valve on and green status LED on
jrodenburg 0:a28a1035c31b 330 Recieves: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 331 Returns: N/A
jrodenburg 0:a28a1035c31b 332 */
jrodenburg 0:a28a1035c31b 333
jrodenburg 1:0182b86f9bd4 334 void turn_valve_on(int chn){
jrodenburg 0:a28a1035c31b 335 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 336 io_control.init();
jrodenburg 1:0182b86f9bd4 337 io_control.writeOutput(1,0,1,0);
jrodenburg 0:a28a1035c31b 338 }
jrodenburg 0:a28a1035c31b 339
jrodenburg 0:a28a1035c31b 340 /* Function: turn_valve_off
jrodenburg 0:a28a1035c31b 341 **************************************************************
jrodenburg 1:0182b86f9bd4 342 Description: Turn valve off and green status LED on
jrodenburg 0:a28a1035c31b 343 Recieves: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 344 Returns: N/A
jrodenburg 0:a28a1035c31b 345 */
jrodenburg 0:a28a1035c31b 346
jrodenburg 1:0182b86f9bd4 347 void turn_valve_off(int chn){
jrodenburg 0:a28a1035c31b 348 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 349 io_control.init();
jrodenburg 1:0182b86f9bd4 350 io_control.writeOutput(0,0,1,0);
jrodenburg 0:a28a1035c31b 351 }
jrodenburg 0:a28a1035c31b 352
jrodenburg 0:a28a1035c31b 353 /* Function: turn_heater_on
jrodenburg 0:a28a1035c31b 354 **************************************************************
jrodenburg 1:0182b86f9bd4 355 Description: Turn heater on and green status LED on
jrodenburg 0:a28a1035c31b 356 Recieves: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 357 Returns: N/A
jrodenburg 0:a28a1035c31b 358 */
jrodenburg 0:a28a1035c31b 359
jrodenburg 1:0182b86f9bd4 360 void turn_heater_on(int chn){
jrodenburg 0:a28a1035c31b 361 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 362 io_control.init();
jrodenburg 1:0182b86f9bd4 363 io_control.writeOutput(0,1,1,0);
jrodenburg 0:a28a1035c31b 364 }
jrodenburg 0:a28a1035c31b 365
jrodenburg 0:a28a1035c31b 366 /* Function: turn_heater_off
jrodenburg 0:a28a1035c31b 367 **************************************************************
jrodenburg 1:0182b86f9bd4 368 Description: Turn heater off and green status LED on
jrodenburg 0:a28a1035c31b 369 Recieves: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 370 Returns: N/A
jrodenburg 0:a28a1035c31b 371 */
jrodenburg 0:a28a1035c31b 372
jrodenburg 1:0182b86f9bd4 373 void turn_heater_off(int chn){
jrodenburg 0:a28a1035c31b 374 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 375 io_control.init();
jrodenburg 1:0182b86f9bd4 376 io_control.writeOutput(0,0,1,0);
jrodenburg 0:a28a1035c31b 377 }
jrodenburg 0:a28a1035c31b 378
jrodenburg 0:a28a1035c31b 379 /* Function: status_led
jrodenburg 0:a28a1035c31b 380 **************************************************************
jrodenburg 0:a28a1035c31b 381 Description: Turn status LED on (turns on green or red)
jrodenburg 0:a28a1035c31b 382 Recieves: chn: the channel of the fixture
jrodenburg 0:a28a1035c31b 383 status: the status of channel (good (1) or bad (0))
jrodenburg 1:0182b86f9bd4 384 Returns: N/A
jrodenburg 0:a28a1035c31b 385 */
jrodenburg 0:a28a1035c31b 386
jrodenburg 1:0182b86f9bd4 387 void status_led(int chn, int status){
jrodenburg 0:a28a1035c31b 388 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 389 if(status){
jrodenburg 1:0182b86f9bd4 390 io_control.writeOutput(0,0,1,0);
jrodenburg 0:a28a1035c31b 391 }
jrodenburg 0:a28a1035c31b 392 else{
jrodenburg 1:0182b86f9bd4 393 io_control.writeOutput(0,0,0,1);
jrodenburg 0:a28a1035c31b 394 }
jrodenburg 0:a28a1035c31b 395 }
jrodenburg 0:a28a1035c31b 396
jrodenburg 1:0182b86f9bd4 397 /* Function: test_mcp23008
jrodenburg 1:0182b86f9bd4 398 **************************************************************
jrodenburg 1:0182b86f9bd4 399 Description: Test each output of the MCP23009
jrodenburg 1:0182b86f9bd4 400 Recieves: N/A
jrodenburg 1:0182b86f9bd4 401 Returns: N/A
jrodenburg 1:0182b86f9bd4 402 */
jrodenburg 1:0182b86f9bd4 403
jrodenburg 1:0182b86f9bd4 404 void test_mcp23008(int chn){
jrodenburg 1:0182b86f9bd4 405 turn_valve_on(chn);
jrodenburg 1:0182b86f9bd4 406 wait(1);
jrodenburg 1:0182b86f9bd4 407 turn_valve_off(chn);
jrodenburg 1:0182b86f9bd4 408 wait(1);
jrodenburg 1:0182b86f9bd4 409 turn_heater_on(chn);
jrodenburg 1:0182b86f9bd4 410 wait(1);
jrodenburg 1:0182b86f9bd4 411 turn_heater_off(chn);
jrodenburg 1:0182b86f9bd4 412 wait(1);
jrodenburg 1:0182b86f9bd4 413 status_led(chn, 0);
jrodenburg 1:0182b86f9bd4 414 wait(1);
jrodenburg 1:0182b86f9bd4 415 status_led(chn, 1);
jrodenburg 1:0182b86f9bd4 416 }
jrodenburg 1:0182b86f9bd4 417
jrodenburg 2:bd118a724f03 418 /* Function: test_ltc2487
jrodenburg 2:bd118a724f03 419 **************************************************************
jrodenburg 2:bd118a724f03 420 Description: Test the reading from LTC2487
jrodenburg 2:bd118a724f03 421 Recieves: N/A
jrodenburg 2:bd118a724f03 422 Returns: N/A
jrodenburg 2:bd118a724f03 423 */
jrodenburg 2:bd118a724f03 424
jrodenburg 2:bd118a724f03 425 void test_ltc2487(int chn){
jrodenburg 5:0f38a0bd4f86 426 get_temp(chn, FRONT_THERM);
jrodenburg 5:0f38a0bd4f86 427 wait(0.1);
jrodenburg 6:c980535393ed 428 get_temp(chn, BACK_THERM);
jrodenburg 6:c980535393ed 429 wait(0.1);
jrodenburg 6:c980535393ed 430 get_temp(chn, VALV_FET_AMP);
jrodenburg 6:c980535393ed 431 wait(0.1);
jrodenburg 4:168a446bd0da 432 //if(DEBUG1) pc.printf("TEMPERATURE READING: %f \r\n", get_temp(chn));
jrodenburg 2:bd118a724f03 433 }
jrodenburg 2:bd118a724f03 434
jrodenburg 6:c980535393ed 435 /* Function: error_check
jrodenburg 6:c980535393ed 436 **************************************************************
jrodenburg 6:c980535393ed 437 Description: Checks for any system errors
jrodenburg 6:c980535393ed 438 Recieves: frontTemp:
jrodenburg 6:c980535393ed 439 backTemp:
jrodenburg 6:c980535393ed 440 valveFet:
jrodenburg 6:c980535393ed 441 heaterFet:
jrodenburg 6:c980535393ed 442 Returns: N/A
jrodenburg 6:c980535393ed 443 */
jrodenburg 6:c980535393ed 444
jrodenburg 6:c980535393ed 445 void error_check(int chn, float frontTemp, float backTemp, float valveFet, float heaterFet){
jrodenburg 6:c980535393ed 446 float setTemp = chnlStatus[chn].setTemp;
jrodenburg 6:c980535393ed 447
jrodenburg 6:c980535393ed 448 //CHECK IF THERMISTOR READINGS ARE OFF (< 2 DEGREES)
jrodenburg 6:c980535393ed 449 if(abs(frontTemp-backTemp) > 2){
jrodenburg 6:c980535393ed 450 //ERROR 6: Thermistor reading off
jrodenburg 6:c980535393ed 451 }
jrodenburg 6:c980535393ed 452
jrodenburg 6:c980535393ed 453 //CHECK IF HEATER'S STUCK ON OR CELL OVERHEATING
jrodenburg 6:c980535393ed 454 if((frontTemp >= (setTemp+TEMP_MARGIN)) || (backTemp >= (setTemp+TEMP_MARGIN))){
jrodenburg 6:c980535393ed 455 //ERROR 0
jrodenburg 6:c980535393ed 456 status_led(chn, STATUS_BAD);
jrodenburg 6:c980535393ed 457 if(heaterFet >= FET_ON_CURRENT){
jrodenburg 6:c980535393ed 458 //ERROR 1: Heater FET stuck on
jrodenburg 6:c980535393ed 459 }
jrodenburg 6:c980535393ed 460 if(valveFet <= FET_ON_CURRENT){
jrodenburg 6:c980535393ed 461 //ERROR 1: valve FET stuck off
jrodenburg 6:c980535393ed 462 }
jrodenburg 6:c980535393ed 463 }
jrodenburg 6:c980535393ed 464
jrodenburg 6:c980535393ed 465 //CHECK IF VALVE STUCK ON OR CELL OVERHEATING
jrodenburg 6:c980535393ed 466 if((frontTemp <= (setTemp-TEMP_MARGIN)) || (backTemp <= (setTemp-TEMP_MARGIN))){
jrodenburg 6:c980535393ed 467 //ERROR 0
jrodenburg 6:c980535393ed 468 status_led(chn, STATUS_BAD);
jrodenburg 6:c980535393ed 469 if(heaterFet <= FET_ON_CURRENT){
jrodenburg 6:c980535393ed 470 //ERROR 2: Heater FET stuck off
jrodenburg 6:c980535393ed 471 }
jrodenburg 6:c980535393ed 472 else if(valveFet >= FET_ON_CURRENT){
jrodenburg 6:c980535393ed 473 //ERROR 3: Chiller FET stuck on
jrodenburg 6:c980535393ed 474 }
jrodenburg 6:c980535393ed 475 }
jrodenburg 5:0f38a0bd4f86 476 }
jrodenburg 5:0f38a0bd4f86 477
jrodenburg 6:c980535393ed 478
jrodenburg 6:c980535393ed 479
jrodenburg 6:c980535393ed 480 /*************************************************************/
jrodenburg 6:c980535393ed 481 /* MAIN FUNCTION */
jrodenburg 6:c980535393ed 482 /*************************************************************/
jrodenburg 0:a28a1035c31b 483
jrodenburg 0:a28a1035c31b 484
jrodenburg 0:a28a1035c31b 485 int main() {
jrodenburg 0:a28a1035c31b 486
jrodenburg 5:0f38a0bd4f86 487 //pc.baud(9600);
jrodenburg 5:0f38a0bd4f86 488 //pc.autoDetectChar('E');
jrodenburg 5:0f38a0bd4f86 489 //pc.attach(&rxInterrupt, MODSERIAL::RxAutoDetect);
jrodenburg 4:168a446bd0da 490
jrodenburg 0:a28a1035c31b 491 myled = 1;
jrodenburg 5:0f38a0bd4f86 492
jrodenburg 5:0f38a0bd4f86 493
jrodenburg 4:168a446bd0da 494 //test_mcp23008(1);
jrodenburg 5:0f38a0bd4f86 495 //FRONT_THERM, BACK_THERM, HEAT_FET_AMP, VALV_FET_AMP
jrodenburg 5:0f38a0bd4f86 496 /*while(1){
jrodenburg 5:0f38a0bd4f86 497 myled = 0;
jrodenburg 5:0f38a0bd4f86 498 //test_all_ADC();
jrodenburg 5:0f38a0bd4f86 499 //test_ltc2487(0);
jrodenburg 5:0f38a0bd4f86 500 test_all_IO();
jrodenburg 5:0f38a0bd4f86 501 wait(1);
jrodenburg 5:0f38a0bd4f86 502 myled = 1;
jrodenburg 4:168a446bd0da 503 }*/
jrodenburg 0:a28a1035c31b 504
jrodenburg 5:0f38a0bd4f86 505
jrodenburg 0:a28a1035c31b 506 while(1) {
jrodenburg 6:c980535393ed 507 //test_all_IO();
jrodenburg 5:0f38a0bd4f86 508 if(DEBUG3) pc.printf("THE PROGRAM STARTED \r\n");
jrodenburg 2:bd118a724f03 509
jrodenburg 2:bd118a724f03 510 //check if we recieved data/need to update TCTF data
jrodenburg 2:bd118a724f03 511 if(dataRecieved){
jrodenburg 2:bd118a724f03 512 dataRecieved = false;
jrodenburg 2:bd118a724f03 513 pc.autoDetectChar('\n');
jrodenburg 2:bd118a724f03 514 pc.move(rxBuf, 50);
jrodenburg 2:bd118a724f03 515 pc.rxBufferFlush();
jrodenburg 2:bd118a724f03 516 parseRXData();
jrodenburg 2:bd118a724f03 517 }
jrodenburg 2:bd118a724f03 518
jrodenburg 5:0f38a0bd4f86 519 for(int chnl = 0; chnl < CHN_COUNT; chnl++){
jrodenburg 5:0f38a0bd4f86 520 //wait(2.5);
jrodenburg 5:0f38a0bd4f86 521 float currentTempFront = get_temp(chnl, FRONT_THERM);
jrodenburg 6:c980535393ed 522 float currentTempBack = get_temp(chnl, BACK_THERM);
jrodenburg 6:c980535393ed 523 float currentTemp = (currentTempFront + currentTempBack)/2;
jrodenburg 2:bd118a724f03 524
jrodenburg 2:bd118a724f03 525 //CONTROL LOOP:
jrodenburg 4:168a446bd0da 526 if(chnlStatus[chnl].status == 1){
jrodenburg 5:0f38a0bd4f86 527 if(DEBUG3) pc.printf("TEMP: %f \r\n", currentTempFront);
jrodenburg 6:c980535393ed 528 if(currentTemp > ((chnlStatus[chnl].setTemp)+HYST)){
jrodenburg 5:0f38a0bd4f86 529 if(DEBUG3) pc.printf("CHILLER ON \r\n");
jrodenburg 2:bd118a724f03 530 //Turn chiller on
jrodenburg 2:bd118a724f03 531 turn_valve_on(chnl);
jrodenburg 2:bd118a724f03 532 }
jrodenburg 6:c980535393ed 533 else if (currentTemp < ((chnlStatus[chnl].setTemp)-HYST)){
jrodenburg 5:0f38a0bd4f86 534 if(DEBUG3) pc.printf("HEATER ON \r\n");
jrodenburg 2:bd118a724f03 535 //Turn heater on
jrodenburg 2:bd118a724f03 536 turn_heater_on(chnl);
jrodenburg 2:bd118a724f03 537 }
jrodenburg 2:bd118a724f03 538 else{
jrodenburg 5:0f38a0bd4f86 539 if(DEBUG3) pc.printf("ALL OFF \r\n");
jrodenburg 2:bd118a724f03 540 //turn off chiller
jrodenburg 2:bd118a724f03 541 turn_valve_off(chnl);
jrodenburg 2:bd118a724f03 542 //turn off heater
jrodenburg 2:bd118a724f03 543 turn_heater_off(chnl);
jrodenburg 2:bd118a724f03 544 //turn on green LED status light
jrodenburg 2:bd118a724f03 545 status_led(chnl, 1);
jrodenburg 2:bd118a724f03 546 }
jrodenburg 2:bd118a724f03 547 }
jrodenburg 2:bd118a724f03 548 }
jrodenburg 0:a28a1035c31b 549 }
jrodenburg 0:a28a1035c31b 550 }