Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
imu_mpu9250_i2c.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Gautier Hattenberger <gautier.hattenberger@enac.fr>
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
28 #include "subsystems/imu.h"
29 #include "mcu_periph/i2c.h"
30 #include "mcu_periph/sys_time.h"
31 #include "subsystems/abi.h"
32 
33 #if !defined IMU_MPU9250_GYRO_LOWPASS_FILTER && !defined IMU_MPU9250_ACCEL_LOWPASS_FILTER && !defined IMU_MPU9250_SMPLRT_DIV
34 #if (PERIODIC_FREQUENCY == 60) || (PERIODIC_FREQUENCY == 120)
35 /* Accelerometer: Bandwidth 41Hz, Delay 5.9ms
36  * Gyroscope: Bandwidth 41Hz, Delay 5.9ms sampling 1kHz
37  * Output rate: 100Hz
38  */
39 #define IMU_MPU9250_GYRO_LOWPASS_FILTER MPU9250_DLPF_GYRO_41HZ
40 #define IMU_MPU9250_ACCEL_LOWPASS_FILTER MPU9250_DLPF_ACCEL_41HZ
41 #define IMU_MPU9250_SMPLRT_DIV 9
42 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
43 #elif PERIODIC_FREQUENCY == 512
44 /* Accelerometer: Bandwidth 184Hz, Delay 5.8ms
45  * Gyroscope: Bandwidth 250Hz, Delay 0.97ms sampling 8kHz
46  * Output rate: 2kHz
47  */
48 #define IMU_MPU9250_GYRO_LOWPASS_FILTER MPU9250_DLPF_GYRO_250HZ
49 #define IMU_MPU9250_ACCEL_LOWPASS_FILTER MPU9250_DLPF_ACCEL_184HZ
50 #define IMU_MPU9250_SMPLRT_DIV 3
51 PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
52 #else
53 /* By default, don't go too fast */
54 #define IMU_MPU9250_SMPLRT_DIV 9
55 #define IMU_MPU9250_GYRO_LOWPASS_FILTER MPU9250_DLPF_GYRO_41HZ
56 #define IMU_MPU9250_ACCEL_LOWPASS_FILTER MPU9250_DLPF_ACCEL_41HZ
57 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
58 #endif
59 #endif
60 PRINT_CONFIG_VAR(IMU_MPU9250_SMPLRT_DIV)
61 PRINT_CONFIG_VAR(IMU_MPU9250_GYRO_LOWPASS_FILTER)
62 PRINT_CONFIG_VAR(IMU_MPU9250_ACCEL_LOWPASS_FILTER)
63 
64 PRINT_CONFIG_VAR(IMU_MPU9250_GYRO_RANGE)
65 PRINT_CONFIG_VAR(IMU_MPU9250_ACCEL_RANGE)
66 
67 #ifndef IMU_MPU9250_I2C_ADDR
68 #define IMU_MPU9250_I2C_ADDR MPU9250_ADDR_ALT
69 #endif
70 PRINT_CONFIG_VAR(IMU_MPU9250_I2C_ADDR)
71 
72 // Default channels order
73 #ifndef IMU_MPU9250_CHAN_X
74 #define IMU_MPU9250_CHAN_X 0
75 #endif
76 PRINT_CONFIG_VAR(IMU_MPU9250_CHAN_X)
77 #ifndef IMU_MPU9250_CHAN_Y
78 #define IMU_MPU9250_CHAN_Y 1
79 #endif
80 PRINT_CONFIG_VAR(IMU_MPU9250_CHAN_Y)
81 #ifndef IMU_MPU9250_CHAN_Z
82 #define IMU_MPU9250_CHAN_Z 2
83 #endif
84 PRINT_CONFIG_VAR(IMU_MPU9250_CHAN_Z)
85 
86 #ifndef IMU_MPU9250_X_SIGN
87 #define IMU_MPU9250_X_SIGN 1
88 #endif
89 PRINT_CONFIG_VAR(IMU_MPU9250_X_SIGN)
90 #ifndef IMU_MPU9250_Y_SIGN
91 #define IMU_MPU9250_Y_SIGN 1
92 #endif
93 PRINT_CONFIG_VAR(IMU_MPU9250_Y_SIGN)
94 #ifndef IMU_MPU9250_Z_SIGN
95 #define IMU_MPU9250_Z_SIGN 1
96 #endif
97 PRINT_CONFIG_VAR(IMU_MPU9250_Z_SIGN)
98 
99 
101 
103 {
104  /* MPU9250 */
105  mpu9250_i2c_init(&imu_mpu9250.mpu, &(IMU_MPU9250_I2C_DEV), IMU_MPU9250_I2C_ADDR);
106  // change the default configuration
112 }
113 
115 {
117 }
118 
120 {
121  uint32_t now_ts = get_sys_time_usec();
122 
123  // If the MPU9250 I2C transaction has succeeded: convert the data
125 
127  // set channel order
128  struct Int32Vect3 accel = {
132  };
133  struct Int32Rates rates = {
137  };
138  // unscaled vector
139  VECT3_COPY(imu.accel_unscaled, accel);
140  RATES_COPY(imu.gyro_unscaled, rates);
141 
143 
146  AbiSendMsgIMU_GYRO_INT32(IMU_MPU9250_ID, now_ts, &imu.gyro);
147  AbiSendMsgIMU_ACCEL_INT32(IMU_MPU9250_ID, now_ts, &imu.accel);
148  }
149 #if IMU_MPU9250_READ_MAG
150  // Test if mag data are updated
152  struct Int32Vect3 mag = {
156  };
159  imu_scale_mag(&imu);
160  AbiSendMsgIMU_MAG_INT32(IMU_MPU9250_ID, now_ts, &imu.mag);
161 
162  }
163 #endif
164 }
165 
Imu::gyro_unscaled
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:46
ImuMpu9250
Definition: imu_mpu9250_i2c.h:73
imu_mpu9250_init
void imu_mpu9250_init(void)
Definition: imu_mpu9250_i2c.c:102
Mpu9250Config::gyro_range
enum Mpu9250GyroRanges gyro_range
deg/s Range
Definition: mpu9250.h:130
ImuMpu9250::mpu
struct Mpu9250_I2c mpu
Definition: imu_mpu9250_i2c.h:74
Int32Rates
angular rates
Definition: pprz_algebra_int.h:179
abi.h
Ak8963::data
union Ak8963::@306 data
Imu::accel
struct Int32Vect3 accel
accelerometer measurements in m/s^2 in BFP with INT32_ACCEL_FRAC
Definition: imu.h:39
imu_mpu9250_periodic
void imu_mpu9250_periodic(void)
Definition: imu_mpu9250_i2c.c:114
Mpu9250_I2c::data_rates
union Mpu9250_I2c::@336 data_rates
Mpu9250Config::dlpf_accel_cfg
enum Mpu9250DLPFAccel dlpf_accel_cfg
Digital Low Pass Filter for accelerometer.
Definition: mpu9250.h:128
uint32_t
unsigned long uint32_t
Definition: types.h:18
Mpu9250Config::dlpf_gyro_cfg
enum Mpu9250DLPFGyro dlpf_gyro_cfg
Digital Low Pass Filter for gyroscope.
Definition: mpu9250.h:129
IMU_MPU9250_CHAN_Z
#define IMU_MPU9250_CHAN_Z
Definition: imu_mpu9250_i2c.c:82
Imu::accel_unscaled
struct Int32Vect3 accel_unscaled
unscaled accelerometer measurements
Definition: imu.h:47
IMU_MPU9250_Y_SIGN
#define IMU_MPU9250_Y_SIGN
Definition: imu_mpu9250_i2c.c:91
Mpu9250_I2c::config
struct Mpu9250Config config
Definition: mpu9250_i2c.h:72
get_sys_time_usec
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
IMU_MPU9250_X_SIGN
#define IMU_MPU9250_X_SIGN
Definition: imu_mpu9250_i2c.c:87
imu.h
imu_mpu9250
struct ImuMpu9250 imu_mpu9250
Definition: imu_mpu9250_i2c.c:100
mpu9250_i2c_periodic
static void mpu9250_i2c_periodic(struct Mpu9250_I2c *mpu)
convenience function: read or start configuration if not already initialized
Definition: mpu9250_i2c.h:86
Int32Vect3
Definition: pprz_algebra_int.h:88
sys_time.h
Architecture independent timing functions.
imu_scale_gyro
void imu_scale_gyro(struct Imu *_imu)
Definition: imu_vectornav.c:43
IMU_MPU9250_GYRO_LOWPASS_FILTER
#define IMU_MPU9250_GYRO_LOWPASS_FILTER
Definition: imu_mpu9250_i2c.c:55
Mpu9250Config::accel_range
enum Mpu9250AccelRanges accel_range
g Range
Definition: mpu9250.h:131
IMU_MPU9250_SMPLRT_DIV
#define IMU_MPU9250_SMPLRT_DIV
Definition: imu_mpu9250_i2c.c:54
IMU_MPU9250_ID
#define IMU_MPU9250_ID
Definition: abi_sender_ids.h:315
mpu9250_i2c_event
void mpu9250_i2c_event(struct Mpu9250_I2c *mpu)
Definition: mpu9250_i2c.c:106
imu
struct Imu imu
global IMU state
Definition: imu.c:108
Mpu9250_I2c::data_accel
union Mpu9250_I2c::@335 data_accel
PRINT_CONFIG_MSG
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
IMU_MPU9250_ACCEL_RANGE
#define IMU_MPU9250_ACCEL_RANGE
Definition: imu_mpu9250_i2c.h:43
Mpu9250Config::smplrt_div
uint8_t smplrt_div
Sample rate divider.
Definition: mpu9250.h:127
int32_t
signed long int32_t
Definition: types.h:19
imu_mpu9250_event
void imu_mpu9250_event(void)
Definition: imu_mpu9250_i2c.c:119
imu_scale_mag
void imu_scale_mag(struct Imu *_imu)
Definition: ahrs_gx3.c:352
IMU_MPU9250_ACCEL_LOWPASS_FILTER
#define IMU_MPU9250_ACCEL_LOWPASS_FILTER
Definition: imu_mpu9250_i2c.c:56
IMU_MPU9250_Z_SIGN
#define IMU_MPU9250_Z_SIGN
Definition: imu_mpu9250_i2c.c:95
IMU_MPU9250_CHAN_X
#define IMU_MPU9250_CHAN_X
Definition: imu_mpu9250_i2c.c:74
Imu::mag
struct Int32Vect3 mag
magnetometer measurements scaled to 1 in BFP with INT32_MAG_FRAC
Definition: imu.h:40
Imu::mag_unscaled
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:48
IMU_MPU9250_CHAN_Y
#define IMU_MPU9250_CHAN_Y
Definition: imu_mpu9250_i2c.c:78
IMU_MPU9250_I2C_ADDR
#define IMU_MPU9250_I2C_ADDR
Definition: imu_mpu9250_i2c.c:68
Mpu9250_I2c::data_available
volatile bool data_available
data ready flag
Definition: mpu9250_i2c.h:62
RATES_COPY
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:337
Ak8963::data_available
volatile bool data_available
data ready flag
Definition: ak8963.h:61
mpu9250_i2c_init
void mpu9250_i2c_init(struct Mpu9250_I2c *mpu, struct i2c_periph *i2c_p, uint8_t addr)
Definition: mpu9250_i2c.c:33
i2c.h
imu_scale_accel
void imu_scale_accel(struct Imu *_imu)
Definition: imu_vectornav.c:44
VECT3_COPY
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:140
IMU_MPU9250_GYRO_RANGE
#define IMU_MPU9250_GYRO_RANGE
Definition: imu_mpu9250_i2c.h:39
Imu::gyro
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:38
Mpu9250_I2c::akm
struct Ak8963 akm
"internal" magnetometer
Definition: mpu9250_i2c.h:75