Control¶
-
class Pid¶
Public Functions
-
inline void reset()¶
Reset the PID states.
-
inline double update(double error, double period)¶
Calculate Pid voltage.
Example
// Construct a Pid controller dlib::Pid pid({1, 2, 3}); // Calculate voltage with a test input double voltage = pid.update(meters(0.05), seconds(0.02));
- Parameters:
reading – the current sensor reading
period – the interval at which the pid is updated
- Returns:
the voltage to send to your mechanism
-
inline PidGains get_gains() const¶
Get Pid gains.
Example
// Construct a Pid controller dlib::Pid pid({1, 2, 3}); // Get the Pid gains dlib::PidGains gains = pid.get_gains();
- Returns:
The current Pid gains
-
inline void set_gains(PidGains gains)¶
Set Pid gains.
Example
// Construct a Pid controller dlib::Pid pid({1, 2, 3}); // Set the Pid gains pid.set_gains({4, 5, 6});
- Parameters:
gains – gains to set the Pid constructor to
-
inline double get_error() const¶
Get Pid error.
Example
v // Construct a Pid controller dlib::Pid pid({1, 2, 3}); // Get Pid error double error = pid.get_error();
- Returns:
The current error (how far the controller is from the setpoint)
-
inline void reset()¶
-
template<typename Units>
class Feedforward¶ Public Functions
-
inline double calculate(double target_velocity, double target_acceleration) const¶
Calculate Feedforward voltage.
Example
// Construct a Feedforward controller dlib::Feedforward ff = dlib::Feedforward({1, 2, 3}); // Calculate the voltage needed to coast at 1 m/s double voltage = ff.calculate(meters_per_second(1), ZERO);
- Parameters:
target_velocity – the target velocity
target_accleration – the target acceleration
- Returns:
the voltage required for the mechanism to achieve the target velocity and acceleration
-
inline FeedforwardGains get_gains() const¶
Get Feedforward Gains.
Example
// Construct a Feedforward controller dlib::Feedforward ff = dlib::Feedforward({1, 2, 3}); // Get the Feedforward gains dlib::FeedforwardGains gains = ff.get_gains();
- Returns:
the current gains for the feedforward controller
-
inline void set_gains(FeedforwardGains new_gains)¶
Set Feedforward gains.
Example
// Construct a Feedforward controller dlib::Feedforward ff = dlib::Feedforward({1, 2, 3}); // Set the Feedforward gains ff.set_gains({4, 5, 6});
- Parameters:
new_gains – the new gains for the feedforward controller
-
inline double calculate(double target_velocity, double target_acceleration) const¶
-
struct FeedforwardGains¶
The gains for a simple motor feedforward controller.
-
template<typename Units>
class ErrorTimeSettler¶ Public Functions
-
inline bool is_settled(double error, double period)¶
Check if controller has settled.
Example
dlib::Pid pid({1, 2, 3}); // Construct a last_period period double last_period; // Construct an TimeErrorSettler controller dlib::TimeErrorSettler pid_settler { 0.01, // the maximum error the pid can settle at 0.250 // the time the Pid needs to remain below the error threshold } bool settled = pid_settler.is_settled(pid.get_error(), seconds(0.02));
- Parameters:
error – distance from setpoint
period – time since last settle iteration - current time - last time
- Returns:
if the controller has settled
-
inline void reset()¶
Reset all of the settler state.
-
inline bool is_settled(double error, double period)¶
-
class ErrorDerivativeSettler¶
Public Functions
-
inline bool is_settled(double error, double derivative)¶
Check if controller has settled.
Example
// Construct a PID controller dlib::Pid pid({1, 2, 3}); // Construct an ErrorDerivativeSettler controller dlib::ErrorDerivativeSettler pid_settler { 0.1, // error threshold, the maximum error the pid can settle at 0.01 // derivative threshold, the maximum instantaneous error over time the pid can settle at } bool settled = pid_settler.is_settled(pid.get_error(), pid.get_derivative());
- Parameters:
error – distance from setpoint
derivative – velocity
- Returns:
if the controller has settled
-
inline void reset()¶
Reset all of the settler state.
-
inline bool is_settled(double error, double derivative)¶