Paparazzi UAS  v6.2.0_stable
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 
28 #include "modules/imu/imu.h"
29 #include "modules/core/abi.h"
30 #include "mcu_periph/spi.h"
32 
33 
34 /* SPI/I2C defaults set in subsystem makefile, can be configured from airframe file */
35 PRINT_CONFIG_VAR(IMU_MPU_SPI_SLAVE_IDX)
36 PRINT_CONFIG_VAR(IMU_MPU_SPI_DEV)
37 PRINT_CONFIG_VAR(IMU_HMC_I2C_DEV)
38 
39 
40 /* MPU60x0 gyro/accel internal lowpass frequency */
41 #if !defined IMU_MPU_LOWPASS_FILTER && !defined IMU_MPU_SMPLRT_DIV
42 #if (PERIODIC_FREQUENCY == 60) || (PERIODIC_FREQUENCY == 120)
43 /* Accelerometer: Bandwidth 44Hz, Delay 4.9ms
44  * Gyroscope: Bandwidth 42Hz, Delay 4.8ms sampling 1kHz
45  */
46 #define IMU_MPU_LOWPASS_FILTER MPU60X0_DLPF_42HZ
47 #define IMU_MPU_SMPLRT_DIV 9
48 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
49 #elif PERIODIC_FREQUENCY == 512
50 /* Accelerometer: Bandwidth 260Hz, Delay 0ms
51  * Gyroscope: Bandwidth 256Hz, Delay 0.98ms sampling 8kHz
52  */
53 #define IMU_MPU_LOWPASS_FILTER MPU60X0_DLPF_256HZ
54 #define IMU_MPU_SMPLRT_DIV 3
55 PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
56 #else
57 #error Non-default PERIODIC_FREQUENCY: please define IMU_MPU_LOWPASS_FILTER and IMU_MPU_SMPLRT_DIV.
58 #endif
59 #endif
60 PRINT_CONFIG_VAR(IMU_MPU_LOWPASS_FILTER)
61 PRINT_CONFIG_VAR(IMU_MPU_SMPLRT_DIV)
62 
63 PRINT_CONFIG_VAR(IMU_MPU_GYRO_RANGE)
64 PRINT_CONFIG_VAR(IMU_MPU_ACCEL_RANGE)
65 
66 // Default channels order
67 #ifndef IMU_MPU_CHAN_X
68 #define IMU_MPU_CHAN_X 0
69 #endif
70 PRINT_CONFIG_VAR(IMU_MPU_CHAN_X)
71 #ifndef IMU_MPU_CHAN_Y
72 #define IMU_MPU_CHAN_Y 1
73 #endif
74 PRINT_CONFIG_VAR(IMU_MPU_CHAN_Y)
75 #ifndef IMU_MPU_CHAN_Z
76 #define IMU_MPU_CHAN_Z 2
77 #endif
78 PRINT_CONFIG_VAR(IMU_MPU_CHAN_Z)
79 
80 #ifndef IMU_MPU_X_SIGN
81 #define IMU_MPU_X_SIGN 1
82 #endif
83 PRINT_CONFIG_VAR(IMU_MPU_X_SIGN)
84 #ifndef IMU_MPU_Y_SIGN
85 #define IMU_MPU_Y_SIGN 1
86 #endif
87 PRINT_CONFIG_VAR(IMU_MPU_Y_SIGN)
88 #ifndef IMU_MPU_Z_SIGN
89 #define IMU_MPU_Z_SIGN 1
90 #endif
91 PRINT_CONFIG_VAR(IMU_MPU_Z_SIGN)
92 
93 /* mag by default rotated by 90deg around z axis relative to MPU */
94 #ifndef IMU_HMC_CHAN_X
95 #define IMU_HMC_CHAN_X 1
96 #endif
97 PRINT_CONFIG_VAR(IMU_HMC_CHAN_X)
98 #ifndef IMU_HMC_CHAN_Y
99 #define IMU_HMC_CHAN_Y 0
100 #endif
101 PRINT_CONFIG_VAR(IMU_HMC_CHAN_Y)
102 #ifndef IMU_HMC_CHAN_Z
103 #define IMU_HMC_CHAN_Z 2
104 #endif
105 PRINT_CONFIG_VAR(IMU_HMC_CHAN_Z)
106 
107 #ifndef IMU_HMC_X_SIGN
108 #define IMU_HMC_X_SIGN 1
109 #endif
110 PRINT_CONFIG_VAR(IMU_HMC_X_SIGN)
111 #ifndef IMU_HMC_Y_SIGN
112 #define IMU_HMC_Y_SIGN -1
113 #endif
114 PRINT_CONFIG_VAR(IMU_HMC_Y_SIGN)
115 #ifndef IMU_HMC_Z_SIGN
116 #define IMU_HMC_Z_SIGN 1
117 #endif
118 PRINT_CONFIG_VAR(IMU_HMC_Z_SIGN)
119 
120 
122 
124 {
125  mpu60x0_spi_init(&imu_mpu_hmc.mpu, &IMU_MPU_SPI_DEV, IMU_MPU_SPI_SLAVE_IDX);
126  // change the default configuration
131 
132  // Set the default scaling
135 
136  /* initialize mag and set default options */
137  hmc58xx_init(&imu_mpu_hmc.hmc, &IMU_HMC_I2C_DEV, HMC58XX_ADDR);
138 }
139 
140 
142 {
144 
145  /* Read HMC58XX every 10 times of main freq
146  * at ~50Hz (main loop for rotorcraft: 512Hz)
147  */
148  RunOnceEvery(10, hmc58xx_periodic(&imu_mpu_hmc.hmc));
149 }
150 
152 {
153  uint32_t now_ts = get_sys_time_usec();
154 
157  // set channel order
158  struct Int32Vect3 accel = {
162  };
163  struct Int32Rates rates = {
167  };
168 
170  AbiSendMsgIMU_GYRO_RAW(IMU_MPU6000_HMC_ID, now_ts, &rates, 1, imu_mpu_hmc.mpu.temp);
171  AbiSendMsgIMU_ACCEL_RAW(IMU_MPU6000_HMC_ID, now_ts, &accel, 1, imu_mpu_hmc.mpu.temp);
172  }
173 
174  /* HMC58XX event task */
177  /* mag by default rotated by 90deg around z axis relative to MPU */
178  struct Int32Vect3 mag = {
182  };
184  AbiSendMsgIMU_MAG_RAW(IMU_MPU6000_HMC_ID, now_ts, &mag);
185  }
186 }
Main include for ABI (AirBorneInterface).
#define IMU_MPU6000_HMC_ID
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:75
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: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::@308 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:440
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:410
Inertial Measurement Unit interface.
#define IMU_MPU_LOWPASS_FILTER
Definition: imu_mpu6000.c:64
#define IMU_MPU_SMPLRT_DIV
Definition: imu_mpu6000.c:65
#define IMU_MPU_GYRO_RANGE
Definition: imu_mpu6000.h:38
#define IMU_MPU_ACCEL_RANGE
Definition: imu_mpu6000.h:42
#define IMU_MPU_Z_SIGN
#define IMU_HMC_Y_SIGN
#define IMU_HMC_CHAN_Y
#define IMU_MPU_X_SIGN
#define IMU_HMC_CHAN_X
#define IMU_HMC_CHAN_Z
struct ImuMpu6000Hmc5883 imu_mpu_hmc
void imu_mpu_hmc_periodic(void)
#define IMU_MPU_CHAN_Y
#define IMU_MPU_CHAN_X
#define IMU_MPU_Y_SIGN
#define IMU_MPU_CHAN_Z
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.
struct Mpu60x0_Spi mpu
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
union Mpu60x0_Spi::@326 data_accel
struct Mpu60x0Config config
Definition: mpu60x0_spi.h:67
float temp
temperature in degrees Celcius
Definition: mpu60x0_spi.h:65
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::@327 data_rates
Architecture independent SPI (Serial Peripheral Interface) API.
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78