Skip to content

Commit eea374f

Browse files
author
Paul Bovbel
committed
Factor out updatePid as negative calls to computeCommand
1 parent 1664561 commit eea374f

File tree

1 file changed

+2
-43
lines changed

1 file changed

+2
-43
lines changed

src/pid.cpp

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -284,19 +284,7 @@ double Pid::computeCommand(double error, ros::Duration dt)
284284

285285
double Pid::updatePid(double error, ros::Duration dt)
286286
{
287-
if (dt == ros::Duration(0.0) || std::isnan(error) || std::isinf(error))
288-
return 0.0;
289-
290-
double error_dot = d_error_;
291-
292-
// Calculate the derivative error
293-
if (dt.toSec() > 0.0)
294-
{
295-
error_dot = (error - p_error_last_) / dt.toSec();
296-
p_error_last_ = error;
297-
}
298-
299-
return updatePid(error, error_dot, dt);
287+
return -computeCommand(error, dt);
300288
}
301289

302290
double Pid::computeCommand(double error, double error_dot, ros::Duration dt)
@@ -335,36 +323,7 @@ double Pid::computeCommand(double error, double error_dot, ros::Duration dt)
335323

336324
double Pid::updatePid(double error, double error_dot, ros::Duration dt)
337325
{
338-
// Get the gain parameters from the realtime buffer
339-
Gains gains = *gains_buffer_.readFromRT();
340-
341-
double p_term, d_term, i_term;
342-
p_error_ = error; //this is pError = pState-pTarget
343-
d_error_ = error_dot;
344-
345-
if (dt == ros::Duration(0.0) || std::isnan(error) || std::isinf(error) || std::isnan(error_dot) || std::isinf(error_dot))
346-
return 0.0;
347-
348-
349-
// Calculate proportional contribution to command
350-
p_term = gains.p_gain_ * p_error_;
351-
352-
// Calculate the integral of the position error
353-
i_error_ += dt.toSec() * p_error_;
354-
355-
// Calculate integral contribution to command
356-
i_term = gains.i_gain_ * i_error_;
357-
358-
// Limit i_term so that the limit is meaningful in the output
359-
i_term = std::max( gains.i_min_, std::min( i_term, gains.i_max_) );
360-
361-
// Calculate derivative contribution to command
362-
d_term = gains.d_gain_ * d_error_;
363-
364-
// Compute the command
365-
cmd_ = - p_term - i_term - d_term;
366-
367-
return cmd_;
326+
return -computeCommand(error, error_dot, dt);
368327
}
369328

370329
void Pid::setCurrentCmd(double cmd)

0 commit comments

Comments
 (0)