Skip to content

Commit 06ccaed

Browse files
author
Adolfo Rodriguez Tsouroukdissian
committed
Merge pull request #44 from clearpathrobotics/merge-pid-calls
Chain calls of computeCommand and updatePid for code reuse
2 parents 95ad337 + 8e7d32a commit 06ccaed

File tree

1 file changed

+8
-49
lines changed

1 file changed

+8
-49
lines changed

src/pid.cpp

Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -266,78 +266,37 @@ void Pid::dynamicReconfigCallback(control_toolbox::ParametersConfig &config, uin
266266

267267
double Pid::computeCommand(double error, ros::Duration dt)
268268
{
269-
// Get the gain parameters from the realtime buffer
270-
Gains gains = *gains_buffer_.readFromRT();
271-
272-
double p_term, d_term, i_term;
273-
p_error_ = error; // this is error = target - state
274269

275270
if (dt == ros::Duration(0.0) || std::isnan(error) || std::isinf(error))
276271
return 0.0;
277272

278-
// Calculate proportional contribution to command
279-
p_term = gains.p_gain_ * p_error_;
280-
281-
// Calculate the integral of the position error
282-
i_error_ += dt.toSec() * p_error_;
283-
284-
// Calculate integral contribution to command
285-
i_term = gains.i_gain_ * i_error_;
286-
287-
// Limit i_term so that the limit is meaningful in the output
288-
i_term = std::max( gains.i_min_, std::min( i_term, gains.i_max_) );
273+
double error_dot = d_error_;
289274

290275
// Calculate the derivative error
291276
if (dt.toSec() > 0.0)
292277
{
293-
d_error_ = (p_error_ - p_error_last_) / dt.toSec();
294-
p_error_last_ = p_error_;
278+
error_dot = (error - p_error_last_) / dt.toSec();
279+
p_error_last_ = error;
295280
}
296-
// Calculate derivative contribution to command
297-
d_term = gains.d_gain_ * d_error_;
298281

299-
// Compute the command
300-
cmd_ = p_term + i_term + d_term;
301-
302-
return cmd_;
282+
return computeCommand(error, error_dot, dt);
303283
}
304284

305285
double Pid::updatePid(double error, ros::Duration dt)
306286
{
307-
// Get the gain parameters from the realtime buffer
308-
Gains gains = *gains_buffer_.readFromRT();
309-
310-
double p_term, d_term, i_term;
311-
p_error_ = error; //this is pError = pState-pTarget
312-
313287
if (dt == ros::Duration(0.0) || std::isnan(error) || std::isinf(error))
314288
return 0.0;
315289

316-
// Calculate proportional contribution to command
317-
p_term = gains.p_gain_ * p_error_;
318-
319-
// Calculate the integral of the position error
320-
i_error_ += dt.toSec() * p_error_;
321-
322-
// Calculate integral contribution to command
323-
i_term = gains.i_gain_ * i_error_;
324-
325-
// Limit i_term so that the limit is meaningful in the output
326-
i_term = std::max( gains.i_min_, std::min( i_term, gains.i_max_) );
290+
double error_dot = d_error_;
327291

328292
// Calculate the derivative error
329293
if (dt.toSec() > 0.0)
330294
{
331-
d_error_ = (p_error_ - p_error_last_) / dt.toSec();
332-
p_error_last_ = p_error_;
295+
error_dot = (error - p_error_last_) / dt.toSec();
296+
p_error_last_ = error;
333297
}
334-
// Calculate derivative contribution to command
335-
d_term = gains.d_gain_ * d_error_;
336298

337-
// Compute the command
338-
cmd_ = - p_term - i_term - d_term;
339-
340-
return cmd_;
299+
return updatePid(error, error_dot, dt);
341300
}
342301

343302
double Pid::computeCommand(double error, double error_dot, ros::Duration dt)

0 commit comments

Comments
 (0)