bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
diff_drive_mpc::LinearMPC Class Reference

Model predictive control with a linear model, a quadratic cost and bounds on the inputs and the states. More...

#include <linear_mpc.h>

Public Types

using QPSettings = maths::optimization::QPSettings
using QPStatus = maths::optimization::QPStatus

Public Member Functions

 LinearMPC (uint_t n_states, uint_t n_inputs, uint_t horizon, const QPSettings &settings=QPSettings())
void set_model (const Matrix &A, const Matrix &B)
 A time-invariant model, \(x_{k+1} = A x_k + B u_k\), in discrete time; see discretise().
void set_model (const std::vector< Matrix > &A, const std::vector< Matrix > &B, const std::vector< Vector > &c={})
 A model that changes along the horizon: N matrices A and B, and N vectors c, or none for c = 0.
void set_weights (const Matrix &Q, const Matrix &R)
 The weights. Without P, it is the Riccati solution of a time-invariant model, or Q.
void set_weights (const Matrix &Q, const Matrix &R, const Matrix &P)
void set_input_bounds (const Vector &lower, const Vector &upper)
 Bounds on each input; \(\pm\infty\) for none.
void set_state_bounds (const Vector &lower, const Vector &upper, real_t penalty=0.0)
 Bounds on each state, at the steps 1 to N.
void set_reference (const Vector &x_ref, const Vector &u_ref)
 The same reference at every step: the state and the input that holds it.
void set_reference (const std::vector< Vector > &x_ref, const std::vector< Vector > &u_ref)
 A reference that changes along the horizon: the states at the steps 1 to N, N of them, and the inputs at the steps 0 to N - 1, N of them.
Vector solve (const Vector &x0)
 Plan from the state x0.
const std::vector< Vector > & predicted_states () const noexcept
const std::vector< Vector > & predicted_inputs () const noexcept
QPStatus status () const noexcept
uint_t iterations () const noexcept
uint_t horizon () const noexcept
uint_t n_states () const noexcept
uint_t n_inputs () const noexcept
const Matrix & terminal_weight () const noexcept

Detailed Description

Model predictive control with a linear model, a quadratic cost and bounds on the inputs and the states.

At every step it plans the inputs \(u_0, \dots, u_{N-1}\) over a horizon of N steps, from the state now, \(x_0\), that minimise

\[\sum_{k=1}^{N-1} (x_k - r_k)^T Q (x_k - r_k) + (x_N - r_N)^T P (x_N - r_N) + \sum_{k=0}^{N-1} (u_k - v_k)^T R (u_k - v_k) \]

subject to the model \(x_{k+1} = A_k x_k + B_k u_k + c_k\) and the bounds \(u_{min} \le u_k \le u_{max}\) and \(x_{min} \le x_k \le x_{max}\), hard or soft, with r the reference of the states and v that of the inputs. The application uses \(u_0\) and plans again at the next step, from the state then.

The model is time-invariant, set_model(A, B), or changes along the horizon, set_model(As, Bs, cs): a nonlinear model linearised along a reference trajectory, with \(c_k = f(\bar x_k, \bar u_k) - A_k \bar x_k - B_k \bar u_k\). The terminal weight P is by default the solution of the discrete Riccati equation of a time-invariant model, which makes the cost beyond the horizon that of the LQR, and Q otherwise.

The problem is condensed: the states are written in terms of \(x_0\) and the inputs, so the QP has the N m inputs as its variables. maths::optimization::QPSolver solves it, starting from the solution of the step before. A bound of \(\pm\infty\) is no bound. State bounds that the system cannot meet make the QP infeasible: solve() then returns the inputs of the last iteration, and status() says that it did not converge.

Member Typedef Documentation

◆ QPSettings

using bitsim::control::LinearMPC::QPSettings = maths::optimization::QPSettings

◆ QPStatus

using bitsim::control::LinearMPC::QPStatus = maths::optimization::QPStatus

Constructor & Destructor Documentation

