Hummingbird Flight Software
Flight software for the Hummingbird FCU quadcopter flight controller. Designed to run on the Teensy 4.1. Developed with VSCode+PlatformIO.
vectors.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // VECTOR OBJECT DEFINITION
3 //
4 // Created By: Michael Wrona
5 // Created: 16 Feb 2021
6 // ----------------------------------------------------------------------------
14 #pragma once
15 
16 #include <Arduino.h>
17 #include "hummingbird_config.h"
18 
19 #if defined(DEBUG) && defined(DEBUG_PORT)
20 /* Enable a message to signal when a vector was created and destroyed */
21 // #define VECTOR_OBJ_DEBUG
22 #endif
23 
24 
34 #define VECTOR_ELEMENT(VEC, i) VEC[i]
35 // #define VECTOR_ELEMENT(VEC, i) *(VEC + i)
36 
37 
46 class Vectorf {
47 public:
48  // --------------------------------------------------------------------
49  // Vectorf(size_t len)
50  // --------------------------------------------------------------------
57  Vectorf(size_t len = 3)
58  {
59  size_t i;
60  float initVal = 0.0f; // Value to initialize and fill array with
61 
62  this->len = len;
63 
64  // Create the array on the heap (RAM2 on Teensy 4.1)
65  this->vec = new float[this->len];
66 
67 
68  // Init. values
69  for (i = 0; i < this->len; i++)
70  this->vec[i] = initVal;
71 
72 
73  #ifdef VECTOR_OBJ_DEBUG
74  DEBUG_PORT.print("Created "); DEBUG_PORT.print(this->len);
75  DEBUG_PORT.print("x1"); DEBUG_PORT.println(" vector");
76  #endif
77  }
78 
79  // --------------------------------------------------------------------
80  // GetNorm()
81  // --------------------------------------------------------------------
87  float GetNorm()
88  {
89  size_t i;
90  float sumsq;
91  float vecNorm;
92 
93  for (i = 0; i < this->len; i++)
94  sumsq += (this->vec[i] * this->vec[i]);
95 
96  vecNorm = sqrtf(sumsq);
97 
98  return vecNorm;
99  }
100 
101 
102  #ifdef VECTOR_OBJ_DEBUG
107  void PrintToDebug(size_t decimals = 4)
108  {
109  size_t i;
110 
111  // Cannot print more than 7 decimals for floats...
112  if (decimals >= 7)
113  decimals = 4;
114 
115  DEBUG_PORT.print("[");
116  for (i = 0; i < this->len; i++)
117  {
118  DEBUG_PORT.print(VECTOR_ELEMENT(this->vec, i), decimals);
119  DEBUG_PORT.print((i != this->len - 1) ? ", " : " ");
120  }
121  DEBUG_PORT.println("]");
122  DEBUG_PORT.println(""); // Newline to help separate vectors
123  }
124  #endif
125 
126 
127  /* VARIABLES */
128  size_t len; // Length of the vector, # of elements
129  float *vec; // Pointer to the array/vector
130 
131 
132  /* Deconstruct/deallocate the vector once it goes out-of-scope. */
134  {
135  delete[] this->vec;
136 
137  #ifdef VECTOR_OBJ_DEBUG
138  DEBUG_PORT.print("Deallocated "); DEBUG_PORT.print(this->len);
139  DEBUG_PORT.print("x1"); DEBUG_PORT.println(" vector");
140  #endif
141  }
142 protected:
143 private:
144 };
145 
146 
155 class Vectord {
156 public:
157  // --------------------------------------------------------------------
158  // Vectord(size_t len)
159  // --------------------------------------------------------------------
166  Vectord(size_t len = 3)
167  {
168  size_t i;
169  double initVal = 0.0; // Value to initialize and fill array with
170 
171  this->len = len;
172 
173  // Create the array on the heap (RAM2 on Teensy 4.1)
174  this->vec = new double[this->len];
175 
176  // Init. values
177  for (i = 0; i < this->len; i++)
178  this->vec[i] = initVal;
179 
180 
181  #ifdef VECTOR_OBJ_DEBUG
182  DEBUG_PORT.print("Created "); DEBUG_PORT.print(this->len);
183  DEBUG_PORT.print("x1"); DEBUG_PORT.println(" vector");
184  #endif
185  }
186 
187  // --------------------------------------------------------------------
188  // GetNorm()
189  // --------------------------------------------------------------------
195  double GetNorm()
196  {
197  size_t i;
198  double sumsq;
199  double vecNorm;
200 
201  for (i = 0; i < this->len; i++)
202  sumsq += (this->vec[i] * this->vec[i]);
203 
204  vecNorm = sqrt(sumsq);
205 
206  return vecNorm;
207  }
208 
209 
210  #ifdef VECTOR_OBJ_DEBUG
215  void PrintToDebug(size_t decimals = 4)
216  {
217  size_t i;
218 
219  DEBUG_PORT.print("[");
220  for (i = 0; i < this->len; i++)
221  {
222  DEBUG_PORT.print(VECTOR_ELEMENT(this->vec, i), decimals);
223  DEBUG_PORT.print((i != this->len - 1) ? ", " : " ");
224  }
225  DEBUG_PORT.println("]");
226  DEBUG_PORT.println(""); // Newline to help separate vectors
227  }
228  #endif
229 
230 
231  /* VARIABLES */
232  size_t len; // Length of the vector, # of elements
233  double *vec; // Pointer to the array/vector
234 
235 
236  /* Deconstruct/deallocate the vector once it goes out-of-scope. */
238  {
239  delete[] this->vec;
240 
241  #ifdef VECTOR_OBJ_DEBUG
242  DEBUG_PORT.print("Deallocated "); DEBUG_PORT.print(this->len);
243  DEBUG_PORT.print("x1"); DEBUG_PORT.println(" vector");
244  #endif
245  }
246 protected:
247 private:
248 };
A vector object is definied by it's rows/length.
Definition: vectors.h:155
~Vectord()
Definition: vectors.h:237
size_t len
Definition: vectors.h:232
double * vec
Definition: vectors.h:233
double GetNorm()
Return the 2-norm (magnitude) of the vector.
Definition: vectors.h:195
Vectord(size_t len=3)
Define a vector by it's rows/length.
Definition: vectors.h:166
A vector object is definied by it's rows/length.
Definition: vectors.h:46
Vectorf(size_t len=3)
Define a vector by it's rows/length.
Definition: vectors.h:57
size_t len
Definition: vectors.h:128
float GetNorm()
Return the 2-norm (magnitude) of the vector.
Definition: vectors.h:87
~Vectorf()
Definition: vectors.h:133
float * vec
Definition: vectors.h:129
#define DEBUG_PORT
Definition: debugging.h:37
#define VECTOR_ELEMENT(VEC, i)
A vector object is definied by it's rows/length.
Definition: vectors.h:34