Robotat Linalg
Loading...
Searching...
No Matches
RobotatControl

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.

Detailed Description

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.

Author
Miguel Zea (mezea.nosp@m.@uvg.nosp@m..edu..nosp@m.gt)
Version
0.1
Date
2021-08-12

Last modified: 31 Jan. 2026 By: Andrea Pineda


Data Structure Documentation

◆ ctr_pid_t

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

◆ ctr_sys_lti_t

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} \]

Collaboration diagram for ctr_sys_lti_t:
[legend]
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.

◆ ctr_sys_nonlin_t

struct ctr_sys_nonlin_t

State space nonlinear system data structure.

Collaboration diagram for ctr_sys_nonlin_t:
[legend]

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

Field Documentation

◆ dt

float ctr_sys_nonlin_t::dt

Sampling period (for discrete time systems).

◆ dynamics

err_status_t(* ctr_sys_nonlin_t::dynamics) (matf32_t *const, const matf32_t *, const matf32_t *)

System dynamics.

◆ input_dim

uint16_t ctr_sys_nonlin_t::input_dim

Number of inputs/actuators/controls.

◆ is_continuous

bool ctr_sys_nonlin_t::is_continuous

System time domain specification.

◆ output_dim

uint16_t ctr_sys_nonlin_t::output_dim

Number of outputs/measurements.

◆ outputs

err_status_t(* ctr_sys_nonlin_t::outputs) (matf32_t *const, const matf32_t *, const matf32_t *)

System outputs.

◆ state

matf32_t* ctr_sys_nonlin_t::state

State at the current time step (used for simulation/integration).

◆ state_dim

uint16_t ctr_sys_nonlin_t::state_dim

Number of state variables. This is redundant but we'll keep it for completeness.

◆ ctr_kalman_t

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} \]

Collaboration diagram for ctr_kalman_t:
[legend]
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.

◆ ctr_mpc_lti_shooting_t

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.

Collaboration diagram for ctr_mpc_lti_shooting_t:
[legend]
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

Enumeration Type Documentation

◆ ctr_discretizations_t

Discretization specification data type.

Dependencies.

Used to specify the numerical integration scheme to use when discretizing LTI systems and controllers.

Enumerator
PURE_DISCRETE 

Sampling period independent.

FWD_EULER 

Forward Euler integration.

BWD_EULER 

Backward Euler integration.

TUSTIN 

Trapezoidal rule.

ZOH 

Zero-order hold.

RK4 

4th order Runge-Kutta.

Function Documentation

◆ ctr_c2d()

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).

Parameters
[in,out]sysContinuous time LTI system data structure.
[in]sample_timeSampling period.
[in]methodDiscretization scheme.
Returns
Execution status MATH_SUCCESS : Operation successful. MATH_SIZE_MISMATCH : Matrix size check failed.
Here is the call graph for this function:

◆ ctr_discretizations_print()

void ctr_discretizations_print ( ctr_discretizations_t pid_alg)

Prints the elements of ctr_discretizations_t.

Parameters
[in]pid_algEnumerate list of discretization algorithms
Returns
None

◆ ctr_gen_impulse()

void ctr_gen_impulse ( float * signal,
uint16_t samples,
uint16_t impulse_index )

Generates an impulse signal based on a previously defined array.

Parameters
[in,out]signalArray for the signal
[in]samplesLength of the signal
[in]impulse_indexIndex at which to generate the impulse
Returns
None

◆ ctr_gen_step()

void ctr_gen_step ( float * signal,
uint16_t samples,
uint16_t step_start_index )

Generates a step signal based on a previously defined array.

Parameters
[in,out]signalArray for the signal
[in]samplesLength of the signal
[in]step_start_indexIndex starting from which to generate the step
Returns
None

◆ ctr_kalman_correct()

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} \]

Parameters
[in,out]kfKalman filter data structure.
[in]measurementsEstimate from the output of the system.
Returns
Execution status MATH_SUCCESS : Operation successful. MATH_SIZE_MISMATCH : Matrix size check failed.
Here is the call graph for this function:

