-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheater.cpp
207 lines (181 loc) · 4.45 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
#include <Arduino.h>
#include "heater.h"
#include "config.h"
#include "PWM.h"
#include "helpers.h"
Heater::Heater(int adc_pin, int pwm_pin)
: _adc_pin(adc_pin),
_pwm_pin(pwm_pin),
_pidcontroller(&_pid_input, &_pid_output, &_pid_setpoint, PID_Kp, PID_Ki, PID_Kd, DIRECT)
{ }
void Heater::init(void)
{
_pidcontroller.Reconfigure(&_pid_input, &_pid_output, &_pid_setpoint);
_pidcontroller.SetMode(AUTOMATIC);
set_error(ERR_NONE);
reset_change();
}
void Heater::increase_temperature(void)
{
switch (_setpoint)
{
case SETPOINT_LOW:
_setpoint = SETPOINT_MID;
break;
case SETPOINT_MID:
_setpoint = SETPOINT_HIGH;
break;
case SETPOINT_HIGH:
break;
}
}
void Heater::decrease_temperature(void)
{
switch (_setpoint)
{
case SETPOINT_LOW:
break;
case SETPOINT_MID:
_setpoint = SETPOINT_LOW;
break;
case SETPOINT_HIGH:
_setpoint = SETPOINT_MID;
break;
}
}
void Heater::toggle_standby(void)
{
_standby = !_standby;
_pidcontroller.Reset();
}
bool Heater::is_in_standby(void) const
{
return _standby;
}
void Heater::set_error(heatererror_t error)
{
if (_error != error)
_status_changed = true;
_error = error;
}
bool Heater::is_in_error(void) const
{
return _error != ERR_NONE;
}
const char* Heater::error_to_text(void) const
{
switch (_error)
{
case ERR_INVALID_MEASUREMENT:
return "Ungueltiger Messwert";
case ERR_NONE:
return "Kein Fehler";
default:
return "<Beschreibung fehlt>";
}
}
bool Heater::has_status_changed(void) const
{
return _status_changed;
}
void Heater::reset_change(void)
{
_status_changed = false;
}
tempsetpoint_t Heater::get_setpoint(void) const
{
return _setpoint;
}
double Heater::get_setpoint_temperature(void) const
{
switch (_setpoint)
{
case SETPOINT_LOW:
return TEMPERATURE_LOW;
case SETPOINT_MID:
return TEMPERATURE_MID;
case SETPOINT_HIGH:
return TEMPERATURE_HIGH;
default:
return 0;
}
}
double Heater::read_current_temperature(void)
{
// Zur Glättung der Temperaturmessung:
// - Mehrfach hintereinander lesen und Mittelwert berechnen
// - Laufenden Mittelwert über mehrere Messungen bilden
uint16_t measurement = 0;
for (uint8_t i = 0; i < MEAS_REPEAT_COUNT; i++)
{
uint16_t read = analogRead(_adc_pin);
if (is_valid_measurement(read))
measurement += analogRead(_adc_pin);
else
set_error(ERR_INVALID_MEASUREMENT);
}
measurement /= MEAS_REPEAT_COUNT;
_prev_measurement = measurement;
// TODO Laufenden Mittelwert bilden
return measurement_to_temperature(measurement);
}
bool Heater::is_valid_measurement(uint16_t measurement) const
{
uint16_t diff =
measurement < _prev_measurement
? _prev_measurement - measurement
: measurement - _prev_measurement;
Serial.print(diff);
Serial.print("|");
Serial.print(measurement);
Serial.print("|");
Serial.print(_prev_measurement);
Serial.print("| ");
return measurement < MEAS_MAX_VALID && diff < MEAS_MAX_JUMP;
}
double Heater::measurement_to_temperature(uint16_t measurement) const
{
return (double)measurement * ADC_GAIN + ADC_OFFSET;
}
void Heater::set_pwm(uint16_t value)
{
pwmWrite(_pwm_pin, value);
}
void Heater::refresh(void)
{
set_pwm(0); // PWM abschalten, um Messen zu können
if (PWM_COOLDOWN_PERIOD > 0)
delay(PWM_COOLDOWN_PERIOD);
double temperature = read_current_temperature();
_pid_setpoint = get_setpoint_temperature();
_pid_input = temperature;
if (is_in_error())
{
Serial.print(temperature);
Serial.print(" - Error ");
Serial.println(error_to_text());
set_pwm(0);
}
else if (!is_in_standby())
{
bool result = _pidcontroller.Compute();
if (is_in_error())
return;
uint16_t pwmvalue = MIN(PWM_MAX_OUTPUT, (uint16_t)(_pid_output * 255.0));
set_pwm(pwmvalue);
if (!result)
Serial.print(" !Compute error! ");
Serial.print(temperature);
Serial.print(" - Setpoint: ");
Serial.print(_pid_setpoint);
Serial.print(" - PID out: ");
Serial.print(_pid_output);
Serial.print(" - PWM: ");
Serial.println(pwmvalue);
}
else
{
Serial.println(temperature);
set_pwm(0);
}
}