Paparazzi UAS  v5.14.0_stable-0-g3f680d1
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
imu_drotek_10dof_v2.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 
35 #include "subsystems/imu.h"
36 #include "subsystems/abi.h"
37 #include "mcu_periph/i2c.h"
38 
39 #if !defined DROTEK_2_LOWPASS_FILTER && !defined DROTEK_2_SMPLRT_DIV
40 #if (PERIODIC_FREQUENCY == 60) || (PERIODIC_FREQUENCY == 120)
41 /* Accelerometer: Bandwidth 44Hz, Delay 4.9ms
42  * Gyroscope: Bandwidth 42Hz, Delay 4.8ms sampling 1kHz
43  */
44 #define DROTEK_2_LOWPASS_FILTER MPU60X0_DLPF_42HZ
45 #define DROTEK_2_SMPLRT_DIV 9
46 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
47 #elif PERIODIC_FREQUENCY == 512
48 /* Accelerometer: Bandwidth 260Hz, Delay 0ms
49  * Gyroscope: Bandwidth 256Hz, Delay 0.98ms sampling 8kHz
50  */
51 #define DROTEK_2_LOWPASS_FILTER MPU60X0_DLPF_256HZ
52 #define DROTEK_2_SMPLRT_DIV 3
53 PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
54 #endif
55 #endif
56 PRINT_CONFIG_VAR(DROTEK_2_SMPLRT_DIV)
57 PRINT_CONFIG_VAR(DROTEK_2_LOWPASS_FILTER)
58 
59 PRINT_CONFIG_VAR(DROTEK_2_GYRO_RANGE)
60 PRINT_CONFIG_VAR(DROTEK_2_ACCEL_RANGE)
61 
62 #ifndef DROTEK_2_MPU_I2C_ADDR
63 #define DROTEK_2_MPU_I2C_ADDR MPU60X0_ADDR_ALT
64 #endif
65 PRINT_CONFIG_VAR(DROTEK_2_MPU_I2C_ADDR)
66 
67 #ifndef DROTEK_2_HMC_I2C_ADDR
68 #define DROTEK_2_HMC_I2C_ADDR HMC58XX_ADDR
69 #endif
70 PRINT_CONFIG_VAR(DROTEK_2_HMC_I2C_ADDR)
71 
72 
74 
75 void imu_drotek2_init(void)
76 {
77  /* MPU-60X0 */
78  mpu60x0_i2c_init(&imu_drotek2.mpu, &(DROTEK_2_I2C_DEV), DROTEK_2_MPU_I2C_ADDR);
79  // change the default configuration
80  imu_drotek2.mpu.config.smplrt_div = DROTEK_2_SMPLRT_DIV;
81  imu_drotek2.mpu.config.dlpf_cfg = DROTEK_2_LOWPASS_FILTER;
84 
85  /* HMC5883 magnetometer */
86  hmc58xx_init(&imu_drotek2.hmc, &(DROTEK_2_I2C_DEV), DROTEK_2_HMC_I2C_ADDR);
87 
88  /* mag is declared as slave to call the configure function,
89  * regardless if it is an actual MPU slave or passthrough
90  */
91  imu_drotek2.mpu.config.nb_slaves = 1;
92  /* set callback function to configure mag */
94 
95  // use hmc mag via passthrough
96  imu_drotek2.mpu.config.i2c_bypass = true;
97 }
98 
100 {
101  // Start reading the latest gyroscope data
103 
104  // Read HMC58XX at ~50Hz (main loop for rotorcraft: 512Hz)
106  RunOnceEvery(10, hmc58xx_read(&imu_drotek2.hmc));
107  }
108 }
109 
111 {
112  uint32_t now_ts = get_sys_time_usec();
113 
114  // If the MPU6050 I2C transaction has succeeded: convert the data
116 
118 #if IMU_DROTEK_2_ORIENTATION_IC_UP
119  /* change orientation, so if ICs face up, z-axis is down */
126 #else
127  /* default orientation as should be printed on the pcb, z-down, ICs down */
130 #endif
131 
135  AbiSendMsgIMU_GYRO_INT32(IMU_DROTEK_ID, now_ts, &imu.gyro);
136  AbiSendMsgIMU_ACCEL_INT32(IMU_DROTEK_ID, now_ts, &imu.accel);
137  }
138 
139  /* HMC58XX event task */
142 #if IMU_DROTEK_2_ORIENTATION_IC_UP
144  imu.mag_unscaled.y = -imu_drotek2.hmc.data.vect.y;
145  imu.mag_unscaled.z = -imu_drotek2.hmc.data.vect.z;
146 #else
148 #endif
150  imu_scale_mag(&imu);
151  AbiSendMsgIMU_MAG_INT32(IMU_DROTEK_ID, now_ts, &imu.mag);
152  }
153 }
154 
158 bool imu_drotek2_configure_mag_slave(Mpu60x0ConfigSet mpu_set __attribute__((unused)),
159  void *mpu __attribute__((unused)))
160 {
163  return true;
164  } else {
165  return false;
166  }
167 }
void imu_drotek2_periodic(void)
bool initialized
config done flag
Definition: hmc58xx.h:63
volatile bool data_available
data ready flag
Definition: hmc58xx.h:65
union Mpu60x0_I2c::@314 data_rates
union Mpu60x0_I2c::@313 data_accel
void mpu60x0_i2c_event(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:82
bool imu_drotek2_configure_mag_slave(Mpu60x0ConfigSet mpu_set, void *mpu)
callback function to configure hmc5883 mag
void hmc58xx_start_configure(struct Hmc58xx *hmc)
Definition: hmc58xx.c:126
bool initialized
config done flag
Definition: mpu60x0.h:133
void mpu60x0_i2c_init(struct Mpu60x0_I2c *mpu, struct i2c_periph *i2c_p, uint8_t addr)
Definition: mpu60x0_i2c.c:31
struct Mpu60x0Config config
Definition: mpu60x0_i2c.h:68
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:140
Main include for ABI (AirBorneInterface).
uint8_t smplrt_div
Sample rate divider.
Definition: mpu60x0.h:125
struct Imu imu
global IMU state
Definition: imu.c:108
enum Mpu60x0GyroRanges gyro_range
deg/s Range
Definition: mpu60x0.h:127
union Hmc58xx::@303 data
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:128
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:126
struct ImuDrotek2 imu_drotek2
Mpu60x0I2cSlaveConfigure configure
Definition: mpu60x0.h:121
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:48
int32_t r
in rad/s with INT32_RATE_FRAC
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
void hmc58xx_read(struct Hmc58xx *hmc)
Definition: hmc58xx.c:139
#define DROTEK_2_HMC_I2C_ADDR
#define DROTEK_2_GYRO_RANGE
#define DROTEK_2_ACCEL_RANGE
unsigned long uint32_t
Definition: types.h:18
uint8_t nb_slaves
number of used I2C slaves
Definition: mpu60x0.h:140
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.
void imu_scale_gyro(struct Imu *_imu)
Definition: imu_vectornav.c:43
void imu_drotek2_init(void)
void imu_scale_mag(struct Imu *_imu)
Definition: ahrs_gx3.c:351
#define DROTEK_2_MPU_I2C_ADDR
struct Hmc58xx hmc
int32_t p
in rad/s with INT32_RATE_FRAC
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
static void mpu60x0_i2c_periodic(struct Mpu60x0_I2c *mpu)
convenience function: read or start configuration if not already initialized
Definition: mpu60x0_i2c.h:79
struct Mpu60x0_I2c mpu
void imu_scale_accel(struct Imu *_imu)
Definition: imu_vectornav.c:44
bool i2c_bypass
Bypass MPU I2C.
Definition: mpu60x0.h:138
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:337
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
#define IMU_DROTEK_ID
void hmc58xx_event(struct Hmc58xx *hmc)
Definition: hmc58xx.c:152
struct Mpu60x0I2cSlave slaves[MPU60X0_I2C_NB_SLAVES]
I2C slaves.
Definition: mpu60x0.h:142
int32_t q
in rad/s with INT32_RATE_FRAC
void(* Mpu60x0ConfigSet)(void *mpu, uint8_t _reg, uint8_t _val)
Configuration function prototype.
Definition: mpu60x0.h:115
void imu_drotek2_event(void)
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:38
volatile bool data_available
data ready flag
Definition: mpu60x0_i2c.h:57
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
struct Mpu60x0_I2c mpu
The MPU gyro/accel device.
Definition: imu_disco.h:73