bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
linear_mpc.h
Go to the documentation of this file.
1#ifndef BITSIM_CONTROL_LINEAR_MPC_H
2#define BITSIM_CONTROL_LINEAR_MPC_H
3
8
9#include <vector>
10
11namespace bitsim
12{
13namespace control
14{
15
46{
47 public:
50
58 const QPSettings &settings = QPSettings());
59
64 void set_model(const Matrix &A, const Matrix &B);
65
70 void set_model(const std::vector<Matrix> &A, const std::vector<Matrix> &B,
71 const std::vector<Vector> &c = {});
72
76 void set_weights(const Matrix &Q, const Matrix &R);
77 void set_weights(const Matrix &Q, const Matrix &R, const Matrix &P);
78
82 void set_input_bounds(const Vector &lower, const Vector &upper);
83
95 void set_state_bounds(const Vector &lower, const Vector &upper, real_t penalty = 0.0);
96
100 void set_reference(const Vector &x_ref, const Vector &u_ref);
101
106 void set_reference(const std::vector<Vector> &x_ref, const std::vector<Vector> &u_ref);
107
113 Vector solve(const Vector &x0);
114
118 const std::vector<Vector> &predicted_states() const noexcept { return x_plan_; }
119
123 const std::vector<Vector> &predicted_inputs() const noexcept { return u_plan_; }
124
125 QPStatus status() const noexcept { return solver_.status(); }
126 uint_t iterations() const noexcept { return solver_.iterations(); }
127 uint_t horizon() const noexcept { return N_; }
128 uint_t n_states() const noexcept { return n_; }
129 uint_t n_inputs() const noexcept { return m_; }
130 const Matrix &terminal_weight() const noexcept { return P_; }
131
132 private:
133 void check_vector_(const Vector &v, uint_t size, const char *what) const;
134 void build_structure_();
135 void build_qp_(const Vector &x0);
136
137 uint_t n_;
138 uint_t m_;
139 uint_t N_;
141
142 std::vector<Matrix> A_;
143 std::vector<Matrix> B_;
144 std::vector<Vector> c_;
145 bool time_invariant_{true};
146 bool has_model_{false};
147
148 Matrix Q_;
149 Matrix R_;
150 Matrix P_;
151 bool has_weights_{false};
152 bool user_terminal_{false};
153
154 Vector u_min_;
155 Vector u_max_;
156 Vector x_min_;
157 Vector x_max_;
158 real_t state_penalty_{0.0};
159
160 std::vector<Vector> x_ref_;
161 std::vector<Vector> u_ref_;
162
163 std::vector<index_t> state_rows_; // the states x_1..x_N with a finite bound
164 bool structure_dirty_{true};
165 bool qp_dirty_{true};
166
167 std::vector<Vector> x_plan_;
168 std::vector<Vector> u_plan_;
169};
170
171} // namespace control
172} // namespace bitsim
173
174#endif // BITSIM_CONTROL_LINEAR_MPC_H
#define BITSIM_EXPORT
Marks a class or a function that bitsim's library exports from a source file compiled with hidden vis...
Definition bitsim_export.h:16
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
uint_t n_states() const noexcept
Definition linear_mpc.h:128
maths::optimization::QPStatus QPStatus
Definition linear_mpc.h:49
uint_t iterations() const noexcept
Definition linear_mpc.h:126
uint_t n_inputs() const noexcept
Definition linear_mpc.h:129
QPStatus status() const noexcept
Definition linear_mpc.h:125
LinearMPC(uint_t n_states, uint_t n_inputs, uint_t horizon, const QPSettings &settings=QPSettings())
Definition linear_mpc.cpp:17
maths::optimization::QPSettings QPSettings
Definition linear_mpc.h:48
const std::vector< Vector > & predicted_states() const noexcept
Definition linear_mpc.h:118
const std::vector< Vector > & predicted_inputs() const noexcept
Definition linear_mpc.h:123
const Matrix & terminal_weight() const noexcept
Definition linear_mpc.h:130
uint_t horizon() const noexcept
Definition linear_mpc.h:127
void set_model(const Matrix &A, const Matrix &B)
A time-invariant model, , in discrete time; see discretise().
Definition linear_mpc.cpp:41
A solver of the convex quadratic program.
Definition qp_solver.h:82
Linear quadratic regulators: the Riccati equations, the discretisation of a linear model,...
Controllers: PID controllers, line following, a quadrotor flight controller, a fixed wing autopilot,...
Definition control_module.h:18
RealMat Matrix
The matrices of the linear models and the controllers.
Definition lqr.h:20
RealColVec Vector
The vectors of the linear models and the controllers: states, inputs.
Definition lqr.h:23
QPStatus
How a solve ended.
Definition qp_solver.h:49
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
The settings of QPSolver.
Definition qp_solver.h:23