Control Code with I/O and ADC working

Dependencies:   MODSERIAL mbed

Committer:
jrodenburg
Date:
Tue May 08 17:08:37 2018 +0000
Revision:
13:604e6933366f
Parent:
12:1cada1fe4743
Child:
14:69cd53434783
Working code with revised cooling (if within 5 degrees will cool until near set point), has run on a row and is functional

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 8:dbf8bd4815f8 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 8:dbf8bd4815f8 11 #define DEBUG 0
jrodenburg 12:1cada1fe4743 12 #define DEBUG1 0
jrodenburg 12:1cada1fe4743 13 #define DEBUG2 0
jrodenburg 8:dbf8bd4815f8 14 #define DEBUG3 1
jrodenburg 8:dbf8bd4815f8 15 #define CHN_COUNT 8
jrodenburg 12:1cada1fe4743 16 #define MIN_TEMP 10
jrodenburg 12:1cada1fe4743 17 #define MAX_TEMP 65
jrodenburg 12:1cada1fe4743 18 #define TEMP_MARGIN 20
jrodenburg 8:dbf8bd4815f8 19 #define HYST_LOW 0.3
jrodenburg 13:604e6933366f 20 #define HYST_HIGH 1
jrodenburg 8:dbf8bd4815f8 21 #define SAMPLES 5
jrodenburg 8:dbf8bd4815f8 22 #define I2C_Freq 2000
jrodenburg 8:dbf8bd4815f8 23 #define VALVE 1
jrodenburg 8:dbf8bd4815f8 24 #define HEATER 2
jrodenburg 8:dbf8bd4815f8 25 #define STATUS_GOOD 3
jrodenburg 12:1cada1fe4743 26 #define STATUS_BAD 0
jrodenburg 8:dbf8bd4815f8 27 #define sizeLUT 34
jrodenburg 12:1cada1fe4743 28 #define BACK_THERM 0
jrodenburg 12:1cada1fe4743 29 #define FRONT_THERM 1
jrodenburg 8:dbf8bd4815f8 30 #define HEAT_FET_AMP 2
jrodenburg 7:8a5e65e63e2a 31 #define VALVE_FET_AMP 3
jrodenburg 8:dbf8bd4815f8 32 #define FET_ON_CURRENT 1.12
jrodenburg 12:1cada1fe4743 33 #define ROOM_TEMP 22
jrodenburg 12:1cada1fe4743 34
jrodenburg 8:dbf8bd4815f8 35 // Defines for use with Serial communication
jrodenburg 8:dbf8bd4815f8 36 #pragma pack (1)
jrodenburg 8:dbf8bd4815f8 37 #define RX_SOF 0x7B
jrodenburg 8:dbf8bd4815f8 38 #define RX_EOF 0x7D
jrodenburg 8:dbf8bd4815f8 39 #define TX_SOF 0x7B
jrodenburg 8:dbf8bd4815f8 40 #define TX_EOF 0x7D
jrodenburg 8:dbf8bd4815f8 41 #define DELIMETER 0x2E
jrodenburg 8:dbf8bd4815f8 42 #define SET_TEMPERATURE 0xB0
jrodenburg 8:dbf8bd4815f8 43 #define SELECT_CHANNEL 0xB1
jrodenburg 8:dbf8bd4815f8 44 #define CMD_RESPONSE 0xD0
jrodenburg 8:dbf8bd4815f8 45 #define CMD_DATA 0xD1
jrodenburg 12:1cada1fe4743 46 #define ERROR_DATA 0xD2
jrodenburg 12:1cada1fe4743 47
jrodenburg 0:a28a1035c31b 48
jrodenburg 8:dbf8bd4815f8 49 unsigned int chanSel_SendTemp = 0;
jrodenburg 8:dbf8bd4815f8 50 unsigned int chanSel_SetTemp = 0;
jrodenburg 8:dbf8bd4815f8 51 // Default to chan 0
jrodenburg 8:dbf8bd4815f8 52 int currChan = 0;
jrodenburg 8:dbf8bd4815f8 53 bool newTempSet = false;
jrodenburg 8:dbf8bd4815f8 54
jrodenburg 12:1cada1fe4743 55
jrodenburg 8:dbf8bd4815f8 56 //***********************************************
jrodenburg 8:dbf8bd4815f8 57 // Serial Rx Packet Format
jrodenburg 8:dbf8bd4815f8 58 //***********************************************
jrodenburg 8:dbf8bd4815f8 59 typedef struct {
jrodenburg 8:dbf8bd4815f8 60 unsigned char SOF_flag;
jrodenburg 8:dbf8bd4815f8 61 unsigned char cmd_type;
jrodenburg 8:dbf8bd4815f8 62 unsigned char len;
jrodenburg 8:dbf8bd4815f8 63 unsigned char Delim;
jrodenburg 8:dbf8bd4815f8 64 } HOST_CMD_HEADER;
jrodenburg 8:dbf8bd4815f8 65
jrodenburg 8:dbf8bd4815f8 66 typedef struct {
jrodenburg 8:dbf8bd4815f8 67 HOST_CMD_HEADER cmd_header;
jrodenburg 8:dbf8bd4815f8 68 unsigned char chanID;
jrodenburg 8:dbf8bd4815f8 69 unsigned char tempDelim;
jrodenburg 8:dbf8bd4815f8 70 float setTemp;
jrodenburg 8:dbf8bd4815f8 71 unsigned char chanStatDelim;
jrodenburg 8:dbf8bd4815f8 72 unsigned char chanStat;
jrodenburg 8:dbf8bd4815f8 73 } SET_TEMPERATURE_CMD;
jrodenburg 8:dbf8bd4815f8 74
jrodenburg 8:dbf8bd4815f8 75 typedef struct {
jrodenburg 8:dbf8bd4815f8 76 HOST_CMD_HEADER cmd_header;
jrodenburg 8:dbf8bd4815f8 77 unsigned char chanIDSel;
jrodenburg 8:dbf8bd4815f8 78 unsigned char chanDelim;
jrodenburg 8:dbf8bd4815f8 79 unsigned char chanStat;
jrodenburg 8:dbf8bd4815f8 80 } SELECT_CHANNEL_CMD;
jrodenburg 8:dbf8bd4815f8 81
jrodenburg 8:dbf8bd4815f8 82 typedef struct {
jrodenburg 8:dbf8bd4815f8 83 unsigned char SOF_flag;
jrodenburg 8:dbf8bd4815f8 84 unsigned char cmd_type;
jrodenburg 8:dbf8bd4815f8 85 unsigned char len;
jrodenburg 8:dbf8bd4815f8 86 unsigned char Delim;
jrodenburg 8:dbf8bd4815f8 87 float data;
jrodenburg 8:dbf8bd4815f8 88 unsigned char EOF_flag;
jrodenburg 8:dbf8bd4815f8 89 } RESPONSE_CMD;
jrodenburg 2:bd118a724f03 90
jrodenburg 2:bd118a724f03 91 //TCTF CHANNEL DATA
jrodenburg 2:bd118a724f03 92 struct CHNL_DATA{
jrodenburg 2:bd118a724f03 93 bool status;
jrodenburg 2:bd118a724f03 94 float setTemp;
jrodenburg 7:8a5e65e63e2a 95 bool error;
jrodenburg 2:bd118a724f03 96 };
jrodenburg 2:bd118a724f03 97
jrodenburg 4:168a446bd0da 98 CHNL_DATA chnlStatus[] = {
jrodenburg 8:dbf8bd4815f8 99 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 100 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 101 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 102 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 103 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 104 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 105 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 106 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 107 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 108 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 109 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 110 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 111 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 112 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 113 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 114 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 115 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 116 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 117 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 118 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 119 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 120 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 121 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 122 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 123 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 124 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 125 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 126 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 127 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 128 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 129 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 130 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 131 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 132 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 133 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 134 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 135 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 136 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 137 {0, NULL, 0},
jrodenburg 7:8a5e65e63e2a 138 {0, NULL, 0},
jrodenburg 12:1cada1fe4743 139 {0, NULL, 0},
jrodenburg 12:1cada1fe4743 140 {0, NULL, 0},
jrodenburg 12:1cada1fe4743 141 {0, NULL, 0},
jrodenburg 12:1cada1fe4743 142 {0, NULL, 0},
jrodenburg 12:1cada1fe4743 143 {0, NULL, 0},
jrodenburg 12:1cada1fe4743 144 {0, NULL, 0},
jrodenburg 12:1cada1fe4743 145 {0, NULL, 0},
jrodenburg 12:1cada1fe4743 146 {0, NULL, 0},
jrodenburg 2:bd118a724f03 147 };
jrodenburg 2:bd118a724f03 148
jrodenburg 2:bd118a724f03 149
jrodenburg 0:a28a1035c31b 150 //I2C AADRESS LOOK UP TABLE, CREATED IN EXCEL SHEET (COUNT, TEMP)
jrodenburg 2:bd118a724f03 151 struct I2C_ADDR_LUT{
jrodenburg 0:a28a1035c31b 152 int adc;
jrodenburg 0:a28a1035c31b 153 int io;
jrodenburg 0:a28a1035c31b 154 };
jrodenburg 0:a28a1035c31b 155
jrodenburg 0:a28a1035c31b 156 I2C_ADDR_LUT addrLUT[] = {
jrodenburg 0:a28a1035c31b 157 {0x23, 0x10},
jrodenburg 0:a28a1035c31b 158 {0x53, 0x60},
jrodenburg 0:a28a1035c31b 159 {0x43, 0x70},
jrodenburg 0:a28a1035c31b 160 {0x73, 0x40},
jrodenburg 0:a28a1035c31b 161 {0x63, 0x50},
jrodenburg 0:a28a1035c31b 162 {0x22, 0x11},
jrodenburg 0:a28a1035c31b 163 {0x52, 0x61},
jrodenburg 0:a28a1035c31b 164 {0x42, 0x71},
jrodenburg 0:a28a1035c31b 165 {0x72, 0x41},
jrodenburg 0:a28a1035c31b 166 {0x62, 0x51},
jrodenburg 0:a28a1035c31b 167 {0x21, 0x12},
jrodenburg 0:a28a1035c31b 168 {0x51, 0x62},
jrodenburg 0:a28a1035c31b 169 {0x41, 0x72},
jrodenburg 0:a28a1035c31b 170 {0x71, 0x42},
jrodenburg 0:a28a1035c31b 171 {0x61, 0x52},
jrodenburg 0:a28a1035c31b 172 {0x20, 0x13},
jrodenburg 0:a28a1035c31b 173 {0x50, 0x63},
jrodenburg 0:a28a1035c31b 174 {0x40, 0x73},
jrodenburg 0:a28a1035c31b 175 {0x70, 0x43},
jrodenburg 0:a28a1035c31b 176 {0x60, 0x53},
jrodenburg 0:a28a1035c31b 177 {0x26, 0x14},
jrodenburg 0:a28a1035c31b 178 {0x56, 0x64},
jrodenburg 0:a28a1035c31b 179 {0x46, 0x74},
jrodenburg 0:a28a1035c31b 180 {0x76, 0x44},
jrodenburg 0:a28a1035c31b 181 {0x66, 0x54},
jrodenburg 0:a28a1035c31b 182 {0x2C, 0x1F},
jrodenburg 0:a28a1035c31b 183 {0x5C, 0x6F},
jrodenburg 0:a28a1035c31b 184 {0x4C, 0x7F},
jrodenburg 0:a28a1035c31b 185 {0x7C, 0x4F},
jrodenburg 0:a28a1035c31b 186 {0x6C, 0x5F},
jrodenburg 0:a28a1035c31b 187 {0x2D, 0x1E},
jrodenburg 0:a28a1035c31b 188 {0x5D, 0x6E},
jrodenburg 0:a28a1035c31b 189 {0x4D, 0x7E},
jrodenburg 0:a28a1035c31b 190 {0x7D, 0x4E},
jrodenburg 0:a28a1035c31b 191 {0x6D, 0x5E},
jrodenburg 0:a28a1035c31b 192 {0x2E, 0x1D},
jrodenburg 0:a28a1035c31b 193 {0x5E, 0x6D},
jrodenburg 0:a28a1035c31b 194 {0x4E, 0x7D},
jrodenburg 0:a28a1035c31b 195 {0x7E, 0x4D},
jrodenburg 0:a28a1035c31b 196 {0x6E, 0x5D},
jrodenburg 0:a28a1035c31b 197 {0x2F, 0x1C},
jrodenburg 0:a28a1035c31b 198 {0x5F, 0x6C},
jrodenburg 0:a28a1035c31b 199 {0x4F, 0x7C},
jrodenburg 0:a28a1035c31b 200 {0x7F, 0x4C},
jrodenburg 0:a28a1035c31b 201 {0x6F, 0x5C},
jrodenburg 0:a28a1035c31b 202 {0x20, 0x1B},
jrodenburg 0:a28a1035c31b 203 {0x50, 0x6B},
jrodenburg 12:1cada1fe4743 204 {0x40, 0x7B},
jrodenburg 0:a28a1035c31b 205 };
jrodenburg 0:a28a1035c31b 206
jrodenburg 0:a28a1035c31b 207 //THERMISTOR LOOK UP TABLE, CREATED IN EXCEL SHEET (COUNT, TEMP)
jrodenburg 2:bd118a724f03 208 struct THERM_LUT{
jrodenburg 0:a28a1035c31b 209 int adc;
jrodenburg 0:a28a1035c31b 210 int temp;
jrodenburg 0:a28a1035c31b 211 };
jrodenburg 0:a28a1035c31b 212
jrodenburg 0:a28a1035c31b 213 THERM_LUT thermLUT[] = {
jrodenburg 0:a28a1035c31b 214 {113779,-40},
jrodenburg 0:a28a1035c31b 215 {109152,-35},
jrodenburg 0:a28a1035c31b 216 {103830,-30},
jrodenburg 0:a28a1035c31b 217 {97855,-25},
jrodenburg 0:a28a1035c31b 218 {91319,-20},
jrodenburg 0:a28a1035c31b 219 {84352,-15},
jrodenburg 0:a28a1035c31b 220 {77124,-10},
jrodenburg 0:a28a1035c31b 221 {69820,-5},
jrodenburg 0:a28a1035c31b 222 {62621,0},
jrodenburg 0:a28a1035c31b 223 {55693,5},
jrodenburg 0:a28a1035c31b 224 {49169,10},
jrodenburg 0:a28a1035c31b 225 {43144,15},
jrodenburg 0:a28a1035c31b 226 {37669,20},
jrodenburg 0:a28a1035c31b 227 {32768,25},
jrodenburg 0:a28a1035c31b 228 {28429,30},
jrodenburg 0:a28a1035c31b 229 {24622,35},
jrodenburg 0:a28a1035c31b 230 {21309,40},
jrodenburg 0:a28a1035c31b 231 {18439,45},
jrodenburg 0:a28a1035c31b 232 {15962,50},
jrodenburg 0:a28a1035c31b 233 {13831,55},
jrodenburg 0:a28a1035c31b 234 {12002,60},
jrodenburg 0:a28a1035c31b 235 {10428,65},
jrodenburg 0:a28a1035c31b 236 {9080,70},
jrodenburg 0:a28a1035c31b 237 {7919,75},
jrodenburg 0:a28a1035c31b 238 {6923,80},
jrodenburg 0:a28a1035c31b 239 {6063,85},
jrodenburg 0:a28a1035c31b 240 {5323,90},
jrodenburg 0:a28a1035c31b 241 {4685,95},
jrodenburg 0:a28a1035c31b 242 {4130,100},
jrodenburg 0:a28a1035c31b 243 {3653,105},
jrodenburg 0:a28a1035c31b 244 {3234,110},
jrodenburg 0:a28a1035c31b 245 {2876,115},
jrodenburg 0:a28a1035c31b 246 {2563,120},
jrodenburg 0:a28a1035c31b 247 {2284,125}
jrodenburg 0:a28a1035c31b 248 };
jrodenburg 0:a28a1035c31b 249
jrodenburg 2:bd118a724f03 250 //SERIAL COMMUNICATION SETUP
jrodenburg 2:bd118a724f03 251 MODSERIAL pc(USBTX, USBRX);
jrodenburg 1:0182b86f9bd4 252
jrodenburg 0:a28a1035c31b 253 //DEFINE PINS
jrodenburg 0:a28a1035c31b 254 DigitalOut myled(LED2);
jrodenburg 0:a28a1035c31b 255
jrodenburg 0:a28a1035c31b 256 //I2C FOR MCP23008 (I/O Control)
jrodenburg 12:1cada1fe4743 257 MCP23008 io_control(PTC9, PTC8, 0x10, 100000); //sda, scl
jrodenburg 0:a28a1035c31b 258
jrodenburg 0:a28a1035c31b 259 //I2C FOR LTC2487 (ADC Control)
jrodenburg 12:1cada1fe4743 260 LTC2487 ltc2487(PTC11, PTC10, 0x23, 100000); //sda, scl
jrodenburg 0:a28a1035c31b 261
jrodenburg 0:a28a1035c31b 262 //GLOBAL VARIABLES
jrodenburg 12:1cada1fe4743 263 volatile bool dataReceived = false; //used to check if data has been received
jrodenburg 8:dbf8bd4815f8 264 volatile bool dataTxReady = false;
jrodenburg 2:bd118a724f03 265 char rxBuf[50];
jrodenburg 2:bd118a724f03 266 int chnlSel;
jrodenburg 0:a28a1035c31b 267
jrodenburg 7:8a5e65e63e2a 268
jrodenburg 7:8a5e65e63e2a 269 /* Function: turnOffChannel
jrodenburg 7:8a5e65e63e2a 270 **************************************************************
jrodenburg 7:8a5e65e63e2a 271 Description: Turns off a channel
jrodenburg 7:8a5e65e63e2a 272 Recieves: chnl: channel to turn off
jrodenburg 7:8a5e65e63e2a 273 Returns: N/A
jrodenburg 7:8a5e65e63e2a 274 */
jrodenburg 7:8a5e65e63e2a 275
jrodenburg 7:8a5e65e63e2a 276 void turnOffChannel(int chnl){
jrodenburg 7:8a5e65e63e2a 277 io_control.setAddress(addrLUT[chnl].io);
jrodenburg 7:8a5e65e63e2a 278 io_control.init();
jrodenburg 7:8a5e65e63e2a 279 io_control.writeOutput(0,0,0,0);
jrodenburg 7:8a5e65e63e2a 280 }
jrodenburg 7:8a5e65e63e2a 281
jrodenburg 8:dbf8bd4815f8 282
jrodenburg 2:bd118a724f03 283 /* Function: rxInterrupt
jrodenburg 1:0182b86f9bd4 284 **************************************************************
jrodenburg 8:dbf8bd4815f8 285 Description: serial rx interupt handler
jrodenburg 8:dbf8bd4815f8 286 Receives: N/A
jrodenburg 8:dbf8bd4815f8 287 Returns: N/A
jrodenburg 2:bd118a724f03 288 */
jrodenburg 2:bd118a724f03 289
jrodenburg 8:dbf8bd4815f8 290 DigitalOut rLed(LED1);
jrodenburg 8:dbf8bd4815f8 291 DigitalOut gLed(LED2);
jrodenburg 8:dbf8bd4815f8 292
jrodenburg 8:dbf8bd4815f8 293 //***********************************************
jrodenburg 8:dbf8bd4815f8 294 // Rx Interrupt from serial Host interface
jrodenburg 8:dbf8bd4815f8 295 //***********************************************
jrodenburg 8:dbf8bd4815f8 296 void rxInterrupt(MODSERIAL_IRQ_INFO *info)
jrodenburg 8:dbf8bd4815f8 297 {
jrodenburg 8:dbf8bd4815f8 298 gLed = 0;
jrodenburg 8:dbf8bd4815f8 299 dataReceived = true;
jrodenburg 8:dbf8bd4815f8 300 gLed = 1;
jrodenburg 2:bd118a724f03 301 }
jrodenburg 2:bd118a724f03 302
jrodenburg 8:dbf8bd4815f8 303
jrodenburg 8:dbf8bd4815f8 304 //***************************************************************
jrodenburg 8:dbf8bd4815f8 305 // Tx Interrupt from serial Host interface
jrodenburg 8:dbf8bd4815f8 306 //***************************************************************
jrodenburg 8:dbf8bd4815f8 307 void txInterrupt(MODSERIAL_IRQ_INFO *info)
jrodenburg 8:dbf8bd4815f8 308 {
jrodenburg 8:dbf8bd4815f8 309 dataTxReady = true;
jrodenburg 8:dbf8bd4815f8 310 }
jrodenburg 8:dbf8bd4815f8 311
jrodenburg 8:dbf8bd4815f8 312
jrodenburg 2:bd118a724f03 313 /* Function: parseRXData
jrodenburg 2:bd118a724f03 314 **************************************************************
jrodenburg 12:1cada1fe4743 315 Description: The parse received data into
jrodenburg 12:1cada1fe4743 316 Receives: chn: The channel we want to put into the channel
jrodenburg 12:1cada1fe4743 317 Returns: N/A
jrodenburg 1:0182b86f9bd4 318 */
jrodenburg 0:a28a1035c31b 319
jrodenburg 8:dbf8bd4815f8 320 void parseRXData()
jrodenburg 8:dbf8bd4815f8 321 {
jrodenburg 8:dbf8bd4815f8 322 HOST_CMD_HEADER *pRxPktHdr;
jrodenburg 2:bd118a724f03 323 string data = "";
jrodenburg 8:dbf8bd4815f8 324 unsigned char *ptemp;
jrodenburg 8:dbf8bd4815f8 325 int i;
jrodenburg 8:dbf8bd4815f8 326
jrodenburg 8:dbf8bd4815f8 327 pRxPktHdr = (HOST_CMD_HEADER *)rxBuf;
jrodenburg 8:dbf8bd4815f8 328
jrodenburg 8:dbf8bd4815f8 329 #ifdef DEBUG
jrodenburg 8:dbf8bd4815f8 330 pc.printf("DBG: fl = %02x\n", pRxPktHdr->SOF_flag);
jrodenburg 8:dbf8bd4815f8 331 pc.printf("DBG: len = %02x\n", pRxPktHdr->len);
jrodenburg 8:dbf8bd4815f8 332 #endif
jrodenburg 8:dbf8bd4815f8 333
jrodenburg 8:dbf8bd4815f8 334 // Exit if the packet does not contain correct header
jrodenburg 8:dbf8bd4815f8 335 // Maybe send NAK?
jrodenburg 8:dbf8bd4815f8 336 if ((pRxPktHdr->SOF_flag != RX_SOF) || (pRxPktHdr->Delim != DELIMETER))
jrodenburg 8:dbf8bd4815f8 337 return;
jrodenburg 2:bd118a724f03 338
jrodenburg 8:dbf8bd4815f8 339 switch (pRxPktHdr->cmd_type)
jrodenburg 8:dbf8bd4815f8 340 {
jrodenburg 8:dbf8bd4815f8 341 case SET_TEMPERATURE:
jrodenburg 8:dbf8bd4815f8 342 // Process set temp for specified channel
jrodenburg 8:dbf8bd4815f8 343 {
jrodenburg 8:dbf8bd4815f8 344 SET_TEMPERATURE_CMD *pRxPkt = (SET_TEMPERATURE_CMD *)(rxBuf);
jrodenburg 8:dbf8bd4815f8 345 #if 1 //def DEBUG
jrodenburg 8:dbf8bd4815f8 346 pc.printf("DBG: ch = %02x\n", pRxPkt->chanID);
jrodenburg 8:dbf8bd4815f8 347 pc.printf("DBG: tempSet = %f\n", pRxPkt->setTemp);
jrodenburg 8:dbf8bd4815f8 348 pc.printf("DBG: chanStat = %02x\n", pRxPkt->chanStat);
jrodenburg 8:dbf8bd4815f8 349 #endif
jrodenburg 8:dbf8bd4815f8 350 if ((pRxPkt->tempDelim != DELIMETER) || (pRxPkt->chanStatDelim != DELIMETER)) {
jrodenburg 8:dbf8bd4815f8 351 // Send NAK back
jrodenburg 8:dbf8bd4815f8 352 pc.printf("DBG: Error\n");
jrodenburg 2:bd118a724f03 353 }
jrodenburg 8:dbf8bd4815f8 354 else {
jrodenburg 8:dbf8bd4815f8 355 chanSel_SetTemp = pRxPkt->chanID;
jrodenburg 8:dbf8bd4815f8 356 chnlStatus[pRxPkt->chanID].status = pRxPkt->chanStat;
jrodenburg 8:dbf8bd4815f8 357 chnlStatus[pRxPkt->chanID].setTemp = pRxPkt->setTemp;
jrodenburg 12:1cada1fe4743 358 chnlStatus[pRxPkt->chanID].error = 0;
jrodenburg 8:dbf8bd4815f8 359 newTempSet = true;
jrodenburg 2:bd118a724f03 360 }
jrodenburg 2:bd118a724f03 361 }
jrodenburg 8:dbf8bd4815f8 362 break;
jrodenburg 8:dbf8bd4815f8 363
jrodenburg 8:dbf8bd4815f8 364 case SELECT_CHANNEL:
jrodenburg 8:dbf8bd4815f8 365 // Select channel to send temp data to
jrodenburg 8:dbf8bd4815f8 366 {
jrodenburg 8:dbf8bd4815f8 367 SELECT_CHANNEL_CMD *pRxPkt = (SELECT_CHANNEL_CMD *)(rxBuf);
jrodenburg 8:dbf8bd4815f8 368
jrodenburg 8:dbf8bd4815f8 369 chanSel_SendTemp = pRxPkt->chanIDSel;
jrodenburg 8:dbf8bd4815f8 370 #if 1 //def DEBUG
jrodenburg 8:dbf8bd4815f8 371 pc.printf("DBG: chanSel = %02x\n", pRxPkt->chanIDSel);
jrodenburg 8:dbf8bd4815f8 372 pc.printf("DBG: chanStat = %02x\n", pRxPkt->chanStat);
jrodenburg 8:dbf8bd4815f8 373 #endif
jrodenburg 2:bd118a724f03 374 }
jrodenburg 8:dbf8bd4815f8 375 break;
jrodenburg 8:dbf8bd4815f8 376
jrodenburg 8:dbf8bd4815f8 377 default:
jrodenburg 8:dbf8bd4815f8 378 // Error
jrodenburg 8:dbf8bd4815f8 379 break;
jrodenburg 2:bd118a724f03 380 }
jrodenburg 1:0182b86f9bd4 381 }
jrodenburg 0:a28a1035c31b 382
jrodenburg 0:a28a1035c31b 383 /* Function: get_temp
jrodenburg 0:a28a1035c31b 384 **************************************************************
jrodenburg 0:a28a1035c31b 385 Description: Retrieve data from thermistor
jrodenburg 12:1cada1fe4743 386 Receives: chn: the channel of the fixture to read temp. from
jrodenburg 5:0f38a0bd4f86 387 port: the I/O channel to read
jrodenburg 0:a28a1035c31b 388 Returns: the temperature of the fixture (front or back)
jrodenburg 0:a28a1035c31b 389 */
jrodenburg 0:a28a1035c31b 390
jrodenburg 5:0f38a0bd4f86 391 float get_temp(int chn, int port){
jrodenburg 1:0182b86f9bd4 392 myled = 1;
jrodenburg 0:a28a1035c31b 393 ltc2487.setAddress(addrLUT[chn].adc);
jrodenburg 12:1cada1fe4743 394
jrodenburg 5:0f38a0bd4f86 395 float ADC_val = ltc2487.readOutput(port); //(65536*1.334)/2.5
jrodenburg 12:1cada1fe4743 396
jrodenburg 0:a28a1035c31b 397 int i = 0;
jrodenburg 12:1cada1fe4743 398
jrodenburg 0:a28a1035c31b 399 while((i < sizeLUT) && (thermLUT[i].adc > ADC_val)){
jrodenburg 12:1cada1fe4743 400 i++;
jrodenburg 0:a28a1035c31b 401 } //find the temp. above therm temp
jrodenburg 12:1cada1fe4743 402
jrodenburg 12:1cada1fe4743 403 //Point slope formula extrapolation:
jrodenburg 0:a28a1035c31b 404 // y1 = m (x1-x0)+ y0 , y = temp. value, x = adc value
jrodenburg 0:a28a1035c31b 405 // y1 = thermLUT[i-1].temp y0 = thermLUT[i].temp
jrodenburg 0:a28a1035c31b 406 // x1 = thermLUT[i-1].adc x0 =thermLUT[i].adc
jrodenburg 0:a28a1035c31b 407 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 408 float b = float(thermLUT[i-1].adc - thermLUT[i].adc); //slope of adc between points where therm adc is between (Amax - Amin)
jrodenburg 12:1cada1fe4743 409
jrodenburg 0:a28a1035c31b 410 float m = a/b;
jrodenburg 0:a28a1035c31b 411 float y = (m*(ADC_val-thermLUT[i].adc))+thermLUT[i].temp;
jrodenburg 12:1cada1fe4743 412
jrodenburg 12:1cada1fe4743 413 if(DEBUG2) pc.printf("DBG: CHAN: %i PORT: %i ADC VAL: %f TEMP: %f \r\n", chn, port, ADC_val, y);
jrodenburg 12:1cada1fe4743 414
jrodenburg 12:1cada1fe4743 415 return y;
jrodenburg 0:a28a1035c31b 416 }
jrodenburg 0:a28a1035c31b 417
jrodenburg 0:a28a1035c31b 418 /* Function: get_heater_current
jrodenburg 0:a28a1035c31b 419 **************************************************************
jrodenburg 0:a28a1035c31b 420 Description: Retrieve current into heater control MOSFET
jrodenburg 12:1cada1fe4743 421 Receives: chn: the channel of the fixture to read current from
jrodenburg 0:a28a1035c31b 422 Returns: the current into the heater control MOSFET
jrodenburg 0:a28a1035c31b 423 */
jrodenburg 0:a28a1035c31b 424
jrodenburg 6:c980535393ed 425 float get_heater_current(int chn, int port){
jrodenburg 6:c980535393ed 426 return ltc2487.readOutput(port);
jrodenburg 0:a28a1035c31b 427 }
jrodenburg 0:a28a1035c31b 428
jrodenburg 0:a28a1035c31b 429 /* Function: get_valve_current
jrodenburg 0:a28a1035c31b 430 **************************************************************
jrodenburg 0:a28a1035c31b 431 Description: Retrieve current into valve control MOSFET
jrodenburg 12:1cada1fe4743 432 Receives: chn: the channel of the fixture to read current from
jrodenburg 0:a28a1035c31b 433 Returns: the current into the valve control MOSFET
jrodenburg 0:a28a1035c31b 434 */
jrodenburg 0:a28a1035c31b 435
jrodenburg 6:c980535393ed 436 float get_valve_current(int chn, int port){
jrodenburg 6:c980535393ed 437 return ltc2487.readOutput(port);
jrodenburg 0:a28a1035c31b 438 }
jrodenburg 0:a28a1035c31b 439
jrodenburg 0:a28a1035c31b 440 /* Function: turn_valve_on
jrodenburg 0:a28a1035c31b 441 **************************************************************
jrodenburg 1:0182b86f9bd4 442 Description: Turn valve on and green status LED on
jrodenburg 12:1cada1fe4743 443 Receives: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 444 Returns: N/A
jrodenburg 0:a28a1035c31b 445 */
jrodenburg 0:a28a1035c31b 446
jrodenburg 1:0182b86f9bd4 447 void turn_valve_on(int chn){
jrodenburg 0:a28a1035c31b 448 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 449 io_control.init();
jrodenburg 1:0182b86f9bd4 450 io_control.writeOutput(1,0,1,0);
jrodenburg 0:a28a1035c31b 451 }
jrodenburg 0:a28a1035c31b 452
jrodenburg 0:a28a1035c31b 453 /* Function: turn_valve_off
jrodenburg 0:a28a1035c31b 454 **************************************************************
jrodenburg 1:0182b86f9bd4 455 Description: Turn valve off and green status LED on
jrodenburg 12:1cada1fe4743 456 Receives: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 457 Returns: N/A
jrodenburg 0:a28a1035c31b 458 */
jrodenburg 0:a28a1035c31b 459
jrodenburg 1:0182b86f9bd4 460 void turn_valve_off(int chn){
jrodenburg 0:a28a1035c31b 461 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 462 io_control.init();
jrodenburg 1:0182b86f9bd4 463 io_control.writeOutput(0,0,1,0);
jrodenburg 0:a28a1035c31b 464 }
jrodenburg 0:a28a1035c31b 465
jrodenburg 0:a28a1035c31b 466 /* Function: turn_heater_on
jrodenburg 0:a28a1035c31b 467 **************************************************************
jrodenburg 1:0182b86f9bd4 468 Description: Turn heater on and green status LED on
jrodenburg 12:1cada1fe4743 469 Receives: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 470 Returns: N/A
jrodenburg 0:a28a1035c31b 471 */
jrodenburg 0:a28a1035c31b 472
jrodenburg 1:0182b86f9bd4 473 void turn_heater_on(int chn){
jrodenburg 0:a28a1035c31b 474 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 475 io_control.init();
jrodenburg 1:0182b86f9bd4 476 io_control.writeOutput(0,1,1,0);
jrodenburg 0:a28a1035c31b 477 }
jrodenburg 0:a28a1035c31b 478
jrodenburg 0:a28a1035c31b 479 /* Function: turn_heater_off
jrodenburg 0:a28a1035c31b 480 **************************************************************
jrodenburg 1:0182b86f9bd4 481 Description: Turn heater off and green status LED on
jrodenburg 12:1cada1fe4743 482 Receives: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 483 Returns: N/A
jrodenburg 0:a28a1035c31b 484 */
jrodenburg 0:a28a1035c31b 485
jrodenburg 1:0182b86f9bd4 486 void turn_heater_off(int chn){
jrodenburg 0:a28a1035c31b 487 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 488 io_control.init();
jrodenburg 1:0182b86f9bd4 489 io_control.writeOutput(0,0,1,0);
jrodenburg 0:a28a1035c31b 490 }
jrodenburg 0:a28a1035c31b 491
jrodenburg 0:a28a1035c31b 492 /* Function: status_led
jrodenburg 0:a28a1035c31b 493 **************************************************************
jrodenburg 0:a28a1035c31b 494 Description: Turn status LED on (turns on green or red)
jrodenburg 12:1cada1fe4743 495 Receives: chn: the channel of the fixture
jrodenburg 0:a28a1035c31b 496 status: the status of channel (good (1) or bad (0))
jrodenburg 1:0182b86f9bd4 497 Returns: N/A
jrodenburg 0:a28a1035c31b 498 */
jrodenburg 0:a28a1035c31b 499
jrodenburg 1:0182b86f9bd4 500 void status_led(int chn, int status){
jrodenburg 0:a28a1035c31b 501 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 502 if(status){
jrodenburg 1:0182b86f9bd4 503 io_control.writeOutput(0,0,1,0);
jrodenburg 0:a28a1035c31b 504 }
jrodenburg 0:a28a1035c31b 505 else{
jrodenburg 8:dbf8bd4815f8 506 //turn valve on too
jrodenburg 8:dbf8bd4815f8 507 io_control.writeOutput(1,0,0,1);
jrodenburg 0:a28a1035c31b 508 }
jrodenburg 0:a28a1035c31b 509 }
jrodenburg 0:a28a1035c31b 510
jrodenburg 1:0182b86f9bd4 511 /* Function: test_mcp23008
jrodenburg 1:0182b86f9bd4 512 **************************************************************
jrodenburg 1:0182b86f9bd4 513 Description: Test each output of the MCP23009
jrodenburg 12:1cada1fe4743 514 Receives: N/A
jrodenburg 1:0182b86f9bd4 515 Returns: N/A
jrodenburg 1:0182b86f9bd4 516 */
jrodenburg 1:0182b86f9bd4 517
jrodenburg 1:0182b86f9bd4 518 void test_mcp23008(int chn){
jrodenburg 1:0182b86f9bd4 519 turn_valve_on(chn);
jrodenburg 7:8a5e65e63e2a 520 wait(0.5);
jrodenburg 1:0182b86f9bd4 521 turn_valve_off(chn);
jrodenburg 7:8a5e65e63e2a 522 wait(0.5);
jrodenburg 1:0182b86f9bd4 523 turn_heater_on(chn);
jrodenburg 7:8a5e65e63e2a 524 wait(0.5);
jrodenburg 1:0182b86f9bd4 525 turn_heater_off(chn);
jrodenburg 7:8a5e65e63e2a 526 wait(0.5);
jrodenburg 1:0182b86f9bd4 527 status_led(chn, 0);
jrodenburg 7:8a5e65e63e2a 528 wait(0.5);
jrodenburg 1:0182b86f9bd4 529 status_led(chn, 1);
jrodenburg 1:0182b86f9bd4 530 }
jrodenburg 1:0182b86f9bd4 531
jrodenburg 2:bd118a724f03 532 /* Function: test_ltc2487
jrodenburg 2:bd118a724f03 533 **************************************************************
jrodenburg 2:bd118a724f03 534 Description: Test the reading from LTC2487
jrodenburg 12:1cada1fe4743 535 Receives: N/A
jrodenburg 2:bd118a724f03 536 Returns: N/A
jrodenburg 2:bd118a724f03 537 */
jrodenburg 2:bd118a724f03 538
jrodenburg 2:bd118a724f03 539 void test_ltc2487(int chn){
jrodenburg 5:0f38a0bd4f86 540 get_temp(chn, FRONT_THERM);
jrodenburg 5:0f38a0bd4f86 541 wait(0.1);
jrodenburg 6:c980535393ed 542 get_temp(chn, BACK_THERM);
jrodenburg 6:c980535393ed 543 wait(0.1);
jrodenburg 8:dbf8bd4815f8 544 get_temp(chn, VALVE_FET_AMP);
jrodenburg 8:dbf8bd4815f8 545 wait(0.1);
jrodenburg 4:168a446bd0da 546 //if(DEBUG1) pc.printf("TEMPERATURE READING: %f \r\n", get_temp(chn));
jrodenburg 2:bd118a724f03 547 }
jrodenburg 2:bd118a724f03 548
jrodenburg 6:c980535393ed 549 /* Function: error_check
jrodenburg 6:c980535393ed 550 **************************************************************
jrodenburg 6:c980535393ed 551 Description: Checks for any system errors
jrodenburg 7:8a5e65e63e2a 552 Recieves: frontTemp: temp. of front thermistor
jrodenburg 7:8a5e65e63e2a 553 backTemp: temp. of back thermistor
jrodenburg 7:8a5e65e63e2a 554 valveFet: current through valve FET
jrodenburg 7:8a5e65e63e2a 555 heaterFet: current through heater FET
jrodenburg 6:c980535393ed 556 Returns: N/A
jrodenburg 6:c980535393ed 557 */
jrodenburg 6:c980535393ed 558
jrodenburg 6:c980535393ed 559 void error_check(int chn, float frontTemp, float backTemp, float valveFet, float heaterFet){
jrodenburg 6:c980535393ed 560 float setTemp = chnlStatus[chn].setTemp;
jrodenburg 12:1cada1fe4743 561
jrodenburg 7:8a5e65e63e2a 562 //CHECK IF THERMISTOR READINGS ARE OFF (> 5 DEGREES)
jrodenburg 7:8a5e65e63e2a 563 if(abs(frontTemp-backTemp) > 5){
jrodenburg 6:c980535393ed 564 //ERROR 6: Thermistor reading off
jrodenburg 7:8a5e65e63e2a 565 chnlStatus[chn].error = 1;
jrodenburg 12:1cada1fe4743 566 }
jrodenburg 12:1cada1fe4743 567
jrodenburg 6:c980535393ed 568 //CHECK IF HEATER'S STUCK ON OR CELL OVERHEATING
jrodenburg 6:c980535393ed 569 if((frontTemp >= (setTemp+TEMP_MARGIN)) || (backTemp >= (setTemp+TEMP_MARGIN))){
jrodenburg 6:c980535393ed 570 //ERROR 0
jrodenburg 7:8a5e65e63e2a 571 chnlStatus[chn].error = 1;
jrodenburg 6:c980535393ed 572 status_led(chn, STATUS_BAD);
jrodenburg 6:c980535393ed 573 if(heaterFet >= FET_ON_CURRENT){
jrodenburg 12:1cada1fe4743 574 //ERROR 1: Heater FET stuck on
jrodenburg 6:c980535393ed 575 }
jrodenburg 6:c980535393ed 576 if(valveFet <= FET_ON_CURRENT){
jrodenburg 12:1cada1fe4743 577 //ERROR 2: valve FET stuck off
jrodenburg 6:c980535393ed 578 }
jrodenburg 6:c980535393ed 579 }
jrodenburg 12:1cada1fe4743 580
jrodenburg 6:c980535393ed 581 //CHECK IF VALVE STUCK ON OR CELL OVERHEATING
jrodenburg 6:c980535393ed 582 if((frontTemp <= (setTemp-TEMP_MARGIN)) || (backTemp <= (setTemp-TEMP_MARGIN))){
jrodenburg 6:c980535393ed 583 //ERROR 0
jrodenburg 7:8a5e65e63e2a 584 chnlStatus[chn].error = 1;
jrodenburg 6:c980535393ed 585 status_led(chn, STATUS_BAD);
jrodenburg 6:c980535393ed 586 if(heaterFet <= FET_ON_CURRENT){
jrodenburg 12:1cada1fe4743 587 //ERROR 2: Heater FET stuck off
jrodenburg 6:c980535393ed 588 }
jrodenburg 6:c980535393ed 589 else if(valveFet >= FET_ON_CURRENT){
jrodenburg 12:1cada1fe4743 590 //ERROR 3: Chiller FET stuck on
jrodenburg 6:c980535393ed 591 }
jrodenburg 12:1cada1fe4743 592 }
jrodenburg 5:0f38a0bd4f86 593 }
jrodenburg 5:0f38a0bd4f86 594
jrodenburg 8:dbf8bd4815f8 595 //***************************************************************
jrodenburg 8:dbf8bd4815f8 596 // Build packet with temperature readings to send to GUI
jrodenburg 8:dbf8bd4815f8 597 //***************************************************************
jrodenburg 8:dbf8bd4815f8 598 void sendTempReadings (int chan, float currentTemp)
jrodenburg 8:dbf8bd4815f8 599 {
jrodenburg 8:dbf8bd4815f8 600 RESPONSE_CMD response;
jrodenburg 8:dbf8bd4815f8 601 unsigned char *ptr = (unsigned char *)&response;
jrodenburg 8:dbf8bd4815f8 602 int i;
jrodenburg 8:dbf8bd4815f8 603
jrodenburg 8:dbf8bd4815f8 604 response.SOF_flag = TX_SOF;
jrodenburg 8:dbf8bd4815f8 605 response.cmd_type = CMD_DATA;
jrodenburg 8:dbf8bd4815f8 606 response.len = 9;
jrodenburg 8:dbf8bd4815f8 607 response.Delim = DELIMETER;
jrodenburg 8:dbf8bd4815f8 608 response.data = (float)currentTemp;
jrodenburg 8:dbf8bd4815f8 609 response.EOF_flag = TX_EOF;
jrodenburg 8:dbf8bd4815f8 610
jrodenburg 8:dbf8bd4815f8 611 // Send response to GUI
jrodenburg 8:dbf8bd4815f8 612 for (i=0; i < response.len; i++, ptr++)
jrodenburg 8:dbf8bd4815f8 613 pc.printf("%02x", *ptr);
jrodenburg 8:dbf8bd4815f8 614 pc.printf("\n");
jrodenburg 8:dbf8bd4815f8 615 }
jrodenburg 8:dbf8bd4815f8 616
jrodenburg 12:1cada1fe4743 617 //***************************************************************
jrodenburg 12:1cada1fe4743 618 // Build packet with errors to send to GUI
jrodenburg 12:1cada1fe4743 619 //***************************************************************
jrodenburg 12:1cada1fe4743 620 void sendError (int chan, float error)
jrodenburg 12:1cada1fe4743 621 {
jrodenburg 12:1cada1fe4743 622 RESPONSE_CMD response;
jrodenburg 12:1cada1fe4743 623 unsigned char *ptr = (unsigned char *)&response;
jrodenburg 12:1cada1fe4743 624 int i;
jrodenburg 12:1cada1fe4743 625
jrodenburg 12:1cada1fe4743 626 response.SOF_flag = TX_SOF;
jrodenburg 12:1cada1fe4743 627 response.cmd_type = ERROR_DATA;
jrodenburg 12:1cada1fe4743 628 response.len = 9;
jrodenburg 12:1cada1fe4743 629 response.Delim = DELIMETER;
jrodenburg 12:1cada1fe4743 630 response.data = (float)error;
jrodenburg 12:1cada1fe4743 631 response.EOF_flag = TX_EOF;
jrodenburg 12:1cada1fe4743 632
jrodenburg 12:1cada1fe4743 633 // Send response to GUI
jrodenburg 12:1cada1fe4743 634 for (i=0; i < response.len; i++, ptr++)
jrodenburg 12:1cada1fe4743 635 pc.printf("%02x", *ptr);
jrodenburg 12:1cada1fe4743 636 pc.printf("\n");
jrodenburg 12:1cada1fe4743 637 }
jrodenburg 6:c980535393ed 638
jrodenburg 6:c980535393ed 639
jrodenburg 6:c980535393ed 640 /*************************************************************/
jrodenburg 6:c980535393ed 641 /* MAIN FUNCTION */
jrodenburg 6:c980535393ed 642 /*************************************************************/
jrodenburg 0:a28a1035c31b 643
jrodenburg 12:1cada1fe4743 644 int main() {
jrodenburg 0:a28a1035c31b 645
jrodenburg 12:1cada1fe4743 646 Timer t;
jrodenburg 8:dbf8bd4815f8 647
jrodenburg 8:dbf8bd4815f8 648 // Setup serial port
jrodenburg 8:dbf8bd4815f8 649 // Look for RX_EOF
jrodenburg 8:dbf8bd4815f8 650 pc.baud(9600);
jrodenburg 8:dbf8bd4815f8 651 pc.autoDetectChar(RX_EOF);
jrodenburg 8:dbf8bd4815f8 652 pc.attach(&rxInterrupt, MODSERIAL::RxAutoDetect);
jrodenburg 8:dbf8bd4815f8 653
jrodenburg 8:dbf8bd4815f8 654 myled = 1;
jrodenburg 12:1cada1fe4743 655 rLed = 1;
jrodenburg 12:1cada1fe4743 656 gLed = 0;
jrodenburg 12:1cada1fe4743 657
jrodenburg 8:dbf8bd4815f8 658 t.start();
jrodenburg 12:1cada1fe4743 659
jrodenburg 0:a28a1035c31b 660 while(1) {
jrodenburg 12:1cada1fe4743 661
jrodenburg 12:1cada1fe4743 662 if(DEBUG3)
jrodenburg 12:1cada1fe4743 663 pc.printf("DBG: PROGRAM STARTED \r\n");
jrodenburg 12:1cada1fe4743 664
jrodenburg 13:604e6933366f 665 /*float time_sec = t.read();
jrodenburg 13:604e6933366f 666
jrodenburg 13:604e6933366f 667 if(time_sec >= 60){
jrodenburg 13:604e6933366f 668 t.reset();
jrodenburg 13:604e6933366f 669 }
jrodenburg 13:604e6933366f 670
jrodenburg 13:604e6933366f 671 pc.printf("Time: %f \r\n", time_sec);*/
jrodenburg 13:604e6933366f 672
jrodenburg 5:0f38a0bd4f86 673 for(int chnl = 0; chnl < CHN_COUNT; chnl++){
jrodenburg 5:0f38a0bd4f86 674 float currentTempFront = get_temp(chnl, FRONT_THERM);
jrodenburg 13:604e6933366f 675 //wait(0.08);
jrodenburg 13:604e6933366f 676
jrodenburg 12:1cada1fe4743 677 //float valveCurrent = get_valve_current(chnl, VALVE_FET_AMP);
jrodenburg 12:1cada1fe4743 678 //float heaterCurrent = get_heater_current(chnl, HEAT_FET_AMP);
jrodenburg 13:604e6933366f 679 //float currentTemp = currentTempFront;
jrodenburg 12:1cada1fe4743 680 //currentTemp = currentTempFront;
jrodenburg 13:604e6933366f 681
jrodenburg 12:1cada1fe4743 682
jrodenburg 8:dbf8bd4815f8 683 //check if we received data/need to update TCTF data
jrodenburg 8:dbf8bd4815f8 684 if(dataReceived){
jrodenburg 8:dbf8bd4815f8 685 dataReceived = false;
jrodenburg 12:1cada1fe4743 686 pc.printf("DBG: %d bytes Rx'd\n", pc.rxBufferGetCount());
jrodenburg 8:dbf8bd4815f8 687 pc.move(rxBuf, 50);
jrodenburg 8:dbf8bd4815f8 688 parseRXData();
jrodenburg 8:dbf8bd4815f8 689 pc.rxBufferFlush();
jrodenburg 12:1cada1fe4743 690 }
jrodenburg 8:dbf8bd4815f8 691
jrodenburg 13:604e6933366f 692 float currentTempBack = get_temp(chnl, BACK_THERM);
jrodenburg 13:604e6933366f 693 float currentTemp = currentTempBack;
jrodenburg 13:604e6933366f 694
jrodenburg 8:dbf8bd4815f8 695 if (chnl == chanSel_SendTemp){
jrodenburg 12:1cada1fe4743 696 //Send temp readings for selected chan to GUI
jrodenburg 8:dbf8bd4815f8 697 sendTempReadings(currChan, currentTemp);
jrodenburg 8:dbf8bd4815f8 698 }
jrodenburg 13:604e6933366f 699
jrodenburg 13:604e6933366f 700
jrodenburg 12:1cada1fe4743 701
jrodenburg 2:bd118a724f03 702 //CONTROL LOOP:
jrodenburg 4:168a446bd0da 703 if(chnlStatus[chnl].status == 1){
jrodenburg 12:1cada1fe4743 704 if(DEBUG3) pc.printf("DBG: [%d] Temp: F: %f B: %f\r\n", chnl, currentTempFront, currentTempBack);
jrodenburg 7:8a5e65e63e2a 705
jrodenburg 7:8a5e65e63e2a 706 //Error check on fixture
jrodenburg 8:dbf8bd4815f8 707 //error_check(chnl, currentTempFront, currentTempBack, valveCurrent, heaterCurrent);
jrodenburg 12:1cada1fe4743 708 if(((currentTempFront >= MAX_TEMP) && (currentTempBack >= MAX_TEMP)) ||
jrodenburg 12:1cada1fe4743 709 ((currentTempFront == 0) || (currentTempBack == 0))||
jrodenburg 12:1cada1fe4743 710 (abs(currentTempBack - currentTempFront) >= TEMP_MARGIN)||
jrodenburg 13:604e6933366f 711 ((currentTempFront <= MIN_TEMP) && (currentTempBack <= MIN_TEMP))||
jrodenburg 13:604e6933366f 712 (chnlStatus[chnl].error == 1)){
jrodenburg 12:1cada1fe4743 713 status_led(chnl, STATUS_BAD);
jrodenburg 12:1cada1fe4743 714 sendError(chnl, 1);
jrodenburg 12:1cada1fe4743 715 chnlStatus[chnl].error = 1;
jrodenburg 8:dbf8bd4815f8 716 }
jrodenburg 7:8a5e65e63e2a 717
jrodenburg 7:8a5e65e63e2a 718 if(chnlStatus[chnl].error == 0){
jrodenburg 12:1cada1fe4743 719 if(currentTemp > ((chnlStatus[chnl].setTemp)+HYST_HIGH)){
jrodenburg 12:1cada1fe4743 720 if(DEBUG3) pc.printf("DBG: [%d] Chiller ON \r\n", chnl);
jrodenburg 13:604e6933366f 721 //check if the temp. diff. is small and can't wait for an entire period (~4.18) to turn valve off
jrodenburg 13:604e6933366f 722 if(abs(currentTemp - (chnlStatus[chnl].setTemp)) < 5){
jrodenburg 13:604e6933366f 723 turn_valve_on(chnl);
jrodenburg 13:604e6933366f 724 while(currentTemp > ((chnlStatus[chnl].setTemp)+HYST_HIGH)){
jrodenburg 13:604e6933366f 725 currentTemp = get_temp(chnl, BACK_THERM);
jrodenburg 13:604e6933366f 726 }
jrodenburg 13:604e6933366f 727 turn_valve_off(chnl);
jrodenburg 13:604e6933366f 728 }
jrodenburg 13:604e6933366f 729 else{
jrodenburg 13:604e6933366f 730 //Turn chiller on
jrodenburg 13:604e6933366f 731 turn_valve_on(chnl);
jrodenburg 13:604e6933366f 732 }
jrodenburg 7:8a5e65e63e2a 733 }
jrodenburg 8:dbf8bd4815f8 734 else if (currentTemp < ((chnlStatus[chnl].setTemp)-HYST_LOW)){
jrodenburg 12:1cada1fe4743 735 if(DEBUG3) pc.printf("DBG: [%d] Heater ON \r\n", chnl);
jrodenburg 7:8a5e65e63e2a 736 //Turn heater on
jrodenburg 7:8a5e65e63e2a 737 turn_heater_on(chnl);
jrodenburg 7:8a5e65e63e2a 738 }
jrodenburg 7:8a5e65e63e2a 739 else{
jrodenburg 12:1cada1fe4743 740 if(DEBUG3) pc.printf("DBG: [%d] All OFF \r\n", chnl);
jrodenburg 7:8a5e65e63e2a 741 //turn off chiller
jrodenburg 7:8a5e65e63e2a 742 turn_valve_off(chnl);
jrodenburg 12:1cada1fe4743 743 //turn off heater
jrodenburg 7:8a5e65e63e2a 744 turn_heater_off(chnl);
jrodenburg 7:8a5e65e63e2a 745 //turn on green LED status light
jrodenburg 12:1cada1fe4743 746 status_led(chnl, 1);
jrodenburg 12:1cada1fe4743 747 }
jrodenburg 12:1cada1fe4743 748 }
jrodenburg 2:bd118a724f03 749 }
jrodenburg 13:604e6933366f 750 else{
jrodenburg 13:604e6933366f 751 //turn off chiller
jrodenburg 13:604e6933366f 752 turn_valve_off(chnl);
jrodenburg 13:604e6933366f 753 //turn off heater
jrodenburg 13:604e6933366f 754 turn_heater_off(chnl);
jrodenburg 13:604e6933366f 755 //turn on green LED status light
jrodenburg 13:604e6933366f 756 status_led(chnl, 1);
jrodenburg 13:604e6933366f 757 }
jrodenburg 2:bd118a724f03 758 }
jrodenburg 0:a28a1035c31b 759 }
jrodenburg 12:1cada1fe4743 760 }