aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/pid.c
blob: 9aa8e9acdee32dc9a7b92d6e33c0287a230f32e1 (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
/*
 * PID controller
 *
 * Copyright (c) 2015-2018 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 "pid.h"
#include "util.h"
#include "debug_uart.h"

#include <string.h>


void pid_reset(struct pid *pid)
{
	pid->prev_e = int_to_fixpt(0);
	pid->integr = int_to_fixpt(0);
}

void pid_set_factors(struct pid *pid, const struct pid_k_set *k)
{
	pid->k = *k;
	pid_reset(pid);
}

fixpt_t pid_run(struct pid *pid, fixpt_t dt, fixpt_t r)
{
	fixpt_t e, de;
	fixpt_t kp, ki, kd;
	fixpt_t p, i, d;
	fixpt_t pid_result;

	/* Calculate the deviation. */
	e = fixpt_sub(pid->setpoint, r);

#if CONF_DEBUG
	debug_report_fixpt(DEBUG_PREFIX2(pid->name, PSTR("e")),
			   &pid->debug_old_e, e);
#endif

	/* P term */
	kp = pid->k.kp;
	p = fixpt_mul(kp, e);
	pid_result = p;

#if CONF_DEBUG
	debug_report_fixpt(DEBUG_PREFIX2(pid->name, PSTR("p")),
			   &pid->debug_old_p, p);
#endif

	/* I term */
	ki = pid->k.ki;
	i = fixpt_add(pid->integr, fixpt_mul(fixpt_mul(ki, e), dt));
	i = clamp(i, pid->i_neglim, pid->i_poslim);
	pid->integr = i;
	pid_result = fixpt_add(pid_result, i);

#if CONF_DEBUG
	debug_report_fixpt(DEBUG_PREFIX2(pid->name, PSTR("i")),
			   &pid->debug_old_i, i);
#endif

	/* D term */
	kd = pid->k.kd;
	de = fixpt_sub(e, pid->prev_e);
	d = fixpt_mul_div(de, kd, dt);
	pid->prev_e = fixpt_div(e, pid->k.d_decay_div);
	pid_result = fixpt_add(pid_result, d);

#if CONF_DEBUG
	debug_report_fixpt(DEBUG_PREFIX2(pid->name, PSTR("d")),
			   &pid->debug_old_d, d);
	debug_report_fixpt(DEBUG_PREFIX2(pid->name, PSTR("pe")),
			   &pid->debug_old_pe, pid->prev_e);
#endif

	pid_result = clamp(pid_result, pid->y_neglim, pid->y_poslim);

	return pid_result;
}

void pid_init(struct pid *pid,
#if CONF_DEBUG
	      const char __flash *name,
#endif
	      const struct pid_k_set *k,
	      fixpt_t i_neglim, fixpt_t i_poslim,
	      fixpt_t y_neglim, fixpt_t y_poslim)
{
#if CONF_DEBUG
	pid->name = name;
#endif
	pid->i_neglim = i_neglim;
	pid->i_poslim = i_poslim;
	pid->y_neglim = y_neglim;
	pid->y_poslim = y_poslim;
	pid_set_factors(pid, k);
}
bues.ch cgit interface