Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures 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 
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
63 
64 #ifndef DROTEK_2_ACCEL_RANGE
65 #define DROTEK_2_ACCEL_RANGE MPU60X0_ACCEL_RANGE_8G
66 #endif
68 
69 #ifndef DROTEK_2_MPU_I2C_ADDR
70 #define DROTEK_2_MPU_I2C_ADDR MPU60X0_ADDR_ALT
71 #endif
73 
74 #ifndef DROTEK_2_HMC_I2C_ADDR
75 #define DROTEK_2_HMC_I2C_ADDR HMC58XX_ADDR
76 #endif
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  imu_drotek2.gyro_valid = FALSE;
106  imu_drotek2.accel_valid = FALSE;
107  imu_drotek2.mag_valid = FALSE;
108 }
109 
110 void imu_periodic(void)
111 {
112  // Start reading the latest gyroscope data
114 
115  // Read HMC58XX at ~50Hz (main loop for rotorcraft: 512Hz)
117  RunOnceEvery(10, hmc58xx_read(&imu_drotek2.hmc));
118  }
119 }
120 
122 {
123  // If the MPU6050 I2C transaction has succeeded: convert the data
125 
127 #if IMU_DROTEK_2_ORIENTATION_IC_UP
128  /* change orientation, so if ICs face up, z-axis is down */
135 #else
136  /* default orientation as should be printed on the pcb, z-down, ICs down */
139 #endif
140 
144  }
145 
146  /* HMC58XX event task */
149 #if IMU_DROTEK_2_ORIENTATION_IC_UP
153 #else
155 #endif
158  }
159 }
160 
164 bool_t imu_drotek2_configure_mag_slave(Mpu60x0ConfigSet mpu_set __attribute__ ((unused)), void* mpu __attribute__ ((unused)))
165 {
168  return TRUE;
169  else
170  return FALSE;
171 }
void mpu60x0_i2c_event(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:81
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:75
void hmc58xx_start_configure(struct Hmc58xx *hmc)
Definition: hmc58xx.c:125
struct Hmc58xx hmc
int32_t p
in rad/s with INT32_RATE_FRAC
void mpu60x0_i2c_init(struct Mpu60x0_I2c *mpu, struct i2c_periph *i2c_p, uint8_t addr)
Definition: mpu60x0_i2c.c:31
void imu_periodic(void)
bool_t imu_drotek2_configure_mag_slave(Mpu60x0ConfigSet mpu_set, void *mpu)
callback function to configure hmc5883 mag
uint8_t nb_slaves
number of used I2C slaves
Definition: mpu60x0.h:89
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:326
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:49
union Mpu60x0_I2c::@38 data_rates
uint8_t smplrt_div
Sample rate divider.
Definition: mpu60x0.h:74
volatile bool_t gyro_valid
union Hmc58xx::@29 data
#define DROTEK_2_GYRO_RANGE
struct ImuDrotek2 imu_drotek2
#define FALSE
Definition: imu_chimu.h:141
Mpu60x0I2cSlaveConfigure configure
Definition: mpu60x0.h:70
volatile bool_t mag_valid
struct Int32Vect3 accel_unscaled
unscaled accelerometer measurements
Definition: imu.h:50
void hmc58xx_read(struct Hmc58xx *hmc)
Definition: hmc58xx.c:138
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:77
#define DROTEK_2_HMC_I2C_ADDR
struct Imu imu
global IMU state
Definition: imu_aspirin2.c:47
bool_t initialized
config done flag
Definition: mpu60x0.h:82
struct Mpu60x0_I2c mpu
enum Mpu60x0GyroRanges gyro_range
deg/s Range
Definition: mpu60x0.h:76
#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: mpu60x0_i2c.h:57
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:51
struct Int16Rates rates
rates data as angular rates in gyro coordinate system
Definition: mpu60x0_i2c.h:63
struct Int16Vect3 vect
data vector in mag coordinate system
Definition: hmc58xx.h:67
#define TRUE
Definition: imu_chimu.h:144
struct Mpu60x0Config config
Definition: mpu60x0_i2c.h:67
bool_t i2c_bypass
Bypass MPU I2C.
Definition: mpu60x0.h:87
#define DROTEK_2_MPU_I2C_ADDR
struct Int16Vect3 vect
accel data vector in accel coordinate system
Definition: mpu60x0_i2c.h:59
void imu_impl_init(void)
int32_t q
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
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:129
static void mpu60x0_i2c_periodic(struct Mpu60x0_I2c *mpu)
convenience function: read or start configuration if not already initialized
Definition: mpu60x0_i2c.h:78
int32_t r
in rad/s with INT32_RATE_FRAC
bool_t initialized
config done flag
Definition: hmc58xx.h:63
union Mpu60x0_I2c::@37 data_accel
void hmc58xx_event(struct Hmc58xx *hmc)
Definition: hmc58xx.c:151
volatile bool_t data_available
data ready flag
Definition: hmc58xx.h:65
void(* Mpu60x0ConfigSet)(void *mpu, uint8_t _reg, uint8_t _val)
Configuration function prototype.
Definition: mpu60x0.h:64
volatile bool_t accel_valid
void imu_drotek2_event(void)
Architecture independent I2C (Inter-Integrated Circuit Bus) API.