|
| 1 | +#include "pos_pid_controller.h" |
| 2 | + |
| 3 | +#define DBG_SECTION_NAME "pos_pid_controller" |
| 4 | +#define DBG_LEVEL DBG_LOG |
| 5 | +#include <rtdbg.h> |
| 6 | + |
| 7 | +static rt_err_t pos_pid_controller_reset(void *pid) |
| 8 | +{ |
| 9 | + rt_memset(pid, 0, sizeof(struct pos_pid_controller)); |
| 10 | + return RT_EOK; |
| 11 | +} |
| 12 | + |
| 13 | +static rt_err_t pos_pid_controller_destroy(void *pid) |
| 14 | +{ |
| 15 | + rt_free(pid); |
| 16 | + return RT_EOK; |
| 17 | +} |
| 18 | + |
| 19 | +static rt_err_t pos_pid_controller_update(void *pid, float current_point) |
| 20 | +{ |
| 21 | + pos_pid_controller_t pos_pid = (pos_pid_controller_t)pid; |
| 22 | + |
| 23 | + if((rt_tick_get() - pos_pid->last_time) < rt_tick_from_millisecond(pos_pid->controller.sample_time)) |
| 24 | + { |
| 25 | + LOG_D("PID waiting ... "); |
| 26 | + return RT_EBUSY; |
| 27 | + } |
| 28 | + pos_pid->last_time = rt_tick_get(); |
| 29 | + |
| 30 | + pos_pid->error = pos_pid->controller.target - current_point; |
| 31 | + |
| 32 | + pos_pid->integral += pos_pid->error; |
| 33 | + |
| 34 | + //Perform integral value capping to avoid internal PID state to blows up |
| 35 | + //when controllertuators saturate: |
| 36 | + if(pos_pid->integral > pos_pid->anti_windup_value) { |
| 37 | + pos_pid->integral = pos_pid->anti_windup_value; |
| 38 | + } else if (pos_pid->integral < -pos_pid->anti_windup_value) { |
| 39 | + pos_pid->integral = -pos_pid->anti_windup_value; |
| 40 | + } |
| 41 | + |
| 42 | + pos_pid->p_error = pos_pid->kp * pos_pid->error; |
| 43 | + pos_pid->i_error = pos_pid->ki * pos_pid->integral; |
| 44 | + pos_pid->d_error = pos_pid->kd * (pos_pid->error - pos_pid->error_l); |
| 45 | + |
| 46 | + pos_pid->last_out = pos_pid->p_error + pos_pid->i_error + pos_pid->d_error; |
| 47 | + if (pos_pid->last_out > pos_pid->maximum) |
| 48 | + { |
| 49 | + pos_pid->last_out = pos_pid->maximum; |
| 50 | + } |
| 51 | + if (pos_pid->last_out < pos_pid->minimum) |
| 52 | + { |
| 53 | + pos_pid->last_out = pos_pid->minimum; |
| 54 | + } |
| 55 | + |
| 56 | + pos_pid->error_l = pos_pid->error; |
| 57 | + |
| 58 | + pos_pid->controller.output = pos_pid->last_out; |
| 59 | + |
| 60 | + // rt_kprintf("%d - %d\n", current_point, pid->set_point); |
| 61 | + // LOG_D("PID current: %d : setpoint %d - P%d I%d D%d - [%d]", current_point, pid->set_point, (int)(pid->p_error + 0.5f), (int)(pid->i_error + 0.5f), (int)(pid->d_error + 0.5f), (int)(pid->out + 0.5f)); |
| 62 | + // LOG_D("PID P Error: %d", (int)(pid->p_error + 0.5f)); |
| 63 | + // LOG_D("PID I Error: %d", (int)(pid->i_error + 0.5f)); |
| 64 | + // LOG_D("PID D Error: %d", (int)(pid->d_error + 0.5f)); |
| 65 | + // LOG_D("PID Last Out: %d", (int)(pid->last_out + 0.5f)); |
| 66 | + // LOG_D("PID Out: %d", (int)(pid->out + 0.5f)); |
| 67 | + |
| 68 | + return RT_EOK; |
| 69 | +} |
| 70 | + |
| 71 | +pos_pid_controller_t pos_pid_controller_create(float kp, float ki, float kd) |
| 72 | +{ |
| 73 | + pos_pid_controller_t new_pid = (pos_pid_controller_t)controller_create(sizeof(struct pos_pid_controller)); |
| 74 | + if(new_pid == RT_NULL) |
| 75 | + { |
| 76 | + return RT_NULL; |
| 77 | + } |
| 78 | + |
| 79 | + new_pid->kp = kp; |
| 80 | + new_pid->ki = ki; |
| 81 | + new_pid->kd = kd; |
| 82 | + |
| 83 | + new_pid->maximum = +1000; |
| 84 | + new_pid->minimum = -1000; |
| 85 | + new_pid->anti_windup_value = 0.0f; |
| 86 | + |
| 87 | + new_pid->integral = 0.0f; |
| 88 | + new_pid->p_error = 0.0f; |
| 89 | + new_pid->i_error = 0.0f; |
| 90 | + new_pid->d_error = 0.0f; |
| 91 | + |
| 92 | + new_pid->error = 0.0f; |
| 93 | + new_pid->error_l = 0.0f; |
| 94 | + |
| 95 | + new_pid->last_out = 0.0f; |
| 96 | + |
| 97 | + new_pid->controller.reset = pos_pid_controller_reset; |
| 98 | + new_pid->controller.destroy = pos_pid_controller_destroy; |
| 99 | + new_pid->controller.update = pos_pid_controller_update; |
| 100 | + |
| 101 | + return new_pid; |
| 102 | +} |
| 103 | + |
| 104 | +rt_err_t pos_pid_controller_set_kp(pos_pid_controller_t pid, float kp) |
| 105 | +{ |
| 106 | + pid->kp = kp; |
| 107 | + return RT_EOK; |
| 108 | +} |
| 109 | + |
| 110 | +rt_err_t pos_pid_controller_set_ki(pos_pid_controller_t pid, float ki) |
| 111 | +{ |
| 112 | + pid->ki = ki; |
| 113 | + return RT_EOK; |
| 114 | +} |
| 115 | + |
| 116 | +rt_err_t pos_pid_controller_set_kd(pos_pid_controller_t pid, float kd) |
| 117 | +{ |
| 118 | + pid->kd = kd; |
| 119 | + return RT_EOK; |
| 120 | +} |
0 commit comments