WPILibC++ 2024.3.2
frc::LinearSystemLoop< States, Inputs, Outputs > Class Template Reference

Combines a controller, feedforward, and observer for controlling a mechanism with full state feedback. More...

#include <frc/system/LinearSystemLoop.h>

Public Types

using StateVector = Vectord< States >
 
using InputVector = Vectord< Inputs >
 
using OutputVector = Vectord< Outputs >
 

Public Member Functions

 LinearSystemLoop (LinearSystem< States, Inputs, Outputs > &plant, LinearQuadraticRegulator< States, Inputs > &controller, KalmanFilter< States, Inputs, Outputs > &observer, units::volt_t maxVoltage, units::second_t dt)
 Constructs a state-space loop with the given plant, controller, and observer. More...
 
 LinearSystemLoop (LinearSystem< States, Inputs, Outputs > &plant, LinearQuadraticRegulator< States, Inputs > &controller, KalmanFilter< States, Inputs, Outputs > &observer, std::function< InputVector(const InputVector &)> clampFunction, units::second_t dt)
 Constructs a state-space loop with the given plant, controller, and observer. More...
 
 LinearSystemLoop (LinearQuadraticRegulator< States, Inputs > &controller, const LinearPlantInversionFeedforward< States, Inputs > &feedforward, KalmanFilter< States, Inputs, Outputs > &observer, units::volt_t maxVoltage)
 Constructs a state-space loop with the given controller, feedforward and observer. More...
 
 LinearSystemLoop (LinearQuadraticRegulator< States, Inputs > &controller, const LinearPlantInversionFeedforward< States, Inputs > &feedforward, KalmanFilter< States, Inputs, Outputs > &observer, std::function< InputVector(const InputVector &)> clampFunction)
 Constructs a state-space loop with the given controller, feedforward, observer and clamp function. More...
 
 LinearSystemLoop (LinearSystemLoop &&)=default
 
LinearSystemLoopoperator= (LinearSystemLoop &&)=default
 
const StateVectorXhat () const
 Returns the observer's state estimate x-hat. More...
 
double Xhat (int i) const
 Returns an element of the observer's state estimate x-hat. More...
 
const StateVectorNextR () const
 Returns the controller's next reference r. More...
 
double NextR (int i) const
 Returns an element of the controller's next reference r. More...
 
InputVector U () const
 Returns the controller's calculated control input u. More...
 
double U (int i) const
 Returns an element of the controller's calculated control input u. More...
 
void SetXhat (const StateVector &xHat)
 Set the initial state estimate x-hat. More...
 
void SetXhat (int i, double value)
 Set an element of the initial state estimate x-hat. More...
 
void SetNextR (const StateVector &nextR)
 Set the next reference r. More...
 
const LinearQuadraticRegulator< States, Inputs > & Controller () const
 Return the controller used internally. More...
 
const LinearPlantInversionFeedforward< States, Inputs > Feedforward () const
 Return the feedforward used internally. More...
 
const KalmanFilter< States, Inputs, Outputs > & Observer () const
 Return the observer used internally. More...
 
void Reset (const StateVector &initialState)
 Zeroes reference r and controller output u. More...
 
StateVector Error () const
 Returns difference between reference r and current state x-hat. More...
 
void Correct (const OutputVector &y)
 Correct the state estimate x-hat using the measurements in y. More...
 
void Predict (units::second_t dt)
 Sets new controller output, projects model forward, and runs observer prediction. More...
 
InputVector ClampInput (const InputVector &u) const
 Clamps input vector between system's minimum and maximum allowable input. More...
 

Protected Attributes

LinearQuadraticRegulator< States, Inputs > * m_controller
 
LinearPlantInversionFeedforward< States, Inputs > m_feedforward
 
KalmanFilter< States, Inputs, Outputs > * m_observer
 
std::function< InputVector(const InputVector &)> m_clampFunc
 Clamping function. More...
 
StateVector m_nextR
 

Static Protected Attributes

static constexpr int kStates = States
 
static constexpr int kInputs = Inputs
 
static constexpr int kOutputs = Outputs
 

Detailed Description

template<int States, int Inputs, int Outputs>
class frc::LinearSystemLoop< States, Inputs, Outputs >

Combines a controller, feedforward, and observer for controlling a mechanism with full state feedback.

