Skip to content

Commit a2755f8

Browse files
authored
Merge pull request #13 from uLipe/uLipe/pid_windup
pid: added basic anti-windup mechanism on pid controller
2 parents e4ee200 + 822536e commit a2755f8

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

pid/pid.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pid_control_t pid_create(void)
2020

2121
new_pid->maximum = 100;
2222
new_pid->minimum = 20;
23+
new_pid->anti_windup_value = new_pid->maximum * 2.0f;
2324

2425
new_pid->p_error = 10.0f;
2526
new_pid->i_error = 1.0f;
@@ -75,6 +76,12 @@ rt_err_t pid_set_target(pid_control_t pid, rt_int16_t set_point)
7576
return RT_EOK;
7677
}
7778

79+
rt_err_t pid_set_anti_windup_value(pid_control_t pid, float anti_windup_value)
80+
{
81+
pid->anti_windup_value = anti_windup_value;
82+
return RT_EOK;
83+
}
84+
7885
rt_err_t pid_set_sample_time(pid_control_t pid, rt_uint16_t sample_time)
7986
{
8087
// TODO
@@ -96,6 +103,14 @@ float pid_update(pid_control_t pid, rt_int16_t current_point)
96103

97104
pid->integral += pid->error;
98105

106+
//Perform integral value capping to avoid internal PID state to blows up
107+
//when actuators saturate:
108+
if(pid->integral > pid->anti_windup_value) {
109+
pid->integral = pid->anti_windup_value;
110+
} else if (pid->integral < -pid->anti_windup_value) {
111+
pid->integral = -pid->anti_windup_value;
112+
}
113+
99114
pid->p_error = pid->kp * pid->error;
100115
pid->i_error = pid->ki * pid->integral;
101116
pid->d_error = pid->kd * (pid->error - 2 * pid->error_l + pid->error_ll);

pid/pid.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct pid_control
1313

1414
float minimum;
1515
float maximum;
16+
float anti_windup_value;
1617
rt_int16_t set_point;
1718
rt_uint16_t sample_time; // unit:ms
1819

@@ -39,7 +40,7 @@ rt_err_t pid_set_ki(pid_control_t pid, float ki);
3940
rt_err_t pid_set_kd(pid_control_t pid, float kd);
4041
rt_err_t pid_set_target(pid_control_t pid, rt_int16_t set_point);
4142
rt_err_t pid_set_sample_time(pid_control_t pid, rt_uint16_t sample_time);
42-
43+
rt_err_t pid_set_anti_windup_value(pid_control_t pid, float anti_windup_value);
4344
float pid_update(pid_control_t pid, rt_int16_t current_point);
4445

4546
// PID Thread

0 commit comments

Comments
 (0)