summaryrefslogtreecommitdiffstats
path: root/firmware/measure_temp.c
blob: fb94aa95c43b3bbddd3089efd9235e7b804902ef (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
/*
 * Xytronic LF-1600
 * Temperature measurement routines
 *
 * Copyright (c) 2015 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 <string.h>


struct meastemp_context {
	fixpt_t prev_feedback;
};


static struct meastemp_context meastemp;


static void meastemp_result_callback(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);
	}
}

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

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

	.averaging_timeout_ms	= 50,

#ifdef HW_LEGACY
	.scale_raw_lo		= 210,
	.scale_raw_hi		= 411,
	.scale_phys_lo		= FLOAT_TO_FIXPT(150.0),
	.scale_phys_hi		= FLOAT_TO_FIXPT(480.0),
#endif
#ifdef HW_SMD
#warning "FIXME: The temperature scaling is not correct for the SMD hardware."
	.scale_raw_lo		= 210,
	.scale_raw_hi		= 411,
	.scale_phys_lo		= FLOAT_TO_FIXPT(150.0),
	.scale_phys_hi		= FLOAT_TO_FIXPT(480.0),
#endif

	.plaus_neglim		= FLOAT_TO_FIXPT(-20.0),
	.plaus_poslim		= FLOAT_TO_FIXPT(500.0),
	.plaus_timeout_ms	= 3000,

	.filter_callback	= NULL,
	.result_callback	= meastemp_result_callback,
};

void meastemp_init(void)
{
	measure_register_channel(MEAS_CHAN_1, &meastemp_config);
}
bues.ch cgit interface