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 double get_derivative() const

Get Pid derivative.

Example

// Construct a Pid controller
dlib::Pid pid({1, 2, 3});

// Get Pid derivative
double derivative = pid.get_derivative();

Returns:

The current derivative (how quickly the error is changing)

inline double get_p() const

Get the P term of the Pid.

Returns:

double

inline double get_i() const

Get the I term of the Pid.

Returns:

double

inline double get_d() const

Get the D term of the Pid.

Returns:

double

struct PidGains

The gains for a standard Pid controller.

Public Members

double kp = 0

The porpotional gain: how aggresively the controller responds to error

double ki = 0

The integral gain: how much to increase the response if error persists too long

double kd = 0

The derivative gain: used to limit the speed of the controllers response

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

struct FeedforwardGains

The gains for a simple motor feedforward controller.

Public Members

double ks = 0

The the voltage needed to overcome static friction

double kv = 0

The voltage needed to cruise at a constant velocity while overcoming back EMF and any other movement induced friction

double ka = 0

The voltage needed to induce a given acceleration

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.

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.