Paparazzi UAS  v7.0_unstable
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 "modules/imu/imu_px4fmu.h"
28 #include "modules/imu/imu.h"
29 #include "modules/core/abi.h"
30 #include "mcu_periph/spi.h"
32 #include "generated/modules.h"
33 
34 
35 /* MPU60x0 gyro/accel internal lowpass frequency */
36 #if !defined PX4FMU_LOWPASS_FILTER && !defined PX4FMU_SMPLRT_DIV
37 #if (PERIODIC_FREQUENCY == 60) || (PERIODIC_FREQUENCY == 120)
38 /* Accelerometer: Bandwidth 44Hz, Delay 4.9ms
39  * Gyroscope: Bandwidth 42Hz, Delay 4.8ms sampling 1kHz
40  */
41 #define PX4FMU_LOWPASS_FILTER MPU60X0_DLPF_42HZ
42 #define PX4FMU_SMPLRT_DIV 9
43 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
44 #elif PERIODIC_FREQUENCY == 512
45 /* Accelerometer: Bandwidth 260Hz, Delay 0ms
46  * Gyroscope: Bandwidth 256Hz, Delay 0.98ms sampling 8kHz
47  */
48 #define PX4FMU_LOWPASS_FILTER MPU60X0_DLPF_256HZ
49 #define PX4FMU_SMPLRT_DIV 3
50 PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
51 #else
52 #error Non-default PERIODIC_FREQUENCY: please define PX4FMU_LOWPASS_FILTER and PX4FMU_SMPLRT_DIV.
53 #endif
54 #endif
55 PRINT_CONFIG_VAR(PX4FMU_LOWPASS_FILTER)
56 PRINT_CONFIG_VAR(PX4FMU_SMPLRT_DIV)
57 
60 
61 struct ImuPx4fmu imu_px4fmu;
62 
63 void imu_px4fmu_init(void)
64 {
65  /* MPU is on spi1 and CS is SLAVE2 */
67  // change the default configuration
68  imu_px4fmu.mpu.config.smplrt_div = PX4FMU_SMPLRT_DIV;
69  imu_px4fmu.mpu.config.dlpf_cfg = PX4FMU_LOWPASS_FILTER;
72 
73  // Set the default scaling
76 
77  /* initialize mag on i2c2 and set default options */
79 }
80 
81 
83 {
85 
86  // Read HMC58XX at 50Hz (main loop for rotorcraft: 512Hz)
87  RunOnceEvery(10, hmc58xx_periodic(&imu_px4fmu.hmc));
88 }
89 
90 void imu_px4fmu_event(void)
91 {
92  uint32_t now_ts = get_sys_time_usec();
93 
96  struct Int32Rates gyro = {
97  imu_px4fmu.mpu.data_rates.rates.q,
98  imu_px4fmu.mpu.data_rates.rates.p,
99  -imu_px4fmu.mpu.data_rates.rates.r
100  };
101  struct Int32Vect3 accel = {
102  imu_px4fmu.mpu.data_accel.vect.y,
103  imu_px4fmu.mpu.data_accel.vect.x,
104  -imu_px4fmu.mpu.data_accel.vect.z
105  };
106  imu_px4fmu.mpu.data_available = false;
107  AbiSendMsgIMU_GYRO_RAW(IMU_BOARD_ID, now_ts, &gyro, 1, IMU_PX4FMU_PERIODIC_FREQ, imu_px4fmu.mpu.temp);
108  AbiSendMsgIMU_ACCEL_RAW(IMU_BOARD_ID, now_ts, &accel, 1, IMU_PX4FMU_PERIODIC_FREQ, imu_px4fmu.mpu.temp);
109  }
110 
111  /* HMC58XX event task */
114  struct Int32Vect3 mag = {
115  imu_px4fmu.hmc.data.vect.y,
116  imu_px4fmu.hmc.data.vect.x,
117  -imu_px4fmu.hmc.data.vect.z
118  };
119  imu_px4fmu.hmc.data_available = false;
120  AbiSendMsgIMU_MAG_RAW(IMU_BOARD_ID, now_ts, &mag);
121  }
122 }
123 
Main include for ABI (AirBorneInterface).
#define IMU_BOARD_ID
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:71
angular rates
process_rx_dma_interrupt & spi1
receive transferred over DMA
Definition: spi_arch.c:967
#define SPI_SLAVE2
Definition: spi.h:194
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
void hmc58xx_event(struct Hmc58xx *hmc)
Definition: hmc58xx.c:152
static void hmc58xx_periodic(struct Hmc58xx *hmc)
convenience function: read or start configuration if not already initialized
Definition: hmc58xx.h:85
union Hmc58xx::@321 data
volatile bool data_available
data ready flag
Definition: hmc58xx.h:65
Register defs for Honeywell HMC5843 and HMC5883 magnetometers.
#define HMC58XX_ADDR
Definition: hmc58xx_regs.h:31
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.
void imu_px4fmu_periodic(void)
Definition: imu_px4fmu.c:82
void imu_px4fmu_init(void)
Definition: imu_px4fmu.c:63
struct ImuPx4fmu imu_px4fmu
Definition: imu_px4fmu.c:61
void imu_px4fmu_event(void)
Definition: imu_px4fmu.c:90
Driver for the PX4FMU SPI1 for the MPU6000 and I2C2 for the HMC5883.
#define PX4FMU_GYRO_RANGE
Definition: imu_px4fmu.h:38
struct Hmc58xx hmc
Definition: imu_px4fmu.h:47
#define PX4FMU_ACCEL_RANGE
Definition: imu_px4fmu.h:42
struct Mpu60x0_Spi mpu
Definition: imu_px4fmu.h:46
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
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 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::@342 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::@341 data_accel
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
Architecture independent SPI (Serial Peripheral Interface) API.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78