◆ LinearMPC()

bitsim::control::LinearMPC::LinearMPC ( uint_t n_states,
uint_t n_inputs,
uint_t horizon,
const QPSettings & settings = QPSettings() )
Parameters
n_statesThe number of states
n_inputsThe number of inputs
horizonThe number of steps it plans, N
settingsThe settings of the QP solver

Member Function Documentation

◆ horizon()

uint_t bitsim::control::LinearMPC::horizon ( ) const
inlinenoexcept

◆ iterations()

◆ n_inputs()

uint_t bitsim::control::LinearMPC::n_inputs ( ) const
inlinenoexcept

◆ n_states()

uint_t bitsim::control::LinearMPC::n_states ( ) const
inlinenoexcept

◆ predicted_inputs()

const std::vector< Vector > & bitsim::control::LinearMPC::predicted_inputs ( ) const
inlinenoexcept
Returns
The planned inputs, \(u_0\) to \(u_{N-1}\), of the last solve

◆ predicted_states()

const std::vector< Vector > & bitsim::control::LinearMPC::predicted_states ( ) const
inlinenoexcept
Returns
The planned states, \(x_0\) to \(x_N\), of the last solve

◆ set_input_bounds()

void bitsim::control::LinearMPC::set_input_bounds ( const Vector & lower,
const Vector & upper )

Bounds on each input; \(\pm\infty\) for none.

◆ set_model() [1/2]

void bitsim::control::LinearMPC::set_model ( const Matrix & A,
const Matrix & B )

A time-invariant model, \(x_{k+1} = A x_k + B u_k\), in discrete time; see discretise().

◆ set_model() [2/2]

void bitsim::control::LinearMPC::set_model ( const std::vector< Matrix > & A,
const std::vector< Matrix > & B,
const std::vector< Vector > & c = {} )

A model that changes along the horizon: N matrices A and B, and N vectors c, or none for c = 0.

◆ set_reference() [1/2]

void bitsim::control::LinearMPC::set_reference ( const std::vector< Vector > & x_ref,
const std::vector< Vector > & u_ref )

A reference that changes along the horizon: the states at the steps 1 to N, N of them, and the inputs at the steps 0 to N - 1, N of them.

◆ set_reference() [2/2]

void bitsim::control::LinearMPC::set_reference ( const Vector & x_ref,
const Vector & u_ref )

The same reference at every step: the state and the input that holds it.

◆ set_state_bounds()

void bitsim::control::LinearMPC::set_state_bounds ( const Vector & lower,
const Vector & upper,
real_t penalty = 0.0 )

Bounds on each state, at the steps 1 to N.

Parameters
lowerThe lower bound of each state; \(-\infty\) for none
upperThe upper bound of each state; \(+\infty\) for none
penalty0 for hard bounds. A positive penalty makes them soft: the plan may break a bound by s, at the cost penalty \((s + s^2)\), which keeps the QP feasible when the system cannot keep to the bounds, as after a large disturbance. The linear part makes the penalty exact: when the bounds can be met and the penalty is large enough, the plan meets them. ADMM converges slowly when the penalty dwarfs the weights: 100 to 1000 times the largest weight of Q is enough to make the bounds hold, and converges well.

◆ set_weights() [1/2]

void bitsim::control::LinearMPC::set_weights ( const Matrix & Q,
const Matrix & R )

The weights. Without P, it is the Riccati solution of a time-invariant model, or Q.

◆ set_weights() [2/2]

void bitsim::control::LinearMPC::set_weights ( const Matrix & Q,
const Matrix & R,
const Matrix & P )

◆ solve()

Plan from the state x0.

Returns
The input to use now, \(u_0\)
Exceptions
std::logic_errorif the model or the weights are not set

◆ status()

QPStatus bitsim::control::LinearMPC::status ( ) const
inlinenoexcept

◆ terminal_weight()

const Matrix & bitsim::control::LinearMPC::terminal_weight ( ) const
inlinenoexcept

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