Paparazzi UAS  v5.10_stable-5-g83a0da5-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  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
27 #include "subsystems/imu.h"
28 #include "subsystems/abi.h"
29 #include "mcu_periph/spi.h"
30 
31 /* SPI defaults set in subsystem makefile, can be configured from airframe file */
32 PRINT_CONFIG_VAR(IMU_MPU_SPI_SLAVE_IDX)
33 PRINT_CONFIG_VAR(IMU_MPU_SPI_DEV)
34 
35 
36 /* MPU60x0 gyro/accel internal lowpass frequency */
37 #if !defined IMU_MPU_LOWPASS_FILTER && !defined IMU_MPU_SMPLRT_DIV
38 #if (PERIODIC_FREQUENCY == 60) || (PERIODIC_FREQUENCY == 120)
39 /* Accelerometer: Bandwidth 44Hz, Delay 4.9ms
40  * Gyroscope: Bandwidth 42Hz, Delay 4.8ms sampling 1kHz
41  */
42 #define IMU_MPU_LOWPASS_FILTER MPU60X0_DLPF_42HZ
43 #define IMU_MPU_SMPLRT_DIV 9
44 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
45 #elif PERIODIC_FREQUENCY == 512
46 /* Accelerometer: Bandwidth 260Hz, Delay 0ms
47  * Gyroscope: Bandwidth 256Hz, Delay 0.98ms sampling 8kHz
48  */
49 #define IMU_MPU_LOWPASS_FILTER MPU60X0_DLPF_256HZ
50 #define IMU_MPU_SMPLRT_DIV 3
51 PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
52 #else
53 #error Non-default PERIODIC_FREQUENCY: please define MPU_HMC_LOWPASS_FILTER and MPU_HMC_SMPLRT_DIV.
54 #endif
55 #endif
56 PRINT_CONFIG_VAR(IMU_MPU_LOWPASS_FILTER)
57 PRINT_CONFIG_VAR(IMU_MPU_SMPLRT_DIV)
58 
59 #ifndef IMU_MPU_GYRO_RANGE
60 #define IMU_MPU_GYRO_RANGE MPU60X0_GYRO_RANGE_2000
61 #endif
62 PRINT_CONFIG_VAR(IMU_MPU_GYRO_RANGE)
63 
64 #ifndef IMU_MPU_ACCEL_RANGE
65 #define IMU_MPU_ACCEL_RANGE MPU60X0_ACCEL_RANGE_16G
66 #endif
67 PRINT_CONFIG_VAR(IMU_MPU_ACCEL_RANGE)
68 
69 
71 
72 void imu_mpu_spi_init(void)
73 {
74  mpu60x0_spi_init(&imu_mpu_spi.mpu, &IMU_MPU_SPI_DEV, IMU_MPU_SPI_SLAVE_IDX);
75  // change the default configuration
76  imu_mpu_spi.mpu.config.smplrt_div = IMU_MPU_SMPLRT_DIV;
77  imu_mpu_spi.mpu.config.dlpf_cfg = IMU_MPU_LOWPASS_FILTER;
80 }
81 
82 
84 {
86 }
87 
89 {
92  uint32_t now_ts = get_sys_time_usec();
98  AbiSendMsgIMU_GYRO_INT32(IMU_MPU6000_ID, now_ts, &imu.gyro);
99  AbiSendMsgIMU_ACCEL_INT32(IMU_MPU6000_ID, now_ts, &imu.accel);
100  }
101 }
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_GYRO_RANGE
Definition: imu_mpu6000.c:60
void imu_scale_gyro(struct Imu *_imu)
Definition: ahrs_gx3.c:349
void imu_scale_accel(struct Imu *_imu)
Definition: ahrs_gx3.c:350
struct Mpu60x0Config config
Definition: mpu60x0_spi.h:67
void imu_mpu_spi_event(void)
Definition: imu_mpu6000.c:88
void mpu60x0_spi_event(struct Mpu60x0_Spi *mpu)
Definition: mpu60x0_spi.c:110
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:139
Main include for ABI (AirBorneInterface).
union Mpu60x0_Spi::@314 data_accel
uint8_t smplrt_div
Sample rate divider.
Definition: mpu60x0.h:79
struct Imu imu
global IMU state
Definition: imu.c:108
enum Mpu60x0GyroRanges gyro_range
deg/s Range
Definition: mpu60x0.h:81
void imu_mpu_spi_periodic(void)
Definition: imu_mpu6000.c:83
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:82
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:80
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
unsigned long uint32_t
Definition: types.h:18
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
#define IMU_MPU6000_ID
Inertial Measurement Unit interface.
#define IMU_MPU_ACCEL_RANGE
Definition: imu_mpu6000.c:65
volatile bool data_available
data ready flag
Definition: mpu60x0_spi.h:56
union Mpu60x0_Spi::@315 data_rates
struct Int32Vect3 accel_unscaled
unscaled accelerometer measurements
Definition: imu.h:47
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:336
struct ImuMpu6000 imu_mpu_spi
Definition: imu_mpu6000.c:70
void imu_mpu_spi_init(void)
Definition: imu_mpu6000.c:72
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
struct Mpu60x0_Spi mpu
Definition: imu_mpu6000.h:39
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:38