-
Notifications
You must be signed in to change notification settings - Fork 17
/
heater.cpp
executable file
·367 lines (269 loc) · 11.5 KB
/
heater.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Header files
extern "C" {
#include <asf.h>
}
#include <string.h>
#include "common.h"
#include "eeprom.h"
#include "heater.h"
// Definitions
#define UPDATE_TEMPERATURE_MILLISECONDS 333
#define HEATER_READ_ADC ADC_MODULE
#define HEATER_READ_ADC_FREQUENCY 200000
#define HEATER_READ_ADC_SAMPLE_SIZE 50
#define IDEAL_HEATER_RESISTANCE_M 245.0
#define IDEAL_HEATER_TEMPERATURE_MEASUREMENT_B -450.0
#define HEATER_VOLTAGE_TO_TEMPERATURE_SCALAR (-IDEAL_HEATER_RESISTANCE_M / IDEAL_HEATER_TEMPERATURE_MEASUREMENT_B * 4)
#define INVALID_HEATER_CALIBRATION_MODE 0
#define MAX_SUPPORTED_HEATER_CALIBRATION_MODE 1
// Heater Pins
#define HEATER_MODE_SELECT_PIN IOPORT_CREATE_PIN(PORTE, 2)
#define HEATER_ENABLE_PIN IOPORT_CREATE_PIN(PORTA, 2)
#define HEATER_READ_POSITIVE_PIN IOPORT_CREATE_PIN(PORTA, 3)
#define HEATER_READ_NEGATIVE_PIN IOPORT_CREATE_PIN(PORTA, 4)
#define RESISTANCE_READ_PIN IOPORT_CREATE_PIN(PORTA, 5)
// Heater ADC
#define HEATER_READ_ADC_CHANNEL ADC_CH0
#define HEATER_READ_POSITIVE_INPUT ADCCH_POS_PIN3
#define HEATER_READ_NEGATIVE_INPUT ADCCH_NEG_PIN4
#define RESISTANCE_READ_INPUT ADCCH_POS_PIN5
// Pin states
#define HEATER_ON IOPORT_PIN_LEVEL_HIGH
#define HEATER_OFF IOPORT_PIN_LEVEL_LOW
#define HEATER_ENABLE IOPORT_PIN_LEVEL_HIGH
#define HEATER_DISABLE IOPORT_PIN_LEVEL_LOW
// Static class variables
bool Heater::isWorking;
// Global variables
float idealTemperature;
float actualTemperature;
adc_config heaterReadAdcController;
adc_channel_config heaterReadAdcChannel;
adc_config resistanceReadAdcController;
adc_channel_config resistanceReadAdcChannel;
uint8_t heaterCalibrationMode;
float heaterTemperatureMeasurementB;
float heaterResistanceM;
// Function prototypes
/*
Name: Get heater value
Purpose: Returns heater's ADC value
*/
int16_t getHeaterValue(bool testConnection) noexcept;
// Supporting function implementation
int16_t getHeaterValue(bool testConnection) noexcept {
// Get heater value
adc_write_configuration(&HEATER_READ_ADC, &heaterReadAdcController);
adcch_write_configuration(&HEATER_READ_ADC, HEATER_READ_ADC_CHANNEL, &heaterReadAdcChannel);
int32_t heaterValue = 0;
uint8_t sampleSize = testConnection ? 1 : HEATER_READ_ADC_SAMPLE_SIZE;
for(uint8_t i = sampleSize; i > 0; --i) {
adc_start_conversion(&HEATER_READ_ADC, HEATER_READ_ADC_CHANNEL);
adc_wait_for_interrupt_flag(&HEATER_READ_ADC, HEATER_READ_ADC_CHANNEL);
heaterValue += adc_get_signed_result(&HEATER_READ_ADC, HEATER_READ_ADC_CHANNEL);
}
// Return heater value
return heaterValue / sampleSize;
}
void Heater::initialize() noexcept {
// Configure heater select and enable pins
ioport_set_pin_dir(HEATER_MODE_SELECT_PIN, IOPORT_DIR_OUTPUT);
ioport_set_pin_dir(HEATER_ENABLE_PIN, IOPORT_DIR_OUTPUT);
ioport_set_pin_level(HEATER_ENABLE_PIN, HEATER_ENABLE);
// Configure heater read pins
ioport_set_pin_dir(HEATER_READ_POSITIVE_PIN, IOPORT_DIR_INPUT);
ioport_set_pin_mode(HEATER_READ_POSITIVE_PIN, IOPORT_MODE_PULLDOWN);
ioport_set_pin_dir(HEATER_READ_NEGATIVE_PIN, IOPORT_DIR_INPUT);
ioport_set_pin_mode(HEATER_READ_NEGATIVE_PIN, IOPORT_MODE_PULLDOWN);
// Configure resistance read pin
ioport_set_pin_dir(RESISTANCE_READ_PIN, IOPORT_DIR_INPUT);
ioport_set_pin_mode(RESISTANCE_READ_PIN, IOPORT_MODE_PULLDOWN);
// Reset
reset();
// Set ADC heater controller to use signed, 12-bit, bandgap refrence, and manual trigger
adc_read_configuration(&HEATER_READ_ADC, &heaterReadAdcController);
adc_set_conversion_parameters(&heaterReadAdcController, ADC_SIGN_ON, ADC_RES_12, ADC_REF_BANDGAP);
adc_set_conversion_trigger(&heaterReadAdcController, ADC_TRIG_MANUAL, ADC_NR_OF_CHANNELS, 0);
adc_set_clock_rate(&heaterReadAdcController, HEATER_READ_ADC_FREQUENCY);
// Set ADC heater channel to use heater read pins as a differential input with 1/2x gain
adcch_read_configuration(&HEATER_READ_ADC, HEATER_READ_ADC_CHANNEL, &heaterReadAdcChannel);
adcch_set_input(&heaterReadAdcChannel, HEATER_READ_POSITIVE_INPUT, HEATER_READ_NEGATIVE_INPUT, 0);
// Set ADC resistance controller to use unsigned, 12-bit, bandgap refrence, and manual trigger
adc_read_configuration(&HEATER_READ_ADC, &resistanceReadAdcController);
adc_set_conversion_parameters(&resistanceReadAdcController, ADC_SIGN_OFF, ADC_RES_12, ADC_REF_BANDGAP);
adc_set_conversion_trigger(&resistanceReadAdcController, ADC_TRIG_MANUAL, ADC_NR_OF_CHANNELS, 0);
adc_set_clock_rate(&resistanceReadAdcController, HEATER_READ_ADC_FREQUENCY);
// Set ADC resistance channel to use resistance read pin as a single input with no gain
adcch_read_configuration(&HEATER_READ_ADC, HEATER_READ_ADC_CHANNEL, &resistanceReadAdcChannel);
adcch_set_input(&resistanceReadAdcChannel, RESISTANCE_READ_INPUT, ADCCH_NEG_NONE, 1);
// Configure update temperature timer
tc_enable(&TEMPERATURE_TIMER);
tc_set_wgm(&TEMPERATURE_TIMER, TC_WG_NORMAL);
tc_write_period(&TEMPERATURE_TIMER, sysclk_get_cpu_hz() / 1024 * UPDATE_TEMPERATURE_MILLISECONDS / 1000);
tc_set_overflow_interrupt_callback(&TEMPERATURE_TIMER, []() noexcept -> void {
// Check if setting the temperature and heater is working
if(idealTemperature != HEATER_OFF_TEMPERATURE && testConnection()) {
// Turn on heater
ioport_set_pin_level(HEATER_MODE_SELECT_PIN, HEATER_ON);
// Wait enough time for heater voltage to stabilize
delayMicroseconds(500);
// Get resistance value
adc_write_configuration(&HEATER_READ_ADC, &resistanceReadAdcController);
adcch_write_configuration(&HEATER_READ_ADC, HEATER_READ_ADC_CHANNEL, &resistanceReadAdcChannel);
uint32_t resistanceValue = 0;
for(uint8_t i = 0; i < HEATER_READ_ADC_SAMPLE_SIZE; ++i) {
adc_start_conversion(&HEATER_READ_ADC, HEATER_READ_ADC_CHANNEL);
adc_wait_for_interrupt_flag(&HEATER_READ_ADC, HEATER_READ_ADC_CHANNEL);
resistanceValue += adc_get_unsigned_result(&HEATER_READ_ADC, HEATER_READ_ADC_CHANNEL);
}
resistanceValue /= HEATER_READ_ADC_SAMPLE_SIZE;
// Check which heater calibration mode was used
switch(heaterCalibrationMode) {
// Heater calibration mode 1
case 1:
default:
// Update actual temperature
actualTemperature = HEATER_VOLTAGE_TO_TEMPERATURE_SCALAR * getHeaterValue(false) / resistanceValue * heaterResistanceM + heaterTemperatureMeasurementB;
}
// Check if temperature has been reached
if(actualTemperature >= idealTemperature)
// Turn heater off
ioport_set_pin_level(HEATER_MODE_SELECT_PIN, HEATER_OFF);
}
// Otherwise
else
// Clear temperature
clearTemperature();
});
tc_write_clock_source(&TEMPERATURE_TIMER, TC_CLKSEL_DIV1024_gc);
}
bool Heater::testConnection() noexcept {
// Prevent updating temperature
tc_set_overflow_interrupt_level(&TEMPERATURE_TIMER, TC_INT_LVL_OFF);
// Check if heater isn't working
if(!(isWorking = getHeaterValue(true) < INT12_MAX / 2))
// Clear temperature
clearTemperature();
// Allow updating temperature
tc_set_overflow_interrupt_level(&TEMPERATURE_TIMER, TC_INT_LVL_LO);
// Return if heater is working
return isWorking;
}
bool Heater::setTemperature(uint16_t value, bool wait) noexcept {
// Set if heater is working
testConnection();
// Check if heater calibration mode isn't supported
if(!updateHeaterChanges(false))
// Return false
return false;
// Check if heating
if((idealTemperature = value) != HEATER_OFF_TEMPERATURE) {
// Set if newer temperature is lower
bool lowerNewValue = value < getTemperature();
// Turn on or off heater depending on new temperature
ioport_set_pin_level(HEATER_MODE_SELECT_PIN, lowerNewValue ? HEATER_OFF : HEATER_ON);
// Wait until temperature has been reached
while(wait) {
// Break if an emergency stop occured or heater isn't working
if(emergencyStopRequest || !isWorking)
// Break
break;
// Set response to temperature
char buffer[FLOAT_BUFFER_SIZE + sizeof("T:\n") - 1];
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcpy_P(buffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("T:"))));
#else
strcpy(buffer, "T:");
#endif
// Prevent updating temperature
tc_set_overflow_interrupt_level(&TEMPERATURE_TIMER, TC_INT_LVL_OFF);
// Set if done heating
bool doneHeating = ioport_get_pin_output_level(HEATER_MODE_SELECT_PIN) != (lowerNewValue ? HEATER_OFF : HEATER_ON);
// Append correctly looking temperature to response
ftoa(doneHeating && (lowerNewValue ? actualTemperature >= idealTemperature : actualTemperature <= idealTemperature) ? idealTemperature + (lowerNewValue ? -1 : 1) / pow(10, NUMBER_OF_DECIMAL_PLACES) : actualTemperature, &buffer[sizeof("T:") - 1]);
// Allow updating temperature
tc_set_overflow_interrupt_level(&TEMPERATURE_TIMER, TC_INT_LVL_LO);
// Append newline to response
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
strcat_P(buffer, reinterpret_cast<PGM_P>(pgm_read_ptr(PSTR("\n"))));
#else
strcat(buffer, "\n");
#endif
// Send temperature
sendDataToUsb(buffer, true);
// Check if done heating
if(doneHeating)
// Break
break;
// Delay one second or until heater stops working
delayHundredsOfMicroseconds(1 * SECONDS_TO_HUNDREDS_OF_MICROSECONDS_SCALAR, &isWorking);
}
}
// Otherwise
else
// Clear temperature
clearTemperature();
// Return true
return true;
}
float Heater::getTemperature() noexcept {
// Set if heater is working
testConnection();
// Prevent updating temperature
tc_set_overflow_interrupt_level(&TEMPERATURE_TIMER, TC_INT_LVL_OFF);
// Get actual temperature
float value = actualTemperature;
// Allow updating temperature
tc_set_overflow_interrupt_level(&TEMPERATURE_TIMER, TC_INT_LVL_LO);
// Return value
return value;
}
void Heater::clearTemperature() noexcept {
// Prevent updating temperature
tc_set_overflow_interrupt_level(&TEMPERATURE_TIMER, TC_INT_LVL_OFF);
// Clear ideal and actual temperature
idealTemperature = actualTemperature = HEATER_OFF_TEMPERATURE;
// Allow updating temperature
tc_set_overflow_interrupt_level(&TEMPERATURE_TIMER, TC_INT_LVL_LO);
// Turn off heater
ioport_set_pin_level(HEATER_MODE_SELECT_PIN, HEATER_OFF);
}
bool Heater::updateHeaterChanges(bool enableUpdatingTemperature) noexcept {
// Prevent updating temperature
tc_set_overflow_interrupt_level(&TEMPERATURE_TIMER, TC_INT_LVL_OFF);
// Get heater calibration mode
heaterCalibrationMode = nvm_eeprom_read_byte(EEPROM_HEATER_CALIBRATION_MODE_OFFSET);
// Get heater temperature measurement B
nvm_eeprom_read_buffer(EEPROM_HEATER_TEMPERATURE_MEASUREMENT_B_OFFSET, &heaterTemperatureMeasurementB, EEPROM_HEATER_TEMPERATURE_MEASUREMENT_B_LENGTH);
// Get heater resistance M
nvm_eeprom_read_buffer(EEPROM_HEATER_RESISTANCE_M_OFFSET, &heaterResistanceM, EEPROM_HEATER_RESISTANCE_M_LENGTH);
// Check if heater calibration mode isn't supported
if(heaterCalibrationMode == INVALID_HEATER_CALIBRATION_MODE || heaterCalibrationMode > MAX_SUPPORTED_HEATER_CALIBRATION_MODE) {
// Clear temperature
clearTemperature();
// Return false
return false;
}
// Check if enabling updating temperature
if(enableUpdatingTemperature)
// Allow updating temperature
tc_set_overflow_interrupt_level(&TEMPERATURE_TIMER, TC_INT_LVL_LO);
// Return true
return true;
}
void Heater::reset() noexcept {
// Clear temperature
clearTemperature();
}
bool Heater::isOn() noexcept {
// Set if heater is working
testConnection();
// Prevent updating temperature
tc_set_overflow_interrupt_level(&TEMPERATURE_TIMER, TC_INT_LVL_OFF);
// Set if on
bool on = idealTemperature != HEATER_OFF_TEMPERATURE;
// Allow updating temperature
tc_set_overflow_interrupt_level(&TEMPERATURE_TIMER, TC_INT_LVL_LO);
// Return if heater is on
return on;
}