Skip to content

Commit

Permalink
Custom PID error rate (gazebosim#525)
Browse files Browse the repository at this point in the history
* Custom PID error rate

Signed-off-by: Nate Koenig <natekoenig@gmail.com>

* added test

Signed-off-by: Nate Koenig <natekoenig@gmail.com>

---------

Signed-off-by: Nate Koenig <natekoenig@gmail.com>
  • Loading branch information
nkoenig authored and danilogsch committed Jun 1, 2023
1 parent 1b1c6b8 commit ea6f2fb
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
15 changes: 15 additions & 0 deletions include/gz/math/PID.hh
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,21 @@ namespace ignition
/// \return The offset value
public: double CmdOffset() const;

/// \brief Update the Pid loop with nonuniform time step size.
/// \param[in] _error Error since last call (p_state - p_target).
/// \param[in] _errorRate Estimate of error rate, that can be used
/// when a smoother estimate is available than the finite difference
/// used by Update(const double _error,
/// const std::chrono::duration<double> &_dt)
/// \param[in] _dt Change in time since last update call.
/// Normally, this is called at every time step,
/// The return value is an updated command to be passed
/// to the object being controlled.
/// \return the command value
public: double Update(const double _error,
double _errorRate,
const std::chrono::duration<double> &_dt);

/// \brief Update the Pid loop with nonuniform time step size.
/// \param[in] _error Error since last call (p_state - p_target).
/// \param[in] _dt Change in time since last update call.
Expand Down
25 changes: 19 additions & 6 deletions src/PID.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,22 @@ double PID::Update(const double _error,
return 0.0;
}

// Pass in the derivative error
return this->Update(_error, (_error - this->pErrLast) / _dt.count(), _dt);
}

/////////////////////////////////////////////////
double PID::Update(const double _error,
double _errorRate,
const std::chrono::duration<double> &_dt)
{
if (_dt == std::chrono::duration<double>(0) ||
isnan(_error) || std::isinf(_error) ||
isnan(_errorRate) || std::isinf(_errorRate))
{
return 0.0;
}

double pTerm, dTerm;
this->pErr = _error;

Expand All @@ -156,12 +172,9 @@ double PID::Update(const double _error,
if (this->iMax >= this->iMin)
this->iErr = clamp(this->iErr, this->iMin, this->iMax);

// Calculate the derivative error
if (_dt != std::chrono::duration<double>(0))
{
this->dErr = (this->pErr - this->pErrLast) / _dt.count();
this->pErrLast = this->pErr;
}
// Use the provided error rate
this->dErr = _errorRate;
this->pErrLast = this->pErr;

// Calculate derivative contribution to command
dTerm = this->dGain * this->dErr;
Expand Down
9 changes: 9 additions & 0 deletions src/PID_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ TEST(PidTest, Update)
EXPECT_DOUBLE_EQ(pe, 5);
EXPECT_DOUBLE_EQ(ie, 1.4);
EXPECT_DOUBLE_EQ(de, 0.0);

pid.Reset();
pid.SetIGain(0.0);
pid.SetIMin(0.0);
result = pid.Update(5.0, 1.0, std::chrono::duration<double>(10.0));
EXPECT_DOUBLE_EQ(result, -5.5);

result = pid.Update(5.0, 1.0, std::chrono::duration<double>(0.0));
EXPECT_DOUBLE_EQ(result, 0);
}

/////////////////////////////////////////////////
Expand Down
8 changes: 7 additions & 1 deletion src/python_pybind11/src/PID.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,13 @@ void defineMathPID(py::module &m, const std::string &typestr)
&Class::CmdOffset,
"Get the offset value for the command.")
.def("update",
&Class::Update,
py::overload_cast<const double, double,
const std::chrono::duration<double> &>(&Class::Update),
"Update the Pid loop with nonuniform time step size, and custom error "
"rate.")
.def("update",
py::overload_cast<const double,
const std::chrono::duration<double> &>(&Class::Update),
"Update the Pid loop with nonuniform time step size.")
.def("set_cmd",
&Class::SetCmd,
Expand Down
5 changes: 5 additions & 0 deletions src/ruby/PID.i
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ namespace ignition
double Update(const double error, const double dt) {
return (*$self).Update(error, std::chrono::duration<double>(dt));
}
double Update(const double error, double errorRate, const double dt) {
return (*$self).Update(error, errorRate,
std::chrono::duration<double>(dt));
}

}
}
}

0 comments on commit ea6f2fb

Please sign in to comment.