Paparazzi UAS  v6.2_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 
27 #include "modules/imu/imu.h"
28 #include "modules/core/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 IMU_MPU_LOWPASS_FILTER and IMU_MPU_SMPLRT_DIV.
57 #endif
58 #endif
59 PRINT_CONFIG_VAR(IMU_MPU_LOWPASS_FILTER)
60 PRINT_CONFIG_VAR(IMU_MPU_SMPLRT_DIV)
61 
62 PRINT_CONFIG_VAR(IMU_MPU_GYRO_RANGE)
63 PRINT_CONFIG_VAR(IMU_MPU_ACCEL_RANGE)
64 
65 // Default channels order
66 #ifndef IMU_MPU_CHAN_X
67 #define IMU_MPU_CHAN_X 0
68 #endif
69 PRINT_CONFIG_VAR(IMU_MPU_CHAN_X)
70 #ifndef IMU_MPU_CHAN_Y
71 #define IMU_MPU_CHAN_Y 1
72 #endif
73 PRINT_CONFIG_VAR(IMU_MPU_CHAN_Y)
74 #ifndef IMU_MPU_CHAN_Z
75 #define IMU_MPU_CHAN_Z 2
76 #endif
77 PRINT_CONFIG_VAR(IMU_MPU_CHAN_Z)
78 
79 #ifndef IMU_MPU_X_SIGN
80 #define IMU_MPU_X_SIGN 1
81 #endif
82 PRINT_CONFIG_VAR(IMU_MPU_X_SIGN)
83 #ifndef IMU_MPU_Y_SIGN
84 #define IMU_MPU_Y_SIGN 1
85 #endif
86 PRINT_CONFIG_VAR(IMU_MPU_Y_SIGN)
87 #ifndef IMU_MPU_Z_SIGN
88 #define IMU_MPU_Z_SIGN 1
89 #endif
90 PRINT_CONFIG_VAR(IMU_MPU_Z_SIGN)
91 
92 /* mag by default rotated by 90deg around z axis relative to MPU */
93 #ifndef IMU_HMC_CHAN_X
94 #define IMU_HMC_CHAN_X 1
95 #endif
96 PRINT_CONFIG_VAR(IMU_HMC_CHAN_X)
97 #ifndef IMU_HMC_CHAN_Y
98 #define IMU_HMC_CHAN_Y 0
99 #endif
100 PRINT_CONFIG_VAR(IMU_HMC_CHAN_Y)
101 #ifndef IMU_HMC_CHAN_Z
102 #define IMU_HMC_CHAN_Z 2
103 #endif
104 PRINT_CONFIG_VAR(IMU_HMC_CHAN_Z)
105 
106 #ifndef IMU_HMC_X_SIGN
107 #define IMU_HMC_X_SIGN 1
108 #endif
109 PRINT_CONFIG_VAR(IMU_HMC_X_SIGN)
110 #ifndef IMU_HMC_Y_SIGN
111 #define IMU_HMC_Y_SIGN -1
112 #endif
113 PRINT_CONFIG_VAR(IMU_HMC_Y_SIGN)
114 #ifndef IMU_HMC_Z_SIGN
115 #define IMU_HMC_Z_SIGN 1
116 #endif
117 PRINT_CONFIG_VAR(IMU_HMC_Z_SIGN)
118 
119 
121 
123 {
124  mpu60x0_spi_init(&imu_mpu_hmc.mpu, &IMU_MPU_SPI_DEV, IMU_MPU_SPI_SLAVE_IDX);
125  // change the default configuration
130 
131  /* initialize mag and set default options */
132  hmc58xx_init(&imu_mpu_hmc.hmc, &IMU_HMC_I2C_DEV, HMC58XX_ADDR);
133 }
134 
135 
137 {
139 
140  /* Read HMC58XX every 10 times of main freq
141  * at ~50Hz (main loop for rotorcraft: 512Hz)
142  */
143  RunOnceEvery(10, hmc58xx_periodic(&imu_mpu_hmc.hmc));
144 }
145 
147 {
148  uint32_t now_ts = get_sys_time_usec();
149 
152  // set channel order
153  struct Int32Vect3 accel = {
157  };
158  struct Int32Rates rates = {
162  };
163  // unscaled vector
164  VECT3_COPY(imu.accel_unscaled, accel);
165  RATES_COPY(imu.gyro_unscaled, rates);
166 
170  AbiSendMsgIMU_GYRO_INT32(IMU_MPU6000_HMC_ID, now_ts, &imu.gyro);
171  AbiSendMsgIMU_ACCEL_INT32(IMU_MPU6000_HMC_ID, now_ts, &imu.accel);
172  }
173 
174  /* HMC58XX event task */
177  /* mag by default rotated by 90deg around z axis relative to MPU */
182  imu_scale_mag(&imu);
183  AbiSendMsgIMU_MAG_INT32(IMU_MPU6000_HMC_ID, now_ts, &imu.mag);
184  }
185 }
imu_mpu_hmc
struct ImuMpu6000Hmc5883 imu_mpu_hmc
Definition: imu_mpu6000_hmc5883.c:120
Imu::gyro_unscaled
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:46
uint32_t
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
Mpu60x0Config::smplrt_div
uint8_t smplrt_div
Sample rate divider.
Definition: mpu60x0.h:140
IMU_MPU6000_HMC_ID
#define IMU_MPU6000_HMC_ID
Definition: abi_sender_ids.h:327
hmc58xx_event
void hmc58xx_event(struct Hmc58xx *hmc)
Definition: hmc58xx.c:152
IMU_MPU_ACCEL_RANGE
#define IMU_MPU_ACCEL_RANGE
Definition: imu_mpu6000.h:41
Int32Rates
angular rates
Definition: pprz_algebra_int.h:179
Mpu60x0Config::gyro_range
enum Mpu60x0GyroRanges gyro_range
deg/s Range
Definition: mpu60x0.h:143
abi.h
Imu::accel
struct Int32Vect3 accel
accelerometer measurements in m/s^2 in BFP with INT32_ACCEL_FRAC
Definition: imu.h:39
spi.h
IMU_MPU_GYRO_RANGE
#define IMU_MPU_GYRO_RANGE
Definition: imu_mpu6000.h:37
Int32Vect3::z
int32_t z
Definition: pprz_algebra_int.h:91
IMU_HMC_Y_SIGN
#define IMU_HMC_Y_SIGN
Definition: imu_mpu6000_hmc5883.c:111
IMU_MPU_Y_SIGN
#define IMU_MPU_Y_SIGN
Definition: imu_mpu6000_hmc5883.c:84
Mpu60x0_Spi::data_rates
union Mpu60x0_Spi::@325 data_rates
Mpu60x0Config::accel_range
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:144
Mpu60x0Config::dlpf_cfg
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:141
IMU_MPU_CHAN_X
#define IMU_MPU_CHAN_X
Definition: imu_mpu6000_hmc5883.c:67
ImuMpu6000Hmc5883
Definition: imu_mpu6000_hmc5883.h:73
imu_scale_gyro
void WEAK imu_scale_gyro(struct Imu *_imu)
Definition: imu.c:208
Imu::accel_unscaled
struct Int32Vect3 accel_unscaled
unscaled accelerometer measurements
Definition: imu.h:47
Hmc58xx::data
union Hmc58xx::@308 data
hmc58xx_regs.h
get_sys_time_usec
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:75
HMC58XX_ADDR
#define HMC58XX_ADDR
Definition: hmc58xx_regs.h:31
imu.h
IMU_MPU_X_SIGN
#define IMU_MPU_X_SIGN
Definition: imu_mpu6000_hmc5883.c:80
IMU_HMC_CHAN_Z
#define IMU_HMC_CHAN_Z
Definition: imu_mpu6000_hmc5883.c:102
imu_mpu_hmc_periodic
void imu_mpu_hmc_periodic(void)
Definition: imu_mpu6000_hmc5883.c:136
IMU_MPU_SMPLRT_DIV
#define IMU_MPU_SMPLRT_DIV
Definition: imu_mpu6000.c:64
Int32Vect3
Definition: pprz_algebra_int.h:88
imu_scale_accel
void WEAK imu_scale_accel(struct Imu *_imu)
Definition: imu.c:219
Int32Vect3::y
int32_t y
Definition: pprz_algebra_int.h:90
IMU_HMC_CHAN_X
#define IMU_HMC_CHAN_X
Definition: imu_mpu6000_hmc5883.c:94
IMU_MPU_Z_SIGN
#define IMU_MPU_Z_SIGN
Definition: imu_mpu6000_hmc5883.c:88
ImuMpu6000Hmc5883::mpu
struct Mpu60x0_Spi mpu
Definition: imu_mpu6000_hmc5883.h:74
hmc58xx_periodic
static void hmc58xx_periodic(struct Hmc58xx *hmc)
convenience function: read or start configuration if not already initialized
Definition: hmc58xx.h:85
IMU_MPU_CHAN_Z
#define IMU_MPU_CHAN_Z
Definition: imu_mpu6000_hmc5883.c:75
IMU_HMC_X_SIGN
#define IMU_HMC_X_SIGN
Definition: imu_mpu6000_hmc5883.c:107
imu
struct Imu imu
global IMU state
Definition: imu.c:108
ImuMpu6000Hmc5883::hmc
struct Hmc58xx hmc
Definition: imu_mpu6000_hmc5883.h:75
imu_mpu_hmc_event
void imu_mpu_hmc_event(void)
Definition: imu_mpu6000_hmc5883.c:146
hmc58xx_init
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
PRINT_CONFIG_MSG
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
imu_mpu_hmc_init
void imu_mpu_hmc_init(void)
Definition: imu_mpu6000_hmc5883.c:122
mpu60x0_spi_periodic
static void mpu60x0_spi_periodic(struct Mpu60x0_Spi *mpu)
convenience function: read or start configuration if not already initialized
Definition: mpu60x0_spi.h:78
int32_t
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83
IMU_MPU_LOWPASS_FILTER
#define IMU_MPU_LOWPASS_FILTER
Definition: imu_mpu6000.c:63
IMU_HMC_CHAN_Y
#define IMU_HMC_CHAN_Y
Definition: imu_mpu6000_hmc5883.c:98
Mpu60x0_Spi::data_available
volatile bool data_available
data ready flag
Definition: mpu60x0_spi.h:56
Int32Vect3::x
int32_t x
Definition: pprz_algebra_int.h:89
IMU_HMC_Z_SIGN
#define IMU_HMC_Z_SIGN
Definition: imu_mpu6000_hmc5883.c:115
IMU_MPU_CHAN_Y
#define IMU_MPU_CHAN_Y
Definition: imu_mpu6000_hmc5883.c:71
Imu::mag
struct Int32Vect3 mag
magnetometer measurements scaled to 1 in BFP with INT32_MAG_FRAC
Definition: imu.h:40
Imu::mag_unscaled
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:48
Mpu60x0_Spi::data_accel
union Mpu60x0_Spi::@324 data_accel
RATES_COPY
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:337
Hmc58xx::data_available
volatile bool data_available
data ready flag
Definition: hmc58xx.h:65
Mpu60x0_Spi::config
struct Mpu60x0Config config
Definition: mpu60x0_spi.h:67
mpu60x0_spi_init
void mpu60x0_spi_init(struct Mpu60x0_Spi *mpu, struct spi_periph *spi_p, uint8_t slave_idx)
Definition: mpu60x0_spi.c:31
imu_scale_mag
void WEAK imu_scale_mag(struct Imu *_imu)
Definition: imu.c:246
mpu60x0_spi_event
void mpu60x0_spi_event(struct Mpu60x0_Spi *mpu)
Definition: mpu60x0_spi.c:128
VECT3_COPY
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:140
Imu::gyro
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:38