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_hmc5883.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 /* SPI/I2C defaults set in subsystem makefile, can be configured from airframe file */
34 PRINT_CONFIG_VAR(IMU_MPU_SPI_SLAVE_IDX)
35 PRINT_CONFIG_VAR(IMU_MPU_SPI_DEV)
36 PRINT_CONFIG_VAR(IMU_HMC_I2C_DEV)
37 
38 
39 /* MPU60x0 gyro/accel internal lowpass frequency */
40 #if !defined IMU_MPU_LOWPASS_FILTER && !defined IMU_MPU_SMPLRT_DIV
41 #if (PERIODIC_FREQUENCY == 60) || (PERIODIC_FREQUENCY == 120)
42 /* Accelerometer: Bandwidth 44Hz, Delay 4.9ms
43  * Gyroscope: Bandwidth 42Hz, Delay 4.8ms sampling 1kHz
44  */
45 #define IMU_MPU_LOWPASS_FILTER MPU60X0_DLPF_42HZ
46 #define IMU_MPU_SMPLRT_DIV 9
47 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
48 #elif PERIODIC_FREQUENCY == 512
49 /* Accelerometer: Bandwidth 260Hz, Delay 0ms
50  * Gyroscope: Bandwidth 256Hz, Delay 0.98ms sampling 8kHz
51  */
52 #define IMU_MPU_LOWPASS_FILTER MPU60X0_DLPF_256HZ
53 #define IMU_MPU_SMPLRT_DIV 3
54 PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
55 #else
56 #error Non-default PERIODIC_FREQUENCY: please define MPU_HMC_LOWPASS_FILTER and MPU_HMC_SMPLRT_DIV.
57 #endif
58 #endif
59 PRINT_CONFIG_VAR(IMU_MPU_LOWPASS_FILTER)
60 PRINT_CONFIG_VAR(IMU_MPU_SMPLRT_DIV)
61 
62 #ifndef IMU_MPU_GYRO_RANGE
63 #define IMU_MPU_GYRO_RANGE MPU60X0_GYRO_RANGE_2000
64 #endif
65 PRINT_CONFIG_VAR(IMU_MPU_GYRO_RANGE)
66 
67 #ifndef IMU_MPU_ACCEL_RANGE
68 #define IMU_MPU_ACCEL_RANGE MPU60X0_ACCEL_RANGE_16G
69 #endif
70 PRINT_CONFIG_VAR(IMU_MPU_ACCEL_RANGE)
71 
72 // Default channels order
73 #ifndef IMU_MPU_CHAN_X
74 #define IMU_MPU_CHAN_X 0
75 #endif
76 PRINT_CONFIG_VAR(IMU_MPU_CHAN_X)
77 #ifndef IMU_MPU_CHAN_Y
78 #define IMU_MPU_CHAN_Y 1
79 #endif
80 PRINT_CONFIG_VAR(IMU_MPU_CHAN_Y)
81 #ifndef IMU_MPU_CHAN_Z
82 #define IMU_MPU_CHAN_Z 2
83 #endif
84 PRINT_CONFIG_VAR(IMU_MPU_CHAN_Z)
85 
86 #ifndef IMU_MPU_X_SIGN
87 #define IMU_MPU_X_SIGN 1
88 #endif
89 PRINT_CONFIG_VAR(IMU_MPU_X_SIGN)
90 #ifndef IMU_MPU_Y_SIGN
91 #define IMU_MPU_Y_SIGN 1
92 #endif
93 PRINT_CONFIG_VAR(IMU_MPU_Y_SIGN)
94 #ifndef IMU_MPU_Z_SIGN
95 #define IMU_MPU_Z_SIGN 1
96 #endif
97 PRINT_CONFIG_VAR(IMU_MPU_Z_SIGN)
98 
99 /* mag by default rotated by 90deg around z axis relative to MPU */
100 #ifndef IMU_HMC_CHAN_X
101 #define IMU_HMC_CHAN_X 1
102 #endif
103 PRINT_CONFIG_VAR(IMU_HMC_CHAN_X)
104 #ifndef IMU_HMC_CHAN_Y
105 #define IMU_HMC_CHAN_Y 0
106 #endif
107 PRINT_CONFIG_VAR(IMU_HMC_CHAN_Y)
108 #ifndef IMU_HMC_CHAN_Z
109 #define IMU_HMC_CHAN_Z 2
110 #endif
111 PRINT_CONFIG_VAR(IMU_HMC_CHAN_Z)
112 
113 #ifndef IMU_HMC_X_SIGN
114 #define IMU_HMC_X_SIGN 1
115 #endif
116 PRINT_CONFIG_VAR(IMU_HMC_X_SIGN)
117 #ifndef IMU_HMC_Y_SIGN
118 #define IMU_HMC_Y_SIGN -1
119 #endif
120 PRINT_CONFIG_VAR(IMU_HMC_Y_SIGN)
121 #ifndef IMU_HMC_Z_SIGN
122 #define IMU_HMC_Z_SIGN 1
123 #endif
124 PRINT_CONFIG_VAR(IMU_HMC_Z_SIGN)
125 
126 
128 
130 {
131  mpu60x0_spi_init(&imu_mpu_hmc.mpu, &IMU_MPU_SPI_DEV, IMU_MPU_SPI_SLAVE_IDX);
132  // change the default configuration
133  imu_mpu_hmc.mpu.config.smplrt_div = IMU_MPU_SMPLRT_DIV;
134  imu_mpu_hmc.mpu.config.dlpf_cfg = IMU_MPU_LOWPASS_FILTER;
135  imu_mpu_hmc.mpu.config.gyro_range = IMU_MPU_GYRO_RANGE;
137 
138  /* initialize mag and set default options */
139  hmc58xx_init(&imu_mpu_hmc.hmc, &IMU_HMC_I2C_DEV, HMC58XX_ADDR);
140 }
141 
142 
144 {
146 
147  /* Read HMC58XX every 10 times of main freq
148  * at ~50Hz (main loop for rotorcraft: 512Hz)
149  */
150  RunOnceEvery(10, hmc58xx_periodic(&imu_mpu_hmc.hmc));
151 }
152 
154 {
155  uint32_t now_ts = get_sys_time_usec();
156 
159  // set channel order
160  struct Int32Vect3 accel = {
164  };
165  struct Int32Rates rates = {
169  };
170  // unscaled vector
171  VECT3_COPY(imu.accel_unscaled, accel);
172  RATES_COPY(imu.gyro_unscaled, rates);
173 
177  AbiSendMsgIMU_GYRO_INT32(IMU_MPU6000_HMC_ID, now_ts, &imu.gyro);
178  AbiSendMsgIMU_ACCEL_INT32(IMU_MPU6000_HMC_ID, now_ts, &imu.accel);
179  }
180 
181  /* HMC58XX event task */
184  /* mag by default rotated by 90deg around z axis relative to MPU */
189  imu_scale_mag(&imu);
190  AbiSendMsgIMU_MAG_INT32(IMU_MPU6000_HMC_ID, now_ts, &imu.mag);
191  }
192 }
angular rates
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: hmc58xx.h:65
#define IMU_HMC_CHAN_X
#define IMU_MPU_X_SIGN
union Hmc58xx::@301 data
#define IMU_HMC_Z_SIGN
#define IMU_MPU_ACCEL_RANGE
void imu_scale_gyro(struct Imu *_imu)
Definition: ahrs_gx3.c:349
#define IMU_MPU_CHAN_Z
struct Mpu60x0_Spi mpu
void imu_scale_accel(struct Imu *_imu)
Definition: ahrs_gx3.c:350
struct Mpu60x0Config config
Definition: mpu60x0_spi.h:67
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
#define IMU_HMC_CHAN_Y
struct Imu imu
global IMU state
Definition: imu.c:108
enum Mpu60x0GyroRanges gyro_range
deg/s Range
Definition: mpu60x0.h:81
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:82
struct ImuMpu6000Hmc5883 imu_mpu_hmc
#define IMU_MPU_GYRO_RANGE
void imu_mpu_hmc_event(void)
#define HMC58XX_ADDR
Definition: hmc58xx_regs.h:31
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:80
Architecture independent SPI (Serial Peripheral Interface) API.
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:48
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
#define IMU_MPU_Z_SIGN
#define IMU_MPU6000_HMC_ID
#define IMU_MPU_CHAN_Y
#define IMU_HMC_Y_SIGN
unsigned long uint32_t
Definition: types.h:18
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
struct Int32Vect3 mag
magnetometer measurements scaled to 1 in BFP with INT32_MAG_FRAC
Definition: imu.h:40
Inertial Measurement Unit interface.
signed long int32_t
Definition: types.h:19
volatile bool data_available
data ready flag
Definition: mpu60x0_spi.h:56
Register defs for Honeywell HMC5843 and HMC5883 magnetometers.
void imu_scale_mag(struct Imu *_imu)
Definition: ahrs_gx3.c:351
union Mpu60x0_Spi::@315 data_rates
static void hmc58xx_periodic(struct Hmc58xx *hmc)
convenience function: read or start configuration if not already initialized
Definition: hmc58xx.h:85
#define IMU_HMC_CHAN_Z
void imu_mpu_hmc_periodic(void)
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
struct Int32Vect3 accel_unscaled
unscaled accelerometer measurements
Definition: imu.h:47
void imu_mpu_hmc_init(void)
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:336
#define IMU_MPU_Y_SIGN
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
void hmc58xx_event(struct Hmc58xx *hmc)
Definition: hmc58xx.c:152
#define IMU_HMC_X_SIGN
#define IMU_MPU_CHAN_X
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:38