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