Paparazzi UAS  v6.0_unstable-92-g17422e4-dirty
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 
28 #include "subsystems/imu.h"
29 #include "subsystems/abi.h"
30 #include "mcu_periph/spi.h"
31 #if IMU_MPU_USE_MEDIAN_FILTER
32 #include "filters/median_filter.h"
33 #endif
34 
35 /* SPI defaults set in subsystem makefile, can be configured from airframe file */
36 PRINT_CONFIG_VAR(IMU_MPU_SPI_SLAVE_IDX)
37 PRINT_CONFIG_VAR(IMU_MPU_SPI_DEV)
38 
39 /* MPU60x0 gyro/accel internal lowpass frequency */
40 #if !defined IMU_MPU_LOWPASS_FILTER && !defined IMU_MPU_SMPLRT_DIV
41 #if (PERIODIC_FREQUENCY >= 60) && (PERIODIC_FREQUENCY <= 120)
42 /* Accelerometer: Bandwidth 44Hz, Delay 4.9ms
43  * Gyroscope: Bandwidth 42Hz, Delay 4.8ms sampling 1kHz
44  */
45 #define IMU_MPU_LOWPASS_FILTER MPU60X0_DLPF_42HZ
46 #define IMU_MPU_SMPLRT_DIV 9
47 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
48 #ifndef IMU_MPU_ACCEL_LOWPASS_FILTER
49 #define IMU_MPU_ACCEL_LOWPASS_FILTER MPU60X0_DLPF_ACC_44HZ // for ICM sensors
50 #endif
51 #elif (PERIODIC_FREQUENCY == 512) || (PERIODIC_FREQUENCY == 500)
52 /* Accelerometer: Bandwidth 260Hz, Delay 0ms
53  * Gyroscope: Bandwidth 256Hz, Delay 0.98ms sampling 8kHz
54  */
55 #define IMU_MPU_LOWPASS_FILTER MPU60X0_DLPF_256HZ
56 #define IMU_MPU_SMPLRT_DIV 3
57 PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
58 #ifndef IMU_MPU_ACCEL_LOWPASS_FILTER
59 #define IMU_MPU_ACCEL_LOWPASS_FILTER MPU60X0_DLPF_ACC_218HZ // for ICM sensors
60 #endif
61 #else
62 /* By default, don't go too fast */
63 #define IMU_MPU_LOWPASS_FILTER MPU60X0_DLPF_42HZ
64 #define IMU_MPU_SMPLRT_DIV 9
65 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
66 #ifndef IMU_MPU_ACCEL_LOWPASS_FILTER
67 #define IMU_MPU_ACCEL_LOWPASS_FILTER MPU60X0_DLPF_ACC_44HZ // for ICM sensors
68 #endif
69 INFO("Non-default PERIODIC_FREQUENCY: using default IMU_MPU_LOWPASS_FILTER and IMU_MPU_SMPLRT_DIV.")
70 #endif
71 #endif
72 PRINT_CONFIG_VAR(IMU_MPU_LOWPASS_FILTER)
73 PRINT_CONFIG_VAR(IMU_MPU_ACCEL_LOWPASS_FILTER)
74 PRINT_CONFIG_VAR(IMU_MPU_SMPLRT_DIV)
75 
76 PRINT_CONFIG_VAR(IMU_MPU_GYRO_RANGE)
77 PRINT_CONFIG_VAR(IMU_MPU_ACCEL_RANGE)
78 
79 // Default channels order
80 #ifndef IMU_MPU_CHAN_X
81 #define IMU_MPU_CHAN_X 0
82 #endif
83 PRINT_CONFIG_VAR(IMU_MPU_CHAN_X)
84 #ifndef IMU_MPU_CHAN_Y
85 #define IMU_MPU_CHAN_Y 1
86 #endif
87 PRINT_CONFIG_VAR(IMU_MPU_CHAN_Y)
88 #ifndef IMU_MPU_CHAN_Z
89 #define IMU_MPU_CHAN_Z 2
90 #endif
91 PRINT_CONFIG_VAR(IMU_MPU_CHAN_Z)
92 
93 // Default channel signs
94 #ifndef IMU_MPU_X_SIGN
95 #define IMU_MPU_X_SIGN 1
96 #endif
97 PRINT_CONFIG_VAR(IMU_MPU_X_SIGN)
98 #ifndef IMU_MPU_Y_SIGN
99 #define IMU_MPU_Y_SIGN 1
100 #endif
101 PRINT_CONFIG_VAR(IMU_MPU_Y_SIGN)
102 #ifndef IMU_MPU_Z_SIGN
103 #define IMU_MPU_Z_SIGN 1
104 #endif
105 PRINT_CONFIG_VAR(IMU_MPU_Z_SIGN)
106 
108 
109 #if IMU_MPU_USE_MEDIAN_FILTER
110 static struct MedianFilter3Int medianfilter_accel;
111 static struct MedianFilter3Int medianfilter_rates;
112 #endif
113 
115 {
116 
117 #if IMU_MPU_USE_MEDIAN_FILTER
118  InitMedianFilterVect3Int(medianfilter_accel, 3);
119  InitMedianFilterRatesInt(medianfilter_rates, 3);
120 #endif
121 
122  mpu60x0_spi_init(&imu_mpu_spi.mpu, &IMU_MPU_SPI_DEV, IMU_MPU_SPI_SLAVE_IDX);
123  // change the default configuration
124  imu_mpu_spi.mpu.config.smplrt_div = IMU_MPU_SMPLRT_DIV;
126  imu_mpu_spi.mpu.config.dlpf_cfg_acc = IMU_MPU_ACCEL_LOWPASS_FILTER; // only for ICM sensors
127  imu_mpu_spi.mpu.config.gyro_range = IMU_MPU_GYRO_RANGE;
129 }
130 
132 {
133  mpu60x0_spi_periodic(&imu_mpu_spi.mpu);
134 }
135 
137 {
138  mpu60x0_spi_event(&imu_mpu_spi.mpu);
139  if (imu_mpu_spi.mpu.data_available) {
140  uint32_t now_ts = get_sys_time_usec();
141 
142  // set channel order
143  struct Int32Vect3 accel = {
144  IMU_MPU_X_SIGN * (int32_t)(imu_mpu_spi.mpu.data_accel.value[IMU_MPU_CHAN_X]),
145  IMU_MPU_Y_SIGN * (int32_t)(imu_mpu_spi.mpu.data_accel.value[IMU_MPU_CHAN_Y]),
146  IMU_MPU_Z_SIGN * (int32_t)(imu_mpu_spi.mpu.data_accel.value[IMU_MPU_CHAN_Z])
147  };
148  struct Int32Rates rates = {
149  IMU_MPU_X_SIGN * (int32_t)(imu_mpu_spi.mpu.data_rates.value[IMU_MPU_CHAN_X]),
150  IMU_MPU_Y_SIGN * (int32_t)(imu_mpu_spi.mpu.data_rates.value[IMU_MPU_CHAN_Y]),
151  IMU_MPU_Z_SIGN * (int32_t)(imu_mpu_spi.mpu.data_rates.value[IMU_MPU_CHAN_Z])
152  };
153 
154  // In case sensor exhibits faulty large spike values in raw output remove them
155 #if IMU_MPU_USE_MEDIAN_FILTER
156  UpdateMedianFilterVect3Int(medianfilter_accel, accel);
157  UpdateMedianFilterRatesInt(medianfilter_rates, rates);
158 #endif
159  // unscaled vector
160  VECT3_COPY(imu.accel_unscaled, accel);
161  RATES_COPY(imu.gyro_unscaled, rates);
162 
163  imu_mpu_spi.mpu.data_available = false;
164 
165  // Scale the gyro and accelerometer
168 
169  // Send the scaled values over ABI
170  AbiSendMsgIMU_GYRO_INT32(IMU_MPU6000_ID, now_ts, &imu.gyro);
171  AbiSendMsgIMU_ACCEL_INT32(IMU_MPU6000_ID, now_ts, &imu.accel);
172  }
173 }
angular rates
void mpu60x0_spi_init(struct Mpu60x0_Spi *mpu, struct spi_periph *spi_p, uint8_t slave_idx)
Definition: mpu60x0_spi.c:31
#define IMU_MPU_CHAN_X
Definition: imu_mpu6000.c:81
#define IMU_MPU_Z_SIGN
Definition: imu_mpu6000.c:103
#define IMU_MPU_X_SIGN
Definition: imu_mpu6000.c:95
struct Mpu60x0Config config
Definition: mpu60x0_spi.h:67
union Mpu60x0_Spi::@324 data_rates
void imu_mpu_spi_event(void)
Definition: imu_mpu6000.c:136
void mpu60x0_spi_event(struct Mpu60x0_Spi *mpu)
Definition: mpu60x0_spi.c:128
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:140
#define InitMedianFilterVect3Int(_f, _n)
Definition: median_filter.h:97
Main include for ABI (AirBorneInterface).
uint8_t smplrt_div
Sample rate divider.
Definition: mpu60x0.h:140
enum Mpu60x0ACCDLPF dlpf_cfg_acc
Digital Low Pass Filter for acceleremoter (ICM devices only)
Definition: mpu60x0.h:142
struct Imu imu
global IMU state
Definition: imu.c:108
enum Mpu60x0GyroRanges gyro_range
deg/s Range
Definition: mpu60x0.h:143
void imu_mpu_spi_periodic(void)
Definition: imu_mpu6000.c:131
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:144
#define UpdateMedianFilterRatesInt(_f, _v)
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:141
#define IMU_MPU_ACCEL_LOWPASS_FILTER
Definition: imu_mpu6000.c:67
union Mpu60x0_Spi::@323 data_accel
Architecture independent SPI (Serial Peripheral Interface) API.
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:46
struct Int32Vect3 accel
accelerometer measurements in m/s^2 in BFP with INT32_ACCEL_FRAC
Definition: imu.h:39
static void mpu60x0_spi_periodic(struct Mpu60x0_Spi *mpu)
convenience function: read or start configuration if not already initialized
Definition: mpu60x0_spi.h:78
#define IMU_MPU_CHAN_Y
Definition: imu_mpu6000.c:85
#define IMU_MPU_SMPLRT_DIV
Definition: imu_mpu6000.c:64
#define IMU_MPU_CHAN_Z
Definition: imu_mpu6000.c:89
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83
#define IMU_MPU_Y_SIGN
Definition: imu_mpu6000.c:99
#define IMU_MPU6000_ID
Inertial Measurement Unit interface.
void imu_scale_gyro(struct Imu *_imu)
Definition: imu_vectornav.c:43
volatile bool data_available
data ready flag
Definition: mpu60x0_spi.h:56
#define InitMedianFilterRatesInt(_f, _n)
#define UpdateMedianFilterVect3Int(_f, _v)
struct Int32Vect3 accel_unscaled
unscaled accelerometer measurements
Definition: imu.h:47
#define IMU_MPU_GYRO_RANGE
Definition: imu_mpu6000.h:37
void imu_scale_accel(struct Imu *_imu)
Definition: imu_vectornav.c:44
#define IMU_MPU_LOWPASS_FILTER
Definition: imu_mpu6000.c:63
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:337
struct ImuMpu6000 imu_mpu_spi
Definition: imu_mpu6000.c:107
void imu_mpu_spi_init(void)
Definition: imu_mpu6000.c:114
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:71
#define IMU_MPU_ACCEL_RANGE
Definition: imu_mpu6000.h:41
struct Mpu60x0_Spi mpu
Definition: imu_mpu6000.h:72
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:38
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78