Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
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
27
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 <= 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
44PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
45
46#elif PERIODIC_FREQUENCY < 500
47/* Accelerometer: Bandwidth 92Hz
48 * Gyroscope: Bandwidth 92Hz, Delay 3.9ms sampling 1kHz
49 * Output rate: 500Hz
50 */
51#define IMU_MPU9250_GYRO_LOWPASS_FILTER MPU9250_DLPF_GYRO_92HZ
52#define IMU_MPU9250_ACCEL_LOWPASS_FILTER MPU9250_DLPF_ACCEL_92HZ
53#define IMU_MPU9250_SMPLRT_DIV 1
54PRINT_CONFIG_MSG("Gyro/Accel output rate is 500Hz at 1kHz internal sampling")
55
56#else // PERIODIC_FREQUENCY >= 500
57/* Accelerometer: Bandwidth 184Hz, Delay 5.8ms
58 * Gyroscope: Bandwidth 250Hz, Delay 0.97ms sampling 8kHz
59 * Output rate: 2kHz
60 */
61#define IMU_MPU9250_GYRO_LOWPASS_FILTER MPU9250_DLPF_GYRO_250HZ
62#define IMU_MPU9250_ACCEL_LOWPASS_FILTER MPU9250_DLPF_ACCEL_184HZ
63#define IMU_MPU9250_SMPLRT_DIV 3
64PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
65
66#endif
67#endif
68
74
75#ifndef IMU_MPU9250_I2C_ADDR
76#define IMU_MPU9250_I2C_ADDR MPU9250_ADDR_ALT
77#endif
79
80// Default channels order
81#ifndef IMU_MPU9250_CHAN_X
82#define IMU_MPU9250_CHAN_X 0
83#endif
85#ifndef IMU_MPU9250_CHAN_Y
86#define IMU_MPU9250_CHAN_Y 1
87#endif
89#ifndef IMU_MPU9250_CHAN_Z
90#define IMU_MPU9250_CHAN_Z 2
91#endif
93
94#ifndef IMU_MPU9250_X_SIGN
95#define IMU_MPU9250_X_SIGN 1
96#endif
98#ifndef IMU_MPU9250_Y_SIGN
99#define IMU_MPU9250_Y_SIGN 1
100#endif
102#ifndef IMU_MPU9250_Z_SIGN
103#define IMU_MPU9250_Z_SIGN 1
104#endif
106
107
109
111{
112 /* MPU9250 */
113 mpu9250_i2c_init(&imu_mpu9250.mpu, &(IMU_MPU9250_I2C_DEV), IMU_MPU9250_I2C_ADDR);
114 // change the default configuration
115 imu_mpu9250.mpu.config.smplrt_div = IMU_MPU9250_SMPLRT_DIV;
116 imu_mpu9250.mpu.config.dlpf_gyro_cfg = IMU_MPU9250_GYRO_LOWPASS_FILTER;
117 imu_mpu9250.mpu.config.dlpf_accel_cfg = IMU_MPU9250_ACCEL_LOWPASS_FILTER;
118 imu_mpu9250.mpu.config.gyro_range = IMU_MPU9250_GYRO_RANGE;
119 imu_mpu9250.mpu.config.accel_range = IMU_MPU9250_ACCEL_RANGE;
120
121 // Set the default scaling
124}
125
130
132{
133 uint32_t now_ts = get_sys_time_usec();
134
135 // If the MPU9250 I2C transaction has succeeded: convert the data
137
138 if (imu_mpu9250.mpu.data_available) {
139 // set channel order
140 struct Int32Vect3 accel = {
144 };
145 struct Int32Rates rates = {
149 };
150
151 imu_mpu9250.mpu.data_available = false;
152 AbiSendMsgIMU_GYRO_RAW(IMU_MPU9250_ID, now_ts, &rates, 1, IMU_MPU9250_PERIODIC_FREQ, NAN);
153 AbiSendMsgIMU_ACCEL_RAW(IMU_MPU9250_ID, now_ts, &accel, 1, IMU_MPU9250_PERIODIC_FREQ, NAN);
154 }
155#if IMU_MPU9250_READ_MAG
156 // Test if mag data are updated
157 if (imu_mpu9250.mpu.akm.data_available) {
158 struct Int32Vect3 mag = {
162 };
163 imu_mpu9250.mpu.akm.data_available = false;
164 AbiSendMsgIMU_MAG_RAW(IMU_MPU9250_ID, now_ts, &mag);
165
166 }
167#endif
168}
169
Main include for ABI (AirBorneInterface).
#define IMU_MPU9250_ID
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
angular rates
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
void imu_set_defaults_gyro(uint8_t abi_id, const struct Int32RMat *imu_to_sensor, const struct Int32Rates *neutral, const struct FloatRates *scale_f)
Set the defaults for a gyro sensor WARNING: Should be called before sensor is publishing messages to ...
Definition imu.c:842
void imu_set_defaults_accel(uint8_t abi_id, const struct Int32RMat *imu_to_sensor, const struct Int32Vect3 *neutral, const struct FloatVect3 *scale_f)
Set the defaults for a accel sensor WARNING: Should be called before sensor is publishing messages to...
Definition imu.c:871
Inertial Measurement Unit interface.
void imu_mpu9250_init(void)
Definition imu_mpu9250.c:37
#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
#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
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
const struct FloatRates MPU9250_GYRO_SENS_F[4]
Definition mpu9250.c:39
const struct FloatVect3 MPU9250_ACCEL_SENS_F[4]
Definition mpu9250.c:53
void mpu9250_i2c_event(struct Mpu9250_I2c *mpu)
void mpu9250_i2c_init(struct Mpu9250_I2c *mpu, struct i2c_periph *i2c_p, uint8_t addr)
Definition mpu9250_i2c.c:33
static void mpu9250_i2c_periodic(struct Mpu9250_I2c *mpu)
convenience function: read or start configuration if not already initialized
Definition mpu9250_i2c.h:86
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
Architecture independent timing functions.
int int32_t
Typedef defining 32 bit int type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.