Paparazzi UAS  v6.2_unstable
Paparazzi is a free software Unmanned Aircraft System.
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 "modules/imu/imu.h"
29 #include "modules/core/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
129 }
130 
132 {
134 }
135 
137 {
140  uint32_t now_ts = get_sys_time_usec();
141 
142  // set channel order
143  struct Int32Vect3 accel = {
147  };
148  struct Int32Rates rates = {
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 
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 }
Imu::gyro_unscaled
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:46
uint32_t
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
Mpu60x0Config::smplrt_div
uint8_t smplrt_div
Sample rate divider.
Definition: mpu60x0.h:140
InitMedianFilterVect3Int
#define InitMedianFilterVect3Int(_f, _n)
Definition: median_filter.h:97
IMU_MPU_Z_SIGN
#define IMU_MPU_Z_SIGN
Definition: imu_mpu6000.c:103
IMU_MPU_ACCEL_RANGE
#define IMU_MPU_ACCEL_RANGE
Definition: imu_mpu6000.h:41
Int32Rates
angular rates
Definition: pprz_algebra_int.h:179
Mpu60x0Config::gyro_range
enum Mpu60x0GyroRanges gyro_range
deg/s Range
Definition: mpu60x0.h:143
ImuMpu6000::mpu
struct Mpu60x0_Spi mpu
Definition: imu_mpu6000.h:72
abi.h
Imu::accel
struct Int32Vect3 accel
accelerometer measurements in m/s^2 in BFP with INT32_ACCEL_FRAC
Definition: imu.h:39
spi.h
IMU_MPU_GYRO_RANGE
#define IMU_MPU_GYRO_RANGE
Definition: imu_mpu6000.h:37
Mpu60x0_Spi::data_rates
union Mpu60x0_Spi::@325 data_rates
UpdateMedianFilterRatesInt
#define UpdateMedianFilterRatesInt(_f, _v)
Definition: median_filter.h:118
Mpu60x0Config::accel_range
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:144
Mpu60x0Config::dlpf_cfg_acc
enum Mpu60x0ACCDLPF dlpf_cfg_acc
Digital Low Pass Filter for acceleremoter (ICM devices only)
Definition: mpu60x0.h:142
Mpu60x0Config::dlpf_cfg
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:141
IMU_MPU_CHAN_X
#define IMU_MPU_CHAN_X
Definition: imu_mpu6000.c:81
imu_scale_gyro
void WEAK imu_scale_gyro(struct Imu *_imu)
Definition: imu.c:208
Imu::accel_unscaled
struct Int32Vect3 accel_unscaled
unscaled accelerometer measurements
Definition: imu.h:47
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.h
MedianFilter3Int
Definition: median_filter.h:93
InitMedianFilterRatesInt
#define InitMedianFilterRatesInt(_f, _n)
Definition: median_filter.h:104
IMU_MPU_SMPLRT_DIV
#define IMU_MPU_SMPLRT_DIV
Definition: imu_mpu6000.c:64
ImuMpu6000
Definition: imu_mpu6000.h:71
Int32Vect3
Definition: pprz_algebra_int.h:88
IMU_MPU_ACCEL_LOWPASS_FILTER
#define IMU_MPU_ACCEL_LOWPASS_FILTER
Definition: imu_mpu6000.c:67
imu_scale_accel
void WEAK imu_scale_accel(struct Imu *_imu)
Definition: imu.c:219
median_filter.h
IMU_MPU_CHAN_Y
#define IMU_MPU_CHAN_Y
Definition: imu_mpu6000.c:85
imu
struct Imu imu
global IMU state
Definition: imu.c:108
PRINT_CONFIG_MSG
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
imu_mpu_spi_periodic
void imu_mpu_spi_periodic(void)
Definition: imu_mpu6000.c:131
mpu60x0_spi_periodic
static void mpu60x0_spi_periodic(struct Mpu60x0_Spi *mpu)
convenience function: read or start configuration if not already initialized
Definition: mpu60x0_spi.h:78
int32_t
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83
IMU_MPU6000_ID
#define IMU_MPU6000_ID
Definition: abi_sender_ids.h:323
IMU_MPU_LOWPASS_FILTER
#define IMU_MPU_LOWPASS_FILTER
Definition: imu_mpu6000.c:63
imu_mpu_spi_event
void imu_mpu_spi_event(void)
Definition: imu_mpu6000.c:136
IMU_MPU_CHAN_Z
#define IMU_MPU_CHAN_Z
Definition: imu_mpu6000.c:89
Mpu60x0_Spi::data_available
volatile bool data_available
data ready flag
Definition: mpu60x0_spi.h:56
IMU_MPU_X_SIGN
#define IMU_MPU_X_SIGN
Definition: imu_mpu6000.c:95
IMU_MPU_Y_SIGN
#define IMU_MPU_Y_SIGN
Definition: imu_mpu6000.c:99
Mpu60x0_Spi::data_accel
union Mpu60x0_Spi::@324 data_accel
RATES_COPY
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:337
Mpu60x0_Spi::config
struct Mpu60x0Config config
Definition: mpu60x0_spi.h:67
imu_mpu_spi_init
void imu_mpu_spi_init(void)
Definition: imu_mpu6000.c:114
mpu60x0_spi_init
void mpu60x0_spi_init(struct Mpu60x0_Spi *mpu, struct spi_periph *spi_p, uint8_t slave_idx)
Definition: mpu60x0_spi.c:31
mpu60x0_spi_event
void mpu60x0_spi_event(struct Mpu60x0_Spi *mpu)
Definition: mpu60x0_spi.c:128
VECT3_COPY
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:140
UpdateMedianFilterVect3Int
#define UpdateMedianFilterVect3Int(_f, _v)
Definition: median_filter.h:106
Imu::gyro
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:38
imu_mpu_spi
struct ImuMpu6000 imu_mpu_spi
Definition: imu_mpu6000.c:107