Paparazzi UAS  v7.0_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 
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
34 #include "filters/median_filter.h"
35 #endif
36 
37 /* SPI defaults set in subsystem makefile, can be configured from airframe file */
38 PRINT_CONFIG_VAR(IMU_MPU_SPI_SLAVE_IDX)
39 PRINT_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 >= 60) && (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
49 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
50 #ifndef IMU_MPU_ACCEL_LOWPASS_FILTER
51 #define IMU_MPU_ACCEL_LOWPASS_FILTER MPU60X0_DLPF_ACC_44HZ // for ICM sensors
52 #endif
53 #elif (PERIODIC_FREQUENCY == 512) || (PERIODIC_FREQUENCY == 500)
54 /* Accelerometer: Bandwidth 260Hz, Delay 0ms
55  * Gyroscope: Bandwidth 256Hz, Delay 0.98ms sampling 8kHz
56  */
57 #define IMU_MPU_LOWPASS_FILTER MPU60X0_DLPF_256HZ
58 #define IMU_MPU_SMPLRT_DIV 3
59 PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
60 #ifndef IMU_MPU_ACCEL_LOWPASS_FILTER
61 #define IMU_MPU_ACCEL_LOWPASS_FILTER MPU60X0_DLPF_ACC_218HZ // for ICM sensors
62 #endif
63 #else
64 /* By default, don't go too fast */
65 #define IMU_MPU_LOWPASS_FILTER MPU60X0_DLPF_42HZ
66 #define IMU_MPU_SMPLRT_DIV 9
67 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
68 #ifndef IMU_MPU_ACCEL_LOWPASS_FILTER
69 #define IMU_MPU_ACCEL_LOWPASS_FILTER MPU60X0_DLPF_ACC_44HZ // for ICM sensors
70 #endif
71 #warning "Non-default PERIODIC_FREQUENCY: please define IMU_MPU_LOWPASS_FILTER and IMU_MPU_SMPLRT_DIV."
72 #endif
73 #endif
74 PRINT_CONFIG_VAR(IMU_MPU_LOWPASS_FILTER)
75 PRINT_CONFIG_VAR(IMU_MPU_ACCEL_LOWPASS_FILTER)
76 PRINT_CONFIG_VAR(IMU_MPU_SMPLRT_DIV)
77 
78 PRINT_CONFIG_VAR(IMU_MPU_GYRO_RANGE)
79 PRINT_CONFIG_VAR(IMU_MPU_ACCEL_RANGE)
80 
81 // Default channels order
82 #ifndef IMU_MPU_CHAN_X
83 #define IMU_MPU_CHAN_X 0
84 #endif
85 PRINT_CONFIG_VAR(IMU_MPU_CHAN_X)
86 #ifndef IMU_MPU_CHAN_Y
87 #define IMU_MPU_CHAN_Y 1
88 #endif
89 PRINT_CONFIG_VAR(IMU_MPU_CHAN_Y)
90 #ifndef IMU_MPU_CHAN_Z
91 #define IMU_MPU_CHAN_Z 2
92 #endif
93 PRINT_CONFIG_VAR(IMU_MPU_CHAN_Z)
94 
95 // Default channel signs
96 #ifndef IMU_MPU_X_SIGN
97 #define IMU_MPU_X_SIGN 1
98 #endif
99 PRINT_CONFIG_VAR(IMU_MPU_X_SIGN)
100 #ifndef IMU_MPU_Y_SIGN
101 #define IMU_MPU_Y_SIGN 1
102 #endif
103 PRINT_CONFIG_VAR(IMU_MPU_Y_SIGN)
104 #ifndef IMU_MPU_Z_SIGN
105 #define IMU_MPU_Z_SIGN 1
106 #endif
107 PRINT_CONFIG_VAR(IMU_MPU_Z_SIGN)
108 
109 struct ImuMpu6000 imu_mpu_spi;
110 
111 #if IMU_MPU_USE_MEDIAN_FILTER
112 static struct MedianFilter3Int medianfilter_accel;
113 static struct MedianFilter3Int medianfilter_rates;
114 #endif
115 
117 {
118 
119 #if IMU_MPU_USE_MEDIAN_FILTER
120  InitMedianFilterVect3Int(medianfilter_accel, 3);
121  InitMedianFilterRatesInt(medianfilter_rates, 3);
122 #endif
123 
124  mpu60x0_spi_init(&imu_mpu_spi.mpu, &IMU_MPU_SPI_DEV, IMU_MPU_SPI_SLAVE_IDX);
125  // change the default configuration
131 
132  // Set the default scaling
135 }
136 
138 {
140 }
141 
143 {
146  uint32_t now_ts = get_sys_time_usec();
147 
148  // set channel order
149  struct Int32Vect3 accel = {
153  };
154  struct Int32Rates rates = {
158  };
159 
160  // In case sensor exhibits faulty large spike values in raw output remove them
161 #if IMU_MPU_USE_MEDIAN_FILTER
162  UpdateMedianFilterVect3Int(medianfilter_accel, accel);
163  UpdateMedianFilterRatesInt(medianfilter_rates, rates);
164 #endif
165 
167 
168  // Send the scaled values over ABI
169  AbiSendMsgIMU_GYRO_RAW(IMU_MPU6000_ID, now_ts, &rates, 1, IMU_MPU_SPI_PERIODIC_FREQ, imu_mpu_spi.mpu.temp);
170  AbiSendMsgIMU_ACCEL_RAW(IMU_MPU6000_ID, now_ts, &accel, 1, IMU_MPU_SPI_PERIODIC_FREQ, imu_mpu_spi.mpu.temp);
171  }
172 }
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:71
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:521
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:491
Inertial Measurement Unit interface.
struct ImuMpu6000 imu_mpu_spi
Definition: imu_mpu6000.c:109
#define IMU_MPU_Z_SIGN
Definition: imu_mpu6000.c:105
#define IMU_MPU_LOWPASS_FILTER
Definition: imu_mpu6000.c:65
#define IMU_MPU_SMPLRT_DIV
Definition: imu_mpu6000.c:66
void imu_mpu_spi_periodic(void)
Definition: imu_mpu6000.c:137
#define IMU_MPU_X_SIGN
Definition: imu_mpu6000.c:97
#define IMU_MPU_ACCEL_LOWPASS_FILTER
Definition: imu_mpu6000.c:69
void imu_mpu_spi_init(void)
Definition: imu_mpu6000.c:116
#define IMU_MPU_CHAN_Y
Definition: imu_mpu6000.c:87
#define IMU_MPU_CHAN_X
Definition: imu_mpu6000.c:83
void imu_mpu_spi_event(void)
Definition: imu_mpu6000.c:142
#define IMU_MPU_Y_SIGN
Definition: imu_mpu6000.c:101
#define IMU_MPU_CHAN_Z
Definition: imu_mpu6000.c:91
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
struct Mpu60x0Config config
Definition: mpu60x0_spi.h:67
float temp
temperature in degrees Celcius
Definition: mpu60x0_spi.h:65
union Mpu60x0_Spi::@337 data_rates
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::@336 data_accel
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