Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
imu_px4fmu.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 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"
31 
32 
33 /* MPU60x0 gyro/accel internal lowpass frequency */
34 #if !defined PX4FMU_LOWPASS_FILTER && !defined PX4FMU_SMPLRT_DIV
35 #if (PERIODIC_FREQUENCY == 60) || (PERIODIC_FREQUENCY == 120)
36 /* Accelerometer: Bandwidth 44Hz, Delay 4.9ms
37  * Gyroscope: Bandwidth 42Hz, Delay 4.8ms sampling 1kHz
38  */
39 #define PX4FMU_LOWPASS_FILTER MPU60X0_DLPF_42HZ
40 #define PX4FMU_SMPLRT_DIV 9
41 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
42 #elif PERIODIC_FREQUENCY == 512
43 /* Accelerometer: Bandwidth 260Hz, Delay 0ms
44  * Gyroscope: Bandwidth 256Hz, Delay 0.98ms sampling 8kHz
45  */
46 #define PX4FMU_LOWPASS_FILTER MPU60X0_DLPF_256HZ
47 #define PX4FMU_SMPLRT_DIV 3
48 PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
49 #else
50 #error Non-default PERIODIC_FREQUENCY: please define PX4FMU_LOWPASS_FILTER and PX4FMU_SMPLRT_DIV.
51 #endif
52 #endif
53 PRINT_CONFIG_VAR(PX4FMU_LOWPASS_FILTER)
54 PRINT_CONFIG_VAR(PX4FMU_SMPLRT_DIV)
55 
56 PRINT_CONFIG_VAR(PX4FMU_GYRO_RANGE)
57 PRINT_CONFIG_VAR(PX4FMU_ACCEL_RANGE)
58 
60 
61 void imu_px4fmu_init(void)
62 {
63  /* MPU is on spi1 and CS is SLAVE2 */
65  // change the default configuration
66  imu_px4fmu.mpu.config.smplrt_div = PX4FMU_SMPLRT_DIV;
67  imu_px4fmu.mpu.config.dlpf_cfg = PX4FMU_LOWPASS_FILTER;
70 
71  /* initialize mag on i2c2 and set default options */
73 }
74 
75 
77 {
79 
80  // Read HMC58XX at 50Hz (main loop for rotorcraft: 512Hz)
81  RunOnceEvery(10, hmc58xx_periodic(&imu_px4fmu.hmc));
82 }
83 
84 void imu_px4fmu_event(void)
85 {
86  uint32_t now_ts = get_sys_time_usec();
87 
91  imu_px4fmu.mpu.data_rates.rates.q,
92  imu_px4fmu.mpu.data_rates.rates.p,
93  -imu_px4fmu.mpu.data_rates.rates.r);
95  imu_px4fmu.mpu.data_accel.vect.y,
96  imu_px4fmu.mpu.data_accel.vect.x,
97  -imu_px4fmu.mpu.data_accel.vect.z);
101  AbiSendMsgIMU_GYRO_INT32(IMU_BOARD_ID, now_ts, &imu.gyro);
102  AbiSendMsgIMU_ACCEL_INT32(IMU_BOARD_ID, now_ts, &imu.accel);
103  }
104 
105  /* HMC58XX event task */
108  imu.mag_unscaled.x = imu_px4fmu.hmc.data.vect.y;
109  imu.mag_unscaled.y = imu_px4fmu.hmc.data.vect.x;
110  imu.mag_unscaled.z = -imu_px4fmu.hmc.data.vect.z;
111  imu_px4fmu.hmc.data_available = false;
112  imu_scale_mag(&imu);
113  AbiSendMsgIMU_MAG_INT32(IMU_BOARD_ID, now_ts, &imu.mag);
114  }
115 }
116 
Imu::gyro_unscaled
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:46
Mpu60x0Config::smplrt_div
uint8_t smplrt_div
Sample rate divider.
Definition: mpu60x0.h:140
hmc58xx_event
void hmc58xx_event(struct Hmc58xx *hmc)
Definition: hmc58xx.c:152
Hmc58xx::data
union Hmc58xx::@317 data
imu_px4fmu_event
void imu_px4fmu_event(void)
Definition: imu_px4fmu.c:84
Mpu60x0Config::gyro_range
enum Mpu60x0GyroRanges gyro_range
deg/s Range
Definition: mpu60x0.h:143
imu_px4fmu
struct ImuPx4fmu imu_px4fmu
Definition: imu_px4fmu.c:59
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
Int32Vect3::z
int32_t z
Definition: pprz_algebra_int.h:91
SPI_SLAVE2
#define SPI_SLAVE2
Definition: spi.h:194
uint32_t
unsigned long uint32_t
Definition: types.h:18
imu_px4fmu_periodic
void imu_px4fmu_periodic(void)
Definition: imu_px4fmu.c:76
ImuPx4fmu
Definition: imu_px4fmu.h:72
Mpu60x0Config::accel_range
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:144
IMU_BOARD_ID
#define IMU_BOARD_ID
Definition: abi_sender_ids.h:275
Mpu60x0Config::dlpf_cfg
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:141
Imu::accel_unscaled
struct Int32Vect3 accel_unscaled
unscaled accelerometer measurements
Definition: imu.h:47
hmc58xx_regs.h
get_sys_time_usec
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
HMC58XX_ADDR
#define HMC58XX_ADDR
Definition: hmc58xx_regs.h:31
imu.h
RATES_ASSIGN
#define RATES_ASSIGN(_ra, _p, _q, _r)
Definition: pprz_algebra.h:330
ImuPx4fmu::hmc
struct Hmc58xx hmc
Definition: imu_px4fmu.h:74
imu_scale_gyro
void imu_scale_gyro(struct Imu *_imu)
Definition: imu_vectornav.c:43
Int32Vect3::y
int32_t y
Definition: pprz_algebra_int.h:90
hmc58xx_periodic
static void hmc58xx_periodic(struct Hmc58xx *hmc)
convenience function: read or start configuration if not already initialized
Definition: hmc58xx.h:85
imu
struct Imu imu
global IMU state
Definition: imu.c:108
ImuPx4fmu::mpu
struct Mpu60x0_Spi mpu
Definition: imu_px4fmu.h:73
hmc58xx_init
void hmc58xx_init(struct Hmc58xx *hmc, struct i2c_periph *i2c_p, uint8_t addr)
Initialize Hmc58xx struct and set default config options.
Definition: hmc58xx.c:75
PRINT_CONFIG_MSG
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
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
PX4FMU_GYRO_RANGE
#define PX4FMU_GYRO_RANGE
Definition: imu_px4fmu.h:38
imu_px4fmu_init
void imu_px4fmu_init(void)
Definition: imu_px4fmu.c:61
Mpu60x0_Spi::data_accel
union Mpu60x0_Spi::@333 data_accel
imu_scale_mag
void imu_scale_mag(struct Imu *_imu)
Definition: ahrs_gx3.c:352
Mpu60x0_Spi::data_available
volatile bool data_available
data ready flag
Definition: mpu60x0_spi.h:56
Int32Vect3::x
int32_t x
Definition: pprz_algebra_int.h:89
Imu::mag
struct Int32Vect3 mag
magnetometer measurements scaled to 1 in BFP with INT32_MAG_FRAC
Definition: imu.h:40
Imu::mag_unscaled
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:48
spi1
process_rx_dma_interrupt & spi1
receive transferred over DMA
Definition: spi_arch.c:967
VECT3_ASSIGN
#define VECT3_ASSIGN(_a, _x, _y, _z)
Definition: pprz_algebra.h:125
Hmc58xx::data_available
volatile bool data_available
data ready flag
Definition: hmc58xx.h:65
Mpu60x0_Spi::data_rates
union Mpu60x0_Spi::@334 data_rates
Mpu60x0_Spi::config
struct Mpu60x0Config config
Definition: mpu60x0_spi.h:67
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
PX4FMU_ACCEL_RANGE
#define PX4FMU_ACCEL_RANGE
Definition: imu_px4fmu.h:43
mpu60x0_spi_event
void mpu60x0_spi_event(struct Mpu60x0_Spi *mpu)
Definition: mpu60x0_spi.c:128
imu_scale_accel
void imu_scale_accel(struct Imu *_imu)
Definition: imu_vectornav.c:44
Imu::gyro
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:38