aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/measure_temp.c
blob: b63e015c1ebfa2d71fceef656d738fd24724cb55 (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
/*
 * Xytronic LF-1600
 * Temperature measurement routines
 *
 * Copyright (c) 2015-2017 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 "measure_temp.h"
#include "measure.h"
#include "timer.h"
#include "scale.h"
#include "controller_temp.h"
#include "controller_current.h"
#include "debug_uart.h"
#include "menu.h"
#include "settings.h"

#include <string.h>


struct meastemp_context {
	fixpt_t prev_feedback;
};


static struct meastemp_context meastemp;


void meastemp_adjust_set(fixpt_t adjustment)
{
#if CONF_ADJ
	measure_adjust_set(MEAS_CHAN_TEMP, adjustment);
	get_settings()->temp_adj = adjustment;
	store_settings();
	menu_request_display_update();
#endif
}

fixpt_t meastemp_adjust_get(void)
{
	return measure_adjust_get(MEAS_CHAN_TEMP);
}

void meastemp_result_handler(fixpt_t measured_phys_value,
			     enum measure_plausibility plaus);

void meastemp_result_handler(fixpt_t measured_phys_value,
			     enum measure_plausibility plaus)
{
	/* Set/reset emergency status. */
	if (plaus == MEAS_PLAUSIBLE)
		contrtemp_set_emerg(false);
	else if (plaus == MEAS_PLAUS_TIMEOUT)
		contrtemp_set_emerg(true);

	/* Set the controller feedback. */
	if (plaus == MEAS_PLAUSIBLE) {
		meastemp.prev_feedback = measured_phys_value;
		contrcurr_set_restricted(
			measured_phys_value < float_to_fixpt(CONTRCURR_RESTRICT_TOTEMP)
		);
		contrtemp_set_feedback(measured_phys_value);
	} else if (plaus == MEAS_NOT_PLAUSIBLE) {
		/* Just run the controller with the previous feedback. */
		contrtemp_set_feedback(meastemp.prev_feedback);
	}
}

extern
const struct measure_config __flash meastemp_config;
const struct measure_config __flash meastemp_config = {
	.name			= "mt",

	.mux			= MEAS_MUX_ADC1,
	.did			= MEAS_DID_ADC1,
	.ps			= MEAS_PS_64,
	.ref			= MEAS_REF_AREF,

	.averaging_timeout_ms	= 50,

#ifdef HW_LEGACY
	.scale_raw_lo		= 209,
	.scale_raw_hi		= 410,
	.scale_phys_lo		= FLOAT_TO_FIXPT(CELSIUS(150)),
	.scale_phys_hi		= FLOAT_TO_FIXPT(CELSIUS(480)),
#endif
#ifdef HW_SMD
	.scale_raw_lo		= 210,
	.scale_raw_hi		= 411,
	.scale_phys_lo		= FLOAT_TO_FIXPT(CELSIUS(150)),
	.scale_phys_hi		= FLOAT_TO_FIXPT(CELSIUS(480)),
#endif

	.plaus_neglim		= FLOAT_TO_FIXPT(CELSIUS(-20)),
	.plaus_poslim		= FLOAT_TO_FIXPT(CELSIUS(500)),
	.plaus_timeout_ms	= 3000,
};

void meastemp_init(void)
{
#if CONF_ADJ
	measure_adjust_set(MEAS_CHAN_TEMP, get_settings()->temp_adj);
#endif
}
bues.ch cgit interface