SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
AccelerometerUtils.hpp
Go to the documentation of this file.
1
31#pragma once
32
33#include "SensorDefs.hpp"
34
35// *INDENT-OFF*
40{
47 inline float gToMps2(float g_value)
48 {
49 return g_value * 9.80665f; // Standard gravity constant
50 }
51
58 inline float mps2ToG(float mps2_value)
59 {
60 return mps2_value / 9.80665f;
61 }
62
69 inline float rangeToG(AccelFullScaleRange range)
70 {
71 switch (range) {
72 case AccelFullScaleRange::FS_2G: return 2.0f;
73 case AccelFullScaleRange::FS_4G: return 4.0f;
74 case AccelFullScaleRange::FS_8G: return 8.0f;
75 case AccelFullScaleRange::FS_16G: return 16.0f;
76 case AccelFullScaleRange::FS_32G: return 32.0f;
77 case AccelFullScaleRange::FS_64G: return 64.0f;
78 case AccelFullScaleRange::FS_128G: return 128.0f;
79 default: return 8.0f; // Default fallback
80 }
81 }
82
91 inline float calculateMagnitude(const AccelerometerData &data)
92 {
93 const auto &acc = data.mps2;
94 return sqrtf(acc.x * acc.x + acc.y * acc.y + acc.z * acc.z);
95 }
96
107 inline float calculateInclination(const AccelerometerData &data, uint8_t axis)
108 {
109 if (axis > 2) return NAN;
110
111 float magnitude = calculateMagnitude(data);
112 if (magnitude == 0.0f) return NAN;
113
114 switch (axis) {
115 case 0: // X-axis
116 return acosf(data.mps2.x / magnitude);
117 case 1: // Y-axis
118 return acosf(data.mps2.y / magnitude);
119 case 2: // Z-axis
120 return acosf(data.mps2.z / magnitude);
121 default:
122 return NAN;
123 }
124 }
125
135 inline float calculateInclinationDegrees(const AccelerometerData &data, uint8_t axis)
136 {
137 float rad = calculateInclination(data, axis);
138 return isnan(rad) ? NAN : rad * 180.0f / M_PI;
139 }
140
148 {
149 float x = data.mps2.x;
150 float y = data.mps2.y;
151 float z = data.mps2.z;
152
153 if (fabsf(x) > fabsf(y) && fabsf(x) > fabsf(z)) {
155 } else if (fabsf(y) > fabsf(x) && fabsf(y) > fabsf(z)) {
157 } else if (fabsf(z) > fabsf(x) && fabsf(z) > fabsf(y)) {
159 } else {
161 }
162 }
163};
164// *INDENT-ON*
@license MIT License
SensorDirection
@ DIRECTION_TOP_RIGHT
@ DIRECTION_TOP
@ DIRECTION_UNKNOWN
@ DIRECTION_BOTTOM
@ DIRECTION_TOP_LEFT
@ DIRECTION_BOTTOM_LEFT
@ DIRECTION_BOTTOM_RIGHT
AccelFullScaleRange
Enumeration of accelerometer full-scale range settings.
@ FS_128G
±128g range
@ FS_16G
±16g range
@ FS_32G
±32g range
@ FS_64G
±64g range
#define M_PI
Utility class for accelerometer-specific calculations.
float calculateInclination(const AccelerometerData &data, uint8_t axis)
Calculate inclination angle of specified axis relative to gravity.
float rangeToG(AccelFullScaleRange range)
Convert full-scale range enumeration to g-force value.
SensorDirection getDirection(const AccelerometerData &data)
Get the direction of the acceleration vector.
float calculateInclinationDegrees(const AccelerometerData &data, uint8_t axis)
Calculate inclination angle in degrees.
float mps2ToG(float mps2_value)
Convert acceleration from m/s² to g-force.
float gToMps2(float g_value)
Convert acceleration from g-force to m/s²
float calculateMagnitude(const AccelerometerData &data)
Calculate magnitude of acceleration vector.
Structure representing accelerometer data.
SensorVector mps2
Acceleration in meters per second squared (m/s²)
float x
X-axis component.
float y
Y-axis component.
float z
Z-axis component.
int16_t x[5]
int16_t y[5]