Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
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 #ifndef IMU_MPU9250_GYRO_RANGE
65 #define IMU_MPU9250_GYRO_RANGE MPU9250_GYRO_RANGE_1000
66 #endif
67 PRINT_CONFIG_VAR(IMU_MPU9250_GYRO_RANGE)
68 
69 #ifndef IMU_MPU9250_ACCEL_RANGE
70 #define IMU_MPU9250_ACCEL_RANGE MPU9250_ACCEL_RANGE_8G
71 #endif
72 PRINT_CONFIG_VAR(IMU_MPU9250_ACCEL_RANGE)
73 
74 #ifndef IMU_MPU9250_I2C_ADDR
75 #define IMU_MPU9250_I2C_ADDR MPU9250_ADDR_ALT
76 #endif
77 PRINT_CONFIG_VAR(IMU_MPU9250_I2C_ADDR)
78 
79 // Default channels order
80 #ifndef IMU_MPU9250_CHAN_X
81 #define IMU_MPU9250_CHAN_X 0
82 #endif
83 PRINT_CONFIG_VAR(IMU_MPU9250_CHAN_X)
84 #ifndef IMU_MPU9250_CHAN_Y
85 #define IMU_MPU9250_CHAN_Y 1
86 #endif
87 PRINT_CONFIG_VAR(IMU_MPU9250_CHAN_Y)
88 #ifndef IMU_MPU9250_CHAN_Z
89 #define IMU_MPU9250_CHAN_Z 2
90 #endif
91 PRINT_CONFIG_VAR(IMU_MPU9250_CHAN_Z)
92 
93 
95 
96 void imu_mpu9250_init(void)
97 {
98  /* MPU9250 */
99  mpu9250_i2c_init(&imu_mpu9250.mpu, &(IMU_MPU9250_I2C_DEV), IMU_MPU9250_I2C_ADDR);
100  // change the default configuration
106 }
107 
109 {
111 }
112 
114 {
115  uint32_t now_ts = get_sys_time_usec();
116 
117  // If the MPU9250 I2C transaction has succeeded: convert the data
119 
121  // set channel order
122  struct Int32Vect3 accel = {
126  };
127  struct Int32Rates rates = {
131  };
132  // unscaled vector
133  VECT3_COPY(imu.accel_unscaled, accel);
134  RATES_COPY(imu.gyro_unscaled, rates);
135 
137 
140  AbiSendMsgIMU_GYRO_INT32(IMU_MPU9250_ID, now_ts, &imu.gyro);
141  AbiSendMsgIMU_ACCEL_INT32(IMU_MPU9250_ID, now_ts, &imu.accel);
142  }
143 
144  // Test if mag data are updated
146  struct Int32Vect3 mag = {
150  };
153  imu_scale_mag(&imu);
154  AbiSendMsgIMU_MAG_INT32(IMU_MPU9250_ID, now_ts, &imu.mag);
155  }
156 
157 }
158 
angular rates
union Mpu9250_I2c::@316 data_accel
enum Mpu9250DLPFGyro dlpf_gyro_cfg
Digital Low Pass Filter for gyroscope.
Definition: mpu9250.h:83
void imu_mpu9250_periodic(void)
void imu_scale_gyro(struct Imu *_imu)
Definition: ahrs_gx3.c:349
struct ImuMpu9250 imu_mpu9250
struct Mpu9250Config config
Definition: mpu9250_i2c.h:68
volatile bool data_available
data ready flag
Definition: mpu9250_i2c.h:58
void imu_scale_accel(struct Imu *_imu)
Definition: ahrs_gx3.c:350
enum Mpu9250GyroRanges gyro_range
deg/s Range
Definition: mpu9250.h:84
#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).
void mpu9250_i2c_init(struct Mpu9250_I2c *mpu, struct i2c_periph *i2c_p, uint8_t addr)
Definition: mpu9250_i2c.c:33
struct Imu imu
global IMU state
Definition: imu.c:108
union Ak8963::@293 data
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:82
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.
#define IMU_MPU9250_ACCEL_RANGE
signed long int32_t
Definition: types.h:19
enum Mpu9250AccelRanges accel_range
g Range
Definition: mpu9250.h:85
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
void imu_mpu9250_init(void)
struct Int32Vect3 accel_unscaled
unscaled accelerometer measurements
Definition: imu.h:47
union Mpu9250_I2c::@317 data_rates
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:336
uint8_t smplrt_div
Sample rate divider.
Definition: mpu9250.h:81
volatile bool data_available
data ready flag
Definition: ak8963.h:61
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_GYRO_RANGE
#define IMU_MPU9250_CHAN_Z
Architecture independent I2C (Inter-Integrated Circuit Bus) API.