aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/controller_temp.c
blob: 4540229a4f4c819428d6b425316e436e492df22d (plain)
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
/*
 * Xytronic LF-1600
 * Temperature controller
 *
 * Copyright (c) 2015-2016 Michael Buesch <m@bues.ch>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "controller_temp.h"
#include "controller_current.h"
#include "pid.h"
#include "timer.h"
#include "scale.h"
#include "settings.h"
#include "menu.h"
#include "buttons.h"
#include "debug_uart.h"
#include "presets.h"

#include <string.h>


struct temp_contr_context {
	bool enabled;
	bool emergency;

#if CONF_IDLE
	bool idle;
#endif

#if CONF_BOOST
	enum contrtemp_boostmode boost_mode;
#endif

	struct pid pid;
	fixpt_t feedback;
	struct timer dt_timer;

	int24_t old_temp_feedback;
	int24_t old_temp_control1;
	int24_t old_temp_control2;
};

static struct temp_contr_context contrtemp;


enum contrtemp_boostmode contrtemp_get_boost_mode(void)
{
#if CONF_BOOST
	return contrtemp.boost_mode;
#else
	return TEMPBOOST_NORMAL;
#endif
}

static void contrtemp_set_boost_mode(enum contrtemp_boostmode new_boost_mode)
{
	struct pid_k_set *k_set;
	struct settings *settings;

	settings = get_settings();
	k_set = &settings->temp_k[new_boost_mode];

	pid_set_factors(&contrtemp.pid, k_set);
#if CONF_BOOST
	contrtemp.boost_mode = new_boost_mode;
	debug_print_int16(PSTR("tb"), (int16_t)new_boost_mode);
#endif

	menu_request_display_update();
}

bool contrtemp_is_idle(void)
{
#if CONF_IDLE
	return contrtemp.idle;
#else
	return false;
#endif
}

void contrtemp_update_pid_config(void)
{
	/* Setting the currently active boost mode again
	 * will fetch the new PID settings and store
	 * them to the controller.
	 */
	contrtemp_set_boost_mode(contrtemp_get_boost_mode());
}

static fixpt_t temp_to_amps(fixpt_t temp)
{
	fixpt_t current;

	current = rescale(temp,
			  float_to_fixpt(CONTRTEMP_MAP_TEMPLO),
			  float_to_fixpt(CONTRTEMP_MAP_TEMPHI),
			  float_to_fixpt(CONTRTEMP_MAP_CURRLO),
			  float_to_fixpt(CONTRTEMP_MAP_CURRHI));
	current = fixpt_clamp(current,
			      float_to_fixpt(CONTRCURR_NEGLIM),
			      float_to_fixpt(CONTRCURR_POSLIM));

	return current;
}

static void contrtemp_run(fixpt_t r)
{
	fixpt_t dt, y, y_current;
	uint8_t emergency_flags;

	if (!contrtemp.enabled)
		return;
	if (contrtemp.emergency)
		return;

	/* Get delta-t that elapsed since last run, in seconds */
	dt = fixpt_div(int_to_fixpt(timer_ms_since(&contrtemp.dt_timer)),
		       int_to_fixpt(1000));
	timer_set_now(&contrtemp.dt_timer);

	/* Run the PID controller */
	y = pid_run(&contrtemp.pid, dt, r);

	debug_report_fixpt(PSTR("ty1"), &contrtemp.old_temp_control1, y);

	/* Map the requested temperature to a heater current. */
	y_current = temp_to_amps(y);

	emergency_flags = contrcurr_get_emerg();
	if (r > float_to_fixpt(CONTRTEMP_POSLIM)) {
		/* The measured temperature is higher than the maximum.
		 * We need to avoid damage.
		 * Disable current by requesting an emergency in
		 * the current controller.
		 */
		emergency_flags |= CONTRCURR_EMERG_HIGH_TEMP;
		y_current = float_to_fixpt(CONTRCURR_NEGLIM);
	} else {
		emergency_flags &= (uint8_t)~CONTRCURR_EMERG_HIGH_TEMP;
	}
	contrcurr_set_emerg(emergency_flags);

	debug_report_fixpt(PSTR("ty2"), &contrtemp.old_temp_control2,
			   y_current);

	/* Set the current controller setpoint to the requested current. */
	contrcurr_set_setpoint(y_current);
}

void contrtemp_set_feedback(fixpt_t r)
{
	if (r != contrtemp.feedback) {
		contrtemp.feedback = r;
		debug_report_fixpt(PSTR("tr"),
				   &contrtemp.old_temp_feedback, r);
		menu_request_display_update();
	}

	/* Run the controller. */
	contrtemp_run(r);
}

