Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
imu_navstik.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Freek van Tienen <freek.v.tienen@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 "subsystems/imu.h"
28 #include "subsystems/abi.h"
29 #include "mcu_periph/i2c.h"
30 
31 
32 /* defaults suitable for Navstik */
33 #ifndef NAVSTIK_MAG_I2C_DEV
34 #define NAVSTIK_MAG_I2C_DEV i2c3
35 #endif
36 PRINT_CONFIG_VAR(NAVSTIK_MAG_I2C_DEV)
37 
38 #ifndef NAVSTIK_MPU_I2C_DEV
39 #define NAVSTIK_MPU_I2C_DEV i2c1
40 #endif
41 PRINT_CONFIG_VAR(NAVSTIK_MPU_I2C_DEV)
42 
43 #if !defined NAVSTIK_LOWPASS_FILTER && !defined NAVSTIK_SMPLRT_DIV
44 #if (PERIODIC_FREQUENCY == 60) || (PERIODIC_FREQUENCY == 120)
45 /* Accelerometer: Bandwidth 44Hz, Delay 4.9ms
46  * Gyroscope: Bandwidth 42Hz, Delay 4.8ms sampling 1kHz
47  */
48 #define NAVSTIK_LOWPASS_FILTER MPU60X0_DLPF_42HZ
49 #define NAVSTIK_SMPLRT_DIV 9
50 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
51 #elif PERIODIC_FREQUENCY == 512
52 /* Accelerometer: Bandwidth 260Hz, Delay 0ms
53  * Gyroscope: Bandwidth 256Hz, Delay 0.98ms sampling 8kHz
54  */
55 #define NAVSTIK_LOWPASS_FILTER MPU60X0_DLPF_256HZ
56 #define NAVSTIK_SMPLRT_DIV 3
57 PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
58 #endif
59 #endif
60 PRINT_CONFIG_VAR(NAVSTIK_SMPLRT_DIV)
61 PRINT_CONFIG_VAR(NAVSTIK_LOWPASS_FILTER)
62 
63 PRINT_CONFIG_VAR(NAVSTIK_GYRO_RANGE)
64 PRINT_CONFIG_VAR(NAVSTIK_ACCEL_RANGE)
65 
66 
68 
72 void imu_navstik_init(void)
73 {
74  /* MPU-60X0 */
76  imu_navstik.mpu.config.smplrt_div = NAVSTIK_SMPLRT_DIV;
77  imu_navstik.mpu.config.dlpf_cfg = NAVSTIK_LOWPASS_FILTER;
80 
81  /* HMC58XX */
83 }
84 
90 {
91  // Start reading the latest gyroscope data
93 
94  // Read HMC58XX at 50Hz (main loop for rotorcraft: 512Hz)
95  RunOnceEvery(10, hmc58xx_periodic(&imu_navstik.hmc));
96 }
97 
103 {
104  uint32_t now_ts = get_sys_time_usec();
105 
106  /* MPU-60x0 event taks */
108 
110  /* default orientation as should be printed on the pcb, z-down, ICs down */
113 
117  AbiSendMsgIMU_GYRO_INT32(IMU_BOARD_ID, now_ts, &imu.gyro);
118  AbiSendMsgIMU_ACCEL_INT32(IMU_BOARD_ID, now_ts, &imu.accel);
119  }
120 
121  /* HMC58XX event task */
125  imu.mag_unscaled.y = -imu_navstik.hmc.data.vect.x;
128  imu_scale_mag(&imu);
129  AbiSendMsgIMU_MAG_INT32(IMU_BOARD_ID, now_ts, &imu.mag);
130  }
131 }
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
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
MPU60X0_ADDR_ALT
#define MPU60X0_ADDR_ALT
Definition: mpu60x0_regs.h:33
Int32Vect3::z
int32_t z
Definition: pprz_algebra_int.h:91
ImuNavstik::hmc
struct Hmc58xx hmc
Definition: imu_navstik.h:74
uint32_t
unsigned long uint32_t
Definition: types.h:18
Mpu60x0Config::accel_range
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:144
IMU_BOARD_ID
#define IMU_BOARD_ID
Definition: abi_sender_ids.h:275
Mpu60x0Config::dlpf_cfg
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:141
Imu::accel_unscaled
struct Int32Vect3 accel_unscaled
unscaled accelerometer measurements
Definition: imu.h:47
imu_navstik_periodic
void imu_navstik_periodic(void)
Handle all the periodic tasks of the Navstik IMU components.
Definition: imu_navstik.c:89
get_sys_time_usec
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
HMC58XX_ADDR
#define HMC58XX_ADDR
Definition: hmc58xx_regs.h:31
imu.h
NAVSTIK_ACCEL_RANGE
#define NAVSTIK_ACCEL_RANGE
Definition: imu_navstik.h:42
imu_navstik
struct ImuNavstik imu_navstik
Basic Navstik IMU data.
Definition: imu_navstik.c:67
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_navstik_event
void imu_navstik_event(void)
Handle all the events of the Navstik IMU components.
Definition: imu_navstik.c:102
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
NAVSTIK_MPU_I2C_DEV
#define NAVSTIK_MPU_I2C_DEV
Definition: imu_navstik.c:39
ImuNavstik::mpu
struct Mpu60x0_I2c mpu
Definition: imu_navstik.h:73
hmc58xx_periodic
static void hmc58xx_periodic(struct Hmc58xx *hmc)
convenience function: read or start configuration if not already initialized
Definition: hmc58xx.h:85
Mpu60x0_I2c::config
struct Mpu60x0Config config
Definition: mpu60x0_i2c.h:68
imu
struct Imu imu
global IMU state
Definition: imu.c:108
NAVSTIK_MAG_I2C_DEV
#define NAVSTIK_MAG_I2C_DEV
Definition: imu_navstik.c:34
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")
NAVSTIK_GYRO_RANGE
#define NAVSTIK_GYRO_RANGE
Definition: imu_navstik.h:38
Mpu60x0_I2c::data_accel
union Mpu60x0_I2c::@331 data_accel
imu_scale_mag
void imu_scale_mag(struct Imu *_imu)
Definition: ahrs_gx3.c:352
Mpu60x0_I2c::data_rates
union Mpu60x0_I2c::@332 data_rates
imu_navstik_init
void imu_navstik_init(void)
Navstik IMU initializtion of the MPU-60x0 and HMC58xx.
Definition: imu_navstik.c:72
Int32Vect3::x
int32_t x
Definition: pprz_algebra_int.h:89
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
ImuNavstik
Definition: imu_navstik.h:72
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
imu_scale_accel
void imu_scale_accel(struct Imu *_imu)
Definition: imu_vectornav.c:44
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