Paparazzi UAS  v6.2_unstable
Paparazzi is a free software Unmanned Aircraft System.
imu_disco.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Freek van Tienen <freek.v.tienen@gmail.com>
3  * Copyright (C) 2017 Gautier Hattenberger <gautier.hattenberger@enac.fr>
4  *
5  * This file is part of paparazzi.
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, see
19  * <http://www.gnu.org/licenses/>.
20  */
21 
27 #include "modules/imu/imu.h"
28 #include "modules/core/abi.h"
29 #include "mcu_periph/i2c.h"
30 
31 
32 /* I2C is hardwired on Disco autopilot */
33 #define DISCO_MAG_I2C_DEV i2c1
34 PRINT_CONFIG_VAR(DISCO_MAG_I2C_DEV)
35 #define DISCO_MPU_I2C_DEV i2c2
36 PRINT_CONFIG_VAR(DISCO_MPU_I2C_DEV)
37 
38 #if !defined AK8963_HZ
39 #define AK8963_HZ 50
40 #endif
41 
42 #if !defined DISCO_LOWPASS_FILTER && !defined DISCO_SMPLRT_DIV
43 #if (PERIODIC_FREQUENCY == 60) || (PERIODIC_FREQUENCY == 120)
44 /* Accelerometer: Bandwidth 44Hz, Delay 4.9ms
45  * Gyroscope: Bandwidth 42Hz, Delay 4.8ms sampling 1kHz
46  */
47 #define DISCO_LOWPASS_FILTER MPU60X0_DLPF_42HZ
48 #define DISCO_SMPLRT_DIV 9
49 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
50 #elif PERIODIC_FREQUENCY == 512
51 /* Accelerometer: Bandwidth 260Hz, Delay 0ms
52  * Gyroscope: Bandwidth 256Hz, Delay 0.98ms sampling 8kHz
53  */
54 #define DISCO_LOWPASS_FILTER MPU60X0_DLPF_256HZ
55 #define DISCO_SMPLRT_DIV 3
56 PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
57 #endif
58 #endif
59 
60 PRINT_CONFIG_VAR(DISCO_SMPLRT_DIV)
61 PRINT_CONFIG_VAR(DISCO_LOWPASS_FILTER)
62 
63 PRINT_CONFIG_VAR(DISCO_GYRO_RANGE)
64 PRINT_CONFIG_VAR(DISCO_ACCEL_RANGE)
65 
66 
68 
72 void imu_disco_init(void)
73 {
74  /* MPU-60X0 */
76  imu_disco.mpu.config.smplrt_div = DISCO_SMPLRT_DIV;
77  imu_disco.mpu.config.dlpf_cfg = DISCO_LOWPASS_FILTER;
80 
81  /* AKM8963 */
83 }
84 
90 {
91  static uint32_t cntr = 0;
92 
93  // Start reading the latest gyroscope data
95 
96  // AKM8963 if read faster thant datasheet 100Hz over I2C crashes it once in a while
97  if (cntr%((uint32_t)(PERIODIC_FREQUENCY/AK8963_HZ))==0) {
99  cntr=0;
100  }
101  cntr++;
102 }
103 
108 void imu_disco_event(void)
109 {
110  uint32_t now_ts = get_sys_time_usec();
111 
112  /* MPU-60x0 event taks */
114 
116  /* set correct orientation here */
118  -imu_disco.mpu.data_rates.rates.p,
119  -imu_disco.mpu.data_rates.rates.q,
120  imu_disco.mpu.data_rates.rates.r);
122  -imu_disco.mpu.data_accel.vect.x,
123  -imu_disco.mpu.data_accel.vect.y,
124  imu_disco.mpu.data_accel.vect.z);
125 
126  imu_disco.mpu.data_available = false;
129  AbiSendMsgIMU_GYRO_INT32(IMU_BOARD_ID, now_ts, &imu.gyro);
130  AbiSendMsgIMU_ACCEL_INT32(IMU_BOARD_ID, now_ts, &imu.accel);
131  }
132 
133  /* AKM8963 event task */
135 
138  imu_disco.ak.data.vect.x,
139  imu_disco.ak.data.vect.y,
140  imu_disco.ak.data.vect.z);
141 
142  imu_disco.ak.data_available = false;
143  imu_scale_mag(&imu);
144  AbiSendMsgIMU_MAG_INT32(IMU_BOARD_ID, now_ts, &imu.mag);
145  }
146 }
Imu::gyro_unscaled
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:46
uint32_t
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
Mpu60x0Config::smplrt_div
uint8_t smplrt_div
Sample rate divider.
Definition: mpu60x0.h:140
DISCO_MPU_I2C_DEV
#define DISCO_MPU_I2C_DEV
Definition: imu_disco.c:35
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
imu_disco_init
void imu_disco_init(void)
Disco IMU initializtion of the MPU-60x0 and HMC58xx.
Definition: imu_disco.c:72
Ak8963::data
union Ak8963::@297 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
imu_disco
struct ImuDisco imu_disco
Disco IMU data.
Definition: imu_disco.c:67
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:291
Mpu60x0Config::dlpf_cfg
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:141
ak8963_init
void ak8963_init(struct Ak8963 *ak, struct i2c_periph *i2c_p, uint8_t addr)
Initialize AK8963 struct.
Definition: ak8963.c:34
imu_scale_gyro
void WEAK imu_scale_gyro(struct Imu *_imu)
Definition: imu.c:208
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:75
imu.h
RATES_ASSIGN
#define RATES_ASSIGN(_ra, _p, _q, _r)
Definition: pprz_algebra.h:330
DISCO_MAG_I2C_DEV
#define DISCO_MAG_I2C_DEV
Definition: imu_disco.c:33
Mpu60x0_I2c::data_rates
union Mpu60x0_I2c::@323 data_rates
Mpu60x0_I2c::data_accel
union Mpu60x0_I2c::@322 data_accel
ImuDisco
Everything that is in the disco IMU.
Definition: imu_disco.h:72
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_disco_event
void imu_disco_event(void)
Handle all the events of the Disco IMU components.
Definition: imu_disco.c:108
imu_scale_accel
void WEAK imu_scale_accel(struct Imu *_imu)
Definition: imu.c:219
mpu60x0_i2c_event
void mpu60x0_i2c_event(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:82
ImuDisco::ak
struct Ak8963 ak
The AK8963 mag.
Definition: imu_disco.h:74
AK8963_HZ
#define AK8963_HZ
Definition: imu_disco.c:39
Mpu60x0_I2c::config
struct Mpu60x0Config config
Definition: mpu60x0_i2c.h:68
imu
struct Imu imu
global IMU state
Definition: imu.c:108
PRINT_CONFIG_MSG
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
DISCO_GYRO_RANGE
#define DISCO_GYRO_RANGE
Definition: imu_disco.h:38
ak8963_periodic
static void ak8963_periodic(struct Ak8963 *ak)
convenience function: read or start configuration if not already initialized
Definition: ak8963.h:75
MPU60X0_ADDR
#define MPU60X0_ADDR
Definition: mpu60x0_regs.h:32
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
AK8963_ADDR
#define AK8963_ADDR
Definition: ak8963_regs.h:31
Mpu60x0_I2c::data_available
volatile bool data_available
data ready flag
Definition: mpu60x0_i2c.h:57
VECT3_ASSIGN
#define VECT3_ASSIGN(_a, _x, _y, _z)
Definition: pprz_algebra.h:125
Ak8963::data_available
volatile bool data_available
data ready flag
Definition: ak8963.h:61
imu_disco_periodic
void imu_disco_periodic(void)
Handle all the periodic tasks of the Disco IMU components.
Definition: imu_disco.c:89
i2c.h
imu_scale_mag
void WEAK imu_scale_mag(struct Imu *_imu)
Definition: imu.c:246
DISCO_ACCEL_RANGE
#define DISCO_ACCEL_RANGE
Definition: imu_disco.h:42
Imu::gyro
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:38
ak8963_event
void ak8963_event(struct Ak8963 *ak)
Definition: ak8963.c:95