Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
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  */
92  /* set callback function to configure mag */
94 
95  // use hmc mag via passthrough
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 }
imu_drotek2_event
void imu_drotek2_event(void)
Definition: imu_drotek_10dof_v2.c:110
Imu::gyro_unscaled
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:46
Mpu60x0Config::smplrt_div
uint8_t smplrt_div
Sample rate divider.
Definition: mpu60x0.h:140
DROTEK_2_ACCEL_RANGE
#define DROTEK_2_ACCEL_RANGE
Definition: imu_drotek_10dof_v2.h:44
hmc58xx_start_configure
void hmc58xx_start_configure(struct Hmc58xx *hmc)
Definition: hmc58xx.c:126
mpu60x0_i2c_periodic
static void mpu60x0_i2c_periodic(struct Mpu60x0_I2c *mpu)
convenience function: read or start configuration if not already initialized
Definition: mpu60x0_i2c.h:79
hmc58xx_event
void hmc58xx_event(struct Hmc58xx *hmc)
Definition: hmc58xx.c:152
Hmc58xx::data
union Hmc58xx::@317 data
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
ImuDisco::mpu
struct Mpu60x0_I2c mpu
The MPU gyro/accel device.
Definition: imu_disco.h:73
Int32Rates::q
int32_t q
in rad/s with INT32_RATE_FRAC
Definition: pprz_algebra_int.h:181
Int32Vect3::z
int32_t z
Definition: pprz_algebra_int.h:91
uint32_t
unsigned long uint32_t
Definition: types.h:18
Mpu60x0Config::accel_range
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:144
imu_drotek2_init
void imu_drotek2_init(void)
Definition: imu_drotek_10dof_v2.c:75
Mpu60x0Config::dlpf_cfg
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:141
DROTEK_2_GYRO_RANGE
#define DROTEK_2_GYRO_RANGE
Definition: imu_drotek_10dof_v2.h:40
IMU_DROTEK_ID
#define IMU_DROTEK_ID
Definition: abi_sender_ids.h:299
ImuDrotek2
Definition: imu_drotek_10dof_v2.h:75
Mpu60x0ConfigSet
void(* Mpu60x0ConfigSet)(void *mpu, uint8_t _reg, uint8_t _val)
Configuration function prototype.
Definition: mpu60x0.h:129
Imu::accel_unscaled
struct Int32Vect3 accel_unscaled
unscaled accelerometer measurements
Definition: imu.h:47
get_sys_time_usec
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
Int32Rates::p
int32_t p
in rad/s with INT32_RATE_FRAC
Definition: pprz_algebra_int.h:180
imu.h
Mpu60x0I2cSlave::configure
Mpu60x0I2cSlaveConfigure configure
Definition: mpu60x0.h:135
mpu60x0_i2c_init
void mpu60x0_i2c_init(struct Mpu60x0_I2c *mpu, struct i2c_periph *i2c_p, uint8_t addr)
Definition: mpu60x0_i2c.c:31
imu_scale_gyro
void imu_scale_gyro(struct Imu *_imu)
Definition: imu_vectornav.c:43
Int32Vect3::y
int32_t y
Definition: pprz_algebra_int.h:90
mpu60x0_i2c_event
void mpu60x0_i2c_event(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:82
DROTEK_2_MPU_I2C_ADDR
#define DROTEK_2_MPU_I2C_ADDR
Definition: imu_drotek_10dof_v2.c:63
hmc58xx_read
void hmc58xx_read(struct Hmc58xx *hmc)
Definition: hmc58xx.c:139
Mpu60x0_I2c::config
struct Mpu60x0Config config
Definition: mpu60x0_i2c.h:68
imu_drotek2
struct ImuDrotek2 imu_drotek2
Definition: imu_drotek_10dof_v2.c:73
imu
struct Imu imu
global IMU state
Definition: imu.c:108
ImuDrotek2::mpu
struct Mpu60x0_I2c mpu
Definition: imu_drotek_10dof_v2.h:76
Mpu60x0Config::nb_slaves
uint8_t nb_slaves
number of used I2C slaves
Definition: mpu60x0.h:156
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")
Mpu60x0Config::i2c_bypass
bool i2c_bypass
Bypass MPU I2C.
Definition: mpu60x0.h:154
Mpu60x0_I2c::data_accel
union Mpu60x0_I2c::@331 data_accel
ImuDrotek2::hmc
struct Hmc58xx hmc
Definition: imu_drotek_10dof_v2.h:77
imu_drotek2_periodic
void imu_drotek2_periodic(void)
Definition: imu_drotek_10dof_v2.c:99
imu_scale_mag
void imu_scale_mag(struct Imu *_imu)
Definition: ahrs_gx3.c:352
Mpu60x0Config::slaves
struct Mpu60x0I2cSlave slaves[MPU60X0_I2C_NB_SLAVES]
I2C slaves.
Definition: mpu60x0.h:158
DROTEK_2_HMC_I2C_ADDR
#define DROTEK_2_HMC_I2C_ADDR
Definition: imu_drotek_10dof_v2.c:68
Mpu60x0_I2c::data_rates
union Mpu60x0_I2c::@332 data_rates
Int32Vect3::x
int32_t x
Definition: pprz_algebra_int.h:89
imu_drotek2_configure_mag_slave
bool imu_drotek2_configure_mag_slave(Mpu60x0ConfigSet mpu_set, void *mpu)
callback function to configure hmc5883 mag
Definition: imu_drotek_10dof_v2.c:158
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_I2c::data_available
volatile bool data_available
data ready flag
Definition: mpu60x0_i2c.h:57
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
i2c.h
Int32Rates::r
int32_t r
in rad/s with INT32_RATE_FRAC
Definition: pprz_algebra_int.h:182
imu_scale_accel
void imu_scale_accel(struct Imu *_imu)
Definition: imu_vectornav.c:44
Mpu60x0Config::initialized
bool initialized
config done flag
Definition: mpu60x0.h:149
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
Hmc58xx::initialized
bool initialized
config done flag
Definition: hmc58xx.h:63