Working on rewriting how we acquire data from LTC chip and sending all temp data over serial for python logging

Dependencies:   MODSERIAL mbed

Fork of TCTF_Control_Main by Rivian Irvine Team

Committer:
jrodenburg
Date:
Wed Jun 20 00:19:13 2018 +0000
Revision:
20:8c79659c71e4
Parent:
19:fec49ef9944b
Made 3 major changes:; 1. Updated how we read temperatures (I2C write all channel, than read all channels); 2. Got rid of inner cooling loop that stopped control loop if the channel was cooling and really close to set point ; 3. Send all temp. data

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 15:74a01aaeb60e 4 #define FW_VERSION "V.010"
jrodenburg 0:a28a1035c31b 5 #include "mbed.h"
jrodenburg 0:a28a1035c31b 6 #include "MODSERIAL.h"
jrodenburg 8:dbf8bd4815f8 7 #include "MCP23008.h"
jrodenburg 0:a28a1035c31b 8 #include "LTC2487.h"
jrodenburg 2:bd118a724f03 9 #include <string>
jrodenburg 0:a28a1035c31b 10
jrodenburg 0:a28a1035c31b 11 //DEFINITIVE VARIABLES
jrodenburg 16:a7fd53ab4199 12 #define DEBUG 0
jrodenburg 16:a7fd53ab4199 13 #define DEBUG1 0
jrodenburg 16:a7fd53ab4199 14 #define DEBUG2 0
jrodenburg 17:5098d8fbb298 15 #define DEBUG3 0
jrodenburg 16:a7fd53ab4199 16 #define DEBUG5 0
jrodenburg 19:fec49ef9944b 17 #define UID_PRINT 1
jrodenburg 19:fec49ef9944b 18 #define TEMP_PRINT 0
jrodenburg 20:8c79659c71e4 19 #define LOOP_TIME 0
jrodenburg 16:a7fd53ab4199 20 #define CHN_COUNT 8
jrodenburg 16:a7fd53ab4199 21 #define MIN_TEMP 10
jrodenburg 16:a7fd53ab4199 22 #define MAX_TEMP 65
jrodenburg 16:a7fd53ab4199 23 #define TEMP_MARGIN 20
jrodenburg 16:a7fd53ab4199 24 #define HYST_LOW 0.3
jrodenburg 16:a7fd53ab4199 25 #define HYST_HIGH 1
jrodenburg 16:a7fd53ab4199 26 #define SAMPLES 5
jrodenburg 16:a7fd53ab4199 27 #define I2C_Freq 2000
jrodenburg 16:a7fd53ab4199 28 #define VALVE 1
jrodenburg 16:a7fd53ab4199 29 #define HEATER 2
jrodenburg 16:a7fd53ab4199 30 #define STATUS_GOOD 3
jrodenburg 16:a7fd53ab4199 31 #define STATUS_BAD 0
jrodenburg 16:a7fd53ab4199 32 #define sizeLUT 34
jrodenburg 16:a7fd53ab4199 33 #define BACK_THERM 0
jrodenburg 16:a7fd53ab4199 34 #define FRONT_THERM 1
jrodenburg 16:a7fd53ab4199 35 #define HEAT_FET_AMP 2
jrodenburg 16:a7fd53ab4199 36 #define VALVE_FET_AMP 3
jrodenburg 16:a7fd53ab4199 37 #define FET_ON_CURRENT 1.12
jrodenburg 16:a7fd53ab4199 38 #define ROOM_TEMP 22
jrodenburg 14:69cd53434783 39 #define MAX_HEATER_ON_TIME 25
jrodenburg 14:69cd53434783 40 #define MAX_CHILL_TIME 10 //10sec
jrodenburg 9:1cada1fe4743 41
jrodenburg 8:dbf8bd4815f8 42 // Defines for use with Serial communication
jrodenburg 8:dbf8bd4815f8 43 #pragma pack (1)
jrodenburg 8:dbf8bd4815f8 44 #define RX_SOF 0x7B
jrodenburg 8:dbf8bd4815f8 45 #define RX_EOF 0x7D
jrodenburg 8:dbf8bd4815f8 46 #define TX_SOF 0x7B
jrodenburg 8:dbf8bd4815f8 47 #define TX_EOF 0x7D
jrodenburg 8:dbf8bd4815f8 48 #define DELIMETER 0x2E
jrodenburg 8:dbf8bd4815f8 49 #define SET_TEMPERATURE 0xB0
jrodenburg 8:dbf8bd4815f8 50 #define SELECT_CHANNEL 0xB1
jrodenburg 15:74a01aaeb60e 51 #define READ_UID 0xB2
jrodenburg 15:74a01aaeb60e 52 #define RESPONSE_DATA 0xD0
jrodenburg 15:74a01aaeb60e 53 #define TEMP_DATA 0xD1
jrodenburg 15:74a01aaeb60e 54 #define UID_DATA 0xD2
jrodenburg 15:74a01aaeb60e 55 #define ERROR_DATA 0xDF
jrodenburg 9:1cada1fe4743 56
jrodenburg 0:a28a1035c31b 57
jrodenburg 8:dbf8bd4815f8 58 unsigned int chanSel_SendTemp = 0;
jrodenburg 8:dbf8bd4815f8 59 unsigned int chanSel_SetTemp = 0;
jrodenburg 8:dbf8bd4815f8 60 // Default to chan 0
jrodenburg 8:dbf8bd4815f8 61 int currChan = 0;
jrodenburg 8:dbf8bd4815f8 62 bool newTempSet = false;
jrodenburg 8:dbf8bd4815f8 63
jrodenburg 9:1cada1fe4743 64
jrodenburg 8:dbf8bd4815f8 65 //***********************************************
jrodenburg 8:dbf8bd4815f8 66 // Serial Rx Packet Format
jrodenburg 8:dbf8bd4815f8 67 //***********************************************
jrodenburg 8:dbf8bd4815f8 68 typedef struct {
jrodenburg 8:dbf8bd4815f8 69 unsigned char SOF_flag;
jrodenburg 8:dbf8bd4815f8 70 unsigned char cmd_type;
jrodenburg 8:dbf8bd4815f8 71 unsigned char len;
jrodenburg 8:dbf8bd4815f8 72 unsigned char Delim;
jrodenburg 8:dbf8bd4815f8 73 } HOST_CMD_HEADER;
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 chanID;
jrodenburg 8:dbf8bd4815f8 78 unsigned char tempDelim;
jrodenburg 8:dbf8bd4815f8 79 float setTemp;
jrodenburg 8:dbf8bd4815f8 80 unsigned char chanStatDelim;
jrodenburg 8:dbf8bd4815f8 81 unsigned char chanStat;
jrodenburg 8:dbf8bd4815f8 82 } SET_TEMPERATURE_CMD;
jrodenburg 8:dbf8bd4815f8 83
jrodenburg 8:dbf8bd4815f8 84 typedef struct {
jrodenburg 8:dbf8bd4815f8 85 HOST_CMD_HEADER cmd_header;
jrodenburg 8:dbf8bd4815f8 86 unsigned char chanIDSel;
jrodenburg 8:dbf8bd4815f8 87 unsigned char chanDelim;
jrodenburg 8:dbf8bd4815f8 88 unsigned char chanStat;
jrodenburg 8:dbf8bd4815f8 89 } SELECT_CHANNEL_CMD;
jrodenburg 8:dbf8bd4815f8 90
jrodenburg 8:dbf8bd4815f8 91 typedef struct {
jrodenburg 8:dbf8bd4815f8 92 unsigned char SOF_flag;
jrodenburg 8:dbf8bd4815f8 93 unsigned char cmd_type;
jrodenburg 8:dbf8bd4815f8 94 unsigned char len;
jrodenburg 8:dbf8bd4815f8 95 unsigned char Delim;
jrodenburg 8:dbf8bd4815f8 96 float data;
jrodenburg 8:dbf8bd4815f8 97 unsigned char EOF_flag;
jrodenburg 8:dbf8bd4815f8 98 } RESPONSE_CMD;
jrodenburg 2:bd118a724f03 99
jrodenburg 15:74a01aaeb60e 100 typedef struct {
jrodenburg 16:a7fd53ab4199 101 unsigned char SOF_flag;
jrodenburg 16:a7fd53ab4199 102 unsigned char cmd_type;
jrodenburg 16:a7fd53ab4199 103 unsigned char len;
jrodenburg 16:a7fd53ab4199 104 unsigned char Delim;
jrodenburg 16:a7fd53ab4199 105 float chTemp[CHN_COUNT];
jrodenburg 16:a7fd53ab4199 106 unsigned char EOF_flag;
jrodenburg 16:a7fd53ab4199 107 } RESPONSE_TEMP_CMD;
jrodenburg 16:a7fd53ab4199 108
jrodenburg 16:a7fd53ab4199 109 typedef struct {
jrodenburg 15:74a01aaeb60e 110 HOST_CMD_HEADER cmd_header;
jrodenburg 15:74a01aaeb60e 111 unsigned char chanIDSel;
jrodenburg 15:74a01aaeb60e 112 unsigned char chanDelim;
jrodenburg 15:74a01aaeb60e 113 unsigned char chanStat;
jrodenburg 15:74a01aaeb60e 114 } READ_UID_CMD;
jrodenburg 15:74a01aaeb60e 115
jrodenburg 15:74a01aaeb60e 116 typedef struct {
jrodenburg 15:74a01aaeb60e 117 unsigned char SOF_flag;
jrodenburg 15:74a01aaeb60e 118 unsigned char cmd_type;
jrodenburg 15:74a01aaeb60e 119 unsigned char len;
jrodenburg 15:74a01aaeb60e 120 unsigned char Delim;
jrodenburg 15:74a01aaeb60e 121 uint32_t UIDMH;
jrodenburg 15:74a01aaeb60e 122 uint32_t UIDML;
jrodenburg 15:74a01aaeb60e 123 uint32_t UIDL;
jrodenburg 15:74a01aaeb60e 124 unsigned char EOF_flag;
jrodenburg 15:74a01aaeb60e 125 } UID_RESPONSE;
jrodenburg 15:74a01aaeb60e 126
jrodenburg 2:bd118a724f03 127 //TCTF CHANNEL DATA
jrodenburg 2:bd118a724f03 128 struct CHNL_DATA{
jrodenburg 2:bd118a724f03 129 bool status;
jrodenburg 2:bd118a724f03 130 float setTemp;
jrodenburg 7:8a5e65e63e2a 131 bool error;
jrodenburg 14:69cd53434783 132 int heater_init_time;
jrodenburg 2:bd118a724f03 133 };
jrodenburg 2:bd118a724f03 134
jrodenburg 4:168a446bd0da 135 CHNL_DATA chnlStatus[] = {
jrodenburg 14:69cd53434783 136 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 137 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 138 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 139 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 140 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 141 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 142 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 143 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 144 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 145 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 146 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 147 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 148 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 149 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 150 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 151 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 152 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 153 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 154 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 155 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 156 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 157 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 158 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 159 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 160 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 161 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 162 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 163 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 164 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 165 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 166 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 167 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 168 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 169 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 170 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 171 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 172 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 173 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 174 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 175 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 176 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 177 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 178 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 179 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 180 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 181 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 182 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 183 {0, NULL, 0, 0},
jrodenburg 2:bd118a724f03 184 };
jrodenburg 2:bd118a724f03 185
jrodenburg 2:bd118a724f03 186
jrodenburg 0:a28a1035c31b 187 //I2C AADRESS LOOK UP TABLE, CREATED IN EXCEL SHEET (COUNT, TEMP)
jrodenburg 2:bd118a724f03 188 struct I2C_ADDR_LUT{
jrodenburg 0:a28a1035c31b 189 int adc;
jrodenburg 0:a28a1035c31b 190 int io;
jrodenburg 0:a28a1035c31b 191 };
jrodenburg 0:a28a1035c31b 192
jrodenburg 0:a28a1035c31b 193 I2C_ADDR_LUT addrLUT[] = {
jrodenburg 0:a28a1035c31b 194 {0x23, 0x10},
jrodenburg 0:a28a1035c31b 195 {0x53, 0x60},
jrodenburg 0:a28a1035c31b 196 {0x43, 0x70},
jrodenburg 0:a28a1035c31b 197 {0x73, 0x40},
jrodenburg 0:a28a1035c31b 198 {0x63, 0x50},
jrodenburg 0:a28a1035c31b 199 {0x22, 0x11},
jrodenburg 0:a28a1035c31b 200 {0x52, 0x61},
jrodenburg 0:a28a1035c31b 201 {0x42, 0x71},
jrodenburg 0:a28a1035c31b 202 {0x72, 0x41},
jrodenburg 0:a28a1035c31b 203 {0x62, 0x51},
jrodenburg 0:a28a1035c31b 204 {0x21, 0x12},
jrodenburg 0:a28a1035c31b 205 {0x51, 0x62},
jrodenburg 0:a28a1035c31b 206 {0x41, 0x72},
jrodenburg 0:a28a1035c31b 207 {0x71, 0x42},
jrodenburg 0:a28a1035c31b 208 {0x61, 0x52},
jrodenburg 0:a28a1035c31b 209 {0x20, 0x13},
jrodenburg 0:a28a1035c31b 210 {0x50, 0x63},
jrodenburg 0:a28a1035c31b 211 {0x40, 0x73},
jrodenburg 0:a28a1035c31b 212 {0x70, 0x43},
jrodenburg 0:a28a1035c31b 213 {0x60, 0x53},
jrodenburg 0:a28a1035c31b 214 {0x26, 0x14},
jrodenburg 0:a28a1035c31b 215 {0x56, 0x64},
jrodenburg 0:a28a1035c31b 216 {0x46, 0x74},
jrodenburg 0:a28a1035c31b 217 {0x76, 0x44},
jrodenburg 0:a28a1035c31b 218 {0x66, 0x54},
jrodenburg 0:a28a1035c31b 219 {0x2C, 0x1F},
jrodenburg 0:a28a1035c31b 220 {0x5C, 0x6F},
jrodenburg 0:a28a1035c31b 221 {0x4C, 0x7F},
jrodenburg 0:a28a1035c31b 222 {0x7C, 0x4F},
jrodenburg 0:a28a1035c31b 223 {0x6C, 0x5F},
jrodenburg 0:a28a1035c31b 224 {0x2D, 0x1E},
jrodenburg 0:a28a1035c31b 225 {0x5D, 0x6E},
jrodenburg 0:a28a1035c31b 226 {0x4D, 0x7E},
jrodenburg 0:a28a1035c31b 227 {0x7D, 0x4E},
jrodenburg 0:a28a1035c31b 228 {0x6D, 0x5E},
jrodenburg 0:a28a1035c31b 229 {0x2E, 0x1D},
jrodenburg 0:a28a1035c31b 230 {0x5E, 0x6D},
jrodenburg 0:a28a1035c31b 231 {0x4E, 0x7D},
jrodenburg 0:a28a1035c31b 232 {0x7E, 0x4D},
jrodenburg 0:a28a1035c31b 233 {0x6E, 0x5D},
jrodenburg 0:a28a1035c31b 234 {0x2F, 0x1C},
jrodenburg 0:a28a1035c31b 235 {0x5F, 0x6C},
jrodenburg 0:a28a1035c31b 236 {0x4F, 0x7C},
jrodenburg 0:a28a1035c31b 237 {0x7F, 0x4C},
jrodenburg 0:a28a1035c31b 238 {0x6F, 0x5C},
jrodenburg 0:a28a1035c31b 239 {0x20, 0x1B},
jrodenburg 0:a28a1035c31b 240 {0x50, 0x6B},
jrodenburg 9:1cada1fe4743 241 {0x40, 0x7B},
jrodenburg 0:a28a1035c31b 242 };
jrodenburg 0:a28a1035c31b 243
jrodenburg 0:a28a1035c31b 244 //THERMISTOR LOOK UP TABLE, CREATED IN EXCEL SHEET (COUNT, TEMP)
jrodenburg 2:bd118a724f03 245 struct THERM_LUT{
jrodenburg 0:a28a1035c31b 246 int adc;
jrodenburg 0:a28a1035c31b 247 int temp;
jrodenburg 0:a28a1035c31b 248 };
jrodenburg 0:a28a1035c31b 249
jrodenburg 0:a28a1035c31b 250 THERM_LUT thermLUT[] = {
jrodenburg 0:a28a1035c31b 251 {113779,-40},
jrodenburg 0:a28a1035c31b 252 {109152,-35},
jrodenburg 0:a28a1035c31b 253 {103830,-30},
jrodenburg 0:a28a1035c31b 254 {97855,-25},
jrodenburg 0:a28a1035c31b 255 {91319,-20},
jrodenburg 0:a28a1035c31b 256 {84352,-15},
jrodenburg 0:a28a1035c31b 257 {77124,-10},
jrodenburg 0:a28a1035c31b 258 {69820,-5},
jrodenburg 0:a28a1035c31b 259 {62621,0},
jrodenburg 0:a28a1035c31b 260 {55693,5},
jrodenburg 0:a28a1035c31b 261 {49169,10},
jrodenburg 0:a28a1035c31b 262 {43144,15},
jrodenburg 0:a28a1035c31b 263 {37669,20},
jrodenburg 0:a28a1035c31b 264 {32768,25},
jrodenburg 0:a28a1035c31b 265 {28429,30},
jrodenburg 0:a28a1035c31b 266 {24622,35},
jrodenburg 0:a28a1035c31b 267 {21309,40},
jrodenburg 0:a28a1035c31b 268 {18439,45},
jrodenburg 0:a28a1035c31b 269 {15962,50},
jrodenburg 0:a28a1035c31b 270 {13831,55},
jrodenburg 0:a28a1035c31b 271 {12002,60},
jrodenburg 0:a28a1035c31b 272 {10428,65},
jrodenburg 0:a28a1035c31b 273 {9080,70},
jrodenburg 0:a28a1035c31b 274 {7919,75},
jrodenburg 0:a28a1035c31b 275 {6923,80},
jrodenburg 0:a28a1035c31b 276 {6063,85},
jrodenburg 0:a28a1035c31b 277 {5323,90},
jrodenburg 0:a28a1035c31b 278 {4685,95},
jrodenburg 0:a28a1035c31b 279 {4130,100},
jrodenburg 0:a28a1035c31b 280 {3653,105},
jrodenburg 0:a28a1035c31b 281 {3234,110},
jrodenburg 0:a28a1035c31b 282 {2876,115},
jrodenburg 0:a28a1035c31b 283 {2563,120},
jrodenburg 0:a28a1035c31b 284 {2284,125}
jrodenburg 0:a28a1035c31b 285 };
jrodenburg 0:a28a1035c31b 286
jrodenburg 16:a7fd53ab4199 287 //TCTF CHANNEL TEMPERATURE
jrodenburg 16:a7fd53ab4199 288 typedef struct{
jrodenburg 16:a7fd53ab4199 289 float currTempFront;
jrodenburg 16:a7fd53ab4199 290 float currTempBack;
jrodenburg 16:a7fd53ab4199 291 }CHNL_TEMP;
jrodenburg 16:a7fd53ab4199 292
jrodenburg 16:a7fd53ab4199 293 CHNL_TEMP channelTempData[CHN_COUNT];
jrodenburg 16:a7fd53ab4199 294
jrodenburg 2:bd118a724f03 295 //SERIAL COMMUNICATION SETUP
jrodenburg 2:bd118a724f03 296 MODSERIAL pc(USBTX, USBRX);
jrodenburg 1:0182b86f9bd4 297
jrodenburg 0:a28a1035c31b 298 //DEFINE PINS
jrodenburg 0:a28a1035c31b 299 DigitalOut myled(LED2);
jrodenburg 0:a28a1035c31b 300
jrodenburg 0:a28a1035c31b 301 //I2C FOR MCP23008 (I/O Control)
jrodenburg 9:1cada1fe4743 302 MCP23008 io_control(PTC9, PTC8, 0x10, 100000); //sda, scl
jrodenburg 0:a28a1035c31b 303
jrodenburg 0:a28a1035c31b 304 //I2C FOR LTC2487 (ADC Control)
jrodenburg 9:1cada1fe4743 305 LTC2487 ltc2487(PTC11, PTC10, 0x23, 100000); //sda, scl
jrodenburg 0:a28a1035c31b 306
jrodenburg 0:a28a1035c31b 307 //GLOBAL VARIABLES
jrodenburg 9:1cada1fe4743 308 volatile bool dataReceived = false; //used to check if data has been received
jrodenburg 8:dbf8bd4815f8 309 volatile bool dataTxReady = false;
jrodenburg 2:bd118a724f03 310 char rxBuf[50];
jrodenburg 2:bd118a724f03 311 int chnlSel;
jrodenburg 0:a28a1035c31b 312
jrodenburg 7:8a5e65e63e2a 313
jrodenburg 7:8a5e65e63e2a 314 /* Function: turnOffChannel
jrodenburg 7:8a5e65e63e2a 315 **************************************************************
jrodenburg 7:8a5e65e63e2a 316 Description: Turns off a channel
jrodenburg 7:8a5e65e63e2a 317 Recieves: chnl: channel to turn off
jrodenburg 7:8a5e65e63e2a 318 Returns: N/A
jrodenburg 7:8a5e65e63e2a 319 */
jrodenburg 7:8a5e65e63e2a 320
jrodenburg 7:8a5e65e63e2a 321 void turnOffChannel(int chnl){
jrodenburg 7:8a5e65e63e2a 322 io_control.setAddress(addrLUT[chnl].io);
jrodenburg 7:8a5e65e63e2a 323 io_control.init();
jrodenburg 7:8a5e65e63e2a 324 io_control.writeOutput(0,0,0,0);
jrodenburg 7:8a5e65e63e2a 325 }
jrodenburg 7:8a5e65e63e2a 326
jrodenburg 8:dbf8bd4815f8 327
jrodenburg 2:bd118a724f03 328 /* Function: rxInterrupt
jrodenburg 1:0182b86f9bd4 329 **************************************************************
jrodenburg 8:dbf8bd4815f8 330 Description: serial rx interupt handler
jrodenburg 8:dbf8bd4815f8 331 Receives: N/A
jrodenburg 8:dbf8bd4815f8 332 Returns: N/A
jrodenburg 2:bd118a724f03 333 */
jrodenburg 2:bd118a724f03 334
jrodenburg 8:dbf8bd4815f8 335 DigitalOut rLed(LED1);
jrodenburg 8:dbf8bd4815f8 336 DigitalOut gLed(LED2);
jrodenburg 8:dbf8bd4815f8 337
jrodenburg 15:74a01aaeb60e 338 void sendUID ();
jrodenburg 15:74a01aaeb60e 339
jrodenburg 8:dbf8bd4815f8 340 //***********************************************
jrodenburg 8:dbf8bd4815f8 341 // Rx Interrupt from serial Host interface
jrodenburg 8:dbf8bd4815f8 342 //***********************************************
jrodenburg 8:dbf8bd4815f8 343 void rxInterrupt(MODSERIAL_IRQ_INFO *info)
jrodenburg 8:dbf8bd4815f8 344 {
jrodenburg 8:dbf8bd4815f8 345 gLed = 0;
jrodenburg 8:dbf8bd4815f8 346 dataReceived = true;
jrodenburg 8:dbf8bd4815f8 347 gLed = 1;
jrodenburg 2:bd118a724f03 348 }
jrodenburg 2:bd118a724f03 349
jrodenburg 8:dbf8bd4815f8 350
jrodenburg 8:dbf8bd4815f8 351 //***************************************************************
jrodenburg 8:dbf8bd4815f8 352 // Tx Interrupt from serial Host interface
jrodenburg 8:dbf8bd4815f8 353 //***************************************************************
jrodenburg 8:dbf8bd4815f8 354 void txInterrupt(MODSERIAL_IRQ_INFO *info)
jrodenburg 8:dbf8bd4815f8 355 {
jrodenburg 8:dbf8bd4815f8 356 dataTxReady = true;
jrodenburg 8:dbf8bd4815f8 357 }
jrodenburg 8:dbf8bd4815f8 358
jrodenburg 8:dbf8bd4815f8 359
jrodenburg 2:bd118a724f03 360 /* Function: parseRXData
jrodenburg 2:bd118a724f03 361 **************************************************************
jrodenburg 9:1cada1fe4743 362 Description: The parse received data into
jrodenburg 9:1cada1fe4743 363 Receives: chn: The channel we want to put into the channel
jrodenburg 9:1cada1fe4743 364 Returns: N/A
jrodenburg 1:0182b86f9bd4 365 */
jrodenburg 0:a28a1035c31b 366
jrodenburg 8:dbf8bd4815f8 367 void parseRXData()
jrodenburg 8:dbf8bd4815f8 368 {
jrodenburg 8:dbf8bd4815f8 369 HOST_CMD_HEADER *pRxPktHdr;
jrodenburg 2:bd118a724f03 370 string data = "";
jrodenburg 8:dbf8bd4815f8 371 unsigned char *ptemp;
jrodenburg 8:dbf8bd4815f8 372 int i;
jrodenburg 8:dbf8bd4815f8 373
jrodenburg 8:dbf8bd4815f8 374 pRxPktHdr = (HOST_CMD_HEADER *)rxBuf;
jrodenburg 8:dbf8bd4815f8 375
jrodenburg 8:dbf8bd4815f8 376 #ifdef DEBUG
jrodenburg 15:74a01aaeb60e 377 pc.printf("DBG: fl=%02x, len=%02x\n", pRxPktHdr->SOF_flag, pRxPktHdr->len);
jrodenburg 8:dbf8bd4815f8 378 #endif
jrodenburg 8:dbf8bd4815f8 379
jrodenburg 8:dbf8bd4815f8 380 // Exit if the packet does not contain correct header
jrodenburg 8:dbf8bd4815f8 381 // Maybe send NAK?
jrodenburg 8:dbf8bd4815f8 382 if ((pRxPktHdr->SOF_flag != RX_SOF) || (pRxPktHdr->Delim != DELIMETER))
jrodenburg 8:dbf8bd4815f8 383 return;
jrodenburg 2:bd118a724f03 384
jrodenburg 8:dbf8bd4815f8 385 switch (pRxPktHdr->cmd_type)
jrodenburg 8:dbf8bd4815f8 386 {
jrodenburg 8:dbf8bd4815f8 387 case SET_TEMPERATURE:
jrodenburg 8:dbf8bd4815f8 388 // Process set temp for specified channel
jrodenburg 8:dbf8bd4815f8 389 {
jrodenburg 8:dbf8bd4815f8 390 SET_TEMPERATURE_CMD *pRxPkt = (SET_TEMPERATURE_CMD *)(rxBuf);
jrodenburg 15:74a01aaeb60e 391 #ifdef DEBUG
jrodenburg 15:74a01aaeb60e 392 pc.printf("DBG: SETTEMP: ch = %02x, tempSet = %f, chanStat = %02x\n",
jrodenburg 15:74a01aaeb60e 393 pRxPkt->chanID, pRxPkt->setTemp, pRxPkt->chanStat);
jrodenburg 8:dbf8bd4815f8 394 #endif
jrodenburg 8:dbf8bd4815f8 395 if ((pRxPkt->tempDelim != DELIMETER) || (pRxPkt->chanStatDelim != DELIMETER)) {
jrodenburg 8:dbf8bd4815f8 396 // Send NAK back
jrodenburg 8:dbf8bd4815f8 397 pc.printf("DBG: Error\n");
jrodenburg 2:bd118a724f03 398 }
jrodenburg 8:dbf8bd4815f8 399 else {
jrodenburg 8:dbf8bd4815f8 400 chanSel_SetTemp = pRxPkt->chanID;
jrodenburg 8:dbf8bd4815f8 401 chnlStatus[pRxPkt->chanID].status = pRxPkt->chanStat;
jrodenburg 8:dbf8bd4815f8 402 chnlStatus[pRxPkt->chanID].setTemp = pRxPkt->setTemp;
jrodenburg 9:1cada1fe4743 403 chnlStatus[pRxPkt->chanID].error = 0;
jrodenburg 14:69cd53434783 404 chnlStatus[pRxPkt->chanID].heater_init_time = 0;
jrodenburg 8:dbf8bd4815f8 405 newTempSet = true;
jrodenburg 2:bd118a724f03 406 }
jrodenburg 2:bd118a724f03 407 }
jrodenburg 8:dbf8bd4815f8 408 break;
jrodenburg 8:dbf8bd4815f8 409
jrodenburg 8:dbf8bd4815f8 410 case SELECT_CHANNEL:
jrodenburg 8:dbf8bd4815f8 411 // Select channel to send temp data to
jrodenburg 8:dbf8bd4815f8 412 {
jrodenburg 8:dbf8bd4815f8 413 SELECT_CHANNEL_CMD *pRxPkt = (SELECT_CHANNEL_CMD *)(rxBuf);
jrodenburg 8:dbf8bd4815f8 414
jrodenburg 8:dbf8bd4815f8 415 chanSel_SendTemp = pRxPkt->chanIDSel;
jrodenburg 15:74a01aaeb60e 416 #ifdef DEBUG
jrodenburg 15:74a01aaeb60e 417 pc.printf("DBG: CHAN_SEL: chan=%02x, chanStat = %02x\n", pRxPkt->chanIDSel, pRxPkt->chanStat);
jrodenburg 8:dbf8bd4815f8 418 #endif
jrodenburg 2:bd118a724f03 419 }
jrodenburg 8:dbf8bd4815f8 420 break;
jrodenburg 8:dbf8bd4815f8 421
jrodenburg 15:74a01aaeb60e 422 case READ_UID:
jrodenburg 15:74a01aaeb60e 423 {
jrodenburg 15:74a01aaeb60e 424 READ_UID_CMD *pRxPkt = (READ_UID_CMD *)(rxBuf);
jrodenburg 15:74a01aaeb60e 425 #ifdef DEBUG
jrodenburg 15:74a01aaeb60e 426 pc.printf("DBG: Read UID: chan%02x\n", pRxPkt->chanIDSel);
jrodenburg 15:74a01aaeb60e 427 #endif
jrodenburg 15:74a01aaeb60e 428 sendUID();
jrodenburg 15:74a01aaeb60e 429 }
jrodenburg 15:74a01aaeb60e 430 break;
jrodenburg 15:74a01aaeb60e 431
jrodenburg 8:dbf8bd4815f8 432 default:
jrodenburg 8:dbf8bd4815f8 433 // Error
jrodenburg 8:dbf8bd4815f8 434 break;
jrodenburg 2:bd118a724f03 435 }
jrodenburg 1:0182b86f9bd4 436 }
jrodenburg 0:a28a1035c31b 437
jrodenburg 0:a28a1035c31b 438 /* Function: get_temp
jrodenburg 0:a28a1035c31b 439 **************************************************************
jrodenburg 18:029a1283a878 440 Description: Convert A/D count to temperature value
jrodenburg 18:029a1283a878 441 Receives: ADC_val: the count from the A/D reading
jrodenburg 18:029a1283a878 442 Returns: the temp. of the A/D reading
jrodenburg 0:a28a1035c31b 443 */
jrodenburg 0:a28a1035c31b 444
jrodenburg 17:5098d8fbb298 445 float get_temp(float ADC_val){
jrodenburg 1:0182b86f9bd4 446 myled = 1;
jrodenburg 17:5098d8fbb298 447 //ltc2487.setAddress(addrLUT[chn].adc);
jrodenburg 9:1cada1fe4743 448
jrodenburg 17:5098d8fbb298 449 //float ADC_val = ltc2487.readOutput(port); //(65536*1.334)/2.5
jrodenburg 9:1cada1fe4743 450
jrodenburg 0:a28a1035c31b 451 int i = 0;
jrodenburg 9:1cada1fe4743 452
jrodenburg 0:a28a1035c31b 453 while((i < sizeLUT) && (thermLUT[i].adc > ADC_val)){
jrodenburg 9:1cada1fe4743 454 i++;
jrodenburg 0:a28a1035c31b 455 } //find the temp. above therm temp
jrodenburg 9:1cada1fe4743 456
jrodenburg 9:1cada1fe4743 457 //Point slope formula extrapolation:
jrodenburg 14:69cd53434783 458 // m = (y1-y0)/(x1-x0)+ y0 , y = temp. value, x = adc value
jrodenburg 0:a28a1035c31b 459 // y1 = thermLUT[i-1].temp y0 = thermLUT[i].temp
jrodenburg 0:a28a1035c31b 460 // x1 = thermLUT[i-1].adc x0 =thermLUT[i].adc
jrodenburg 0:a28a1035c31b 461 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 462 float b = float(thermLUT[i-1].adc - thermLUT[i].adc); //slope of adc between points where therm adc is between (Amax - Amin)
jrodenburg 9:1cada1fe4743 463
jrodenburg 0:a28a1035c31b 464 float m = a/b;
jrodenburg 0:a28a1035c31b 465 float y = (m*(ADC_val-thermLUT[i].adc))+thermLUT[i].temp;
jrodenburg 9:1cada1fe4743 466
jrodenburg 17:5098d8fbb298 467 if(DEBUG3) pc.printf("DBG: ADC VAL: %f TEMP: %f \r\n", ADC_val, y);
jrodenburg 9:1cada1fe4743 468
jrodenburg 9:1cada1fe4743 469 return y;
jrodenburg 0:a28a1035c31b 470 }
jrodenburg 0:a28a1035c31b 471
jrodenburg 18:029a1283a878 472 /* Function: get_temp_DATA
jrodenburg 18:029a1283a878 473 **************************************************************
jrodenburg 18:029a1283a878 474 Description: Read A/D data from LTC2487 and set array with front and back temp.
jrodenburg 18:029a1283a878 475 Receives: N/A
jrodenburg 18:029a1283a878 476 Returns: N/A
jrodenburg 18:029a1283a878 477 */
jrodenburg 18:029a1283a878 478
jrodenburg 17:5098d8fbb298 479 void get_temp_data(){
jrodenburg 17:5098d8fbb298 480 //Write to all 8 channels selecting the front port to read
jrodenburg 17:5098d8fbb298 481 for(int chnl = 0; chnl < CHN_COUNT; chnl++){
jrodenburg 17:5098d8fbb298 482 ltc2487.setAddress(addrLUT[chnl].adc);
jrodenburg 17:5098d8fbb298 483 ltc2487.writePort(FRONT_THERM);
jrodenburg 17:5098d8fbb298 484 }
jrodenburg 17:5098d8fbb298 485 //wait until next clock cycle on LTC
jrodenburg 17:5098d8fbb298 486 wait(0.08);
jrodenburg 17:5098d8fbb298 487 for(int chnl = 0; chnl < CHN_COUNT; chnl++){
jrodenburg 17:5098d8fbb298 488 ltc2487.setAddress(addrLUT[chnl].adc);
jrodenburg 17:5098d8fbb298 489 channelTempData[chnl].currTempFront = get_temp(ltc2487.read());
jrodenburg 17:5098d8fbb298 490 }
jrodenburg 18:029a1283a878 491 //wait until next clock cycle on LTC
jrodenburg 18:029a1283a878 492 wait(0.08);
jrodenburg 17:5098d8fbb298 493 for(int chnl = 0; chnl < CHN_COUNT; chnl++){
jrodenburg 17:5098d8fbb298 494 ltc2487.setAddress(addrLUT[chnl].adc);
jrodenburg 17:5098d8fbb298 495 ltc2487.writePort(BACK_THERM);
jrodenburg 17:5098d8fbb298 496 }
jrodenburg 17:5098d8fbb298 497 //wait until next clock cycle on LTC
jrodenburg 17:5098d8fbb298 498 wait(0.08);
jrodenburg 17:5098d8fbb298 499 for(int chnl = 0; chnl < CHN_COUNT; chnl++){
jrodenburg 17:5098d8fbb298 500 ltc2487.setAddress(addrLUT[chnl].adc);
jrodenburg 17:5098d8fbb298 501 channelTempData[chnl].currTempBack = get_temp(ltc2487.read());
jrodenburg 17:5098d8fbb298 502 }
jrodenburg 17:5098d8fbb298 503 }
jrodenburg 17:5098d8fbb298 504
jrodenburg 0:a28a1035c31b 505 /* Function: get_heater_current
jrodenburg 0:a28a1035c31b 506 **************************************************************
jrodenburg 0:a28a1035c31b 507 Description: Retrieve current into heater control MOSFET
jrodenburg 9:1cada1fe4743 508 Receives: chn: the channel of the fixture to read current from
jrodenburg 0:a28a1035c31b 509 Returns: the current into the heater control MOSFET
jrodenburg 0:a28a1035c31b 510 */
jrodenburg 0:a28a1035c31b 511
jrodenburg 6:c980535393ed 512 float get_heater_current(int chn, int port){
jrodenburg 18:029a1283a878 513 ltc2487.setAddress(addrLUT[chn].adc);
jrodenburg 18:029a1283a878 514 ltc2487.writePort(HEAT_FET_AMP);
jrodenburg 18:029a1283a878 515 wait(0.08);
jrodenburg 18:029a1283a878 516 ltc2487.read();
jrodenburg 18:029a1283a878 517 wait(0.08);
jrodenburg 0:a28a1035c31b 518 }
jrodenburg 0:a28a1035c31b 519
jrodenburg 0:a28a1035c31b 520 /* Function: get_valve_current
jrodenburg 0:a28a1035c31b 521 **************************************************************
jrodenburg 0:a28a1035c31b 522 Description: Retrieve current into valve control MOSFET
jrodenburg 9:1cada1fe4743 523 Receives: chn: the channel of the fixture to read current from
jrodenburg 0:a28a1035c31b 524 Returns: the current into the valve control MOSFET
jrodenburg 0:a28a1035c31b 525 */
jrodenburg 0:a28a1035c31b 526
jrodenburg 6:c980535393ed 527 float get_valve_current(int chn, int port){
jrodenburg 18:029a1283a878 528 ltc2487.setAddress(addrLUT[chn].adc);
jrodenburg 18:029a1283a878 529 ltc2487.writePort(VALVE_FET_AMP);
jrodenburg 18:029a1283a878 530 wait(0.08);
jrodenburg 18:029a1283a878 531 ltc2487.read();
jrodenburg 18:029a1283a878 532 wait(0.08);
jrodenburg 0:a28a1035c31b 533 }
jrodenburg 0:a28a1035c31b 534
jrodenburg 0:a28a1035c31b 535 /* Function: turn_valve_on
jrodenburg 0:a28a1035c31b 536 **************************************************************
jrodenburg 1:0182b86f9bd4 537 Description: Turn valve on and green status LED on
jrodenburg 9:1cada1fe4743 538 Receives: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 539 Returns: N/A
jrodenburg 0:a28a1035c31b 540 */
jrodenburg 0:a28a1035c31b 541
jrodenburg 1:0182b86f9bd4 542 void turn_valve_on(int chn){
jrodenburg 0:a28a1035c31b 543 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 544 io_control.init();
jrodenburg 1:0182b86f9bd4 545 io_control.writeOutput(1,0,1,0);
jrodenburg 0:a28a1035c31b 546 }
jrodenburg 0:a28a1035c31b 547
jrodenburg 0:a28a1035c31b 548 /* Function: turn_valve_off
jrodenburg 0:a28a1035c31b 549 **************************************************************
jrodenburg 1:0182b86f9bd4 550 Description: Turn valve off and green status LED on
jrodenburg 9:1cada1fe4743 551 Receives: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 552 Returns: N/A
jrodenburg 0:a28a1035c31b 553 */
jrodenburg 0:a28a1035c31b 554
jrodenburg 1:0182b86f9bd4 555 void turn_valve_off(int chn){
jrodenburg 0:a28a1035c31b 556 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 557 io_control.init();
jrodenburg 1:0182b86f9bd4 558 io_control.writeOutput(0,0,1,0);
jrodenburg 0:a28a1035c31b 559 }
jrodenburg 0:a28a1035c31b 560
jrodenburg 0:a28a1035c31b 561 /* Function: turn_heater_on
jrodenburg 0:a28a1035c31b 562 **************************************************************
jrodenburg 1:0182b86f9bd4 563 Description: Turn heater on and green status LED on
jrodenburg 9:1cada1fe4743 564 Receives: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 565 Returns: N/A
jrodenburg 0:a28a1035c31b 566 */
jrodenburg 0:a28a1035c31b 567
jrodenburg 1:0182b86f9bd4 568 void turn_heater_on(int chn){
jrodenburg 0:a28a1035c31b 569 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 570 io_control.init();
jrodenburg 1:0182b86f9bd4 571 io_control.writeOutput(0,1,1,0);
jrodenburg 0:a28a1035c31b 572 }
jrodenburg 0:a28a1035c31b 573
jrodenburg 0:a28a1035c31b 574 /* Function: turn_heater_off
jrodenburg 0:a28a1035c31b 575 **************************************************************
jrodenburg 1:0182b86f9bd4 576 Description: Turn heater off and green status LED on
jrodenburg 9:1cada1fe4743 577 Receives: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 578 Returns: N/A
jrodenburg 0:a28a1035c31b 579 */
jrodenburg 0:a28a1035c31b 580
jrodenburg 1:0182b86f9bd4 581 void turn_heater_off(int chn){
jrodenburg 0:a28a1035c31b 582 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 583 io_control.init();
jrodenburg 1:0182b86f9bd4 584 io_control.writeOutput(0,0,1,0);
jrodenburg 0:a28a1035c31b 585 }
jrodenburg 0:a28a1035c31b 586
jrodenburg 0:a28a1035c31b 587 /* Function: status_led
jrodenburg 0:a28a1035c31b 588 **************************************************************
jrodenburg 0:a28a1035c31b 589 Description: Turn status LED on (turns on green or red)
jrodenburg 9:1cada1fe4743 590 Receives: chn: the channel of the fixture
jrodenburg 0:a28a1035c31b 591 status: the status of channel (good (1) or bad (0))
jrodenburg 1:0182b86f9bd4 592 Returns: N/A
jrodenburg 0:a28a1035c31b 593 */
jrodenburg 0:a28a1035c31b 594
jrodenburg 1:0182b86f9bd4 595 void status_led(int chn, int status){
jrodenburg 0:a28a1035c31b 596 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 597 if(status){
jrodenburg 1:0182b86f9bd4 598 io_control.writeOutput(0,0,1,0);
jrodenburg 0:a28a1035c31b 599 }
jrodenburg 0:a28a1035c31b 600 else{
jrodenburg 8:dbf8bd4815f8 601 //turn valve on too
jrodenburg 8:dbf8bd4815f8 602 io_control.writeOutput(1,0,0,1);
jrodenburg 0:a28a1035c31b 603 }
jrodenburg 0:a28a1035c31b 604 }
jrodenburg 0:a28a1035c31b 605
jrodenburg 1:0182b86f9bd4 606 /* Function: test_mcp23008
jrodenburg 1:0182b86f9bd4 607 **************************************************************
jrodenburg 1:0182b86f9bd4 608 Description: Test each output of the MCP23009
jrodenburg 9:1cada1fe4743 609 Receives: N/A
jrodenburg 1:0182b86f9bd4 610 Returns: N/A
jrodenburg 1:0182b86f9bd4 611 */
jrodenburg 1:0182b86f9bd4 612
jrodenburg 1:0182b86f9bd4 613 void test_mcp23008(int chn){
jrodenburg 1:0182b86f9bd4 614 turn_valve_on(chn);
jrodenburg 7:8a5e65e63e2a 615 wait(0.5);
jrodenburg 1:0182b86f9bd4 616 turn_valve_off(chn);
jrodenburg 7:8a5e65e63e2a 617 wait(0.5);
jrodenburg 1:0182b86f9bd4 618 turn_heater_on(chn);
jrodenburg 7:8a5e65e63e2a 619 wait(0.5);
jrodenburg 1:0182b86f9bd4 620 turn_heater_off(chn);
jrodenburg 7:8a5e65e63e2a 621 wait(0.5);
jrodenburg 1:0182b86f9bd4 622 status_led(chn, 0);
jrodenburg 7:8a5e65e63e2a 623 wait(0.5);
jrodenburg 1:0182b86f9bd4 624 status_led(chn, 1);
jrodenburg 1:0182b86f9bd4 625 }
jrodenburg 1:0182b86f9bd4 626
jrodenburg 2:bd118a724f03 627 /* Function: test_ltc2487
jrodenburg 2:bd118a724f03 628 **************************************************************
jrodenburg 2:bd118a724f03 629 Description: Test the reading from LTC2487
jrodenburg 9:1cada1fe4743 630 Receives: N/A
jrodenburg 2:bd118a724f03 631 Returns: N/A
jrodenburg 2:bd118a724f03 632 */
jrodenburg 2:bd118a724f03 633
jrodenburg 18:029a1283a878 634 void test_ltc2487(){
jrodenburg 18:029a1283a878 635 for(int chnl = 0; chnl < CHN_COUNT; chnl++){
jrodenburg 18:029a1283a878 636 float frontTemp = channelTempData[chnl].currTempFront;
jrodenburg 18:029a1283a878 637 float backTemp = channelTempData[chnl].currTempBack;
jrodenburg 18:029a1283a878 638 pc.printf("TEMPERATURE READING [%i]:: BACK: %f FRONT: %f \r\n", chnl, backTemp, frontTemp);
jrodenburg 18:029a1283a878 639 }
jrodenburg 2:bd118a724f03 640 }
jrodenburg 2:bd118a724f03 641
jrodenburg 8:dbf8bd4815f8 642 //***************************************************************
jrodenburg 8:dbf8bd4815f8 643 // Build packet with temperature readings to send to GUI
jrodenburg 8:dbf8bd4815f8 644 //***************************************************************
jrodenburg 17:5098d8fbb298 645 void sendTempReadings(){
jrodenburg 16:a7fd53ab4199 646 RESPONSE_TEMP_CMD response;
jrodenburg 8:dbf8bd4815f8 647 unsigned char *ptr = (unsigned char *)&response;
jrodenburg 8:dbf8bd4815f8 648 int i;
jrodenburg 8:dbf8bd4815f8 649
jrodenburg 8:dbf8bd4815f8 650 response.SOF_flag = TX_SOF;
jrodenburg 15:74a01aaeb60e 651 response.cmd_type = TEMP_DATA;
jrodenburg 16:a7fd53ab4199 652 response.len = 5+(sizeof(response.chTemp));
jrodenburg 8:dbf8bd4815f8 653 response.Delim = DELIMETER;
jrodenburg 16:a7fd53ab4199 654 response.chTemp[0] = channelTempData[0].currTempBack;
jrodenburg 16:a7fd53ab4199 655 response.chTemp[1] = channelTempData[1].currTempBack;
jrodenburg 16:a7fd53ab4199 656 response.chTemp[2] = channelTempData[2].currTempBack;
jrodenburg 16:a7fd53ab4199 657 response.chTemp[3] = channelTempData[3].currTempBack;
jrodenburg 16:a7fd53ab4199 658 response.chTemp[4] = channelTempData[4].currTempBack;
jrodenburg 16:a7fd53ab4199 659 response.chTemp[5] = channelTempData[5].currTempBack;
jrodenburg 16:a7fd53ab4199 660 response.chTemp[6] = channelTempData[6].currTempBack;
jrodenburg 16:a7fd53ab4199 661 response.chTemp[7] = channelTempData[7].currTempBack;
jrodenburg 16:a7fd53ab4199 662 /*float *dst = (float *)&response.chTemp[0];
jrodenburg 16:a7fd53ab4199 663 float *src = (float *)&channelTempData[0];
jrodenburg 16:a7fd53ab4199 664 memcpy(dst, src, sizeof(channelTempData));*/
jrodenburg 8:dbf8bd4815f8 665 response.EOF_flag = TX_EOF;
jrodenburg 8:dbf8bd4815f8 666
jrodenburg 8:dbf8bd4815f8 667 // Send response to GUI
jrodenburg 8:dbf8bd4815f8 668 for (i=0; i < response.len; i++, ptr++)
jrodenburg 8:dbf8bd4815f8 669 pc.printf("%02x", *ptr);
jrodenburg 8:dbf8bd4815f8 670 pc.printf("\n");
jrodenburg 8:dbf8bd4815f8 671 }
jrodenburg 8:dbf8bd4815f8 672
jrodenburg 9:1cada1fe4743 673 //***************************************************************
jrodenburg 15:74a01aaeb60e 674 // Build packet with Board UID (Unique Identification)
jrodenburg 15:74a01aaeb60e 675 //***************************************************************
jrodenburg 15:74a01aaeb60e 676 void sendUID ()
jrodenburg 15:74a01aaeb60e 677 {
jrodenburg 15:74a01aaeb60e 678 UID_RESPONSE response;
jrodenburg 15:74a01aaeb60e 679 unsigned char *ptr = (unsigned char *)&response;
jrodenburg 15:74a01aaeb60e 680 int i;
jrodenburg 15:74a01aaeb60e 681
jrodenburg 15:74a01aaeb60e 682 response.SOF_flag = TX_SOF;
jrodenburg 15:74a01aaeb60e 683 response.cmd_type = UID_DATA;
jrodenburg 15:74a01aaeb60e 684 response.len = 17;
jrodenburg 15:74a01aaeb60e 685 response.Delim = DELIMETER;
jrodenburg 15:74a01aaeb60e 686 response.UIDMH = (uint32_t)SIM->UIDMH;
jrodenburg 15:74a01aaeb60e 687 response.UIDML = (uint32_t)SIM->UIDML;
jrodenburg 15:74a01aaeb60e 688 response.UIDL = (uint32_t)SIM->UIDL;
jrodenburg 15:74a01aaeb60e 689 response.EOF_flag = TX_EOF;
jrodenburg 15:74a01aaeb60e 690
jrodenburg 15:74a01aaeb60e 691 // Send response to GUI
jrodenburg 15:74a01aaeb60e 692 for (i=0; i < response.len; i++, ptr++)
jrodenburg 15:74a01aaeb60e 693 pc.printf("%02x", *ptr);
jrodenburg 15:74a01aaeb60e 694 pc.printf("\n");
jrodenburg 15:74a01aaeb60e 695 }
jrodenburg 15:74a01aaeb60e 696
jrodenburg 15:74a01aaeb60e 697 //***************************************************************
jrodenburg 9:1cada1fe4743 698 // Build packet with errors to send to GUI
jrodenburg 9:1cada1fe4743 699 //***************************************************************
jrodenburg 9:1cada1fe4743 700 void sendError (int chan, float error)
jrodenburg 9:1cada1fe4743 701 {
jrodenburg 9:1cada1fe4743 702 RESPONSE_CMD response;
jrodenburg 9:1cada1fe4743 703 unsigned char *ptr = (unsigned char *)&response;
jrodenburg 9:1cada1fe4743 704 int i;
jrodenburg 9:1cada1fe4743 705
jrodenburg 9:1cada1fe4743 706 response.SOF_flag = TX_SOF;
jrodenburg 9:1cada1fe4743 707 response.cmd_type = ERROR_DATA;
jrodenburg 9:1cada1fe4743 708 response.len = 9;
jrodenburg 9:1cada1fe4743 709 response.Delim = DELIMETER;
jrodenburg 9:1cada1fe4743 710 response.data = (float)error;
jrodenburg 9:1cada1fe4743 711 response.EOF_flag = TX_EOF;
jrodenburg 9:1cada1fe4743 712
jrodenburg 9:1cada1fe4743 713 // Send response to GUI
jrodenburg 9:1cada1fe4743 714 for (i=0; i < response.len; i++, ptr++)
jrodenburg 9:1cada1fe4743 715 pc.printf("%02x", *ptr);
jrodenburg 9:1cada1fe4743 716 pc.printf("\n");
jrodenburg 9:1cada1fe4743 717 }
jrodenburg 6:c980535393ed 718
jrodenburg 14:69cd53434783 719 /* Function: error_check
jrodenburg 14:69cd53434783 720 **************************************************************
jrodenburg 14:69cd53434783 721 Description: Checks for any system errors
jrodenburg 14:69cd53434783 722 Recieves: frontTemp: temp. of front thermistor
jrodenburg 14:69cd53434783 723 backTemp: temp. of back thermistor
jrodenburg 14:69cd53434783 724 currTimeMin: currentTime in minutes
jrodenburg 14:69cd53434783 725 Returns: N/A
jrodenburg 14:69cd53434783 726 */
jrodenburg 14:69cd53434783 727
jrodenburg 14:69cd53434783 728 void error_check(int chnl, float currentTempFront, float currentTempBack, int currTimeMin){
jrodenburg 14:69cd53434783 729 if((currentTempFront >= MAX_TEMP) && (currentTempBack >= MAX_TEMP)){
jrodenburg 14:69cd53434783 730 status_led(chnl, STATUS_BAD);
jrodenburg 14:69cd53434783 731 sendError(chnl, 1);
jrodenburg 14:69cd53434783 732 chnlStatus[chnl].error = 1;
jrodenburg 14:69cd53434783 733 if(DEBUG3) pc.printf("DBG: [%d] ERROR 1 \r\n", chnl);
jrodenburg 14:69cd53434783 734 }
jrodenburg 14:69cd53434783 735 if(((currentTempFront == 0) || (currentTempBack == 0))){
jrodenburg 14:69cd53434783 736 status_led(chnl, STATUS_BAD);
jrodenburg 14:69cd53434783 737 sendError(chnl, 2);
jrodenburg 14:69cd53434783 738 chnlStatus[chnl].error = 1;
jrodenburg 14:69cd53434783 739 if(DEBUG3) pc.printf("DBG: [%d] ERROR 2 \r\n", chnl);
jrodenburg 14:69cd53434783 740 }
jrodenburg 14:69cd53434783 741 if((abs(currentTempBack - currentTempFront) >= TEMP_MARGIN)){
jrodenburg 14:69cd53434783 742 status_led(chnl, STATUS_BAD);
jrodenburg 14:69cd53434783 743 sendError(chnl, 3);
jrodenburg 14:69cd53434783 744 chnlStatus[chnl].error = 1;
jrodenburg 14:69cd53434783 745 if(DEBUG3) pc.printf("DBG: [%d] ERROR 3 \r\n", chnl);
jrodenburg 14:69cd53434783 746 }
jrodenburg 14:69cd53434783 747 if(((currentTempFront <= MIN_TEMP) && (currentTempBack <= MIN_TEMP))){
jrodenburg 14:69cd53434783 748 status_led(chnl, STATUS_BAD);
jrodenburg 14:69cd53434783 749 sendError(chnl, 4);
jrodenburg 14:69cd53434783 750 chnlStatus[chnl].error = 1;
jrodenburg 14:69cd53434783 751 if(DEBUG3) pc.printf("DBG: [%d] ERROR 4 \r\n", chnl);
jrodenburg 14:69cd53434783 752 }
jrodenburg 14:69cd53434783 753 if(chnlStatus[chnl].heater_init_time != 0){
jrodenburg 14:69cd53434783 754 int init_time = chnlStatus[chnl].heater_init_time;
jrodenburg 15:74a01aaeb60e 755 int on_time_heater = currTimeMin - init_time;
jrodenburg 14:69cd53434783 756 //account for 0 crossover
jrodenburg 14:69cd53434783 757 if(init_time > currTimeMin){
jrodenburg 14:69cd53434783 758 on_time_heater = (60 - init_time)+currTimeMin;
jrodenburg 14:69cd53434783 759 }
jrodenburg 14:69cd53434783 760
jrodenburg 14:69cd53434783 761 if(on_time_heater > MAX_HEATER_ON_TIME){
jrodenburg 14:69cd53434783 762 status_led(chnl, STATUS_BAD);
jrodenburg 14:69cd53434783 763 sendError(chnl, 5);
jrodenburg 14:69cd53434783 764 chnlStatus[chnl].error = 1;
jrodenburg 14:69cd53434783 765 chnlStatus[chnl].heater_init_time = 0;
jrodenburg 14:69cd53434783 766 if(DEBUG3) pc.printf("DBG: [%d] ERROR 5 \r\n", chnl);
jrodenburg 14:69cd53434783 767 }
jrodenburg 14:69cd53434783 768 }
jrodenburg 14:69cd53434783 769 if(chnlStatus[chnl].error == 1){
jrodenburg 14:69cd53434783 770 status_led(chnl, STATUS_BAD);
jrodenburg 14:69cd53434783 771 chnlStatus[chnl].error = 1;
jrodenburg 14:69cd53434783 772 }
jrodenburg 14:69cd53434783 773 }
jrodenburg 14:69cd53434783 774
jrodenburg 6:c980535393ed 775
jrodenburg 6:c980535393ed 776 /*************************************************************/
jrodenburg 6:c980535393ed 777 /* MAIN FUNCTION */
jrodenburg 6:c980535393ed 778 /*************************************************************/
jrodenburg 0:a28a1035c31b 779
jrodenburg 9:1cada1fe4743 780 int main() {
jrodenburg 14:69cd53434783 781 //variables for controlling board
jrodenburg 9:1cada1fe4743 782 Timer t;
jrodenburg 14:69cd53434783 783 Timer t_cool;
jrodenburg 14:69cd53434783 784 int time_min = 0;
jrodenburg 14:69cd53434783 785 int init_heat_time = 0;
jrodenburg 15:74a01aaeb60e 786 uint32_t UIDMH, UIDML, UIDL;
jrodenburg 8:dbf8bd4815f8 787
jrodenburg 8:dbf8bd4815f8 788 // Setup serial port
jrodenburg 8:dbf8bd4815f8 789 // Look for RX_EOF
jrodenburg 8:dbf8bd4815f8 790 pc.baud(9600);
jrodenburg 8:dbf8bd4815f8 791 pc.autoDetectChar(RX_EOF);
jrodenburg 8:dbf8bd4815f8 792 pc.attach(&rxInterrupt, MODSERIAL::RxAutoDetect);
jrodenburg 8:dbf8bd4815f8 793
jrodenburg 8:dbf8bd4815f8 794 myled = 1;
jrodenburg 9:1cada1fe4743 795 rLed = 1;
jrodenburg 9:1cada1fe4743 796 gLed = 0;
jrodenburg 9:1cada1fe4743 797
jrodenburg 8:dbf8bd4815f8 798 t.start();
jrodenburg 9:1cada1fe4743 799
jrodenburg 15:74a01aaeb60e 800 sendUID();
jrodenburg 15:74a01aaeb60e 801
jrodenburg 15:74a01aaeb60e 802 UIDMH = (uint32_t)SIM->UIDMH;
jrodenburg 15:74a01aaeb60e 803 UIDML = (uint32_t)SIM->UIDML;
jrodenburg 15:74a01aaeb60e 804 UIDL = (uint32_t)SIM->UIDL;
jrodenburg 15:74a01aaeb60e 805
jrodenburg 0:a28a1035c31b 806 while(1) {
jrodenburg 17:5098d8fbb298 807 //test_mcp23008(0);
jrodenburg 19:fec49ef9944b 808 if(UID_PRINT) {
jrodenburg 15:74a01aaeb60e 809 pc.printf("DBG: <%f>[UID: %04X%08X%08X] PSTARTED\n", t.read(), UIDMH, UIDML, UIDL);
jrodenburg 15:74a01aaeb60e 810 }
jrodenburg 9:1cada1fe4743 811
jrodenburg 16:a7fd53ab4199 812 //SETUP TIMER USED TO TRACK HOW LONG THE HEATER IS ON
jrodenburg 14:69cd53434783 813 float time_sec = t.read();
jrodenburg 14:69cd53434783 814
jrodenburg 13:604e6933366f 815 if(time_sec >= 60){
jrodenburg 14:69cd53434783 816 time_min++;
jrodenburg 14:69cd53434783 817 //time_sec = 0;
jrodenburg 13:604e6933366f 818 t.reset();
jrodenburg 13:604e6933366f 819 }
jrodenburg 14:69cd53434783 820
jrodenburg 14:69cd53434783 821 if(time_min >= 60){
jrodenburg 14:69cd53434783 822 time_min = 0;
jrodenburg 14:69cd53434783 823 }
jrodenburg 17:5098d8fbb298 824
jrodenburg 17:5098d8fbb298 825 //GET TEMPERATURE DATA
jrodenburg 17:5098d8fbb298 826 get_temp_data();
jrodenburg 14:69cd53434783 827
jrodenburg 16:a7fd53ab4199 828 //SEND TEMPERATURE DATA FOR LOGGING
jrodenburg 16:a7fd53ab4199 829 sendTempReadings();
jrodenburg 17:5098d8fbb298 830
jrodenburg 17:5098d8fbb298 831 time_sec = t.read();
jrodenburg 20:8c79659c71e4 832 if(LOOP_TIME) pc.printf("\r TIME: %f s\r\n", time_sec);
jrodenburg 13:604e6933366f 833
jrodenburg 16:a7fd53ab4199 834 //CONTROL LOOP: LOOPS THROUGH EVERY CHANNEL AND CONTROLS THE FIXTURE IN RESPONSE
jrodenburg 5:0f38a0bd4f86 835 for(int chnl = 0; chnl < CHN_COUNT; chnl++){
jrodenburg 9:1cada1fe4743 836
jrodenburg 8:dbf8bd4815f8 837 //check if we received data/need to update TCTF data
jrodenburg 8:dbf8bd4815f8 838 if(dataReceived){
jrodenburg 8:dbf8bd4815f8 839 dataReceived = false;
jrodenburg 15:74a01aaeb60e 840 #ifdef DEBUG
jrodenburg 9:1cada1fe4743 841 pc.printf("DBG: %d bytes Rx'd\n", pc.rxBufferGetCount());
jrodenburg 15:74a01aaeb60e 842 #endif
jrodenburg 8:dbf8bd4815f8 843 pc.move(rxBuf, 50);
jrodenburg 8:dbf8bd4815f8 844 parseRXData();
jrodenburg 8:dbf8bd4815f8 845 pc.rxBufferFlush();
jrodenburg 9:1cada1fe4743 846 }
jrodenburg 8:dbf8bd4815f8 847
jrodenburg 17:5098d8fbb298 848 //update channel temperature data
jrodenburg 17:5098d8fbb298 849 float currentTempFront = channelTempData[chnl].currTempFront;
jrodenburg 17:5098d8fbb298 850 float currentTempBack = channelTempData[chnl].currTempBack;
jrodenburg 17:5098d8fbb298 851
jrodenburg 17:5098d8fbb298 852 //float currentTempBack = get_temp(chnl, BACK_THERM);
jrodenburg 13:604e6933366f 853 float currentTemp = currentTempBack;
jrodenburg 13:604e6933366f 854
jrodenburg 14:69cd53434783 855 //Error check on fixture
jrodenburg 14:69cd53434783 856 error_check(chnl, currentTempFront, currentTempBack, time_min);
jrodenburg 9:1cada1fe4743 857
jrodenburg 19:fec49ef9944b 858 if(TEMP_PRINT) pc.printf("DBG: TEMPERATURE: [%d] Temp: F: %f B: %f\r\n", chnl, currentTempFront, currentTempBack);
jrodenburg 16:a7fd53ab4199 859
jrodenburg 2:bd118a724f03 860 //CONTROL LOOP:
jrodenburg 4:168a446bd0da 861 if(chnlStatus[chnl].status == 1){
jrodenburg 15:74a01aaeb60e 862 if(DEBUG) pc.printf("DBG: [%d] Temp: F: %f B: %f\r\n", chnl, currentTempFront, currentTempBack);
jrodenburg 14:69cd53434783 863 //main loop for channels
jrodenburg 7:8a5e65e63e2a 864 if(chnlStatus[chnl].error == 0){
jrodenburg 9:1cada1fe4743 865 if(currentTemp > ((chnlStatus[chnl].setTemp)+HYST_HIGH)){
jrodenburg 16:a7fd53ab4199 866 if(DEBUG) pc.printf("DBG: [%d] Chiller ON \r\n", chnl);
jrodenburg 14:69cd53434783 867 //reset heater on time
jrodenburg 14:69cd53434783 868 chnlStatus[chnl].heater_init_time = 0;
jrodenburg 18:029a1283a878 869 //Turn chiller on
jrodenburg 18:029a1283a878 870 turn_valve_on(chnl);
jrodenburg 7:8a5e65e63e2a 871 }
jrodenburg 8:dbf8bd4815f8 872 else if (currentTemp < ((chnlStatus[chnl].setTemp)-HYST_LOW)){
jrodenburg 16:a7fd53ab4199 873 if(DEBUG) pc.printf("DBG: [%d] Heater ON \r\n", chnl);
jrodenburg 14:69cd53434783 874 //establish starting time for heater
jrodenburg 14:69cd53434783 875 if(chnlStatus[chnl].heater_init_time == 0){ //check if it is the first time that heater has turned on
jrodenburg 14:69cd53434783 876 //make sure time isn't zero, otherwise keeping looping until we are not at 0
jrodenburg 14:69cd53434783 877 if(time_min != 0){
jrodenburg 14:69cd53434783 878 chnlStatus[chnl].heater_init_time = time_min;
jrodenburg 14:69cd53434783 879 }
jrodenburg 14:69cd53434783 880 }
jrodenburg 18:029a1283a878 881 if(DEBUG2) pc.printf("DBG: TIME ON: %d %d %f %d %d\r\n", chnlStatus[chnl].heater_init_time, time_min, time_sec, init_heat_time, (chnlStatus[chnl].heater_init_time == NULL));
jrodenburg 7:8a5e65e63e2a 882 //Turn heater on
jrodenburg 7:8a5e65e63e2a 883 turn_heater_on(chnl);
jrodenburg 7:8a5e65e63e2a 884 }
jrodenburg 7:8a5e65e63e2a 885 else{
jrodenburg 16:a7fd53ab4199 886 if(DEBUG) pc.printf("DBG: [%d] All OFF \r\n", chnl);
jrodenburg 14:69cd53434783 887 //reset heater on time
jrodenburg 14:69cd53434783 888 chnlStatus[chnl].heater_init_time = 0;
jrodenburg 7:8a5e65e63e2a 889 //turn off chiller
jrodenburg 7:8a5e65e63e2a 890 turn_valve_off(chnl);
jrodenburg 9:1cada1fe4743 891 //turn off heater
jrodenburg 7:8a5e65e63e2a 892 turn_heater_off(chnl);
jrodenburg 7:8a5e65e63e2a 893 //turn on green LED status light
jrodenburg 9:1cada1fe4743 894 status_led(chnl, 1);
jrodenburg 9:1cada1fe4743 895 }
jrodenburg 9:1cada1fe4743 896 }
jrodenburg 2:bd118a724f03 897 }
jrodenburg 13:604e6933366f 898 else{
jrodenburg 14:69cd53434783 899 if(chnlStatus[chnl].error == 0){
jrodenburg 15:74a01aaeb60e 900 //turn off chiller
jrodenburg 15:74a01aaeb60e 901 turn_valve_off(chnl);
jrodenburg 15:74a01aaeb60e 902 //turn off heater
jrodenburg 15:74a01aaeb60e 903 turn_heater_off(chnl);
jrodenburg 15:74a01aaeb60e 904 //turn on green LED status light
jrodenburg 15:74a01aaeb60e 905 status_led(chnl, 1);
jrodenburg 17:5098d8fbb298 906 }
jrodenburg 14:69cd53434783 907 else{
jrodenburg 14:69cd53434783 908 status_led(chnl, STATUS_BAD);
jrodenburg 14:69cd53434783 909 }
jrodenburg 14:69cd53434783 910 //reset heater on time
jrodenburg 14:69cd53434783 911 chnlStatus[chnl].heater_init_time = 0;
jrodenburg 13:604e6933366f 912 }
jrodenburg 2:bd118a724f03 913 }
jrodenburg 0:a28a1035c31b 914 }
jrodenburg 9:1cada1fe4743 915 }