Paparazzi UAS  v6.2.0_stable
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 
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 
34 #if !defined IMU_MPU9250_GYRO_LOWPASS_FILTER && !defined IMU_MPU9250_ACCEL_LOWPASS_FILTER && !defined IMU_MPU9250_SMPLRT_DIV
35 #if (PERIODIC_FREQUENCY == 60) || (PERIODIC_FREQUENCY == 120)
36 /* Accelerometer: Bandwidth 41Hz, Delay 5.9ms
37  * Gyroscope: Bandwidth 41Hz, Delay 5.9ms sampling 1kHz
38  * Output rate: 100Hz
39  */
40 #define IMU_MPU9250_GYRO_LOWPASS_FILTER MPU9250_DLPF_GYRO_41HZ
41 #define IMU_MPU9250_ACCEL_LOWPASS_FILTER MPU9250_DLPF_ACCEL_41HZ
42 #define IMU_MPU9250_SMPLRT_DIV 9
43 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
44 #elif PERIODIC_FREQUENCY == 512
45 /* Accelerometer: Bandwidth 184Hz, Delay 5.8ms
46  * Gyroscope: Bandwidth 250Hz, Delay 0.97ms sampling 8kHz
47  * Output rate: 2kHz
48  */
49 #define IMU_MPU9250_GYRO_LOWPASS_FILTER MPU9250_DLPF_GYRO_250HZ
50 #define IMU_MPU9250_ACCEL_LOWPASS_FILTER MPU9250_DLPF_ACCEL_184HZ
51 #define IMU_MPU9250_SMPLRT_DIV 3
52 PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
53 #else
54 /* By default, don't go too fast */
55 #define IMU_MPU9250_SMPLRT_DIV 9
56 #define IMU_MPU9250_GYRO_LOWPASS_FILTER MPU9250_DLPF_GYRO_41HZ
57 #define IMU_MPU9250_ACCEL_LOWPASS_FILTER MPU9250_DLPF_ACCEL_41HZ
58 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
59 #endif
60 #endif
61 PRINT_CONFIG_VAR(IMU_MPU9250_SMPLRT_DIV)
62 PRINT_CONFIG_VAR(IMU_MPU9250_GYRO_LOWPASS_FILTER)
63 PRINT_CONFIG_VAR(IMU_MPU9250_ACCEL_LOWPASS_FILTER)
64 
65 PRINT_CONFIG_VAR(IMU_MPU9250_GYRO_RANGE)
66 PRINT_CONFIG_VAR(IMU_MPU9250_ACCEL_RANGE)
67 
68 #ifndef IMU_MPU9250_I2C_ADDR
69 #define IMU_MPU9250_I2C_ADDR MPU9250_ADDR_ALT
70 #endif
71 PRINT_CONFIG_VAR(IMU_MPU9250_I2C_ADDR)
72 
73 // Default channels order
74 #ifndef IMU_MPU9250_CHAN_X
75 #define IMU_MPU9250_CHAN_X 0
76 #endif
77 PRINT_CONFIG_VAR(IMU_MPU9250_CHAN_X)
78 #ifndef IMU_MPU9250_CHAN_Y
79 #define IMU_MPU9250_CHAN_Y 1
80 #endif
81 PRINT_CONFIG_VAR(IMU_MPU9250_CHAN_Y)
82 #ifndef IMU_MPU9250_CHAN_Z
83 #define IMU_MPU9250_CHAN_Z 2
84 #endif
85 PRINT_CONFIG_VAR(IMU_MPU9250_CHAN_Z)
86 
87 #ifndef IMU_MPU9250_X_SIGN
88 #define IMU_MPU9250_X_SIGN 1
89 #endif
90 PRINT_CONFIG_VAR(IMU_MPU9250_X_SIGN)
91 #ifndef IMU_MPU9250_Y_SIGN
92 #define IMU_MPU9250_Y_SIGN 1
93 #endif
94 PRINT_CONFIG_VAR(IMU_MPU9250_Y_SIGN)
95 #ifndef IMU_MPU9250_Z_SIGN
96 #define IMU_MPU9250_Z_SIGN 1
97 #endif
98 PRINT_CONFIG_VAR(IMU_MPU9250_Z_SIGN)
99 
100 
101 struct ImuMpu9250 imu_mpu9250;
102 
104 {
105  /* MPU9250 */
106  mpu9250_i2c_init(&imu_mpu9250.mpu, &(IMU_MPU9250_I2C_DEV), IMU_MPU9250_I2C_ADDR);
107  // change the default configuration
113 
114  // Set the default scaling
117 }
118 
120 {
122 }
123 
125 {
126  uint32_t now_ts = get_sys_time_usec();
127 
128  // If the MPU9250 I2C transaction has succeeded: convert the data
130 
132  // set channel order
133  struct Int32Vect3 accel = {
137  };
138  struct Int32Rates rates = {
142  };
143 
145  AbiSendMsgIMU_GYRO_RAW(IMU_MPU9250_ID, now_ts, &rates, 1, NAN);
146  AbiSendMsgIMU_ACCEL_RAW(IMU_MPU9250_ID, now_ts, &accel, 1, NAN);
147  }
148 #if IMU_MPU9250_READ_MAG
149  // Test if mag data are updated
151  struct Int32Vect3 mag = {
155  };
157  AbiSendMsgIMU_MAG_RAW(IMU_MPU9250_ID, now_ts, &mag);
158 
159  }
160 #endif
161 }
162 
Main include for ABI (AirBorneInterface).
#define IMU_MPU9250_ID
volatile bool data_available
data ready flag
Definition: ak8963.h:61
union Ak8963::@297 data
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:75
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:440
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:410
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
struct Ak8963 akm
"internal" magnetometer
Definition: mpu9250_i2c.h:75
union Mpu9250_I2c::@328 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
union Mpu9250_I2c::@329 data_rates
volatile bool data_available
data ready flag
Definition: mpu9250_i2c.h:62
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