Fang-Robotics-MCB
Fang Robotics Team Codebase
Loading...
Searching...
No Matches
cmsis_mat.hpp
Go to the documentation of this file.
1/*****************************************************************************/
2/********** !!! WARNING: CODE GENERATED BY TAPROOT. DO NOT EDIT !!! **********/
3/*****************************************************************************/
4
5/*
6 * Copyright (c) 2020-2021 Advanced Robotics at the University of Washington <robomstr@uw.edu>
7 *
8 * This file is part of Taproot.
9 *
10 * Taproot is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * Taproot is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Taproot. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24#ifndef TAPROOT_CMSIS_MAT_HPP_
25#define TAPROOT_CMSIS_MAT_HPP_
26
27#include <array>
28#include <cassert>
29#include <cinttypes>
30#include <iostream>
31
32#include "modm/architecture/utils.hpp"
33
34#include "arm_math.h"
35
36namespace tap::algorithms
37{
43template <uint16_t ROWS, uint16_t COLS>
45{
46 std::array<float, ROWS * COLS> data;
47 arm_matrix_instance_f32 matrix;
48
49 CMSISMat() : data(), matrix{ROWS, COLS, data.data()} {}
50
51 CMSISMat(const float (&initialData)[ROWS * COLS])
52 {
53 copyData(initialData);
54 arm_mat_init_f32(&matrix, ROWS, COLS, data.data());
55 }
56
60 CMSISMat(const CMSISMat &other) : matrix{ROWS, COLS, data.data()}
61 {
62 memcpy(&this->data, &other.data, sizeof(this->data));
63 }
64
65 // Move semantics.
67 {
68 this->data = std::move(other.data);
69 matrix.numRows = ROWS;
70 matrix.numCols = COLS;
71 matrix.pData = data.data();
72 }
73
78 {
79 memcpy(&this->data, &other.data, sizeof(this->data));
80 return *this;
81 }
82
83 // Move semantics.
85 {
86 this->data = std::move(other.data);
87 matrix.numRows = ROWS;
88 matrix.numCols = COLS;
89 matrix.pData = data.data();
90 return *this;
91 }
92
93 inline void copyData(const float (&other)[ROWS * COLS])
94 {
95 for (size_t i = 0; i < data.size(); i++)
96 {
97 data[i] = other[i];
98 }
99 }
100
105 {
106 if (ROWS != COLS)
107 {
108 return false;
109 }
110
111 for (int i = 0; i < ROWS; i++)
112 {
113 for (int j = 0; j < COLS; j++)
114 {
115 data[i * COLS + j] = (i == j) ? 1 : 0;
116 }
117 }
118
119 return true;
120 }
121
123 {
125 assert(ARM_MATH_SUCCESS == arm_mat_inverse_f32(&this->matrix, &ret.matrix));
126 return ret;
127 }
128
130 {
132 assert(ARM_MATH_SUCCESS == arm_mat_trans_f32(&this->matrix, &ret.matrix));
133 return ret;
134 }
135};
136
137/* Begin definitions */
138
139template <uint16_t A_ROWS, uint16_t A_COLS, uint16_t B_ROWS, uint16_t B_COLS>
143{
144 static_assert(
145 A_ROWS == B_ROWS && A_COLS == B_COLS,
146 "Invalid size of CMSISMat matricies in operator+");
147
149 assert(ARM_MATH_SUCCESS == arm_mat_add_f32(&a.matrix, &b.matrix, &c.matrix));
150 return c;
151}
152
153template <uint16_t A_ROWS, uint16_t A_COLS, uint16_t B_ROWS, uint16_t B_COLS>
157{
158 static_assert(
159 A_ROWS == B_ROWS && A_COLS == B_COLS,
160 "Invalid size of CMSISMat matricies in operator-");
161
163 assert(ARM_MATH_SUCCESS == arm_mat_sub_f32(&a.matrix, &b.matrix, &c.matrix));
164 return c;
165}
166
167template <uint16_t ROWS, uint16_t COLS>
169{
170 float scale(-1);
172 assert(ARM_MATH_SUCCESS == arm_mat_scale_f32(&a.matrix, scale, &c.matrix));
173 return c;
174}
175
176template <uint16_t A_ROWS, uint16_t A_COLS, uint16_t B_ROWS, uint16_t B_COLS>
180{
181 static_assert(A_COLS == B_ROWS, "Invalid size of CMSISMat matricies in operator*");
182
184 assert(ARM_MATH_SUCCESS == arm_mat_mult_f32(&a.matrix, &b.matrix, &c.matrix));
185 return c;
186}
187
188template <uint16_t ROWS, uint16_t COLS>
190{
192 assert(ARM_MATH_SUCCESS == arm_mat_scale_f32(&a.matrix, b, &c.matrix));
193 return c;
194}
195
196template <uint16_t ROWS, uint16_t COLS>
198{
200 assert(ARM_MATH_SUCCESS == arm_mat_scale_f32(&a.matrix, b, &c.matrix));
201 return c;
202}
203
204template <uint16_t ROWS, uint16_t COLS>
206{
207 b = 1 / b;
209 assert(ARM_MATH_SUCCESS == arm_mat_scale_f32(&a.matrix, b, &c.matrix));
210 return c;
211}
212
213} // namespace tap::algorithms
214
215#endif // TAPROOT_CMSIS_MAT_HPP_
IUnit i
Definition dimensional_smooth_pid.hpp:2
Definition ballistics.cpp:29
CMSISMat< A_ROWS, B_COLS > operator*(const CMSISMat< A_ROWS, A_COLS > &a, const CMSISMat< B_ROWS, B_COLS > &b)
Definition cmsis_mat.hpp:177
CMSISMat< ROWS, COLS > operator/(const CMSISMat< ROWS, COLS > &a, float b)
Definition cmsis_mat.hpp:205
CMSISMat< A_ROWS, A_COLS > operator+(const CMSISMat< A_ROWS, A_COLS > &a, const CMSISMat< B_ROWS, B_COLS > &b)
Definition cmsis_mat.hpp:140
CMSISMat< A_ROWS, A_COLS > operator-(const CMSISMat< A_ROWS, A_COLS > &a, const CMSISMat< B_ROWS, B_COLS > &b)
Definition cmsis_mat.hpp:154
Definition cmsis_mat.hpp:45
arm_matrix_instance_f32 matrix
Definition cmsis_mat.hpp:47
bool constructIdentityMatrix()
Definition cmsis_mat.hpp:104
CMSISMat< COLS, ROWS > transpose() const
Definition cmsis_mat.hpp:129
CMSISMat()
Definition cmsis_mat.hpp:49
CMSISMat(const CMSISMat &other)
Definition cmsis_mat.hpp:60
CMSISMat & operator=(CMSISMat &&other)
Definition cmsis_mat.hpp:84
void copyData(const float(&other)[ROWS *COLS])
Definition cmsis_mat.hpp:93
CMSISMat< COLS, ROWS > inverse() const
Definition cmsis_mat.hpp:122
CMSISMat & operator=(const CMSISMat &other)
Definition cmsis_mat.hpp:77
CMSISMat(const float(&initialData)[ROWS *COLS])
Definition cmsis_mat.hpp:51
CMSISMat(CMSISMat &&other)
Definition cmsis_mat.hpp:66
std::array< float, ROWS *COLS > data
Definition cmsis_mat.hpp:46