NinjaFlight
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
imu.h
Go to the documentation of this file.
1 /*
2  * This file is part of Ninjaflight.
3  *
4  * Ninjaflight is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Ninjaflight is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Ninjaflight. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #pragma once
19 
20 #include <stdbool.h>
21 
22 #include "common/axis.h"
23 #include "common/maths.h"
24 #include "common/quaternion.h"
25 #include "../config/imu.h"
26 
28  int16_t raw[3];
29  struct {
30  int16_t x, y, z;
31  } values;
32 };
33 
35  int16_t raw[XYZ_AXIS_COUNT];
36  struct {
37  // absolute angle inclination in multiple of 0.1 degree 180 deg = 1800
38  int16_t roll;
39  int16_t pitch;
40  int16_t yaw;
41  } values;
42 };
43 
44 // TODO: move these into math lib
45 typedef int32_t gyro_rates_t[3];
46 typedef int32_t acc_rates_t[3];
48 
49 struct imu {
50  int16_t acc[XYZ_AXIS_COUNT];
51  int16_t gyro[XYZ_AXIS_COUNT];
52  int16_t mag[XYZ_AXIS_COUNT];
53 
56 
57  float accTimeSum; // keep track for integration of acc
59  float accVelScale;
60 
61  uint8_t flags;
62 
64 
65  float rMat[3][3];
66 
67  union attitude_euler_angles attitude; // absolute angle inclination in multiple of 0.1 degree 180 deg = 1800
68 
69  // TODO: investigate if we can refactor this so that we can pass already precomputed mag data without need to speficy this
71 
72  // this is yaw that we are currently inputing from gps or some other source
73  int16_t yaw;
74 
75  float gyroScale;
76  int16_t acc_1G;
77 
78  const struct config *config;
79 };
80 
81 void imu_init(struct imu *self, const struct config *config);
82 
83 void imu_reload_config(struct imu *self);
84 void imu_input_accelerometer(struct imu *self, int16_t x, int16_t y, int16_t z);
85 void imu_input_gyro(struct imu *self, int16_t x, int16_t y, int16_t z);
86 void imu_input_magnetometer(struct imu *self, int16_t x, int16_t y, int16_t z);
87 void imu_input_yaw_dd(struct imu *self, int16_t yaw);
88 void imu_update(struct imu *self, float dt);
89 
90 void imu_reset(struct imu *self);
92 int16_t imu_calc_heading(struct imu *self, t_fp_vector *vec);
93 float imu_get_cos_tilt_angle(struct imu *self);
94 bool imu_is_leveled(struct imu *self, uint8_t max_angle);
95 
96 void imu_get_attitude_dd(struct imu *self, union attitude_euler_angles *att);
97 void imu_get_raw_accel(struct imu *self, union imu_accel_reading *acc);
98 
99 void imu_set_acc_scale(struct imu *self, int16_t acc_1G);
100 void imu_set_gyro_scale(struct imu *self, float gyro_scale);
101 
102 // TODO: replace with get_angular_velocity once we have refactored pids to use radians/s
103 void imu_get_gyro(struct imu *self, int16_t gyro[3]);
104 
105 // TODO: this needs to be removed since it is only used in anglerate controller. Leaving for now since don't want to make too many changes.
106 float imu_get_gyro_scale(struct imu *self);
107 
108 // helper functions to extract a specific component from the attitude
109 int16_t imu_get_roll_dd(struct imu *self);
110 int16_t imu_get_pitch_dd(struct imu *self);
111 int16_t imu_get_yaw_dd(struct imu *self);
112 
113 void imu_get_rotation(struct imu *self, quat_t *q);
114 
115 void imu_enable_fast_dcm_convergence(struct imu *self, bool on);
116 
117 float imu_get_avg_vertical_accel_cmss(struct imu *self);
118 float imu_get_est_vertical_vel_cms(struct imu *self);
119 float imu_get_velocity_integration_time(struct imu *self);
120 void imu_reset_velocity_estimate(struct imu *self);
121 
#define XYZ_AXIS_COUNT
Definition: axis.h:26
void imu_input_yaw_dd(struct imu *self, int16_t yaw)
Definition: imu.c:388
int16_t acc_1G
Definition: imu.h:76
int16_t acc[XYZ_AXIS_COUNT]
Definition: imu.h:50
Definition: config.h:81
int16_t x
Definition: imu.h:30
int16_t imu_get_roll_dd(struct imu *self)
Definition: imu.c:438
int16_t imu_get_pitch_dd(struct imu *self)
Definition: imu.c:442
void imu_input_magnetometer(struct imu *self, int16_t x, int16_t y, int16_t z)
Definition: imu.c:375
uint8_t throttle_correction_value
Definition: imu.h:64
Definition: quaternion.h:3
float gyroScale
Definition: imu.h:75
void imu_enable_fast_dcm_convergence(struct imu *self, bool on)
Definition: imu.c:468
struct attitude_euler_angles::@23 values
Definition: imu.h:49
void imu_init(struct imu *self, const struct config *config)
Definition: imu.c:106
int accSumCount
Definition: imu.h:58
int16_t y
Definition: imu.h:30
bool imu_is_leveled(struct imu *self, uint8_t max_angle)
Definition: imu.c:316
int16_t yaw
Definition: imu.h:40
Definition: imu.h:34
int16_t imu_get_yaw_dd(struct imu *self)
Definition: imu.c:446
void imu_set_gyro_scale(struct imu *self, float gyro_scale)
float imu_get_est_vertical_vel_cms(struct imu *self)
Definition: imu.c:464
int32_t acc_rates_t[3]
Definition: imu.h:46
int16_t z
Definition: imu.h:30
quat_t q
Definition: imu.h:63
void imu_get_rotation(struct imu *self, quat_t *q)
Definition: imu.c:393
int16_t yaw
Definition: imu.h:73
void imu_input_accelerometer(struct imu *self, int16_t x, int16_t y, int16_t z)
Definition: imu.c:368
int16_t imu_calc_heading(struct imu *self, t_fp_vector *vec)
struct gyro_config gyro
Definition: config.h:93
float imu_get_velocity_integration_time(struct imu *self)
Definition: imu.c:450
float imu_get_avg_vertical_accel_cmss(struct imu *self)
Definition: imu.c:455
float imu_get_cos_tilt_angle(struct imu *self)
Definition: imu.c:397
uint8_t flags
Definition: imu.h:61
uint8_t z
set the acc deadband for z-Axis, this ignores small accelerations
Definition: accelerometer.h:52
int16_t roll
Definition: imu.h:38
struct accelerometer_config acc
Definition: config.h:93
void imu_input_gyro(struct imu *self, int16_t x, int16_t y, int16_t z)
Definition: imu.c:382
int16_t pitch
Definition: imu.h:39
int16_t yaw
Definition: sensors.h:31
void imu_reload_config(struct imu *self)
int32_t accSum[XYZ_AXIS_COUNT]
Definition: imu.h:55
int16_t mag[XYZ_AXIS_COUNT]
Definition: imu.h:52
void imu_reset(struct imu *self)
Definition: imu.c:94
int32_t gyro_rates_t[3]
Definition: imu.h:45
void imu_reset_velocity_estimate(struct imu *self)
Definition: imu.c:116
int16_t raw[3]
Definition: imu.h:28
void imu_get_gyro(struct imu *self, int16_t gyro[3])
Definition: imu.c:422
void imu_get_raw_accel(struct imu *self, union imu_accel_reading *acc)
Definition: imu.c:432
struct imu_accel_reading::@22 values
const struct config * config
Definition: imu.h:78
void imu_get_attitude_dd(struct imu *self, union attitude_euler_angles *att)
Definition: imu.c:418
float accTimeSum
Definition: imu.h:57
union attitude_euler_angles attitude
Definition: imu.h:67
int16_t accSmooth[XYZ_AXIS_COUNT]
Definition: imu.h:54
int16_t raw[XYZ_AXIS_COUNT]
Definition: imu.h:35
int16_t gyro[XYZ_AXIS_COUNT]
Definition: imu.h:51
float accVelScale
Definition: imu.h:59
void imu_update(struct imu *self, float dt)
Definition: imu.c:329
float magneticDeclination
Definition: imu.h:70
float imu_get_gyro_scale(struct imu *self)
Definition: imu.c:428
Definition: imu.h:27
Definition: maths.h:52
int16_t imu_calc_throttle_angle_correction(struct imu *self, uint8_t throttle_correction_value)
Definition: imu.c:402
void imu_set_acc_scale(struct imu *self, int16_t acc_1G)
float rMat[3][3]
Definition: imu.h:65