Paparazzi UAS  v5.8.2_stable-0-g6260b7c
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 #ifndef DROTEK_2_GYRO_RANGE
60 #define DROTEK_2_GYRO_RANGE MPU60X0_GYRO_RANGE_1000
61 #endif
62 PRINT_CONFIG_VAR(DROTEK_2_GYRO_RANGE)
63 
64 #ifndef DROTEK_2_ACCEL_RANGE
65 #define DROTEK_2_ACCEL_RANGE MPU60X0_ACCEL_RANGE_8G
66 #endif
67 PRINT_CONFIG_VAR(DROTEK_2_ACCEL_RANGE)
68 
69 #ifndef DROTEK_2_MPU_I2C_ADDR
70 #define DROTEK_2_MPU_I2C_ADDR MPU60X0_ADDR_ALT
71 #endif
72 PRINT_CONFIG_VAR(DROTEK_2_MPU_I2C_ADDR)
73 
74 #ifndef DROTEK_2_HMC_I2C_ADDR
75 #define DROTEK_2_HMC_I2C_ADDR HMC58XX_ADDR
76 #endif
77 PRINT_CONFIG_VAR(DROTEK_2_HMC_I2C_ADDR)
78 
79 
81 
82 void imu_impl_init(void)
83 {
84  /* MPU-60X0 */
85  mpu60x0_i2c_init(&imu_drotek2.mpu, &(DROTEK_2_I2C_DEV), DROTEK_2_MPU_I2C_ADDR);
86  // change the default configuration
87  imu_drotek2.mpu.config.smplrt_div = DROTEK_2_SMPLRT_DIV;
88  imu_drotek2.mpu.config.dlpf_cfg = DROTEK_2_LOWPASS_FILTER;
91 
92  /* HMC5883 magnetometer */
93  hmc58xx_init(&imu_drotek2.hmc, &(DROTEK_2_I2C_DEV), DROTEK_2_HMC_I2C_ADDR);
94 
95  /* mag is declared as slave to call the configure function,
96  * regardless if it is an actual MPU slave or passthrough
97  */
98  imu_drotek2.mpu.config.nb_slaves = 1;
99  /* set callback function to configure mag */
101 
102  // use hmc mag via passthrough
103  imu_drotek2.mpu.config.i2c_bypass = TRUE;
104 }
105 
106 void imu_periodic(void)
107 {
108  // Start reading the latest gyroscope data
110 
111  // Read HMC58XX at ~50Hz (main loop for rotorcraft: 512Hz)
113  RunOnceEvery(10, hmc58xx_read(&imu_drotek2.hmc));
114  }
115 }
116 
118 {
119  uint32_t now_ts = get_sys_time_usec();
120 
121  // If the MPU6050 I2C transaction has succeeded: convert the data
123 
125 #if IMU_DROTEK_2_ORIENTATION_IC_UP
126  /* change orientation, so if ICs face up, z-axis is down */
133 #else
134  /* default orientation as should be printed on the pcb, z-down, ICs down */
137 #endif
138 
142  AbiSendMsgIMU_GYRO_INT32(IMU_DROTEK_ID, now_ts, &imu.gyro);
143  AbiSendMsgIMU_ACCEL_INT32(IMU_DROTEK_ID, now_ts, &imu.accel);
144  }
145 
146  /* HMC58XX event task */
149 #if IMU_DROTEK_2_ORIENTATION_IC_UP
151  imu.mag_unscaled.y = -imu_drotek2.hmc.data.vect.y;
152  imu.mag_unscaled.z = -imu_drotek2.hmc.data.vect.z;
153 #else
155 #endif
157  imu_scale_mag(&imu);
158  AbiSendMsgIMU_MAG_INT32(IMU_DROTEK_ID, now_ts, &imu.mag);
159  }
160 }
161 
165 bool_t imu_drotek2_configure_mag_slave(Mpu60x0ConfigSet mpu_set __attribute__((unused)),
166  void *mpu __attribute__((unused)))
167 {
170  return TRUE;
171  } else {
172  return FALSE;
173  }
174 }
void mpu60x0_i2c_event(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:82
void imu_scale_gyro(struct Imu *_imu)
Definition: ahrs_gx3.c:349
void hmc58xx_start_configure(struct Hmc58xx *hmc)
Definition: hmc58xx.c:126
struct Mpu60x0_I2c mpu
The MPU gyro/accel device.
Definition: imu_bebop.h:74
void mpu60x0_i2c_init(struct Mpu60x0_I2c *mpu, struct i2c_periph *i2c_p, uint8_t addr)
Definition: mpu60x0_i2c.c:31
void imu_scale_accel(struct Imu *_imu)
Definition: ahrs_gx3.c:350
void imu_periodic(void)
optional.
struct Mpu60x0Config config
Definition: mpu60x0_i2c.h:68
union Mpu60x0_I2c::@41 data_accel
bool_t imu_drotek2_configure_mag_slave(Mpu60x0ConfigSet mpu_set, void *mpu)
callback function to configure hmc5883 mag
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:139
Main include for ABI (AirBorneInterface).
uint8_t smplrt_div
Sample rate divider.
Definition: mpu60x0.h:74
enum Mpu60x0GyroRanges gyro_range
deg/s Range
Definition: mpu60x0.h:76
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:77
#define DROTEK_2_GYRO_RANGE
union Mpu60x0_I2c::@42 data_rates
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:75
struct ImuDrotek2 imu_drotek2
#define FALSE
Definition: std.h:5
Mpu60x0I2cSlaveConfigure configure
Definition: mpu60x0.h:70
static uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.h:39
bool_t initialized
config done flag
Definition: hmc58xx.h:63
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:52
int32_t r
in rad/s with INT32_RATE_FRAC
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:50
#define TRUE
Definition: std.h:4
struct Int32Vect3 accel
accelerometer measurements in m/s^2 in BFP with INT32_ACCEL_FRAC
Definition: imu.h:43
void hmc58xx_read(struct Hmc58xx *hmc)
Definition: hmc58xx.c:139
#define DROTEK_2_HMC_I2C_ADDR
struct Imu imu
global IMU state
Definition: imu_aspirin2.c:43
unsigned long uint32_t
Definition: types.h:18
uint8_t nb_slaves
number of used I2C slaves
Definition: mpu60x0.h:89
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
bool_t initialized
config done flag
Definition: mpu60x0.h:82
struct Int32Vect3 mag
magnetometer measurements scaled to 1 in BFP with INT32_MAG_FRAC
Definition: imu.h:44
#define DROTEK_2_ACCEL_RANGE
Inertial Measurement Unit interface.
struct Mpu60x0I2cSlave slaves[5]
I2C slaves.
Definition: mpu60x0.h:90
volatile bool_t data_available
data ready flag
Definition: hmc58xx.h:65
bool_t i2c_bypass
Bypass MPU I2C.
Definition: mpu60x0.h:87
union Hmc58xx::@33 data
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 imu_impl_init(void)
must be defined by underlying hardware
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:51
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
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:336
#define IMU_DROTEK_ID
void hmc58xx_event(struct Hmc58xx *hmc)
Definition: hmc58xx.c:152
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:64
volatile bool_t data_available
data ready flag
Definition: mpu60x0_i2c.h:57
void imu_drotek2_event(void)
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:42
Architecture independent I2C (Inter-Integrated Circuit Bus) API.