◆ ctr_kalman_init()

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.

Parameters
[in,out]kfKalman filter data structure.
[in]sysLTI system model.
[in]FCoupling matrix of the process noise.
[in]QwProcess noise covariance matrix.
[in]QvMeasurement noise covariance matrix.
[in]xhatState estimate.
[in]PEstimation covariance matrix.
Returns
Execution status MATH_SUCCESS : Operation successful. MATH_SIZE_MISMATCH : Matrix size check failed. MATH_ARGUMENT_ERROR : LTI system model is not discrete time.

◆ ctr_kalman_predict()

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} \]

Parameters
[in,out]kfKalman filter data structure.
[in]inputsEstimate from the input of the system.
Returns
Execution status MATH_SUCCESS : Operation successful. MATH_SIZE_MISMATCH : Matrix size check failed.
Here is the call graph for this function:

◆ ctr_kalman_print()

void ctr_kalman_print ( ctr_kalman_t * p_kalman)

Prints the elements of ctr_kalman_t.

Parameters
[in]p_kalmanPointer to the struct to print
Returns
None
Here is the call graph for this function:

◆ ctr_linear_state_feedback()

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.

Parameters
[in,out]uController output.
[in]KGain matrix.
[in]xState vector.
[in]xssOperation point (desired state).
[in]ussFeedforward input to reach the desired state.
Returns
Execution status MATH_SUCCESS : Operation successful. MATH_SIZE_MISMATCH : Matrix size check failed.
Here is the call graph for this function:

◆ ctr_linloc()

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.

Parameters
[in]src_sysPoints to nonlinear system struct instance
[in,out]dst_sysPoints to lti system struct instance
[in]xssxss for the operation point (xss,uss)
[in]ussuss for the operation point (xss,uss)
[in]deltaDiferences to divide by for the linearization
Returns
Execution status
Here is the call graph for this function:

◆ ctr_mpc_lti_init()

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.

Parameters
[in,out]mpcPoints to the corresponding MPC struct.
[in]x0Points to the initial operation point matrix
[in]QPoints to the quadratic term matrix for the cost function of the QP
[in]cPoints to the linear term matrix for the cost function of the QP
[in]QhatPoints to the penalization matrix Q
[in]RhatPoints to the penalization matrix R
[in]CPoints to the convolution matrix C
[in]horizon_lengthHorizon length (samples).
[in]MPoints to the horizon length matrix M
[in]u_kPoints to the input trajectory matrix
[in]x_kPoints to the state trajectory matrix
Returns
Execution status.

◆ ctr_mpc_set_C()

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.

Parameters
[in,out]mpcPoints to the MPC struct
[in,out]mpc_C_stackPoints to the stack of matrices needed to build C
[in,out]mpc_C_stack_dataPoints to the array of pointers for C
Returns
Execution status. MATH_SUCCESS : Operation completed succesfully.
Here is the call graph for this function:

◆ ctr_mpc_set_constraints()

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:

  1. 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}, \]

  2. 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.

Parameters
[in,out]mpcPoints to the corresponding MPC struct
[in]ubUpper bound
[in]lbLower bound
[in]state_constraintsBoolean: 0=only input constraints, 1=include state constraints as input constraints
Returns
Execution status
Here is the call graph for this function:

◆ ctr_mpc_set_M()

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.

Parameters
[in,out]mpcPoints to the MPC struct.
[in,out]mpc_M_dataPoints to the data arrays for the submatrices of M
Returns
Execution status. MATH_SUCCESS : Operation completed succesfully.
Here is the call graph for this function:

◆ ctr_mpc_set_qpc()

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.

Parameters
[in]mpcPoints to the MPC struct.
[in,out]qp_cPoints to the c matrix for the quadratic program.
Returns
Execution status. MATH_SUCESS : Operation completed succesfully.
Here is the call graph for this function:

