Paparazzi UAS  v6.2_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 
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 
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 
ImuMpu9250
Definition: imu_mpu9250_i2c.h:46
imu_mpu9250_init
void imu_mpu9250_init(void)
Definition: imu_mpu9250_i2c.c:103
uint32_t
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
Mpu9250Config::gyro_range
enum Mpu9250GyroRanges gyro_range
deg/s Range
Definition: mpu9250.h:131
ImuMpu9250::mpu
struct Mpu9250_I2c mpu
Definition: imu_mpu9250_i2c.h:47
Ak8963::data
union Ak8963::@297 data
Int32Rates
angular rates
Definition: pprz_algebra_int.h:179
abi.h
MPU9250_GYRO_SENS_FRAC
const struct Int32Rates MPU9250_GYRO_SENS_FRAC[4][2]
Definition: mpu9250.c:39
imu_mpu9250_periodic
void imu_mpu9250_periodic(void)
Definition: imu_mpu9250_i2c.c:119
Mpu9250Config::dlpf_accel_cfg
enum Mpu9250DLPFAccel dlpf_accel_cfg
Digital Low Pass Filter for accelerometer.
Definition: mpu9250.h:129
Mpu9250Config::dlpf_gyro_cfg
enum Mpu9250DLPFGyro dlpf_gyro_cfg
Digital Low Pass Filter for gyroscope.
Definition: mpu9250.h:130
Mpu9250_I2c::data_rates
union Mpu9250_I2c::@329 data_rates
IMU_MPU9250_CHAN_Z
#define IMU_MPU9250_CHAN_Z
Definition: imu_mpu9250_i2c.c:83
Mpu9250_I2c::data_accel
union Mpu9250_I2c::@328 data_accel
IMU_MPU9250_Y_SIGN
#define IMU_MPU9250_Y_SIGN
Definition: imu_mpu9250_i2c.c:92
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:75
imu_mpu9250_i2c.h
IMU_MPU9250_X_SIGN
#define IMU_MPU9250_X_SIGN
Definition: imu_mpu9250_i2c.c:88
imu.h
imu_mpu9250
struct ImuMpu9250 imu_mpu9250
Definition: imu_mpu9250_i2c.c:101
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_MPU9250_GYRO_LOWPASS_FILTER
#define IMU_MPU9250_GYRO_LOWPASS_FILTER
Definition: imu_mpu9250_i2c.c:56
Mpu9250Config::accel_range
enum Mpu9250AccelRanges accel_range
g Range
Definition: mpu9250.h:132
IMU_MPU9250_SMPLRT_DIV
#define IMU_MPU9250_SMPLRT_DIV
Definition: imu_mpu9250_i2c.c:55
IMU_MPU9250_ID
#define IMU_MPU9250_ID
Definition: abi_sender_ids.h:335
mpu9250_i2c_event
void mpu9250_i2c_event(struct Mpu9250_I2c *mpu)
Definition: mpu9250_i2c.c:106
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
int32_t
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83
imu_set_defaults_accel
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
Mpu9250Config::smplrt_div
uint8_t smplrt_div
Sample rate divider.
Definition: mpu9250.h:128
imu_mpu9250_event
void imu_mpu9250_event(void)
Definition: imu_mpu9250_i2c.c:124
IMU_MPU9250_ACCEL_LOWPASS_FILTER
#define IMU_MPU9250_ACCEL_LOWPASS_FILTER
Definition: imu_mpu9250_i2c.c:57
imu_set_defaults_gyro
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
IMU_MPU9250_Z_SIGN
#define IMU_MPU9250_Z_SIGN
Definition: imu_mpu9250_i2c.c:96
IMU_MPU9250_CHAN_X
#define IMU_MPU9250_CHAN_X
Definition: imu_mpu9250_i2c.c:75
IMU_MPU9250_CHAN_Y
#define IMU_MPU9250_CHAN_Y
Definition: imu_mpu9250_i2c.c:79
IMU_MPU9250_I2C_ADDR
#define IMU_MPU9250_I2C_ADDR
Definition: imu_mpu9250_i2c.c:69
Mpu9250_I2c::data_available
volatile bool data_available
data ready flag
Definition: mpu9250_i2c.h:62
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_MPU9250_GYRO_RANGE
#define IMU_MPU9250_GYRO_RANGE
Definition: imu_mpu9250_i2c.h:39
MPU9250_ACCEL_SENS_FRAC
const struct Int32Vect3 MPU9250_ACCEL_SENS_FRAC[4][2]
Definition: mpu9250.c:57
Mpu9250_I2c::akm
struct Ak8963 akm
"internal" magnetometer
Definition: mpu9250_i2c.h:75