fixpt_t contrtemp_get_feedback(void)
{
	return contrtemp.feedback;
}

void contrtemp_update_setpoint(void)
{
	struct settings *settings;
	fixpt_t temp_setpoint;
	fixpt_t temp_idle_setpoint;

	temp_setpoint = presets_get_active_value();
	settings = get_settings();
	temp_idle_setpoint = settings->temp_idle_setpoint;

	if (contrtemp_is_idle() && (temp_idle_setpoint < temp_setpoint))
		pid_set_setpoint(&contrtemp.pid, temp_idle_setpoint);
	else
		pid_set_setpoint(&contrtemp.pid, temp_setpoint);
}

void contrtemp_set_idle_setpoint(fixpt_t w)
{
	struct settings *settings;

	if (CONF_IDLE) {
		settings = get_settings();
		settings->temp_idle_setpoint = w;
		contrtemp_update_setpoint();
		store_settings();
	}
}

static void do_set_enabled(bool enabled)
{
	contrtemp.enabled = enabled;

	/* Reset the temp controller. */
	pid_reset(&contrtemp.pid);
	timer_set_now(&contrtemp.dt_timer);
	contrtemp_set_boost_mode(TEMPBOOST_NORMAL);
	/* Reset current controller to no-current. */
	contrcurr_set_setpoint(float_to_fixpt(CONTRCURR_NEGLIM));
}

void contrtemp_set_enabled(bool enabled)
{
	if (contrtemp.enabled != enabled)
		do_set_enabled(enabled);
}

static void do_set_emerg(bool emergency)
{
	contrtemp.emergency = emergency;
	if (emergency) {
		/* In an emergency situation, disable the
		 * heater current to avoid damage.
		 */
		contrcurr_set_setpoint(float_to_fixpt(CONTRCURR_NEGLIM));
	}
}

void contrtemp_set_emerg(bool emergency)
{
	if (emergency != contrtemp.emergency)
		do_set_emerg(emergency);
}

bool contrtemp_in_emerg(void)
{
	return contrtemp.emergency;
}

bool contrtemp_is_heating_up(void)
{
	return contrtemp.feedback < pid_get_setpoint(&contrtemp.pid);
}

#if CONF_BOOST
static void contrtemp_boost_button_handler(enum button_id button,
					   enum button_state bstate)
{
	enum contrtemp_boostmode new_boost_mode;

	if (bstate == BSTATE_POSEDGE) {
		new_boost_mode = contrtemp.boost_mode;
		new_boost_mode++;
		if (new_boost_mode >= NR_BOOST_MODES)
			new_boost_mode = 0;

		contrtemp_set_boost_mode(new_boost_mode);
	}
}
#endif

#if CONF_IDLE
static void contrtemp_idle_button_handler(enum button_id button,
					  enum button_state bstate)
{
	bool idle;

	idle = contrtemp.idle;
#if CONF_IDLETOGGLE
	if (bstate == BSTATE_POSEDGE)
		idle = !idle;
#else
	if (bstate == BSTATE_POSEDGE)
		idle = true;
	if (bstate == BSTATE_NEGEDGE)
		idle = false;
#endif

	if (idle != contrtemp.idle) {
		contrtemp.idle = idle;
		contrtemp_update_setpoint();
		menu_request_display_update();
	}
}
#endif

void contrtemp_button_handler(enum button_id button,
			      enum button_state bstate);

void contrtemp_button_handler(enum button_id button,
			      enum button_state bstate)
{
	if (button != BUTTON_IRON)
		return;

#if CONF_BOOST
	contrtemp_boost_button_handler(button, bstate);
#elif CONF_IDLE
	contrtemp_idle_button_handler(button, bstate);
#endif

#if (CONF_BOOST) && (CONF_IDLE)
# error "Cannot simultaneously enable CONF_BOOST and CONF_IDLE"
#endif
}

void contrtemp_init(void)
{
	struct pid_k_set *k_set;
	struct settings *settings;
	fixpt_t temp_setpoint;

	settings = get_settings();

	k_set = &settings->temp_k[TEMPBOOST_NORMAL];
	pid_init(&contrtemp.pid, k_set,
		 float_to_fixpt(CONTRTEMP_NEGLIM_I),
		 float_to_fixpt(CONTRTEMP_POSLIM_I),
		 float_to_fixpt(CONTRTEMP_NEGLIM),
		 float_to_fixpt(CONTRTEMP_POSLIM));
	temp_setpoint = presets_get_active_value();
	pid_set_setpoint(&contrtemp.pid, temp_setpoint);

	/* Enable the controller. */
	do_set_enabled(true);
	do_set_emerg(false);
}
bues.ch cgit interface