|
Robotat Linalg
|
Library for control algorithms based on Robotat Linalg. Includes routines for PID controllers, LTI systems, non linear systems, Kalman Filters and an LTI, shooting-based MPC. More...
Data Structures | |
| struct | ctr_pid_t |
| PID controller data structure, to represent a discretized PID, based on the following definition of a PID and selecting one of the available discretization methods: More... | |
| struct | ctr_sys_lti_t |
| State space LTI system data structure, based on the following definition: More... | |
| struct | ctr_sys_nonlin_t |
| State space nonlinear system data structure. More... | |
| struct | ctr_kalman_t |
| Linear time-varying Kalman filter data structure, based on the following definition: More... | |
| struct | ctr_mpc_lti_shooting_t |
| Struct for the matrices to be used in the MPC (Unconstrained, Shooting-Based, LTI) Can be implemented with or without constraints. More... | |
Enumerations | |
| enum | ctr_discretizations_t { PURE_DISCRETE , FWD_EULER , BWD_EULER , TUSTIN , ZOH , RK4 } |
| Discretization specification data type. More... | |
Functions | |
| void | ctr_pid_init (ctr_pid_t *const pid, float kp, float ki, float kd, ctr_discretizations_t pid_alg, bool set_i_limits,...) |
| Initializes a PID controller structure. | |
| float | ctr_pid_update (ctr_pid_t *const pid, float r_k, float y_k) |
| Updates a previously initialized PID controller based on the discretization method selected for the controller. The equations corresponding to each method are described below: | |
| err_status_t | ctr_ss_lti (matf32_t *A, matf32_t *B, matf32_t *C, matf32_t *D, float sample_time, ctr_sys_lti_t *const sys) |
| Initializes a state space LTI model. | |
| err_status_t | ctr_c2d (ctr_sys_lti_t *const sys, float sample_time, ctr_discretizations_t method) |
| Discretizes a continuous time state space LTI system model, to get the following form: | |
| err_status_t | ctr_sys_lti_init (ctr_sys_lti_t *const sys, matf32_t *const state, matf32_t *const A, matf32_t *const B, matf32_t *const C, matf32_t *const D, float sample_time) |
| Initialices an instance of the ctr_sys_lti_t struct for lti linear systems. | |
| err_status_t | ctr_sys_nonlin_init (ctr_sys_nonlin_t *const sys, matf32_t *const state, uint16_t input_dim, uint16_t output_dim, err_status_t(*dynamics)(matf32_t *const, const matf32_t *, const matf32_t *), err_status_t(*outputs)(matf32_t *const, const matf32_t *, const matf32_t *), float sample_time) |
| Initializes an instance of the ctr_sys_nonlin_t struct for nonlinear systems. | |
| err_status_t | ctr_linloc (ctr_sys_nonlin_t *const src_sys, ctr_sys_lti_t *const dst_sys, const matf32_t *const xss, const matf32_t *const uss, float delta) |
| Linearizes a nonlinear system: input a nonlinear system, outputs a linear version, using the corresponding structs: ctr_sys_nonlin_t and ctr_sys_lti_t. | |
| err_status_t | ctr_linear_state_feedback (matf32_t *const u, const matf32_t *K, const matf32_t *x, const matf32_t *xss, const matf32_t *uss) |
| Updates/computes the linear state feedback controller u = -K * (x - xss) + uss. | |
| err_status_t | ctr_sys_nonlin_simulate (ctr_sys_nonlin_t *sys, const matf32_t *const x_k, matf32_t *const x_k_1, const matf32_t *const u_k, float delta, ctr_discretizations_t method) |
| Updates the step for a nonlinear system with either forward euler or runge-kutta-4 methods. | |
| err_status_t | ctr_kalman_init (ctr_kalman_t *const kf, ctr_sys_lti_t *const sys, matf32_t *F, matf32_t *Qw, matf32_t *Qv, matf32_t *const xhat, matf32_t *const P) |
| Initializes a linear, time-varying Kalman filter structure. | |
| err_status_t | ctr_kalman_predict (ctr_kalman_t *const kf, const matf32_t *inputs) |
| Computes the prediction step for the Kalman Filter, based on the following equations: | |
| err_status_t | ctr_kalman_correct (ctr_kalman_t *const kf, const matf32_t *measurements) |
| Computes the correction step for the Kalman Filter, based on the following equations: | |
| err_status_t | ctr_mpc_lti_init (ctr_mpc_lti_shooting_t *mpc, quadprog_t *qp, ctr_sys_lti_t *sys, matf32_t *const u_k, matf32_t *const x_k, matf32_t *const Ain, matf32_t *const bin, matf32_t *const mpc_Q, matf32_t *const mpc_R, matf32_t *const mpc_S, matf32_t **const mpc_C, matf32_t **const mpc_M, float N, bool state_constraints) |
| Initializes an unconstrained, shooting-based, LTI, MPC struct. All needed data must be declared and initialized beforehand, including matrices, arrays of matrices, along with the quadratic problem and MPC structs. | |
| err_status_t | ctr_mpc_set_M (ctr_mpc_lti_shooting_t *mpc, float **mpc_M_data) |
| Generates the prediction matrix \( \textbf{M}_x \) for the MPC, generated as an array of matrices, such that: | |
| err_status_t | ctr_mpc_set_C (ctr_mpc_lti_shooting_t *mpc, matf32_t **mpc_C_stack, float **mpc_C_stack_data) |
| Generates the convolution matrix \( \textbf{M}_c \) for the MPc, generated as an array of matrices, such that inside the MPC routines it's operated as a matrix of matrices. In simple terms, that is: | |
| err_status_t | ctr_mpc_set_qpQ (ctr_mpc_lti_shooting_t *mpc, matf32_t *qp_Q) |
| Generates the quadratic term matrix \( \textbf{Q} \) for the quadratic program associated to the MPC, following the equation: | |
| err_status_t | ctr_mpc_set_qpc (ctr_mpc_lti_shooting_t *mpc, matf32_t *qp_c) |
| Generates the linear term matrix \( c \) for the quadratic program of the associated to the MPC, following the equation: | |
| err_status_t | ctr_mpc_update (ctr_mpc_lti_shooting_t *mpc, matf32_t *const qp_Q, matf32_t *const qp_c, matf32_t *const x_k, matf32_t *const u_k) |
| Calculates next state trajectory of the MPC, executing the following steps: | |
| err_status_t | ctr_mpc_set_constraints (ctr_mpc_lti_shooting_t *mpc, float ub, float lb) |
| Generates the inequality matrix and vector according to whether only the input or both the input and state are constrained, according to the following definitions: | |
| void | ctr_pid_print (ctr_pid_t *const p_pid) |
| Prints the ctr_pid_t struct data in formatted text. | |
| void | ctr_discretizations_print (ctr_discretizations_t pid_alg) |
| Prints the elements of ctr_discretizations_t. | |
| void | ctr_sys_lti_print (ctr_sys_lti_t *p_sys_lti) |
| Prints the ctr_sys_lti_t struct in formatted text. | |
| void | ctr_kalman_print (ctr_kalman_t *p_kalman) |
| Prints the elements of ctr_kalman_t. | |
| void | ctr_gen_impulse (float *signal, uint16_t samples, uint16_t impulse_index) |
| Generates an impulse signal based on a previously defined array. | |
| void | ctr_gen_step (float *signal, uint16_t samples, uint16_t step_start_index) |
| Generates a step signal based on a previously defined array. | |
Library for control algorithms based on Robotat Linalg. Includes routines for PID controllers, LTI systems, non linear systems, Kalman Filters and an LTI, shooting-based MPC.
Last modified: 31 Jan. 2026 By: Andrea Pineda
| struct ctr_pid_t |
PID controller data structure, to represent a discretized PID, based on the following definition of a PID and selecting one of the available discretization methods:
\[C(s) = k_P + \frac{k_I}{s} + k_D s \]
| Data Fields | ||
|---|---|---|
| float | dt |
Sampling period. |
| float | e_k |
Error |
| float | e_k_1 |
Last error. |
| float | e_k_2 |
Error previous to last error |
| float | i_max |
Upper integrator saturation threshold. |
| float | i_min |
Lower integrator saturation threshold. |
| float | kd |
Derivative gain. |
| float | ki |
Integral gain. |
| float | kp |
Proportional gain. |
| ctr_discretizations_t | pid_alg |
Specifies the discretization scheme to be used. |
| float | tau |
Time constant of the derivative HPF. |
| float | u_k_1 |
Last controller output. |
| float | u_k_2 |
Control output previous to last controller output |
| struct ctr_sys_lti_t |
State space LTI system data structure, based on the following definition:
\[\begin{aligned} \textbf{x}(t) &= \textbf{Ax}(t) + \textbf{Bu}(t) \\ \textbf{y}(t) &= \textbf{Cx}(t) + \textbf{Du}(t) \\ \end{aligned} \]
| Data Fields | ||
|---|---|---|
| matf32_t * | A |
System matrix. |
| matf32_t * | B |
Input/actuator matrix. |
| matf32_t * | C |
Output/sensor matrix. |
| matf32_t * | D |
Feedforward terms. |
| float | dt |
Sampling period (for discrete time systems). |
| uint16_t | input_dim |
Number of inputs/actuators/controls. |
| bool | is_continuous |
System time domain specification. |
| uint16_t | output_dim |
Number of outputs/measurements. |
| matf32_t * | state |
State at the current time step (used for simulation/integration). |
| uint16_t | state_dim |
Number of state variables. This is redundant but we'll keep it for completeness. |
| struct ctr_sys_nonlin_t |
State space nonlinear system data structure.
Data Fields | |
| matf32_t * | state |
| uint16_t | state_dim |
| uint16_t | input_dim |
| uint16_t | output_dim |
| err_status_t(* | dynamics )(matf32_t *const, const matf32_t *, const matf32_t *) |
| err_status_t(* | outputs )(matf32_t *const, const matf32_t *, const matf32_t *) |
| float | dt |
| bool | is_continuous |
| float ctr_sys_nonlin_t::dt |
Sampling period (for discrete time systems).
| err_status_t(* ctr_sys_nonlin_t::dynamics) (matf32_t *const, const matf32_t *, const matf32_t *) |
System dynamics.
| uint16_t ctr_sys_nonlin_t::input_dim |
Number of inputs/actuators/controls.
| bool ctr_sys_nonlin_t::is_continuous |
System time domain specification.
| uint16_t ctr_sys_nonlin_t::output_dim |
Number of outputs/measurements.
| err_status_t(* ctr_sys_nonlin_t::outputs) (matf32_t *const, const matf32_t *, const matf32_t *) |
System outputs.
| matf32_t* ctr_sys_nonlin_t::state |
State at the current time step (used for simulation/integration).
| uint16_t ctr_sys_nonlin_t::state_dim |
Number of state variables. This is redundant but we'll keep it for completeness.
| struct ctr_kalman_t |
Linear time-varying Kalman filter data structure, based on the following definition:
\[\begin{aligned} \textbf{x}_{k+1} &= \textbf{F} \textbf{x}_k + \textbf{G} \textbf{u}_k + \textbf{v}_k, \\ \textbf{z}_{k+1} &= \textbf{H} \textbf{x}_k + \textbf{w}_k \end{aligned} \]
| Data Fields | ||
|---|---|---|
| matf32_t * | F |
Coupling matrix for the process noise. |
| matf32_t * | P |
Estimation covariance matrix. |
| matf32_t * | Qv |
Measurement noise covariance matrix. |
| matf32_t * | Qw |
Process noise covariance matrix. |
| ctr_sys_lti_t * | sys |
LTI system model(has to be discrete time). |
| matf32_t * | xhat |
State estimate. |
| struct ctr_mpc_lti_shooting_t |
Struct for the matrices to be used in the MPC (Unconstrained, Shooting-Based, LTI) Can be implemented with or without constraints.
| Data Fields | ||
|---|---|---|
| matf32_t * | Ain |
Inequality restrictions matrix |
| matf32_t * | bin |
Inequality restrictions vector |
| float | lb |
Lower bounds limit |
| matf32_t ** | mpc_C |
Points to convolution matrix C data arrays |
| matf32_t ** | mpc_M |
Points to data arrays for the powers of A matrix M |
| matf32_t * | mpc_Q |
Points to penalization matrix Q |
| matf32_t * | mpc_R |
Points to penalization matrix R |
| matf32_t * | mpc_S |
Points to penalization matrix S |
| float | N |
Horizon length |
| quadprog_t * | qp |
Quadratic Program (QP) struct |
| bool | state_constraints |
Boolean: 0=input-only constraints, 1=include state limits as input constraints |
| ctr_sys_lti_t * | sys |
LTI system struct |
| matf32_t * | u_k |
Predicted input trajectory |
| float | ub |
Upper bounds limit |
| matf32_t * | x_k |
Predicted state trajectory |
Discretization specification data type.
Dependencies.
Used to specify the numerical integration scheme to use when discretizing LTI systems and controllers.
| err_status_t ctr_c2d | ( | ctr_sys_lti_t *const | sys, |
| float | sample_time, | ||
| ctr_discretizations_t | method ) |
Discretizes a continuous time state space LTI system model, to get the following form:
\[\begin{aligned} \textbf{x}_{k+1} &= \textbf{Ax} + \textbf{Bu} \\ \textbf{y} &= \textbf{Cx} + \textbf{Du} \\ \end{aligned} \]
WARNING: this routine overwrites the original continuous time system. This also does NOT work for discrete time systems (as it's meant for continuous time systems).
| [in,out] | sys | Continuous time LTI system data structure. |
| [in] | sample_time | Sampling period. |
| [in] | method | Discretization scheme. |
| void ctr_discretizations_print | ( | ctr_discretizations_t | pid_alg | ) |
Prints the elements of ctr_discretizations_t.
| [in] | pid_alg | Enumerate list of discretization algorithms |
| void ctr_gen_impulse | ( | float * | signal, |
| uint16_t | samples, | ||
| uint16_t | impulse_index ) |
Generates an impulse signal based on a previously defined array.
| [in,out] | signal | Array for the signal |
| [in] | samples | Length of the signal |
| [in] | impulse_index | Index at which to generate the impulse |
| void ctr_gen_step | ( | float * | signal, |
| uint16_t | samples, | ||
| uint16_t | step_start_index ) |
Generates a step signal based on a previously defined array.
| [in,out] | signal | Array for the signal |
| [in] | samples | Length of the signal |
| [in] | step_start_index | Index starting from which to generate the step |
| err_status_t ctr_kalman_correct | ( | ctr_kalman_t *const | kf, |
| const matf32_t * | measurements ) |
Computes the correction step for the Kalman Filter, based on the following equations:
\[\begin{aligned} \boldsymbol{\nu}_{k+1} &= \textbf{z}_{k+1} - \textbf{H} \hat{\textbf{x}}_{k+1|k}, \\ \hat{\textbf{x}}_{k+1|k+1} &= \hat{\textbf{x}}_{k+1|k} + \textbf{K}_{k+1} \boldsymbol{\nu}_{k+1}, \\ \hat{\textbf{P}}_{k+1|k+1} &= \hat{\textbf{P}}_{k+1|k} - \textbf{K}_{k+1} \textbf{H} \hat{\textbf{P}}_{k+1|k} \end{aligned} \]
| [in,out] | kf | Kalman filter data structure. |
| [in] | measurements | Estimate from the output of the system. |
| err_status_t ctr_kalman_init | ( | ctr_kalman_t *const | kf, |
| ctr_sys_lti_t *const | sys, | ||
| matf32_t * | F, | ||
| matf32_t * | Qw, | ||
| matf32_t * | Qv, | ||
| matf32_t *const | xhat, | ||
| matf32_t *const | P ) |
Initializes a linear, time-varying Kalman filter structure.
| [in,out] | kf | Kalman filter data structure. |
| [in] | sys | LTI system model. |
| [in] | F | Coupling matrix of the process noise. |
| [in] | Qw | Process noise covariance matrix. |
| [in] | Qv | Measurement noise covariance matrix. |
| [in] | xhat | State estimate. |
| [in] | P | Estimation covariance matrix. |
| err_status_t ctr_kalman_predict | ( | ctr_kalman_t *const | kf, |
| const matf32_t * | inputs ) |
Computes the prediction step for the Kalman Filter, based on the following equations:
\[\begin{aligned} \hat{\textbf{x}}_{k+1|k} &= \textbf{F} \hat{\textbf{x}}_k + \textbf{Gu}_k, \\ \hat{\textbf{P}}_{k+1|k} &= \textbf{F} \hat{\textbf{P}}_{k|k} \textbf{F}^\top + \hat{\textbf{V}} \end{aligned} \]
| [in,out] | kf | Kalman filter data structure. |
| [in] | inputs | Estimate from the input of the system. |
| void ctr_kalman_print | ( | ctr_kalman_t * | p_kalman | ) |
Prints the elements of ctr_kalman_t.
| [in] | p_kalman | Pointer to the struct to print |
| err_status_t ctr_linear_state_feedback | ( | matf32_t *const | u, |
| const matf32_t * | K, | ||
| const matf32_t * | x, | ||
| const matf32_t * | xss, | ||
| const matf32_t * | uss ) |
Updates/computes the linear state feedback controller u = -K * (x - xss) + uss.
| [in,out] | u | Controller output. |
| [in] | K | Gain matrix. |
| [in] | x | State vector. |
| [in] | xss | Operation point (desired state). |
| [in] | uss | Feedforward input to reach the desired state. |
| err_status_t ctr_linloc | ( | ctr_sys_nonlin_t *const | src_sys, |
| ctr_sys_lti_t *const | dst_sys, | ||
| const matf32_t *const | xss, | ||
| const matf32_t *const | uss, | ||
| float | delta ) |
Linearizes a nonlinear system: input a nonlinear system, outputs a linear version, using the corresponding structs: ctr_sys_nonlin_t and ctr_sys_lti_t.
| [in] | src_sys | Points to nonlinear system struct instance |
| [in,out] | dst_sys | Points to lti system struct instance |
| [in] | xss | xss for the operation point (xss,uss) |
| [in] | uss | uss for the operation point (xss,uss) |
| [in] | delta | Diferences to divide by for the linearization |
| err_status_t ctr_mpc_lti_init | ( | ctr_mpc_lti_shooting_t * | mpc, |
| quadprog_t * | qp, | ||
| ctr_sys_lti_t * | sys, | ||
| matf32_t *const | u_k, | ||
| matf32_t *const | x_k, | ||
| matf32_t *const | Ain, | ||
| matf32_t *const | bin, | ||
| matf32_t *const | mpc_Q, | ||
| matf32_t *const | mpc_R, | ||
| matf32_t *const | mpc_S, | ||
| matf32_t **const | mpc_C, | ||
| matf32_t **const | mpc_M, | ||
| float | N, | ||
| bool | state_constraints ) |
Initializes an unconstrained, shooting-based, LTI, MPC struct. All needed data must be declared and initialized beforehand, including matrices, arrays of matrices, along with the quadratic problem and MPC structs.
For an unconstrained MPC, set Ain and bin equal to NULL. Otherwise, declare, initialize and set the corresponding matrices.
| [in,out] | mpc | Points to the corresponding MPC struct. |
| [in] | x0 | Points to the initial operation point matrix |
| [in] | Q | Points to the quadratic term matrix for the cost function of the QP |
| [in] | c | Points to the linear term matrix for the cost function of the QP |
| [in] | Qhat | Points to the penalization matrix Q |
| [in] | Rhat | Points to the penalization matrix R |
| [in] | C | Points to the convolution matrix C |
| [in] | horizon_length | Horizon length (samples). |
| [in] | M | Points to the horizon length matrix M |
| [in] | u_k | Points to the input trajectory matrix |
| [in] | x_k | Points to the state trajectory matrix |
| err_status_t ctr_mpc_set_C | ( | ctr_mpc_lti_shooting_t * | mpc, |
| matf32_t ** | mpc_C_stack, | ||
| float ** | mpc_C_stack_data ) |
Generates the convolution matrix \( \textbf{M}_c \) for the MPc, generated as an array of matrices, such that inside the MPC routines it's operated as a matrix of matrices. In simple terms, that is:
\[ \textbf{M}_c = \begin{bmatrix} \textbf{B} & \textbf{0} & \cdots & \textbf{0} \\ \boldsymbol{A} \textbf{B} & \textbf{B} & \cdots & \textbf{0} \\ \vdots & \vdots & \ddots & \vdots \\ \boldsymbol{A} ^{N-1}\textbf{B} & A ^{N-2} \textbf{B} & \cdots & \textbf{B} \end{bmatrix} \]
Similar to ctr_mpc_set_M, the array of pointers must be declared before calling this routine (ctr_mpc_set_C) and the necessary matrices must be declared and initialized before as well. That is, define and initialize matf32_t matrices for all matrices in the first column of \( \textbf{M}_c \), as well as an additional null matrix to match the dimensiones in the operations that will be done with other routines. Save those matrices in their own array (mpc_C_stack) in the same order as in the first column of the convolution matrix (with the null matrix at the end of the array). This routine computes all matrices individually, except \( \textbf{B} \) which should be defined with the LTI system to be optimized.
| [in,out] | mpc | Points to the MPC struct |
| [in,out] | mpc_C_stack | Points to the stack of matrices needed to build C |
| [in,out] | mpc_C_stack_data | Points to the array of pointers for C |
| err_status_t ctr_mpc_set_constraints | ( | ctr_mpc_lti_shooting_t * | mpc, |
| float | ub, | ||
| float | lb ) |
Generates the inequality matrix and vector according to whether only the input or both the input and state are constrained, according to the following definitions:
If only the input vector is to be constrained:
\[\begin{bmatrix} lb \\ \vdots \\ lb \end{bmatrix} \le \textbf{u}_k \le \begin{bmatrix} ub \\ \vdots \\ ub \end{bmatrix}, \]
If the state is to be constrained as well: in this case, this is done through the restrictions of the input vector, given that this is for a shooting-based MPC. That is, the state constraints are written and computed as follows:
\[\textbf{x}_{lb} - \textbf{Bu}_1 \le \textbf{Au}_k \le \textbf{x}_{ub} - \textbf{B}\textbf{u}_1 \]
In both cases, \( lb \) and \( ub \) refer to the lower and upper constraints (numerical values), while all the matrices in the above equations correspond to those of the LTI system.
| [in,out] | mpc | Points to the corresponding MPC struct |
| [in] | ub | Upper bound |
| [in] | lb | Lower bound |
| [in] | state_constraints | Boolean: 0=only input constraints, 1=include state constraints as input constraints |
| err_status_t ctr_mpc_set_M | ( | ctr_mpc_lti_shooting_t * | mpc, |
| float ** | mpc_M_data ) |
Generates the prediction matrix \( \textbf{M}_x \) for the MPC, generated as an array of matrices, such that:
\[ \textbf{M}_x = [\textbf{A}, \textbf{A}^2, ... , \textbf{A}^N] \]
according to the definition of the matrix in Kouvaritakis, Model Predictive Control.
In terms of code, that means that \( \textbf{M}_x \) is an array of pointers, and each index in it contains a different matrix, that is: M = [&M1, &M2, ... , &MN], where each matrix M corresponds to a power of A. Both the array of pointers and the individual matrices must be declared (and the matrices initialized with matf32_init) before calling this routine. The content of each matrix is computed and set with this routine upon execution, except for \( \textbf{A} \) which should be already defined with the values of the LTI system to be optimized.
| [in,out] | mpc | Points to the MPC struct. |
| [in,out] | mpc_M_data | Points to the data arrays for the submatrices of M |
| err_status_t ctr_mpc_set_qpc | ( | ctr_mpc_lti_shooting_t * | mpc, |
| matf32_t * | qp_c ) |
Generates the linear term matrix \( c \) for the quadratic program of the associated to the MPC, following the equation:
\[ \textbf{c} = 2 \textbf{F} \mathrm{\textbf{x}}_k \]
\[ \textbf{F} = \textbf{M}^\top_c \tilde{\textbf{Q}} \textbf{M}_x \]
where \( \textbf{x}_k \) is the state vector of the LTI system, \( \textbf{M}^\top_c \) and \( \textbf{M}_x \) are the prediction matrices of the MPC, and \( \tilde{\textbf{Q}} \) is the penalization matrix as used in the Linear-Quadratic Regulator (LQR).
The equations above were adapted from the definition of the MPC in Kouvaritakis, Model Predictive Control.
| [in] | mpc | Points to the MPC struct. |
| [in,out] | qp_c | Points to the c matrix for the quadratic program. |
| err_status_t ctr_mpc_set_qpQ | ( | ctr_mpc_lti_shooting_t * | mpc, |
| matf32_t * | qp_Q ) |
Generates the quadratic term matrix \( \textbf{Q} \) for the quadratic program associated to the MPC, following the equation:
\[ \textbf{Q} = 2 \cdot (\textbf{M}^\top_c \tilde{\textbf{Q}} \textbf{M}_c + \tilde{\textbf{R}}) \]
where \( \tilde{\textbf{Q}} \) and \( \tilde{\textbf{R}} \) correspond the penalization matrices as implemented in the Linear-Quadratic Regulator (LQR), while \( \textbf{M}_c \) is one of the prediction matrices of the MPC (see ctr_mpc_set_M).
The above equation was adapted from the definition of the MPC as explained in Kouvaritakis, Model Predictive Control.
| [in] | mpc | Points to the MPC struct. |
| [in,out] | qp_Q | Points to the Q matrix for the quadratic program. |
| err_status_t ctr_mpc_update | ( | ctr_mpc_lti_shooting_t * | mpc, |
| matf32_t *const | qp_Q, | ||
| matf32_t *const | qp_c, | ||
| matf32_t *const | x_k, | ||
| matf32_t *const | u_k ) |
Calculates next state trajectory of the MPC, executing the following steps:
Updating the LTI system, by using the result of the solution of the QP. In this case, the QP is used to recalculate the input vector, generating N values according as a projection to the entire horizon length of the MPC, so only the first value of the new input vector is used to compute the next state of the LTI system, and this is done through the definition of the system:
\[ \textbf{x}_{k+1} = \textbf{Ax}_k + \textbf{Bu} \]
| [in,out] | mpc | Points to the corresponding MPC struct. |
| [in] | x_k | Points to the current state trajectory |
| [in] | u_k | Points to the current input trajectory |
| void ctr_pid_init | ( | ctr_pid_t *const | pid, |
| float | kp, | ||
| float | ki, | ||
| float | kd, | ||
| ctr_discretizations_t | pid_alg, | ||
| bool | set_i_limits, | ||
| ... ) |
Initializes a PID controller structure.
The controller transfer function (except for the PURE_DISCRETE case) is given by: C(s) = kP + kI/s + kD * tau*s / (s + tau). Depending on the discretization scheme and whether or not the integrator saturates, the function can ask for additional parameters:
1) ctr_pid_init(pid, kp, ki, kd, PURE_DISCRETE, 0); 2) ctr_pid_init(pid, kp, ki, kd, PURE_DISCRETE, 1, i_min, i_max); // With saturation limits. 3) ctr_pid_init(pid, kp, ki, kd, pid_alg, 0, dt, tau); // Needs sampling period and time constant. 4) ctr_pid_init(pid, kp, ki, kd, pid_alg, 1, dt, tau, i_min, i_max); // Needs all info.
| [in,out] | pid | PID controller data structure. |
| [in] | kp | Proportional gain. |
| [in] | ki | Integral gain. |
| [in] | kd | Derivative gain. |
| [in] | pid_alg | PID discretization scheme specification. |
| [in] | set_i_limits | Allows to set lower and upper saturation thresholds for the integrator. |
| [in] | dt | Sampling period. |
| [in] | tau | Derivative HPF time constant. |
| [in] | i_min | Lower saturation threshold. |
| [in] | i_max | Upper saturation threshold. |
| void ctr_pid_print | ( | ctr_pid_t *const | p_pid | ) |
Prints the ctr_pid_t struct data in formatted text.
| [in] | p_pid | Pointer to the pid to print |
| float ctr_pid_update | ( | ctr_pid_t *const | pid, |
| float | r_k, | ||
| float | y_k ) |
Updates a previously initialized PID controller based on the discretization method selected for the controller. The equations corresponding to each method are described below:
Pure Discrete:
\[\begin{aligned} \end{aligned} u_k = k_Pe_k + k_I(e_{k-1} + e_k) + k_D(e_k - e_{k-1}) \]
Forward Euler:
\[\begin{aligned} a_0 &= \tau \\ u_k &= k_P \tau + k_I \tau + k_D)e_k \\ &+ (k_P(T - 2\tau) + k_I(T- \tau) - 2k_D)e_{k-1} \\ &+ (k_P(\tau - T) + k_D)e_{k-2} \\ &- (T - 2\tau)e_{k-2} \\ &- (T - 2\tau) u_{k-1} \\ &- (\tau - T)u_{k-2} \\ u_k &= u_k / a_0 \end{aligned} \]
Backward Euler:
\[\begin{aligned} a_0 &= T + \tau \\ u_k &= (k_P(T + \tau) + k_I(T + \tau) + k_D)e_k \\ &+ (-k_P(2\tau + T) - k_I\tau - 2k_D)e_{k-1}) \\ &+ (k_P\tau + k_D)e_{k-2} \\ &+ (2\tau + T)u_{k-1} \\ &- \tau u_{k-2} \\ u_k &= u_k / a_0 \end{aligned} \]
Tustin:
\[\begin{aligned} a_0 &= 2\tau + T \\ u_k &= (k_P(2\tau + T) + \frac{1}{2} k_I(2\tau + T) + 2k_D)e_k \\ &+ (-4k_P\tau + \frac{1}{2} k_I(-2\tau + T) - 4k_D)e_{k-1} \\ &+ (k_P(2\tau + T) + 2k_D)u_{k-2} \\ &+ (4\tau)u_{k-1} \\ &- (2\tau - T)u_{k-2} \\ u_k &= u_k / a_0 \end{aligned} \]
WARNING: this routine does NOT check whether or not the controller was previously initialized. It also does NOT take measurement compensation into consideration.
| [in] | pid | PID controller data structure to update. |
| [in] | r_k | Reference signal at the current time step. |
| [in] | y_k | Measurement (after compensation) at current time step. |
| err_status_t ctr_ss_lti | ( | matf32_t * | A, |
| matf32_t * | B, | ||
| matf32_t * | C, | ||
| matf32_t * | D, | ||
| float | sample_time, | ||
| ctr_sys_lti_t *const | sys ) |
Initializes a state space LTI model.
WARNING: this routine is written to provide somewhat of a MATLAB compatibility but does NOT initialize the complete data structure. The state vector will be initialized until it's needed.
| [in] | A | System matrix. |
| [in] | B | System input/actuator matrix. |
| [in] | C | System output/sensor matrix. |
| [in] | D | System feedforward terms. |
| [in] | sample_time | Sampling period, in case of a discrete time system. |
| [in,out] | sys | LTI state space system data structure. |
| err_status_t ctr_sys_lti_init | ( | ctr_sys_lti_t *const | sys, |
| matf32_t *const | state, | ||
| matf32_t *const | A, | ||
| matf32_t *const | B, | ||
| matf32_t *const | C, | ||
| matf32_t *const | D, | ||
| float | sample_time ) |
Initialices an instance of the ctr_sys_lti_t struct for lti linear systems.
| [in,out] | sys | lti system struct instance |
| [in] | state | Points to matrix for the system state |
| [in] | A | Points to matrix A for the system |
| [in] | B | Points to matrix B for the system |
| [in] | C | Points to matrix C for the system |
| [in] | D | Points to matrix D for the system |
| [in] | sample_time | Float for the sample time of the system |
| void ctr_sys_lti_print | ( | ctr_sys_lti_t * | p_sys_lti | ) |
Prints the ctr_sys_lti_t struct in formatted text.
| [in] | p_sys_lti | Pointer to the struct to print |
| err_status_t ctr_sys_nonlin_init | ( | ctr_sys_nonlin_t *const | sys, |
| matf32_t *const | state, | ||
| uint16_t | input_dim, | ||
| uint16_t | output_dim, | ||
| err_status_t(* | dynamics )(matf32_t *const, const matf32_t *, const matf32_t *), | ||
| err_status_t(* | outputs )(matf32_t *const, const matf32_t *, const matf32_t *), | ||
| float | sample_time ) |
Initializes an instance of the ctr_sys_nonlin_t struct for nonlinear systems.
| [in,out] | sys | Nonlinear system struct instance |
| [in] | state | Points to matrix for the system state |
| [in] | input_dim | Number of inputs of the system |
| [in] | output_dim | Number of outputs of the system |
| [in] | dynamics | Points to function representing the dynamics of the system: output_matrix, xss, uss |
| [in] | outputs | Points to function representing the outputs of the system: output_matrix, xss, uss |
| [in] | sample_time | Float for the sample time of the system |
| err_status_t ctr_sys_nonlin_simulate | ( | ctr_sys_nonlin_t * | sys, |
| const matf32_t *const | x_k, | ||
| matf32_t *const | x_k_1, | ||
| const matf32_t *const | u_k, | ||
| float | delta, | ||
| ctr_discretizations_t | method ) |
Updates the step for a nonlinear system with either forward euler or runge-kutta-4 methods.
Forward Euler:
\[ x_{k+1} = x_k + f(x_k) \Delta t \]
Runge-Kutta4:
\[ x_{k+1} = x_k + \frac{\Delta t}{6}(k_1 + 2k_2 + 2k_3 + k_4) \]
| [in] | sys | Points to the nonlinear system to work with |
| [in] | x_k | Points to the current state |
| [in] | x_k_1 | Points to the next state to save it |
| [in] | u_k | Points to system input |
| [in] | delta | Float for the time step |
| [in] | method | Method to work with: Forward Euler or Runge-Kutta-4 |