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