7#include <smooth/concepts/lie_group.hpp>
8#include <smooth/spline/spline.hpp>
12namespace smooth::feedback {
37template<Time T, LieGroup G>
74 inline Tangent<G>
operator()(
const T & t,
const G & x,
const Tangent<G> & v)
76 const auto [g_des, v_des, a_des] = x_des_(t);
77 const Tangent<G> g_err = g_des - x;
79 if (t_last && t > t_last.value()) {
82 i_err_ = i_err_.cwiseMax(-prm_.windup_limit).cwiseMin(prm_.windup_limit);
86 return a_des + kp_.cwiseProduct(g_err) + kd_.cwiseProduct(v_des - v) + ki_.cwiseProduct(i_err_);
92 inline void set_kp(
double kp) { kp_.setConstant(kp); }
97 template<
typename Derived>
98 inline void set_kp(
const Eigen::MatrixBase<Derived> & kp)
106 inline void set_kd(
double kd) { kd_.setConstant(kd); }
111 template<
typename Derived>
112 inline void set_kd(
const Eigen::MatrixBase<Derived> & kd)
120 inline void set_ki(
double ki) { ki_.setConstant(ki); }
125 template<
typename Derived>
126 inline void set_ki(
const Eigen::MatrixBase<Derived> & ki)
143 inline void set_xdes(T t0,
const smooth::Spline<K, G> & c)
145 set_xdes(t0, smooth::Spline<K, G>(c));
152 inline void set_xdes(T t0, smooth::Spline<K, G> && c)
180 set_xdes(std::move(f_copy));
192 Tangent<G> kd_ = Tangent<G>::Ones();
193 Tangent<G> kp_ = Tangent<G>::Ones();
194 Tangent<G> ki_ = Tangent<G>::Zero();
197 std::optional<T> t_last;
198 Tangent<G> i_err_ = Tangent<G>::Zero();
201 std::function<TrajectoryReturnT(T)> x_des_ = [](T) -> TrajectoryReturnT {
202 return TrajectoryReturnT(Identity<G>(), Tangent<G>::Zero(), Tangent<G>::Zero());
Proportional-Integral-Derivative controller for Lie groups.
void set_kp(const Eigen::MatrixBase< Derived > &kp)
Set proportional gains.
void set_xdes(T t0, const smooth::Spline< K, G > &c)
Set desired trajectory as a smooth::Spline.
void set_kd(const Eigen::MatrixBase< Derived > &kd)
Set derivative gains.
void set_xdes(std::function< TrajectoryReturnT(T)> &&f)
Set desired trajectory (rvalue version).
void set_kd(double kd)
Set all derivative gains to kd.
PID & operator=(const PID &)=default
Default copy assignment.
PID & operator=(PID &&)=default
Default move assignment.
std::tuple< G, Tangent< G >, Tangent< G > > TrajectoryReturnT
Desired trajectory consists of position, velocity, and acceleration.
void reset_integral()
Reset integral state to zero.
void set_ki(double ki)
Set all integral gains to ki.
void set_xdes(const std::function< TrajectoryReturnT(T)> &f)
Set desired trajectory.
void set_xdes(T t0, smooth::Spline< K, G > &&c)
Set desired trajectory as a smooth::Spline (rvalue version)
PID(const PIDParams &prm=PIDParams{}) noexcept
Create a PID controller.
PID(PID &&)=default
Default move constructor.
Tangent< G > operator()(const T &t, const G &x, const Tangent< G > &v)
Calculate control input.
void set_ki(const Eigen::MatrixBase< Derived > &ki)
Set derivative gains.
PID(const PID &)=default
Default copy constructor.
void set_kp(double kp)
Set all proportional gains to kp.
~PID()=default
Default destructor.
double windup_limit
Maximal absolute value for integral states.
Trait class to specify Time operations.