For everything in this file, "inputs" and "outputs" are defined from the perspective of the plant. This means U is an input and Y is an output (because you give the plant U (powers) and it gives you back a Y (sensor values). This is the opposite of what they mean from the perspective of the controller (U is an output because that's what goes to the motors and Y is an input because that's what comes back from the sensors).

For more on the underlying math, read https://file.tavsys.net/control/controls-engineering-in-frc.pdf.

Template Parameters
StatesNumber of states.
InputsNumber of inputs.
OutputsNumber of outputs.

Member Typedef Documentation

◆ InputVector

template<int States, int Inputs, int Outputs>
using frc::LinearSystemLoop< States, Inputs, Outputs >::InputVector = Vectord<Inputs>

◆ OutputVector

template<int States, int Inputs, int Outputs>
using frc::LinearSystemLoop< States, Inputs, Outputs >::OutputVector = Vectord<Outputs>

◆ StateVector

template<int States, int Inputs, int Outputs>
using frc::LinearSystemLoop< States, Inputs, Outputs >::StateVector = Vectord<States>

Constructor & Destructor Documentation

◆ LinearSystemLoop() [1/5]

template<int States, int Inputs, int Outputs>
frc::LinearSystemLoop< States, Inputs, Outputs >::LinearSystemLoop ( LinearSystem< States, Inputs, Outputs > &  plant,
LinearQuadraticRegulator< States, Inputs > &  controller,
KalmanFilter< States, Inputs, Outputs > &  observer,
units::volt_t  maxVoltage,
units::second_t  dt 
)
inline

Constructs a state-space loop with the given plant, controller, and observer.

By default, the initial reference is all zeros. Users should call reset with the initial system state before enabling the loop. This constructor assumes that the input(s) to this system are voltage.

Parameters
plantState-space plant.
controllerState-space controller.
observerState-space observer.
maxVoltageThe maximum voltage that can be applied. Commonly 12.
dtThe nominal timestep.

◆ LinearSystemLoop() [2/5]

template<int States, int Inputs, int Outputs>
frc::LinearSystemLoop< States, Inputs, Outputs >::LinearSystemLoop ( LinearSystem< States, Inputs, Outputs > &  plant,
LinearQuadraticRegulator< States, Inputs > &  controller,
KalmanFilter< States, Inputs, Outputs > &  observer,
std::function< InputVector(const InputVector &)>  clampFunction,
units::second_t  dt 
)
inline

Constructs a state-space loop with the given plant, controller, and observer.

By default, the initial reference is all zeros. Users should call reset with the initial system state before enabling the loop. This constructor assumes that the input(s) to this system are voltage.

Parameters
plantState-space plant.
controllerState-space controller.
observerState-space observer.
clampFunctionThe function used to clamp the input vector.
dtThe nominal timestep.

◆ LinearSystemLoop() [3/5]

template<int States, int Inputs, int Outputs>
frc::LinearSystemLoop< States, Inputs, Outputs >::LinearSystemLoop ( LinearQuadraticRegulator< States, Inputs > &  controller,
const LinearPlantInversionFeedforward< States, Inputs > &  feedforward,
KalmanFilter< States, Inputs, Outputs > &  observer,
units::volt_t  maxVoltage 
)
inline

Constructs a state-space loop with the given controller, feedforward and observer.

By default, the initial reference is all zeros. Users should call reset with the initial system state.

Parameters
controllerState-space controller.
feedforwardPlant inversion feedforward.
observerState-space observer.
maxVoltageThe maximum voltage that can be applied. Assumes that the inputs are voltages.

◆ LinearSystemLoop() [4/5]

template<int States, int Inputs, int Outputs>
frc::LinearSystemLoop< States, Inputs, Outputs >::LinearSystemLoop ( LinearQuadraticRegulator< States, Inputs > &  controller,
const LinearPlantInversionFeedforward< States, Inputs > &  feedforward,
KalmanFilter< States, Inputs, Outputs > &  observer,
std::function< InputVector(const InputVector &)>  clampFunction 
)
inline

Constructs a state-space loop with the given controller, feedforward, observer and clamp function.

By default, the initial reference is all zeros. Users should call reset with the initial system state.

Parameters
controllerState-space controller.
feedforwardPlant-inversion feedforward.
observerState-space observer.
clampFunctionThe function used to clamp the input vector.

◆ LinearSystemLoop() [5/5]

template<int States, int Inputs, int Outputs>
frc::LinearSystemLoop< States, Inputs, Outputs >::LinearSystemLoop ( LinearSystemLoop< States, Inputs, Outputs > &&  )
default

Member Function Documentation

◆ ClampInput()

template<int States, int Inputs, int Outputs>
InputVector frc::LinearSystemLoop< States, Inputs, Outputs >::ClampInput ( const InputVector u) const
inline

Clamps input vector between system's minimum and maximum allowable input.

Parameters
uInput vector to clamp.
Returns
Clamped input vector.

◆ Controller()

template<int States, int Inputs, int Outputs>
const LinearQuadraticRegulator< States, Inputs > & frc::LinearSystemLoop< States, Inputs, Outputs >::Controller ( ) const
inline

Return the controller used internally.

◆ Correct()

template<int States, int Inputs, int Outputs>
void frc::LinearSystemLoop< States, Inputs, Outputs >::Correct ( const OutputVector y)
inline

Correct the state estimate x-hat using the measurements in y.

Parameters
yMeasurement vector.

◆ Error()

template<int States, int Inputs, int Outputs>
StateVector frc::LinearSystemLoop< States, Inputs, Outputs >::Error ( ) const
inline

Returns difference between reference r and current state x-hat.

◆ Feedforward()

template<int States, int Inputs, int Outputs>
const LinearPlantInversionFeedforward< States, Inputs > frc::LinearSystemLoop< States, Inputs, Outputs >::Feedforward ( ) const
inline

Return the feedforward used internally.

Returns
the feedforward used internally.

◆ NextR() [1/2]

template<int States, int Inputs, int Outputs>
const StateVector & frc::LinearSystemLoop< States, Inputs, Outputs >::NextR ( ) const
inline

Returns the controller's next reference r.

◆ NextR() [2/2]

template<int States, int Inputs, int Outputs>
double frc::LinearSystemLoop< States, Inputs, Outputs >::NextR ( int  i) const
inline

Returns an element of the controller's next reference r.

Parameters
iRow of r.

◆ Observer()

template<int States, int Inputs, int Outputs>
const KalmanFilter< States, Inputs, Outputs > & frc::LinearSystemLoop< States, Inputs, Outputs >::Observer ( ) const
inline

Return the observer used internally.

◆ operator=()

template<int States, int Inputs, int Outputs>
LinearSystemLoop & frc::LinearSystemLoop< States, Inputs, Outputs >::operator= ( LinearSystemLoop< States, Inputs, Outputs > &&  )
default

◆ Predict()

template<int States, int Inputs, int Outputs>
void frc::LinearSystemLoop< States, Inputs, Outputs >::Predict ( units::second_t  dt)
inline

Sets new controller output, projects model forward, and runs observer prediction.

After calling this, the user should send the elements of u to the actuators.

Parameters
dtTimestep for model update.

◆ Reset()

template<int States, int Inputs, int Outputs>
void frc::LinearSystemLoop< States, Inputs, Outputs >::Reset ( const StateVector initialState)
inline

Zeroes reference r and controller output u.

The previous reference of the PlantInversionFeedforward and the initial state estimate of the KalmanFilter are set to the initial state provided.

Parameters
initialStateThe initial state.

◆ SetNextR()

template<int States, int Inputs, int Outputs>
void frc::LinearSystemLoop< States, Inputs, Outputs >::SetNextR ( const StateVector nextR)
inline

Set the next reference r.

Parameters
nextRNext reference.

◆ SetXhat() [1/2]

template<int States, int Inputs, int Outputs>
void frc::LinearSystemLoop< States, Inputs, Outputs >::SetXhat ( const StateVector xHat)
inline

Set the initial state estimate x-hat.

Parameters
xHatThe initial state estimate x-hat.

◆ SetXhat() [2/2]

template<int States, int Inputs, int Outputs>
void frc::LinearSystemLoop< States, Inputs, Outputs >::SetXhat ( int  i,
double  value 
)
inline

Set an element of the initial state estimate x-hat.

Parameters
iRow of x-hat.
valueValue for element of x-hat.

◆ U() [1/2]

template<int States, int Inputs, int Outputs>
InputVector frc::LinearSystemLoop< States, Inputs, Outputs >::U ( ) const
inline

Returns the controller's calculated control input u.

◆ U() [2/2]

template<int States, int Inputs, int Outputs>
double frc::LinearSystemLoop< States, Inputs, Outputs >::U ( int  i) const
inline

Returns an element of the controller's calculated control input u.

Parameters
iRow of u.

◆ Xhat() [1/2]

template<int States, int Inputs, int Outputs>
const StateVector & frc::LinearSystemLoop< States, Inputs, Outputs >::Xhat ( ) const
inline

Returns the observer's state estimate x-hat.

◆ Xhat() [2/2]

template<int States, int Inputs, int Outputs>
double frc::LinearSystemLoop< States, Inputs, Outputs >::Xhat ( int  i) const
inline

Returns an element of the observer's state estimate x-hat.

Parameters
iRow of x-hat.

Member Data Documentation

◆ kInputs

template<int States, int Inputs, int Outputs>
constexpr int frc::LinearSystemLoop< States, Inputs, Outputs >::kInputs = Inputs
staticconstexprprotected

◆ kOutputs

template<int States, int Inputs, int Outputs>
constexpr int frc::LinearSystemLoop< States, Inputs, Outputs >::kOutputs = Outputs
staticconstexprprotected

◆ kStates

template<int States, int Inputs, int Outputs>
constexpr int frc::LinearSystemLoop< States, Inputs, Outputs >::kStates = States
staticconstexprprotected

◆ m_clampFunc

template<int States, int Inputs, int Outputs>
std::function<InputVector(const InputVector&)> frc::LinearSystemLoop< States, Inputs, Outputs >::m_clampFunc
protected

Clamping function.

◆ m_controller

template<int States, int Inputs, int Outputs>
LinearQuadraticRegulator<States, Inputs>* frc::LinearSystemLoop< States, Inputs, Outputs >::m_controller
protected

◆ m_feedforward

template<int States, int Inputs, int Outputs>
LinearPlantInversionFeedforward<States, Inputs> frc::LinearSystemLoop< States, Inputs, Outputs >::m_feedforward
protected

◆ m_nextR

template<int States, int Inputs, int Outputs>
StateVector frc::LinearSystemLoop< States, Inputs, Outputs >::m_nextR
protected

◆ m_observer

template<int States, int Inputs, int Outputs>
KalmanFilter<States, Inputs, Outputs>* frc::LinearSystemLoop< States, Inputs, Outputs >::m_observer
protected

The documentation for this class was generated from the following file: