Paparazzi UAS  v7.0_unstable
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 
29 #include "modules/imu/imu.h"
30 #include "mcu_periph/i2c.h"
31 #include "mcu_periph/sys_time.h"
32 #include "modules/core/abi.h"
33 #include "generated/modules.h"
34 
35 #if !defined IMU_MPU9250_GYRO_LOWPASS_FILTER && !defined IMU_MPU9250_ACCEL_LOWPASS_FILTER && !defined IMU_MPU9250_SMPLRT_DIV
36 #if (PERIODIC_FREQUENCY >= 60) && (PERIODIC_FREQUENCY <= 120)
37 /* Accelerometer: Bandwidth 41Hz, Delay 5.9ms
38  * Gyroscope: Bandwidth 41Hz, Delay 5.9ms sampling 1kHz
39  * Output rate: 100Hz
40  */
41 #define IMU_MPU9250_GYRO_LOWPASS_FILTER MPU9250_DLPF_GYRO_41HZ
42 #define IMU_MPU9250_ACCEL_LOWPASS_FILTER MPU9250_DLPF_ACCEL_41HZ
43 #define IMU_MPU9250_SMPLRT_DIV 9
44 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
45 #elif (PERIODIC_FREQUENCY == 512) || (PERIODIC_FREQUENCY == 500)
46 /* Accelerometer: Bandwidth 184Hz, Delay 5.8ms
47  * Gyroscope: Bandwidth 250Hz, Delay 0.97ms sampling 8kHz
48  * Output rate: 2kHz
49  */
50 #define IMU_MPU9250_GYRO_LOWPASS_FILTER MPU9250_DLPF_GYRO_250HZ
51 #define IMU_MPU9250_ACCEL_LOWPASS_FILTER MPU9250_DLPF_ACCEL_184HZ
52 #define IMU_MPU9250_SMPLRT_DIV 3
53 PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
54 #else
55 /* By default, don't go too fast */
56 #define IMU_MPU9250_SMPLRT_DIV 9
57 #define IMU_MPU9250_GYRO_LOWPASS_FILTER MPU9250_DLPF_GYRO_41HZ
58 #define IMU_MPU9250_ACCEL_LOWPASS_FILTER MPU9250_DLPF_ACCEL_41HZ
59 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
60 #warning "Non-default PERIODIC_FREQUENCY: please define IMU_MPU9250_GYRO_LOWPASS_FILTER, IMU_MPU9250_ACCEL_LOWPASS_FILTER and IMU_MPU9250_SMPLRT_DIV."
61 #endif
62 #endif
66 
69 
70 #ifndef IMU_MPU9250_I2C_ADDR
71 #define IMU_MPU9250_I2C_ADDR MPU9250_ADDR_ALT
72 #endif
74 
75 // Default channels order
76 #ifndef IMU_MPU9250_CHAN_X
77 #define IMU_MPU9250_CHAN_X 0
78 #endif
80 #ifndef IMU_MPU9250_CHAN_Y
81 #define IMU_MPU9250_CHAN_Y 1
82 #endif
84 #ifndef IMU_MPU9250_CHAN_Z
85 #define IMU_MPU9250_CHAN_Z 2
86 #endif
88 
89 #ifndef IMU_MPU9250_X_SIGN
90 #define IMU_MPU9250_X_SIGN 1
91 #endif
93 #ifndef IMU_MPU9250_Y_SIGN
94 #define IMU_MPU9250_Y_SIGN 1
95 #endif
97 #ifndef IMU_MPU9250_Z_SIGN
98 #define IMU_MPU9250_Z_SIGN 1
99 #endif
101 
102 
103 struct ImuMpu9250 imu_mpu9250;
104 
106 {
107  /* MPU9250 */
108  mpu9250_i2c_init(&imu_mpu9250.mpu, &(IMU_MPU9250_I2C_DEV), IMU_MPU9250_I2C_ADDR);
109  // change the default configuration
115 
116  // Set the default scaling
119 }
120 
122 {
124 }
125 
127 {
128  uint32_t now_ts = get_sys_time_usec();
129 
130  // If the MPU9250 I2C transaction has succeeded: convert the data
132 
134  // set channel order
135  struct Int32Vect3 accel = {
139  };
140  struct Int32Rates rates = {
144  };
145 
147  AbiSendMsgIMU_GYRO_RAW(IMU_MPU9250_ID, now_ts, &rates, 1, IMU_MPU9250_PERIODIC_FREQ, NAN);
148  AbiSendMsgIMU_ACCEL_RAW(IMU_MPU9250_ID, now_ts, &accel, 1, IMU_MPU9250_PERIODIC_FREQ, NAN);
149  }
150 #if IMU_MPU9250_READ_MAG
151  // Test if mag data are updated
153  struct Int32Vect3 mag = {
157  };
159  AbiSendMsgIMU_MAG_RAW(IMU_MPU9250_ID, now_ts, &mag);
160 
161  }
162 #endif
163 }
164 
Main include for ABI (AirBorneInterface).
#define IMU_MPU9250_ID
volatile bool data_available
data ready flag
Definition: ak8963.h:61
union Ak8963::@310 data
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:71
angular rates
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
void imu_set_defaults_accel(uint8_t abi_id, const struct Int32RMat *imu_to_sensor, const struct Int32Vect3 *neutral, const struct Int32Vect3 *scale)
Set the defaults for a accel sensor WARNING: Should be called before sensor is publishing messages to...
Definition: imu.c:521
void imu_set_defaults_gyro(uint8_t abi_id, const struct Int32RMat *imu_to_sensor, const struct Int32Rates *neutral, const struct Int32Rates *scale)
Set the defaults for a gyro sensor WARNING: Should be called before sensor is publishing messages to ...
Definition: imu.c:491
Inertial Measurement Unit interface.
#define IMU_MPU9250_GYRO_LOWPASS_FILTER
#define IMU_MPU9250_X_SIGN
#define IMU_MPU9250_CHAN_X
void imu_mpu9250_periodic(void)
#define IMU_MPU9250_Y_SIGN
#define IMU_MPU9250_ACCEL_LOWPASS_FILTER
void imu_mpu9250_init(void)
#define IMU_MPU9250_Z_SIGN
#define IMU_MPU9250_SMPLRT_DIV
#define IMU_MPU9250_I2C_ADDR
#define IMU_MPU9250_CHAN_Z
#define IMU_MPU9250_CHAN_Y
struct ImuMpu9250 imu_mpu9250
void imu_mpu9250_event(void)
IMU driver for the MPU9250 using I2C.
#define IMU_MPU9250_ACCEL_RANGE
#define IMU_MPU9250_GYRO_RANGE
struct Mpu9250_I2c mpu
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
const struct Int32Vect3 MPU9250_ACCEL_SENS_FRAC[4][2]
Definition: mpu9250.c:57
const struct Int32Rates MPU9250_GYRO_SENS_FRAC[4][2]
Definition: mpu9250.c:39
enum Mpu9250DLPFGyro dlpf_gyro_cfg
Digital Low Pass Filter for gyroscope.
Definition: mpu9250.h:130
enum Mpu9250GyroRanges gyro_range
deg/s Range
Definition: mpu9250.h:131
enum Mpu9250DLPFAccel dlpf_accel_cfg
Digital Low Pass Filter for accelerometer.
Definition: mpu9250.h:129
uint8_t smplrt_div
Sample rate divider.
Definition: mpu9250.h:128
enum Mpu9250AccelRanges accel_range
g Range
Definition: mpu9250.h:132
void mpu9250_i2c_event(struct Mpu9250_I2c *mpu)
Definition: mpu9250_i2c.c:106
void mpu9250_i2c_init(struct Mpu9250_I2c *mpu, struct i2c_periph *i2c_p, uint8_t addr)
Definition: mpu9250_i2c.c:33
struct Mpu9250Config config
Definition: mpu9250_i2c.h:72
union Mpu9250_I2c::@344 data_rates
struct Ak8963 akm
"internal" magnetometer
Definition: mpu9250_i2c.h:75
union Mpu9250_I2c::@343 data_accel
static void mpu9250_i2c_periodic(struct Mpu9250_I2c *mpu)
convenience function: read or start configuration if not already initialized
Definition: mpu9250_i2c.h:86
volatile bool data_available
data ready flag
Definition: mpu9250_i2c.h:62
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
Architecture independent timing functions.
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78