Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
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
26
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/* SPI/I2C defaults set in subsystem makefile, can be configured from airframe file */
36PRINT_CONFIG_VAR(IMU_MPU_SPI_SLAVE_IDX)
37PRINT_CONFIG_VAR(IMU_MPU_SPI_DEV)
38PRINT_CONFIG_VAR(IMU_HMC_I2C_DEV)
39
40
41/* MPU60x0 gyro/accel internal lowpass frequency */
42#if !defined IMU_MPU_LOWPASS_FILTER && !defined IMU_MPU_SMPLRT_DIV
43#if 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
49PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
50
51#elif PERIODIC_FREQUENCY < 500
52/* Accelerometer: Bandwidth 94Hz, Delay 3ms
53 * Gyroscope: Bandwidth 98Hz, Delay 2.8ms sampling 1kHz
54 */
55#define IMU_MPU_LOWPASS_FILTER MPU60X0_DLPF_98HZ
56#define IMU_MPU_SMPLRT_DIV 1
57PRINT_CONFIG_MSG("Gyro/Accel output rate is 500Hz at 1kHz internal sampling")
58
59#else // PERIODIC_FREQUENCY >= 500
60/* Accelerometer: Bandwidth 260Hz, Delay 0ms
61 * Gyroscope: Bandwidth 256Hz, Delay 0.98ms sampling 8kHz
62 */
63#define IMU_MPU_LOWPASS_FILTER MPU60X0_DLPF_256HZ
64#define IMU_MPU_SMPLRT_DIV 3
65PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
66
67#endif
68#endif
69
74
75// Default channels order
76#ifndef IMU_MPU_CHAN_X
77#define IMU_MPU_CHAN_X 0
78#endif
80#ifndef IMU_MPU_CHAN_Y
81#define IMU_MPU_CHAN_Y 1
82#endif
84#ifndef IMU_MPU_CHAN_Z
85#define IMU_MPU_CHAN_Z 2
86#endif
88
89#ifndef IMU_MPU_X_SIGN
90#define IMU_MPU_X_SIGN 1
91#endif
93#ifndef IMU_MPU_Y_SIGN
94#define IMU_MPU_Y_SIGN 1
95#endif
97#ifndef IMU_MPU_Z_SIGN
98#define IMU_MPU_Z_SIGN 1
99#endif
101
102/* mag by default rotated by 90deg around z axis relative to MPU */
103#ifndef IMU_HMC_CHAN_X
104#define IMU_HMC_CHAN_X 1
105#endif
107#ifndef IMU_HMC_CHAN_Y
108#define IMU_HMC_CHAN_Y 0
109#endif
111#ifndef IMU_HMC_CHAN_Z
112#define IMU_HMC_CHAN_Z 2
113#endif
115
116#ifndef IMU_HMC_X_SIGN
117#define IMU_HMC_X_SIGN 1
118#endif
120#ifndef IMU_HMC_Y_SIGN
121#define IMU_HMC_Y_SIGN -1
122#endif
124#ifndef IMU_HMC_Z_SIGN
125#define IMU_HMC_Z_SIGN 1
126#endif
128
129
131
133{
134 mpu60x0_spi_init(&imu_mpu_hmc.mpu, &IMU_MPU_SPI_DEV, IMU_MPU_SPI_SLAVE_IDX);
135 // change the default configuration
136 imu_mpu_hmc.mpu.config.smplrt_div = IMU_MPU_SMPLRT_DIV;
137 imu_mpu_hmc.mpu.config.dlpf_cfg = IMU_MPU_LOWPASS_FILTER;
138 imu_mpu_hmc.mpu.config.gyro_range = IMU_MPU_GYRO_RANGE;
139 imu_mpu_hmc.mpu.config.accel_range = IMU_MPU_ACCEL_RANGE;
140
141 // Set the default scaling
144
145 /* initialize mag and set default options */
146 hmc58xx_init(&imu_mpu_hmc.hmc, &IMU_HMC_I2C_DEV, HMC58XX_ADDR);
147}
148
149
151{
153
154 /* Read HMC58XX every 10 times of main freq
155 * at ~50Hz (main loop for rotorcraft: 512Hz)
156 */
157 RunOnceEvery(10, hmc58xx_periodic(&imu_mpu_hmc.hmc));
158}
159
161{
162 uint32_t now_ts = get_sys_time_usec();
163
165 if (imu_mpu_hmc.mpu.data_available) {
166 // set channel order
167 struct Int32Vect3 accel = {
168 IMU_MPU_X_SIGN * (int32_t)(imu_mpu_hmc.mpu.data_accel.value[IMU_MPU_CHAN_X]),
169 IMU_MPU_Y_SIGN * (int32_t)(imu_mpu_hmc.mpu.data_accel.value[IMU_MPU_CHAN_Y]),
170 IMU_MPU_Z_SIGN * (int32_t)(imu_mpu_hmc.mpu.data_accel.value[IMU_MPU_CHAN_Z])
171 };
172 struct Int32Rates rates = {
173 IMU_MPU_X_SIGN * (int32_t)(imu_mpu_hmc.mpu.data_rates.value[IMU_MPU_CHAN_X]),
174 IMU_MPU_Y_SIGN * (int32_t)(imu_mpu_hmc.mpu.data_rates.value[IMU_MPU_CHAN_Y]),
175 IMU_MPU_Z_SIGN * (int32_t)(imu_mpu_hmc.mpu.data_rates.value[IMU_MPU_CHAN_Z])
176 };
177
178 imu_mpu_hmc.mpu.data_available = false;
179 AbiSendMsgIMU_GYRO_RAW(IMU_MPU6000_HMC_ID, now_ts, &rates, 1, IMU_MPU_HMC_PERIODIC_FREQ, imu_mpu_hmc.mpu.temp);
180 AbiSendMsgIMU_ACCEL_RAW(IMU_MPU6000_HMC_ID, now_ts, &accel, 1, IMU_MPU_HMC_PERIODIC_FREQ, imu_mpu_hmc.mpu.temp);
181 }
182
183 /* HMC58XX event task */
185 if (imu_mpu_hmc.hmc.data_available) {
186 /* mag by default rotated by 90deg around z axis relative to MPU */
187 struct Int32Vect3 mag = {
188 IMU_HMC_X_SIGN * imu_mpu_hmc.hmc.data.value[IMU_HMC_CHAN_X],
189 IMU_HMC_Y_SIGN * imu_mpu_hmc.hmc.data.value[IMU_HMC_CHAN_Y],
191 };
192 imu_mpu_hmc.hmc.data_available = false;
193 AbiSendMsgIMU_MAG_RAW(IMU_MPU6000_HMC_ID, now_ts, &mag);
194 }
195}
Main include for ABI (AirBorneInterface).
#define IMU_MPU6000_HMC_ID
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
angular rates
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:83
void hmc58xx_event(struct Hmc58xx *hmc)
Definition hmc58xx.c:160
static void hmc58xx_periodic(struct Hmc58xx *hmc)
convenience function: read or start configuration if not already initialized
Definition hmc58xx.h:87
Register defs for Honeywell HMC5843, HMC5883 and HMC5983 magnetometers.
#define HMC58XX_ADDR
void imu_set_defaults_gyro(uint8_t abi_id, const struct Int32RMat *imu_to_sensor, const struct Int32Rates *neutral, const struct FloatRates *scale_f)
Set the defaults for a gyro sensor WARNING: Should be called before sensor is publishing messages to ...
Definition imu.c:842
void imu_set_defaults_accel(uint8_t abi_id, const struct Int32RMat *imu_to_sensor, const struct Int32Vect3 *neutral, const struct FloatVect3 *scale_f)
Set the defaults for a accel sensor WARNING: Should be called before sensor is publishing messages to...
Definition imu.c:871
Inertial Measurement Unit interface.
#define IMU_MPU_Z_SIGN
#define IMU_MPU_LOWPASS_FILTER
Definition imu_mpu6000.c:47
#define IMU_MPU_SMPLRT_DIV
Definition imu_mpu6000.c:48
#define IMU_MPU_X_SIGN
#define IMU_MPU_CHAN_Y
Definition imu_mpu6000.c:94
#define IMU_MPU_CHAN_X
Definition imu_mpu6000.c:90
#define IMU_MPU_Y_SIGN
#define IMU_MPU_CHAN_Z
Definition imu_mpu6000.c:98
#define IMU_MPU_GYRO_RANGE
Definition imu_mpu6000.h:38
#define IMU_MPU_ACCEL_RANGE
Definition imu_mpu6000.h:42
#define IMU_HMC_Y_SIGN
#define IMU_HMC_CHAN_Y
#define IMU_HMC_CHAN_X
#define IMU_HMC_CHAN_Z
struct ImuMpu6000Hmc5883 imu_mpu_hmc
void imu_mpu_hmc_periodic(void)
void imu_mpu_hmc_event(void)
void imu_mpu_hmc_init(void)
#define IMU_HMC_X_SIGN
#define IMU_HMC_Z_SIGN
Driver for IMU with MPU6000 via SPI and HMC5883 via I2c.
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
const struct FloatVect3 MPU60X0_ACCEL_SENS_F[4]
Definition mpu60x0.c:54
const struct FloatRates MPU60X0_GYRO_SENS_F[4]
Definition mpu60x0.c:38
void mpu60x0_spi_event(struct Mpu60x0_Spi *mpu)
void mpu60x0_spi_init(struct Mpu60x0_Spi *mpu, struct spi_periph *spi_p, uint8_t slave_idx)
Definition mpu60x0_spi.c:31
static void mpu60x0_spi_periodic(struct Mpu60x0_Spi *mpu)
convenience function: read or start configuration if not already initialized
Definition mpu60x0_spi.h:78
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
Architecture independent SPI (Serial Peripheral Interface) API.
int int32_t
Typedef defining 32 bit int type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.