Hummingbird Flight Software
Flight software for the Hummingbird FCU quadcopter flight controller. Designed to run on the Teensy 4.1. Developed with VSCode+PlatformIO.
matrices.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // MATRIX OBJECT DEFINITION
3 //
4 // Created By: Michael Wrona
5 // Created: 16 Feb 2021
6 // ----------------------------------------------------------------------------
17 #pragma once
18 
19 #include <Arduino.h>
20 #include "hummingbird_config.h"
21 
22 
23 #if defined(DEBUG) && defined(DEBUG_PORT)
24  // #define MATRIX_OBJ_DEBUG /* Enable a message to signal when a matrix was created and destroyed */
25 #endif
26 
39 #define MATRIX_ELEMENT(MAT, i, j, rows, cols) *(MAT + i*cols + j)
40 
41 
53 class Matrix {
54 public:
55  // ----------------------------------------------------------------------------
56  // Matrix(size_t rows, size_t cols)
57  // ----------------------------------------------------------------------------
65  Matrix(size_t rows = 3, size_t cols = 3)
66  {
67  size_t i;
68  float initVal = 0.0f; // Value to initialize and fill array with
69  float **pmat; // pointer for construction
70 
71  this->rows = rows;
72  this->cols = cols;
73 
74  // Create the array on the heap (RAM2 on Teensy 4.1)
75  this->mat = new float[this->rows*this->cols];
76  pmat = new float*[this->rows];
77  for (i = 1, *pmat = this->mat; i < this->rows; i++)
78  *(pmat + i) = *(pmat + i - 1) + this->cols;
79 
80  // Init. values
81  size_t rc = this->rows * this->cols;
82  for (i = 0; i < rc; i++)
83  this->mat[i] = initVal;
84 
85  #ifdef MATRIX_OBJ_DEBUG
86  DEBUG_PORT.print("Created "); DEBUG_PORT.print(this->rows); DEBUG_PORT.print("x");
87  DEBUG_PORT.print(this->cols); DEBUG_PORT.println(" matrix");
88  #endif
89  }
90 
91 
92  #ifdef MATRIX_OBJ_DEBUG
97  void PrintToDebug(size_t decimals = 2)
98  {
99  size_t i, j;
100 
101  // Cannot print more than 7 decimals for floats...
102  if (decimals >= 7)
103  decimals = 4;
104 
105  for (i = 0; i < this->rows; i++)
106  {
107  DEBUG_PORT.print("[[ ");
108  for (j = 0; j < this->cols; j++)
109  {
110  DEBUG_PORT.print(MATRIX_ELEMENT(this->mat, i, j, this->rows, this->cols), decimals);
111  DEBUG_PORT.print((j != this->cols - 1) ? ", " : "");
112  }
113  DEBUG_PORT.println(" ]]");
114  }
115  DEBUG_PORT.println(""); // Newline to help separate matrices
116  }
117  #endif
118 
119 
120  /* VARIABLES */
121  size_t rows; // Rows of the matrix
122  size_t cols; // Columns of the matrix
123  float *mat; // Pointer to the 2D array/matrix
124 
125 
126  /* Deconstruct/deallocate the matrix once it goes out-of-scope. */
128  {
129  delete[] this->mat;
130 
131  #ifdef MATRIX_OBJ_DEBUG
132  DEBUG_PORT.print("Deallocated "); DEBUG_PORT.print(this->rows); DEBUG_PORT.print("x");
133  DEBUG_PORT.print(this->cols); DEBUG_PORT.println(" matrix");
134  #endif
135  }
136 protected:
137 private:
138 };
A matrix object is definied by it's rows and columns.
Definition: matrices.h:53
float * mat
Definition: matrices.h:123
size_t cols
Definition: matrices.h:122
size_t rows
Definition: matrices.h:121
~Matrix()
Definition: matrices.h:127
Matrix(size_t rows=3, size_t cols=3)
Define a matrix by rows and columns.
Definition: matrices.h:65
#define DEBUG_PORT
Definition: debugging.h:37
#define MATRIX_ELEMENT(MAT, i, j, rows, cols)
A matrix object is definied by it's rows and columns.
Definition: matrices.h:39