Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom PID error rate #525

Merged
merged 2 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the mix of const double and double looks funny. Can we make them both one or the other?

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));
}

}
}
}