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