math.hpp
Go to the documentation of this file.
1 // =========================================================================
2 // This library is placed under the MIT License
3 // Copyright 2017-2018 Natanael Josue Rabello. All rights reserved.
4 // For the license information refer to LICENSE file in root directory.
5 // =========================================================================
6 
12 #ifndef _MPU_MATH_HPP_
13 #define _MPU_MATH_HPP_
14 
15 #include <math.h>
16 #include <stdint.h>
17 #include "sdkconfig.h"
18 #include "mpu/types.hpp"
19 
20 
22 namespace mpud {
23 
25 inline namespace math {
26 
27 inline uint8_t accelFSRvalue(const accel_fs_t fs) {
28  return 2 << fs;
29 }
30 
31 inline uint16_t gyroFSRvalue(const gyro_fs_t fs) {
32  return 250 << fs;
33 }
34 
35 inline uint16_t accelSensitivity(const accel_fs_t fs) {
36  return 16384 >> fs;
37 }
38 
39 inline float gyroSensitivity(const gyro_fs_t fs) {
40  return 131.f / (1 << fs);
41 }
42 
43 inline float accelResolution(const accel_fs_t fs) {
44  return static_cast<float>(accelFSRvalue(fs)) / INT16_MAX;
45 }
46 
47 inline float gyroResolution(const gyro_fs_t fs) {
48  return static_cast<float>(gyroFSRvalue(fs)) / INT16_MAX;
49 }
50 
51 inline float accelGravity(const int16_t axis, const accel_fs_t fs) {
52  return axis * accelResolution(fs);
53 }
54 
55 inline float_axes_t accelGravity(const raw_axes_t& raw_axes, const accel_fs_t fs) {
56  float_axes_t axes;
57  axes.x = raw_axes.x * accelResolution(fs);
58  axes.y = raw_axes.y * accelResolution(fs);
59  axes.z = raw_axes.z * accelResolution(fs);
60  return axes;
61 }
62 
63 inline float gyroDegPerSec(const int16_t axis, const gyro_fs_t fs) {
64  return axis * gyroResolution(fs);
65 }
66 
67 inline float_axes_t gyroDegPerSec(const raw_axes_t& raw_axes, const gyro_fs_t fs) {
68  float_axes_t axes;
69  axes.x = raw_axes.x * gyroResolution(fs);
70  axes.y = raw_axes.y * gyroResolution(fs);
71  axes.z = raw_axes.z * gyroResolution(fs);
72  return axes;
73 }
74 
75 inline float gyroRadPerSec(const int16_t axis, const gyro_fs_t fs) {
76  return (M_PI / 180) * gyroDegPerSec(axis, fs);
77 }
78 
79 inline float_axes_t gyroRadPerSec(const raw_axes_t& raw_axes, const gyro_fs_t fs) {
80  float_axes_t axes;
81  axes.x = (M_PI / 180) * gyroDegPerSec(raw_axes.x, fs);
82  axes.y = (M_PI / 180) * gyroDegPerSec(raw_axes.y, fs);
83  axes.z = (M_PI / 180) * gyroDegPerSec(raw_axes.z, fs);
84  return axes;
85 }
86 
87 
88 #if defined CONFIG_MPU6500 || defined CONFIG_MPU9250
89 constexpr int16_t kRoomTempOffset = 0; // LSB
90 constexpr float kCelsiusOffset = 21.f; // ºC
91 constexpr float kTempSensitivity = 333.87f; // LSB/ºC
92 
93 #elif defined CONFIG_MPU6000 || defined CONFIG_MPU6050 || defined CONFIG_MPU9150
94 constexpr int16_t kRoomTempOffset = -521; // LSB
95 constexpr float kCelsiusOffset = 35.f; // ºC
96 constexpr float kTempSensitivity = 340.f; // LSB/ºC
97 #endif
98 
99 constexpr float kTempResolution = 98.67f / INT16_MAX;
100 constexpr float kFahrenheitOffset = kCelsiusOffset * 1.8f + 32; // ºF
101 
102 
103 inline float tempCelsius(const int16_t temp) {
104  // TEMP_degC = ((TEMP_OUT – RoomTemp_Offset)/Temp_Sensitivity) + DegreesCelsius_Offset
105  return (temp - kRoomTempOffset) * kTempResolution + kCelsiusOffset;
106 }
107 
108 inline float tempFahrenheit(const int16_t temp) {
109  return (temp - kRoomTempOffset) * kTempResolution * 1.8f + kFahrenheitOffset;
110 }
111 
112 #if defined CONFIG_MPU_AK89xx
113 inline int16_t magAdjust(const int16_t axis, const uint8_t adjValue) {
114  // Hadj = H * ((((ASA - 128) * 0.5) / 128) + 1)
115  // return axis * ((((adjValue - 128) * 0.5f) / 128) + 1);
116  constexpr float factor = 0.5f / 128;
117  return axis * ((adjValue - 128) * factor + 1);
118 }
119 #endif
120 
121 } // namespace math
122 
123 } // namespace mpud
124 
125 #endif /* end of include guard: _MPU_MATH_HPP_ */
gyro_fs_t
Gyroscope full-scale range.
Definition: types.hpp:51
constexpr float kTempSensitivity
Definition: math.hpp:91
float gyroRadPerSec(const int16_t axis, const gyro_fs_t fs)
Definition: math.hpp:75
float accelGravity(const int16_t axis, const accel_fs_t fs)
Definition: math.hpp:51
constexpr int16_t kRoomTempOffset
Definition: math.hpp:89
constexpr float kTempResolution
Definition: math.hpp:99
Declare Types and Definitions used within mpud namespace.
float tempFahrenheit(const int16_t temp)
Definition: math.hpp:108
float accelResolution(const accel_fs_t fs)
Definition: math.hpp:43
accel_fs_t
Accel full-scale range.
Definition: types.hpp:59
axes_t< float > float_axes_t
Axes type to hold converted sensor data.
Definition: types.hpp:493
uint8_t accelFSRvalue(const accel_fs_t fs)
Definition: math.hpp:27
float gyroSensitivity(const gyro_fs_t fs)
Definition: math.hpp:39
constexpr float kCelsiusOffset
Definition: math.hpp:90
axes_t< int16_t > raw_axes_t
Axes type to hold gyroscope, accelerometer, magnetometer raw data.
Definition: types.hpp:492
uint16_t accelSensitivity(const accel_fs_t fs)
Definition: math.hpp:35
float gyroResolution(const gyro_fs_t fs)
Definition: math.hpp:47
constexpr float kFahrenheitOffset
Definition: math.hpp:100
int16_t magAdjust(const int16_t axis, const uint8_t adjValue)
Definition: math.hpp:113
float gyroDegPerSec(const int16_t axis, const gyro_fs_t fs)
Definition: math.hpp:63
float tempCelsius(const int16_t temp)
Definition: math.hpp:103
uint16_t gyroFSRvalue(const gyro_fs_t fs)
Definition: math.hpp:31