TeensyNanoExoCode
Loading...
Searching...
No Matches
Utilities.h
Go to the documentation of this file.
1
11#ifndef Utilities_h
12#define Utilities_h
13
14#include "ParseIni.h"
15#include "Arduino.h"
16#include <stdint.h>
17#include <utility> // std::pair
18#include <queue> // std::queue
19
23namespace utils
24{
33
41 bool get_is_left(uint8_t id);
42
52
61 uint8_t get_joint_type(uint8_t id);
62
80 bool schmitt_trigger(float value, bool is_high, float lower_threshold, float upper_threshold);
81
93 int rate_limit(int setpoint, int last_value, int* last_time, int rate_per_ms);
94
104 uint8_t update_bit(uint8_t original, bool val, uint8_t loc);
105
115 uint16_t update_bit(uint16_t original, bool val, uint8_t loc);
116
125 bool get_bit(uint8_t original, uint8_t loc);
126
135 bool get_bit(uint16_t original, uint8_t loc);
136
144 float degrees_to_radians(float);
145
153 float radians_to_degrees(float);
154
159 String remove_all_chars(String str, char rmv);
160 String remove_all_chars(char* arr, int len, char rmv);
161
166 int get_char_length(int ofInt);
167
172 template <typename T>
173 int elements_are_equal(T arr1, T arr2, int length)
174 {
175 for (int i=0; i<length; i++)
176 {
177 if(arr1[i] != arr2[i])
178 {
179 return 0;
180 }
181 }
182 return 1;
183 };
184
189 template <typename T>
190 void set_elements_equal(T arr1, T arr2, int length)
191 {
192 for (int i=0; i<length; i++)
193 {
194 arr1[i] = arr2[i];
195 }
196 };
197
203 {
204 public:
205 SpeedCheck(int pin);
206
210 void toggle();
211
212 private:
213 int _pin;
214 bool _state;
215 };
216
223 bool is_little_endian();
224
233 void float_to_uint8(float num_to_convert, uint8_t *converted_bytes);
234
243 void uint8_to_float(uint8_t *bytes_to_convert, float *converted_float);
244
251 void float_to_short_fixed_point_bytes(float num_to_convert, uint8_t *converted_bytes, uint8_t factor);
252
259 void short_fixed_point_bytes_to_float(uint8_t *bytes_to_convert, float *converted_val, uint8_t factor);
260
269 float ewma(float new_value, float filter_value, float alpha);
270
276 void spin_on_error_with(String message);
277
287 bool is_close_to(float val1, float val2, float tolerance);
288
295 std::pair<float, float> online_std_dev(std::queue<float> set);
296
306 bool is_outside_range(float val, float min, float max);
307}
308
309
310#endif
Declares the functions needed and defines mapping between the INI keys and the exo components.
Class used to check the loop speed without serial prints, by toggling a pin after initialized,...
Definition Utilities.h:203
void toggle()
toggles the pin attached to the object
Definition Utilities.cpp:157
joint_id
Definition ParseIni.h:106
contains general utility functions for the exo
Definition Utilities.cpp:9
std::pair< float, float > online_std_dev(std::queue< float > set)
Given a set of data and maximum size. Returns the new mean and standard deviation.
Definition Utilities.cpp:350
float radians_to_degrees(float angle_rad)
converts from radians to degrees
Definition Utilities.cpp:93
bool get_bit(uint8_t original, uint8_t loc)
Returns the bit in a specific location in a uint8_t.
Definition Utilities.cpp:74
int elements_are_equal(T arr1, T arr2, int length)
Checks if all elements of the array are equal. Arrays must be the same length and type todo: Chance u...
Definition Utilities.h:173
bool is_close_to(float val1, float val2, float tolerance)
Checks if two floats are close to each other within a tolerance.
Definition Utilities.cpp:345
uint8_t update_bit(uint8_t original, bool val, uint8_t loc)
sets/clears the specified bit in a unit8_t.
Definition Utilities.cpp:60
int rate_limit(int setpoint, int last_value, int *last_time, int rate_per_ms)
Limits the rate at which a value can change. This is useful when you would like a variable to gradu...
Definition Utilities.cpp:48
float degrees_to_radians(float angle_deg)
converts from degrees to radians
Definition Utilities.cpp:88
uint8_t get_joint_type(config_defs::joint_id id)
Takes in the joint id and returns the id with the left/right bits masked out. Returning uint8_t rathe...
Definition Utilities.cpp:21
int get_char_length(int ofInt)
given and integer, return the number of characters in it todo: Chance update comments
Definition Utilities.cpp:122
void float_to_short_fixed_point_bytes(float num_to_convert, uint8_t *converted_bytes, uint8_t factor)
converts float into individual bytes of a fixed point short.
Definition Utilities.cpp:254
bool is_little_endian()
Returns 1 if system uses little endian floating points. This confirms that the floating points match ...
Definition Utilities.cpp:181
void spin_on_error_with(String message)
Never returns from this function, used for critical errors.
Definition Utilities.cpp:336
String remove_all_chars(String str, char rmv)
Searches str for 'rmv characters and deletes them all, returns new string todo: Chance update comment...
Definition Utilities.cpp:98
float ewma(float new_value, float filter_value, float alpha)
Exponential Weighted Moving Average. Used to smooth out noisy data.
Definition Utilities.cpp:317
void uint8_to_float(uint8_t *bytes_to_convert, float *converted_float)
Takes in a byte array address in little endian form containing a broken up float Returns a reconstitu...
Definition Utilities.cpp:225
bool get_is_left(config_defs::joint_id id)
Takes in the joint id and returns if the left indicator bit is set as a bool.
Definition Utilities.cpp:10
void set_elements_equal(T arr1, T arr2, int length)
Sets arr2 elements equal to arr1 elements. Arrays must be the same length and type todo: Chance updat...
Definition Utilities.h:190
bool schmitt_trigger(float value, bool is_high, float lower_threshold, float upper_threshold)
Returns the new state of the system based on the value and past state. Takes in the current value,...
Definition Utilities.cpp:31
void float_to_uint8(float num_to_convert, uint8_t *converted_bytes)
Takes in a float and a byte array reference Puts the bytes of the float into the array in little endi...
Definition Utilities.cpp:201
bool is_outside_range(float val, float min, float max)
Checks if a value is outside of a range.
Definition Utilities.cpp:384
void short_fixed_point_bytes_to_float(uint8_t *bytes_to_convert, float *converted_val, uint8_t factor)
converts a set of bytes in a fixed point short int into a float
Definition Utilities.cpp:278