Skip to content

Commit d35e494

Browse files
authored
Merge pull request vedderb#283 from powerdesigns/speed_pid_loop_antiwindup
speed_pid: improved integral term antiwindup.
2 parents 1469e25 + c7819a8 commit d35e494

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

mcpwm_foc.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -4153,23 +4153,25 @@ static void run_pid_control_speed(float dt, volatile motor_all_state_t *motor) {
41534153

41544154
// Compute parameters
41554155
p_term = error * conf_now->s_pid_kp * (1.0 / 20.0);
4156-
motor->m_speed_i_term += error * (conf_now->s_pid_ki * dt) * (1.0 / 20.0);
41574156
d_term = (error - motor->m_speed_prev_error) * (conf_now->s_pid_kd / dt) * (1.0 / 20.0);
41584157

41594158
// Filter D
41604159
UTILS_LP_FAST(motor->m_speed_d_filter, d_term, conf_now->s_pid_kd_filter);
41614160
d_term = motor->m_speed_d_filter;
41624161

4163-
// I-term wind-up protection
4164-
utils_truncate_number((float*)&motor->m_speed_i_term, -1.0, 1.0);
4165-
41664162
// Store previous error
41674163
motor->m_speed_prev_error = error;
41684164

41694165
// Calculate output
41704166
float output = p_term + motor->m_speed_i_term + d_term;
4167+
float pre_output = output;
4168+
41714169
utils_truncate_number(&output, -1.0, 1.0);
41724170

4171+
float output_saturation = output - pre_output;
4172+
4173+
motor->m_speed_i_term += error * (conf_now->s_pid_ki * dt) * (1.0 / 20.0) + output_saturation;
4174+
41734175
// Optionally disable braking
41744176
if (!conf_now->s_pid_allow_braking) {
41754177
if (rpm > 20.0 && output < 0.0) {

0 commit comments

Comments
 (0)