Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
imu_mpu6000.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2013-2015 Felix Ruess <felix.ruess@gmail.com>
3 * Copyright (C) 2019 Gautier Hattenberger <gautier.hattenberger@enac.fr>
4 *
5 * This file is part of paparazzi.
6 *
7 * paparazzi is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * paparazzi is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with paparazzi; see the file COPYING. If not, write to
19 * the Free Software Foundation, 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
27
29#include "modules/imu/imu.h"
30#include "modules/core/abi.h"
31#include "mcu_periph/spi.h"
32#include "generated/modules.h"
33#if IMU_MPU_USE_MEDIAN_FILTER
35#endif
36
37/* SPI defaults set in subsystem makefile, can be configured from airframe file */
38PRINT_CONFIG_VAR(IMU_MPU_SPI_SLAVE_IDX)
39PRINT_CONFIG_VAR(IMU_MPU_SPI_DEV)
40
41/* MPU60x0 gyro/accel internal lowpass frequency */
42#if !defined IMU_MPU_LOWPASS_FILTER && !defined IMU_MPU_SMPLRT_DIV
43#if PERIODIC_FREQUENCY <= 120
44/* Accelerometer: Bandwidth 44Hz, Delay 4.9ms
45 * Gyroscope: Bandwidth 42Hz, Delay 4.8ms sampling 1kHz
46 */
47#define IMU_MPU_LOWPASS_FILTER MPU60X0_DLPF_42HZ
48#define IMU_MPU_SMPLRT_DIV 9
49PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
50
51#ifndef IMU_MPU_ACCEL_LOWPASS_FILTER
52#define IMU_MPU_ACCEL_LOWPASS_FILTER MPU60X0_DLPF_ACC_44HZ // for ICM sensors
53#endif
54
55#elif PERIODIC_FREQUENCY < 500
56/* Accelerometer: Bandwidth 94Hz, Delay 3ms
57 * Gyroscope: Bandwidth 98Hz, Delay 2.8ms sampling 1kHz
58 */
59#define IMU_MPU_LOWPASS_FILTER MPU60X0_DLPF_98HZ
60#define IMU_MPU_SMPLRT_DIV 1
61PRINT_CONFIG_MSG("Gyro/Accel output rate is 500Hz at 1kHz internal sampling")
62
63#ifndef IMU_MPU_ACCEL_LOWPASS_FILTER
64#define IMU_MPU_ACCEL_LOWPASS_FILTER MPU60X0_DLPF_ACC_99HZ // for ICM sensors
65#endif
66
67#else // PERIODIC_FREQUENCY >= 500
68/* Accelerometer: Bandwidth 260Hz, Delay 0ms
69 * Gyroscope: Bandwidth 256Hz, Delay 0.98ms sampling 8kHz
70 */
71#define IMU_MPU_LOWPASS_FILTER MPU60X0_DLPF_256HZ
72#define IMU_MPU_SMPLRT_DIV 3
73PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
74
75#ifndef IMU_MPU_ACCEL_LOWPASS_FILTER
76#define IMU_MPU_ACCEL_LOWPASS_FILTER MPU60X0_DLPF_ACC_218HZ // for ICM sensors
77#endif
78
79#endif
80#endif
81
87
88// Default channels order
89#ifndef IMU_MPU_CHAN_X
90#define IMU_MPU_CHAN_X 0
91#endif
93#ifndef IMU_MPU_CHAN_Y
94#define IMU_MPU_CHAN_Y 1
95#endif
97#ifndef IMU_MPU_CHAN_Z
98#define IMU_MPU_CHAN_Z 2
99#endif
101
102// Default channel signs
103#ifndef IMU_MPU_X_SIGN
104#define IMU_MPU_X_SIGN 1
105#endif
107#ifndef IMU_MPU_Y_SIGN
108#define IMU_MPU_Y_SIGN 1
109#endif
111#ifndef IMU_MPU_Z_SIGN
112#define IMU_MPU_Z_SIGN 1
113#endif
115
117
118#if IMU_MPU_USE_MEDIAN_FILTER
119static struct MedianFilter3Int medianfilter_accel;
120static struct MedianFilter3Int medianfilter_rates;
121#endif
122
124{
125
126#if IMU_MPU_USE_MEDIAN_FILTER
127 InitMedianFilterVect3Int(medianfilter_accel, 3);
128 InitMedianFilterRatesInt(medianfilter_rates, 3);
129#endif
130
131 mpu60x0_spi_init(&imu_mpu_spi.mpu, &IMU_MPU_SPI_DEV, IMU_MPU_SPI_SLAVE_IDX);
132 // change the default configuration
133 imu_mpu_spi.mpu.config.smplrt_div = IMU_MPU_SMPLRT_DIV;
134 imu_mpu_spi.mpu.config.dlpf_cfg = IMU_MPU_LOWPASS_FILTER;
135 imu_mpu_spi.mpu.config.dlpf_cfg_acc = IMU_MPU_ACCEL_LOWPASS_FILTER; // only for ICM sensors
136 imu_mpu_spi.mpu.config.gyro_range = IMU_MPU_GYRO_RANGE;
137 imu_mpu_spi.mpu.config.accel_range = IMU_MPU_ACCEL_RANGE;
138
139 // Set the default scaling
142}
143
148
150{
152 if (imu_mpu_spi.mpu.data_available) {
153 uint32_t now_ts = get_sys_time_usec();
154
155 // set channel order
156 struct Int32Vect3 accel = {
157 IMU_MPU_X_SIGN * (int32_t)(imu_mpu_spi.mpu.data_accel.value[IMU_MPU_CHAN_X]),
158 IMU_MPU_Y_SIGN * (int32_t)(imu_mpu_spi.mpu.data_accel.value[IMU_MPU_CHAN_Y]),
159 IMU_MPU_Z_SIGN * (int32_t)(imu_mpu_spi.mpu.data_accel.value[IMU_MPU_CHAN_Z])
160 };
161 struct Int32Rates rates = {
162 IMU_MPU_X_SIGN * (int32_t)(imu_mpu_spi.mpu.data_rates.value[IMU_MPU_CHAN_X]),
163 IMU_MPU_Y_SIGN * (int32_t)(imu_mpu_spi.mpu.data_rates.value[IMU_MPU_CHAN_Y]),
164 IMU_MPU_Z_SIGN * (int32_t)(imu_mpu_spi.mpu.data_rates.value[IMU_MPU_CHAN_Z])
165 };
166
167 // In case sensor exhibits faulty large spike values in raw output remove them
168#if IMU_MPU_USE_MEDIAN_FILTER
169 UpdateMedianFilterVect3Int(medianfilter_accel, accel);
170 UpdateMedianFilterRatesInt(medianfilter_rates, rates);
171#endif
172
173 imu_mpu_spi.mpu.data_available = false;
174
175 // Send the scaled values over ABI
176 AbiSendMsgIMU_GYRO_RAW(IMU_MPU6000_ID, now_ts, &rates, 1, IMU_MPU_SPI_PERIODIC_FREQ, imu_mpu_spi.mpu.temp);
177 AbiSendMsgIMU_ACCEL_RAW(IMU_MPU6000_ID, now_ts, &accel, 1, IMU_MPU_SPI_PERIODIC_FREQ, imu_mpu_spi.mpu.temp);
178 }
179}
Main include for ABI (AirBorneInterface).
#define IMU_MPU6000_ID
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
angular rates
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.
struct ImuMpu6000 imu_mpu_spi
#define IMU_MPU_Z_SIGN
#define IMU_MPU_LOWPASS_FILTER
Definition imu_mpu6000.c:47
#define IMU_MPU_SMPLRT_DIV
Definition imu_mpu6000.c:48
void imu_mpu_spi_periodic(void)
#define IMU_MPU_X_SIGN
#define IMU_MPU_ACCEL_LOWPASS_FILTER
Definition imu_mpu6000.c:52
void imu_mpu_spi_init(void)
#define IMU_MPU_CHAN_Y
Definition imu_mpu6000.c:94
#define IMU_MPU_CHAN_X
Definition imu_mpu6000.c:90
void imu_mpu_spi_event(void)
#define IMU_MPU_Y_SIGN
#define IMU_MPU_CHAN_Z
Definition imu_mpu6000.c:98
Driver for IMU with only MPU6000 via SPI.
#define IMU_MPU_GYRO_RANGE
Definition imu_mpu6000.h:38
#define IMU_MPU_ACCEL_RANGE
Definition imu_mpu6000.h:42
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
#define UpdateMedianFilterVect3Int(_f, _v)
#define InitMedianFilterVect3Int(_f, _n)
#define InitMedianFilterRatesInt(_f, _n)
#define UpdateMedianFilterRatesInt(_f, _v)
const struct FloatVect3 MPU60X0_ACCEL_SENS_F[4]
Definition mpu60x0.c:54
const struct FloatRates MPU60X0_GYRO_SENS_F[4]
Definition mpu60x0.c:38
void mpu60x0_spi_event(struct Mpu60x0_Spi *mpu)
void mpu60x0_spi_init(struct Mpu60x0_Spi *mpu, struct spi_periph *spi_p, uint8_t slave_idx)
Definition mpu60x0_spi.c:31
static void mpu60x0_spi_periodic(struct Mpu60x0_Spi *mpu)
convenience function: read or start configuration if not already initialized
Definition mpu60x0_spi.h:78
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
Architecture independent SPI (Serial Peripheral Interface) API.
int int32_t
Typedef defining 32 bit int type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.