Brick
Loading...
Searching...
No Matches
discretization.h
1#pragma once
2
3#include <tuple>
4
5#include "../vendor/eigen/unsupported/Eigen/MatrixFunctions"
6#include "core/math/eigen_interface.h"
7
8/**
9 * Discretizes the continuous system and input matrices (A and B) over the
10 * timestep dt in seconds.
11 *
12 * @tparam STATES Dimension of the state matrix.
13 * @tparam INPUTS Dimension of the input matrix.
14 *
15 * @param Ac The continuous state matrix A.
16 * @param Bc The continuous input matrix B.
17 * @param dt The timestep in seconds.
18 */
19template <int STATES, int INPUTS>
20constexpr std::tuple<EMat<STATES, STATES>, EMat<STATES, INPUTS>> discretize_AB(
21 EMat<STATES, STATES> Ac, EMat<STATES, INPUTS> Bc, const double& dt) {
22 /*
23 * Form the intermediate matrix M
24 * [A B]
25 * M = [0 0]
26 */
27 EMat<STATES + INPUTS, STATES + INPUTS> M;
28 M.template block<STATES, STATES>(0, 0) = Ac;
29 M.template block<STATES, INPUTS>(0, STATES) = Bc;
30 M.template block<INPUTS, STATES + INPUTS>(STATES, 0).setZero();
31
32 /*
33 * M * T [Ad Bd]
34 * e = [0 I]
35 */
36 EMat<STATES + INPUTS, STATES + INPUTS> phi = (M * dt).exp();
37
38 // Extract Ad and Bd from phi and put them in a tuple
39 return std::make_tuple(phi.template block<STATES, STATES>(0, 0),
40 phi.template block<STATES, INPUTS>(0, STATES));
41}