Control Code with I/O and ADC working

Dependencies:   MODSERIAL mbed

Committer:
jrodenburg
Date:
Tue Mar 20 19:22:55 2018 +0000
Revision:
7:8a5e65e63e2a
Parent:
6:c980535393ed
Child:
8:dbf8bd4815f8
Code before integrating Huyen's code

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