@@ -111,7 +111,7 @@ void Pid::reset(bool save_i_term)
111
111
}
112
112
}
113
113
114
- void Pid::clear_saved_iterm () { i_error_ = 0.0 ; }
114
+ void Pid::clear_saved_iterm () { i_term_ = 0.0 ; }
115
115
116
116
void Pid::get_gains (double & p, double & i, double & d, double & i_max, double & i_min)
117
117
{
@@ -202,7 +202,7 @@ double Pid::compute_command(double error, double error_dot, const double & dt_s)
202
202
// Get the gain parameters from the realtime buffer
203
203
Gains gains = *gains_buffer_.readFromRT ();
204
204
205
- double p_term, d_term, i_term ;
205
+ double p_term, d_term;
206
206
p_error_ = error; // this is error = target - state
207
207
d_error_ = error_dot;
208
208
@@ -219,31 +219,23 @@ double Pid::compute_command(double error, double error_dot, const double & dt_s)
219
219
// Calculate proportional contribution to command
220
220
p_term = gains.p_gain_ * p_error_;
221
221
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_ )
226
224
{
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_ );
231
227
}
232
-
233
- // Calculate integral contribution to command
234
- i_term = gains.i_gain_ * i_error_;
235
-
236
- if (!gains.antiwindup_ )
228
+ else
237
229
{
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_;
240
231
}
241
232
242
233
// Calculate derivative contribution to command
243
234
d_term = gains.d_gain_ * d_error_;
244
235
245
236
// 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;
247
239
248
240
return cmd_;
249
241
}
@@ -255,7 +247,7 @@ double Pid::get_current_cmd() { return cmd_; }
255
247
void Pid::get_current_pid_errors (double & pe, double & ie, double & de)
256
248
{
257
249
pe = p_error_;
258
- ie = i_error_ ;
250
+ ie = i_term_ ;
259
251
de = d_error_;
260
252
}
261
253
0 commit comments