Paparazzi UAS  v5.12_stable-4-g9b43e9b
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 
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 }
164 
angular rates
enum Mpu9250DLPFGyro dlpf_gyro_cfg
Digital Low Pass Filter for gyroscope.
Definition: mpu9250.h:129
union Mpu9250_I2c::@318 data_rates
void imu_mpu9250_periodic(void)
struct ImuMpu9250 imu_mpu9250
struct Mpu9250Config config
Definition: mpu9250_i2c.h:68
volatile bool data_available
data ready flag
Definition: mpu9250_i2c.h:58
enum Mpu9250GyroRanges gyro_range
deg/s Range
Definition: mpu9250.h:130
#define IMU_MPU9250_Y_SIGN
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:139
void imu_mpu9250_event(void)
#define IMU_MPU9250_I2C_ADDR
Main include for ABI (AirBorneInterface).
#define IMU_MPU9250_X_SIGN
void mpu9250_i2c_init(struct Mpu9250_I2c *mpu, struct i2c_periph *i2c_p, uint8_t addr)
Definition: mpu9250_i2c.c:33
union Mpu9250_I2c::@317 data_accel
struct Imu imu
global IMU state
Definition: imu.c:108
void mpu9250_i2c_event(struct Mpu9250_I2c *mpu)
Definition: mpu9250_i2c.c:101
enum Mpu9250DLPFAccel dlpf_accel_cfg
Digital Low Pass Filter for accelerometer.
Definition: mpu9250.h:128
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:48
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:46
struct Int32Vect3 accel
accelerometer measurements in m/s^2 in BFP with INT32_ACCEL_FRAC
Definition: imu.h:39
#define IMU_MPU9250_ACCEL_LOWPASS_FILTER
struct Ak8963 akm
"internal" magnetometer
Definition: mpu9250_i2c.h:70
Architecture independent timing functions.
unsigned long uint32_t
Definition: types.h:18
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
struct Mpu9250_I2c mpu
#define IMU_MPU9250_ID
struct Int32Vect3 mag
magnetometer measurements scaled to 1 in BFP with INT32_MAG_FRAC
Definition: imu.h:40
#define IMU_MPU9250_CHAN_Y
Inertial Measurement Unit interface.
void imu_scale_gyro(struct Imu *_imu)
Definition: imu_vectornav.c:43
signed long int32_t
Definition: types.h:19
#define IMU_MPU9250_ACCEL_RANGE
#define IMU_MPU9250_Z_SIGN
enum Mpu9250AccelRanges accel_range
g Range
Definition: mpu9250.h:131
void imu_scale_mag(struct Imu *_imu)
Definition: ahrs_gx3.c:351
static void mpu9250_i2c_periodic(struct Mpu9250_I2c *mpu)
convenience function: read or start configuration if not already initialized
Definition: mpu9250_i2c.h:80
#define IMU_MPU9250_GYRO_RANGE
void imu_mpu9250_init(void)
struct Int32Vect3 accel_unscaled
unscaled accelerometer measurements
Definition: imu.h:47
void imu_scale_accel(struct Imu *_imu)
Definition: imu_vectornav.c:44
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:336
uint8_t smplrt_div
Sample rate divider.
Definition: mpu9250.h:127
volatile bool data_available
data ready flag
Definition: ak8963.h:61
union Ak8963::@295 data
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
#define IMU_MPU9250_SMPLRT_DIV
#define IMU_MPU9250_GYRO_LOWPASS_FILTER
#define IMU_MPU9250_CHAN_X
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:38
#define IMU_MPU9250_CHAN_Z
Architecture independent I2C (Inter-Integrated Circuit Bus) API.