Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
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 #include "generated/modules.h"
33 
34 
35 /* SPI/I2C defaults set in subsystem makefile, can be configured from airframe file */
36 PRINT_CONFIG_VAR(IMU_MPU_SPI_SLAVE_IDX)
37 PRINT_CONFIG_VAR(IMU_MPU_SPI_DEV)
38 PRINT_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 == 60) || (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
49 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
50 #elif PERIODIC_FREQUENCY == 512
51 /* Accelerometer: Bandwidth 260Hz, Delay 0ms
52  * Gyroscope: Bandwidth 256Hz, Delay 0.98ms sampling 8kHz
53  */
54 #define IMU_MPU_LOWPASS_FILTER MPU60X0_DLPF_256HZ
55 #define IMU_MPU_SMPLRT_DIV 3
56 PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
57 #else
58 #error Non-default PERIODIC_FREQUENCY: please define IMU_MPU_LOWPASS_FILTER and IMU_MPU_SMPLRT_DIV.
59 #endif
60 #endif
61 PRINT_CONFIG_VAR(IMU_MPU_LOWPASS_FILTER)
62 PRINT_CONFIG_VAR(IMU_MPU_SMPLRT_DIV)
63 
64 PRINT_CONFIG_VAR(IMU_MPU_GYRO_RANGE)
65 PRINT_CONFIG_VAR(IMU_MPU_ACCEL_RANGE)
66 
67 // Default channels order
68 #ifndef IMU_MPU_CHAN_X
69 #define IMU_MPU_CHAN_X 0
70 #endif
71 PRINT_CONFIG_VAR(IMU_MPU_CHAN_X)
72 #ifndef IMU_MPU_CHAN_Y
73 #define IMU_MPU_CHAN_Y 1
74 #endif
75 PRINT_CONFIG_VAR(IMU_MPU_CHAN_Y)
76 #ifndef IMU_MPU_CHAN_Z
77 #define IMU_MPU_CHAN_Z 2
78 #endif
79 PRINT_CONFIG_VAR(IMU_MPU_CHAN_Z)
80 
81 #ifndef IMU_MPU_X_SIGN
82 #define IMU_MPU_X_SIGN 1
83 #endif
84 PRINT_CONFIG_VAR(IMU_MPU_X_SIGN)
85 #ifndef IMU_MPU_Y_SIGN
86 #define IMU_MPU_Y_SIGN 1
87 #endif
88 PRINT_CONFIG_VAR(IMU_MPU_Y_SIGN)
89 #ifndef IMU_MPU_Z_SIGN
90 #define IMU_MPU_Z_SIGN 1
91 #endif
92 PRINT_CONFIG_VAR(IMU_MPU_Z_SIGN)
93 
94 /* mag by default rotated by 90deg around z axis relative to MPU */
95 #ifndef IMU_HMC_CHAN_X
96 #define IMU_HMC_CHAN_X 1
97 #endif
98 PRINT_CONFIG_VAR(IMU_HMC_CHAN_X)
99 #ifndef IMU_HMC_CHAN_Y
100 #define IMU_HMC_CHAN_Y 0
101 #endif
102 PRINT_CONFIG_VAR(IMU_HMC_CHAN_Y)
103 #ifndef IMU_HMC_CHAN_Z
104 #define IMU_HMC_CHAN_Z 2
105 #endif
106 PRINT_CONFIG_VAR(IMU_HMC_CHAN_Z)
107 
108 #ifndef IMU_HMC_X_SIGN
109 #define IMU_HMC_X_SIGN 1
110 #endif
111 PRINT_CONFIG_VAR(IMU_HMC_X_SIGN)
112 #ifndef IMU_HMC_Y_SIGN
113 #define IMU_HMC_Y_SIGN -1
114 #endif
115 PRINT_CONFIG_VAR(IMU_HMC_Y_SIGN)
116 #ifndef IMU_HMC_Z_SIGN
117 #define IMU_HMC_Z_SIGN 1
118 #endif
119 PRINT_CONFIG_VAR(IMU_HMC_Z_SIGN)
120 
121 
123 
125 {
126  mpu60x0_spi_init(&imu_mpu_hmc.mpu, &IMU_MPU_SPI_DEV, IMU_MPU_SPI_SLAVE_IDX);
127  // change the default configuration
132 
133  // Set the default scaling
136 
137  /* initialize mag and set default options */
138  hmc58xx_init(&imu_mpu_hmc.hmc, &IMU_HMC_I2C_DEV, HMC58XX_ADDR);
139 }
140 
141 
143 {
145 
146  /* Read HMC58XX every 10 times of main freq
147  * at ~50Hz (main loop for rotorcraft: 512Hz)
148  */
149  RunOnceEvery(10, hmc58xx_periodic(&imu_mpu_hmc.hmc));
150 }
151 
153 {
154  uint32_t now_ts = get_sys_time_usec();
155 
158  // set channel order
159  struct Int32Vect3 accel = {
163  };
164  struct Int32Rates rates = {
168  };
169 
171  AbiSendMsgIMU_GYRO_RAW(IMU_MPU6000_HMC_ID, now_ts, &rates, 1, IMU_MPU_HMC_PERIODIC_FREQ, imu_mpu_hmc.mpu.temp);
172  AbiSendMsgIMU_ACCEL_RAW(IMU_MPU6000_HMC_ID, now_ts, &accel, 1, IMU_MPU_HMC_PERIODIC_FREQ, imu_mpu_hmc.mpu.temp);
173  }
174 
175  /* HMC58XX event task */
178  /* mag by default rotated by 90deg around z axis relative to MPU */
179  struct Int32Vect3 mag = {
183  };
185  AbiSendMsgIMU_MAG_RAW(IMU_MPU6000_HMC_ID, now_ts, &mag);
186  }
187 }
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:71
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
union Hmc58xx::@311 data
static void hmc58xx_periodic(struct Hmc58xx *hmc)
convenience function: read or start configuration if not already initialized
Definition: hmc58xx.h:85
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.
#define IMU_MPU_LOWPASS_FILTER
Definition: imu_mpu6000.c:65
#define IMU_MPU_SMPLRT_DIV
Definition: imu_mpu6000.c:66
#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
struct Mpu60x0Config config
Definition: mpu60x0_spi.h:67
float temp
temperature in degrees Celcius
Definition: mpu60x0_spi.h:65
union Mpu60x0_Spi::@332 data_rates
union Mpu60x0_Spi::@331 data_accel
static void mpu60x0_spi_periodic(struct Mpu60x0_Spi *mpu)
convenience function: read or start configuration if not already initialized
Definition: mpu60x0_spi.h:78
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