@@ -111,7 +111,7 @@ void Pid::reset(bool save_i_term)
111111 }
112112}
113113
114- void Pid::clear_saved_iterm () { i_error_ = 0.0 ; }
114+ void Pid::clear_saved_iterm () { i_term_ = 0.0 ; }
115115
116116void Pid::get_gains (double & p, double & i, double & d, double & i_max, double & i_min)
117117{
@@ -202,7 +202,7 @@ double Pid::compute_command(double error, double error_dot, const double & dt_s)
202202 // Get the gain parameters from the realtime buffer
203203 Gains gains = *gains_buffer_.readFromRT ();
204204
205- double p_term, d_term, i_term ;
205+ double p_term, d_term;
206206 p_error_ = error; // this is error = target - state
207207 d_error_ = error_dot;
208208
@@ -219,31 +219,23 @@ double Pid::compute_command(double error, double error_dot, const double & dt_s)
219219 // Calculate proportional contribution to command
220220 p_term = gains.p_gain_ * p_error_;
221221
222- // Calculate the integral of the position error
223- i_error_ += dt_s * p_error_;
224-
225- if (gains.antiwindup_ && gains.i_gain_ != 0 )
222+ // Calculate integral contribution to command
223+ if (gains.antiwindup_ )
226224 {
227- // Prevent i_error_ from climbing higher than permitted by i_max_/i_min_
228- std::pair<double , double > bounds =
229- std::minmax<double >(gains.i_min_ / gains.i_gain_ , gains.i_max_ / gains.i_gain_ );
230- i_error_ = std::clamp (i_error_, bounds.first , bounds.second );
225+ // Prevent i_term_ from climbing higher than permitted by i_max_/i_min_
226+ i_term_ = std::clamp (i_term_ + gains.i_gain_ * dt_s * p_error_, gains.i_min_ , gains.i_max_ );
231227 }
232-
233- // Calculate integral contribution to command
234- i_term = gains.i_gain_ * i_error_;
235-
236- if (!gains.antiwindup_ )
228+ else
237229 {
238- // Limit i_term so that the limit is meaningful in the output
239- i_term = std::clamp (i_term, gains.i_min_ , gains.i_max_ );
230+ i_term_ += gains.i_gain_ * dt_s * p_error_;
240231 }
241232
242233 // Calculate derivative contribution to command
243234 d_term = gains.d_gain_ * d_error_;
244235
245236 // Compute the command
246- cmd_ = p_term + i_term + d_term;
237+ // Limit i_term so that the limit is meaningful in the output
238+ cmd_ = p_term + std::clamp (i_term_, gains.i_min_ , gains.i_max_ ) + d_term;
247239
248240 return cmd_;
249241}
@@ -255,7 +247,7 @@ double Pid::get_current_cmd() { return cmd_; }
255247void Pid::get_current_pid_errors (double & pe, double & ie, double & de)
256248{
257249 pe = p_error_;
258- ie = i_error_ ;
250+ ie = i_term_ ;
259251 de = d_error_;
260252}
261253
0 commit comments