◆ ctr_mpc_set_qpQ()

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.

Parameters
[in]mpcPoints to the MPC struct.
[in,out]qp_QPoints to the Q matrix for the quadratic program.
Returns
Execution status. MATH_SUCCESS : Operation completed succesfully.
Here is the call graph for this function:

◆ ctr_mpc_update()

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:

  1. Solving the quadratic program (QP) associated to the MPC. If the QP is unconstrained, a simple solution with linsolve is calculated. Otherwise, if there are constraints (whether of equality or inequality), then the active-set method of quadprog is used (see quadprog_sqp).
  2. 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} \]

Parameters
[in,out]mpcPoints to the corresponding MPC struct.
[in]x_kPoints to the current state trajectory
[in]u_kPoints to the current input trajectory
Returns
Execution status MATH_SUCCESS : MATH_SIZE_MISMATCH :
Here is the call graph for this function:

◆ ctr_pid_init()

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.

Parameters
[in,out]pidPID controller data structure.
[in]kpProportional gain.
[in]kiIntegral gain.
[in]kdDerivative gain.
[in]pid_algPID discretization scheme specification.
[in]set_i_limitsAllows to set lower and upper saturation thresholds for the integrator.
[in]dtSampling period.
[in]tauDerivative HPF time constant.
[in]i_minLower saturation threshold.
[in]i_maxUpper saturation threshold.
Returns
None.

◆ ctr_pid_print()

void ctr_pid_print ( ctr_pid_t *const p_pid)

Prints the ctr_pid_t struct data in formatted text.

Parameters
[in]p_pidPointer to the pid to print
Returns
None
Here is the call graph for this function:

◆ ctr_pid_update()

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.

Parameters
[in]pidPID controller data structure to update.
[in]r_kReference signal at the current time step.
[in]y_kMeasurement (after compensation) at current time step.
Returns
Controller output.

◆ ctr_ss_lti()

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.

Parameters
[in]ASystem matrix.
[in]BSystem input/actuator matrix.
[in]CSystem output/sensor matrix.
[in]DSystem feedforward terms.
[in]sample_timeSampling period, in case of a discrete time system.
[in,out]sysLTI state space system data structure.
Returns
Execution status MATH_SUCCESS : Operation successful. MATH_SIZE_MISMATCH : Matrix size check failed.

◆ ctr_sys_lti_init()

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.

Parameters
[in,out]syslti system struct instance
[in]statePoints to matrix for the system state
[in]APoints to matrix A for the system
[in]BPoints to matrix B for the system
[in]CPoints to matrix C for the system
[in]DPoints to matrix D for the system
[in]sample_timeFloat for the sample time of the system
Returns
Execution status

◆ ctr_sys_lti_print()

void ctr_sys_lti_print ( ctr_sys_lti_t * p_sys_lti)

Prints the ctr_sys_lti_t struct in formatted text.

Parameters
[in]p_sys_ltiPointer to the struct to print
Returns
None
Here is the call graph for this function:

◆ ctr_sys_nonlin_init()

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.

Parameters
[in,out]sysNonlinear system struct instance
[in]statePoints to matrix for the system state
[in]input_dimNumber of inputs of the system
[in]output_dimNumber of outputs of the system
[in]dynamicsPoints to function representing the dynamics of the system: output_matrix, xss, uss
[in]outputsPoints to function representing the outputs of the system: output_matrix, xss, uss
[in]sample_timeFloat for the sample time of the system
Returns
Execution status
Here is the call graph for this function:

◆ ctr_sys_nonlin_simulate()

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) \]

Parameters
[in]sysPoints to the nonlinear system to work with
[in]x_kPoints to the current state
[in]x_k_1Points to the next state to save it
[in]u_kPoints to system input
[in]deltaFloat for the time step
[in]methodMethod to work with: Forward Euler or Runge-Kutta-4
Returns
Execution status
Here is the call graph for this function: