block_pid_aw.c 6.23 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2007
	Copyright Stichting C.A. Muller Radioastronomiestation, 2007

	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 3 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, see <http://www.gnu.org/licenses/>.

 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Jeroen Vreeken's avatar
Jeroen Vreeken committed
24
#include <controller/controller_block.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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

/*
       inputs         outputs
       nr name        nr name

     --------------------------
     |                        |
  ---| 0  in          0  out  |----
     |	                      |
  ---| 1  enable      1  outp |----
     |	                      |
     |	              2  outi |----
     |	                      |
     |	              3  outd |----
     --------------------------
     
     Schematic:
     (Same as PID but with additional anti-windup)
     
                     ------                           outp         
          in --------| kp |------------------------------------    
                 |   ------                                   |    
                 |                         /-----\            |    
	         |   ------    /---\       |   - |    outi  /---\  
                 ----| ki |----| + |-------|  /  |----------| + |--- out
	         |   ------    \---/       | -   |  |       \---/  
	         |               |         \-----/  |         |    
	         |               |  -----           |         |
                 |               ---|z-1|------------         |
                 |                  -----                     |
                 |                                            |
	         |       /---\    ------              outd    |
                 --------| - |----| kd |-----------------------
	         |       \---/    ------
                 |         |
                 |  -----  |
                 ---|z-1|---
	            -----
 */

struct controller_block_private {
	float *input0;
	bool *enable;
	float out;
	float outp;
	float outi;
	float outd;
	float kp;	/* K factors */
	float ki;
	float kd;
	float t;	/* sample period */
	float ci;	/* Factors used in calculate (K * T) */
	float cd;
	float previous;
	float maxi;
	float mini;
};

83
static void pid_aw_calculate(struct controller_block *pid)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
84
{
85
	struct controller_block_private *priv = pid->private;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
86
	float outp, outi, outd;
87
88
89
90
91
92
	float input = *priv->input0;
	float previous = priv->previous;
	float maxi = priv->maxi;
	float mini = priv->mini;
	
	outp = input * priv->kp;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
93

94
95
	outi = priv->outi + input * priv->ci;
	outi = fmaxf(fminf(outi, maxi), mini);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
96

97
98
	outd = (input - previous ) * priv->cd;
	priv->previous = input;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
99

100
	if (!*priv->enable) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
101
102
103
		outp = 0;
		outi = 0;
		outd = 0;
104
		priv->previous = 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
105
106
	}
	
107
108
109
	priv->outp = outp;
	priv->outi = outi;
	priv->outd = outd;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
110
	
111
	priv->out = outp + outi + outd;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
}

static struct controller_block_param_list params[] = {
	{ "kp",    true  },
	{ "ki",    true  },
	{ "kd",    true  },
	{ "t",     true  },
	{ "maxw",  false },
	{ "minw",  false },
	{ "reset", true  },
	{ NULL },
};

static void param_get(struct controller_block *pid, int param, void *val)
{
	switch (param) {
		case 0:
			*(float*)val = pid->private->kp;
			break;
		case 1:
			*(float*)val = pid->private->ki;
			break;
		case 2:
			*(float*)val = pid->private->kd;
			break;
		case 3:
			*(float*)val = pid->private->t;
			break;
		case 4:
			*(float*)val = pid->private->maxi;
			break;
		case 5:
			*(float*)val = pid->private->mini;
			break;
		case 6:
			break;
	}
}

static void param_set(struct controller_block *pid, int param, va_list val)
{
	switch (param) {
		case 0:
			pid->private->kp = va_arg(val, double);
			break;
		case 1:
			pid->private->ki = va_arg(val, double);
			break;
		case 2:
			pid->private->kd = va_arg(val, double);
			break;
		case 3:
			pid->private->t = va_arg(val, double);
			break;
		case 4:
			pid->private->maxi = va_arg(val, double);
			break;
		case 5:
			pid->private->mini = va_arg(val, double);
			break;
		case 6:
			pid->private->previous = 0.0;
			pid->private->outi = 0.0;
			pid->private->outp = 0.0;
			pid->private->outd = 0.0;
			pid->private->out = 0.0;
			break;
	}
	pid->private->ci = pid->private->ki * pid->private->t;
	pid->private->cd = pid->private->kd / pid->private->t;
	
}

185
186
187
188
189
190
191
192
193
194
195
196
197
198
static struct controller_block_interm_list interms[] = {
	{ "in", CONTROLLER_BLOCK_TERM_FLOAT, offsetof(struct controller_block_private, input0) },
	{ "enable", CONTROLLER_BLOCK_TERM_BOOL, offsetof(struct controller_block_private, enable) },
	{ NULL }
};

static struct controller_block_outterm_list outterms[] = {
	{ "out", CONTROLLER_BLOCK_TERM_FLOAT, offsetof(struct controller_block_private, out) },
	{ "outp", CONTROLLER_BLOCK_TERM_FLOAT, offsetof(struct controller_block_private, outp) },
	{ "outi", CONTROLLER_BLOCK_TERM_FLOAT, offsetof(struct controller_block_private, outi) },
	{ "outd", CONTROLLER_BLOCK_TERM_FLOAT, offsetof(struct controller_block_private, outd) },
	{ NULL }
};

Jeroen Vreeken's avatar
Jeroen Vreeken committed
199
200
201
202
struct controller_block * block_pid_aw_create(char *name)
{
	struct controller_block *pid;
	
203
	if (!(pid = controller_block_alloc("pid_aw", name, sizeof(struct controller_block_private))))
Jeroen Vreeken's avatar
Jeroen Vreeken committed
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
		return NULL;

	pid->private->out = 0.0;
	pid->private->outp = 0.0;
	pid->private->outi = 0.0;
	pid->private->outd = 0.0;
	pid->private->kp = 0.0;
	pid->private->ki = 0.0;
	pid->private->kd = 0.0;
	pid->private->t = 1.0;
	pid->private->ci = 0.0;
	pid->private->cd = 0.0;
	pid->private->maxi = 0.0;
	pid->private->mini = 0.0;

	pid->private->previous = 0.0;

221
222
	if (controller_block_interm_list_init(pid, interms))
		goto err_block;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
223

224
225
	if (controller_block_outterm_list_init(pid, outterms))
		goto err_block;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
226

227
	pid->calculate = pid_aw_calculate;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
228
229

	if (controller_block_param_list_init(pid, params))
230
		goto err_block;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
231
232
233
234
235
236
237

	pid->param_get = param_get;
	pid->param_set = param_set;

	controller_block_add(pid);
	return pid;

238
239
err_block:
	controller_block_free(pid);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
240
241
	return NULL;
}