Paparazzi UAS  v6.2.0_stable
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 
29 #include "modules/imu/imu.h"
30 #include "modules/core/abi.h"
31 #include "mcu_periph/spi.h"
32 #if IMU_MPU_USE_MEDIAN_FILTER
33 #include "filters/median_filter.h"
34 #endif
35 
36 /* SPI defaults set in subsystem makefile, can be configured from airframe file */
37 PRINT_CONFIG_VAR(IMU_MPU_SPI_SLAVE_IDX)
38 PRINT_CONFIG_VAR(IMU_MPU_SPI_DEV)
39 
40 /* MPU60x0 gyro/accel internal lowpass frequency */
41 #if !defined IMU_MPU_LOWPASS_FILTER && !defined IMU_MPU_SMPLRT_DIV
42 #if (PERIODIC_FREQUENCY >= 60) && (PERIODIC_FREQUENCY <= 120)
43 /* Accelerometer: Bandwidth 44Hz, Delay 4.9ms
44  * Gyroscope: Bandwidth 42Hz, Delay 4.8ms sampling 1kHz
45  */
46 #define IMU_MPU_LOWPASS_FILTER MPU60X0_DLPF_42HZ
47 #define IMU_MPU_SMPLRT_DIV 9
48 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
49 #ifndef IMU_MPU_ACCEL_LOWPASS_FILTER
50 #define IMU_MPU_ACCEL_LOWPASS_FILTER MPU60X0_DLPF_ACC_44HZ // for ICM sensors
51 #endif
52 #elif (PERIODIC_FREQUENCY == 512) || (PERIODIC_FREQUENCY == 500)
53 /* Accelerometer: Bandwidth 260Hz, Delay 0ms
54  * Gyroscope: Bandwidth 256Hz, Delay 0.98ms sampling 8kHz
55  */
56 #define IMU_MPU_LOWPASS_FILTER MPU60X0_DLPF_256HZ
57 #define IMU_MPU_SMPLRT_DIV 3
58 PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
59 #ifndef IMU_MPU_ACCEL_LOWPASS_FILTER
60 #define IMU_MPU_ACCEL_LOWPASS_FILTER MPU60X0_DLPF_ACC_218HZ // for ICM sensors
61 #endif
62 #else
63 /* By default, don't go too fast */
64 #define IMU_MPU_LOWPASS_FILTER MPU60X0_DLPF_42HZ
65 #define IMU_MPU_SMPLRT_DIV 9
66 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
67 #ifndef IMU_MPU_ACCEL_LOWPASS_FILTER
68 #define IMU_MPU_ACCEL_LOWPASS_FILTER MPU60X0_DLPF_ACC_44HZ // for ICM sensors
69 #endif
70 INFO("Non-default PERIODIC_FREQUENCY: using default IMU_MPU_LOWPASS_FILTER and IMU_MPU_SMPLRT_DIV.")
71 #endif
72 #endif
73 PRINT_CONFIG_VAR(IMU_MPU_LOWPASS_FILTER)
74 PRINT_CONFIG_VAR(IMU_MPU_ACCEL_LOWPASS_FILTER)
75 PRINT_CONFIG_VAR(IMU_MPU_SMPLRT_DIV)
76 
77 PRINT_CONFIG_VAR(IMU_MPU_GYRO_RANGE)
78 PRINT_CONFIG_VAR(IMU_MPU_ACCEL_RANGE)
79 
80 // Default channels order
81 #ifndef IMU_MPU_CHAN_X
82 #define IMU_MPU_CHAN_X 0
83 #endif
84 PRINT_CONFIG_VAR(IMU_MPU_CHAN_X)
85 #ifndef IMU_MPU_CHAN_Y
86 #define IMU_MPU_CHAN_Y 1
87 #endif
88 PRINT_CONFIG_VAR(IMU_MPU_CHAN_Y)
89 #ifndef IMU_MPU_CHAN_Z
90 #define IMU_MPU_CHAN_Z 2
91 #endif
92 PRINT_CONFIG_VAR(IMU_MPU_CHAN_Z)
93 
94 // Default channel signs
95 #ifndef IMU_MPU_X_SIGN
96 #define IMU_MPU_X_SIGN 1
97 #endif
98 PRINT_CONFIG_VAR(IMU_MPU_X_SIGN)
99 #ifndef IMU_MPU_Y_SIGN
100 #define IMU_MPU_Y_SIGN 1
101 #endif
102 PRINT_CONFIG_VAR(IMU_MPU_Y_SIGN)
103 #ifndef IMU_MPU_Z_SIGN
104 #define IMU_MPU_Z_SIGN 1
105 #endif
106 PRINT_CONFIG_VAR(IMU_MPU_Z_SIGN)
107 
108 struct ImuMpu6000 imu_mpu_spi;
109 
110 #if IMU_MPU_USE_MEDIAN_FILTER
111 static struct MedianFilter3Int medianfilter_accel;
112 static struct MedianFilter3Int medianfilter_rates;
113 #endif
114 
116 {
117 
118 #if IMU_MPU_USE_MEDIAN_FILTER
119  InitMedianFilterVect3Int(medianfilter_accel, 3);
120  InitMedianFilterRatesInt(medianfilter_rates, 3);
121 #endif
122 
123  mpu60x0_spi_init(&imu_mpu_spi.mpu, &IMU_MPU_SPI_DEV, IMU_MPU_SPI_SLAVE_IDX);
124  // change the default configuration
130 
131  // Set the default scaling
134 }
135 
137 {
139 }
140 
142 {
145  uint32_t now_ts = get_sys_time_usec();
146 
147  // set channel order
148  struct Int32Vect3 accel = {
152  };
153  struct Int32Rates rates = {
157  };
158 
159  // In case sensor exhibits faulty large spike values in raw output remove them
160 #if IMU_MPU_USE_MEDIAN_FILTER
161  UpdateMedianFilterVect3Int(medianfilter_accel, accel);
162  UpdateMedianFilterRatesInt(medianfilter_rates, rates);
163 #endif
164 
166 
167  // Send the scaled values over ABI
168  AbiSendMsgIMU_GYRO_RAW(IMU_MPU6000_ID, now_ts, &rates, 1, imu_mpu_spi.mpu.temp);
169  AbiSendMsgIMU_ACCEL_RAW(IMU_MPU6000_ID, now_ts, &accel, 1, imu_mpu_spi.mpu.temp);
170  }
171 }
Main include for ABI (AirBorneInterface).
#define IMU_MPU6000_ID
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:75
angular rates
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
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
Inertial Measurement Unit interface.
struct ImuMpu6000 imu_mpu_spi
Definition: imu_mpu6000.c:108
#define IMU_MPU_Z_SIGN
Definition: imu_mpu6000.c:104
#define IMU_MPU_LOWPASS_FILTER
Definition: imu_mpu6000.c:64
#define IMU_MPU_SMPLRT_DIV
Definition: imu_mpu6000.c:65
void imu_mpu_spi_periodic(void)
Definition: imu_mpu6000.c:136
#define IMU_MPU_X_SIGN
Definition: imu_mpu6000.c:96
#define IMU_MPU_ACCEL_LOWPASS_FILTER
Definition: imu_mpu6000.c:68
void imu_mpu_spi_init(void)
Definition: imu_mpu6000.c:115
#define IMU_MPU_CHAN_Y
Definition: imu_mpu6000.c:86
#define IMU_MPU_CHAN_X
Definition: imu_mpu6000.c:82
void imu_mpu_spi_event(void)
Definition: imu_mpu6000.c:141
#define IMU_MPU_Y_SIGN
Definition: imu_mpu6000.c:100
#define IMU_MPU_CHAN_Z
Definition: imu_mpu6000.c:90
Driver for IMU with only MPU6000 via SPI.
struct Mpu60x0_Spi mpu
Definition: imu_mpu6000.h:46
#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)
Definition: median_filter.h:97
#define InitMedianFilterRatesInt(_f, _n)
#define UpdateMedianFilterRatesInt(_f, _v)
const struct Int32Rates MPU60X0_GYRO_SENS_FRAC[4][2]
Definition: mpu60x0.c:38
const struct Int32Vect3 MPU60X0_ACCEL_SENS_FRAC[4][2]
Definition: mpu60x0.c:56
uint8_t smplrt_div
Sample rate divider.
Definition: mpu60x0.h:140
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:141
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:144
enum Mpu60x0ACCDLPF dlpf_cfg_acc
Digital Low Pass Filter for acceleremoter (ICM devices only)
Definition: mpu60x0.h:142
enum Mpu60x0GyroRanges gyro_range
deg/s Range
Definition: mpu60x0.h:143
void mpu60x0_spi_event(struct Mpu60x0_Spi *mpu)
Definition: mpu60x0_spi.c:128
void mpu60x0_spi_init(struct Mpu60x0_Spi *mpu, struct spi_periph *spi_p, uint8_t slave_idx)
Definition: mpu60x0_spi.c:31
volatile bool data_available
data ready flag
Definition: mpu60x0_spi.h:56
union Mpu60x0_Spi::@326 data_accel
struct Mpu60x0Config config
Definition: mpu60x0_spi.h:67
float temp
temperature in degrees Celcius
Definition: mpu60x0_spi.h:65
static void mpu60x0_spi_periodic(struct Mpu60x0_Spi *mpu)
convenience function: read or start configuration if not already initialized
Definition: mpu60x0_spi.h:78
union Mpu60x0_Spi::@327 data_rates
Architecture independent SPI (Serial Peripheral Interface) API.
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78