Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
imu_mpu60x0_i2c.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013-2015 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 
27 #include <math.h>
28 #include "subsystems/imu.h"
29 #include "subsystems/abi.h"
30 #include "mcu_periph/i2c.h"
31 #include "led.h"
32 
33 /* MPU60x0 gyro/accel internal lowpass frequency */
34 #if !defined IMU_MPU60X0_LOWPASS_FILTER && !defined IMU_MPU60X0_SMPLRT_DIV
35 #if (PERIODIC_FREQUENCY == 60) || (PERIODIC_FREQUENCY == 120)
36 /* Accelerometer: Bandwidth 44Hz, Delay 4.9ms
37  * Gyroscope: Bandwidth 42Hz, Delay 4.8ms sampling 1kHz
38  */
39 #define IMU_MPU60X0_LOWPASS_FILTER MPU60X0_DLPF_42HZ
40 #define IMU_MPU60X0_SMPLRT_DIV 9
41 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
42 #elif PERIODIC_FREQUENCY == 512
43 /* Accelerometer: Bandwidth 260Hz, Delay 0ms
44  * Gyroscope: Bandwidth 256Hz, Delay 0.98ms sampling 8kHz
45  */
46 #define IMU_MPU60X0_LOWPASS_FILTER MPU60X0_DLPF_256HZ
47 #define IMU_MPU60X0_SMPLRT_DIV 3
48 PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
49 #else
50 #error Non-default PERIODIC_FREQUENCY: please define IMU_MPU60X0_LOWPASS_FILTER and IMU_MPU60X0_SMPLRT_DIV.
51 #endif
52 #endif
53 PRINT_CONFIG_VAR(IMU_MPU60X0_LOWPASS_FILTER)
54 PRINT_CONFIG_VAR(IMU_MPU60X0_SMPLRT_DIV)
55 
56 PRINT_CONFIG_VAR(IMU_MPU60X0_GYRO_RANGE)
57 PRINT_CONFIG_VAR(IMU_MPU60X0_ACCEL_RANGE)
58 
59 #ifndef IMU_MPU60X0_I2C_ADDR
60 #define IMU_MPU60X0_I2C_ADDR MPU60X0_ADDR
61 #endif
62 
64 
65 void imu_mpu_i2c_init(void)
66 {
67  mpu60x0_i2c_init(&imu_mpu_i2c.mpu, &(IMU_MPU60X0_I2C_DEV), IMU_MPU60X0_I2C_ADDR);
68  // change the default configuration
69  imu_mpu_i2c.mpu.config.smplrt_div = IMU_MPU60X0_SMPLRT_DIV;
70  imu_mpu_i2c.mpu.config.dlpf_cfg = IMU_MPU60X0_LOWPASS_FILTER;
73 }
74 
76 {
78 }
79 
81 {
82  uint32_t now_ts = get_sys_time_usec();
83 
84  // If the MPU60X0 I2C transaction has succeeded: convert the data
92  AbiSendMsgIMU_GYRO_INT32(IMU_MPU60X0_ID, now_ts, &imu.gyro);
93  AbiSendMsgIMU_ACCEL_INT32(IMU_MPU60X0_ID, now_ts, &imu.accel);
94  }
95 }
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
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
imu_mpu_i2c_event
void imu_mpu_i2c_event(void)
Definition: imu_mpu60x0_i2c.c:80
uint32_t
unsigned long uint32_t
Definition: types.h:18
Mpu60x0Config::accel_range
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:144
Mpu60x0Config::dlpf_cfg
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:141
IMU_MPU60X0_I2C_ADDR
#define IMU_MPU60X0_I2C_ADDR
Definition: imu_mpu60x0_i2c.c:60
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
imu.h
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_MPU60X0_ACCEL_RANGE
#define IMU_MPU60X0_ACCEL_RANGE
Definition: imu_mpu60x0_i2c.h:41
imu_scale_gyro
void imu_scale_gyro(struct Imu *_imu)
Definition: imu_vectornav.c:43
mpu60x0_i2c_event
void mpu60x0_i2c_event(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:82
IMU_MPU60X0_ID
#define IMU_MPU60X0_ID
Definition: abi_sender_ids.h:335
led.h
arch independent LED (Light Emitting Diodes) API
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")
Mpu60x0_I2c::data_accel
union Mpu60x0_I2c::@331 data_accel
ImuMpu60x0::mpu
struct Mpu60x0_I2c mpu
Definition: imu_mpu60x0_i2c.h:71
imu_mpu_i2c_periodic
void imu_mpu_i2c_periodic(void)
Definition: imu_mpu60x0_i2c.c:75
Mpu60x0_I2c::data_rates
union Mpu60x0_I2c::@332 data_rates
imu_mpu_i2c_init
void imu_mpu_i2c_init(void)
Definition: imu_mpu60x0_i2c.c:65
Mpu60x0_I2c::data_available
volatile bool data_available
data ready flag
Definition: mpu60x0_i2c.h:57
imu_mpu_i2c
struct ImuMpu60x0 imu_mpu_i2c
Definition: imu_mpu60x0_i2c.c:63
ImuMpu60x0
Definition: imu_mpu60x0_i2c.h:70
RATES_COPY
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:337
IMU_MPU60X0_GYRO_RANGE
#define IMU_MPU60X0_GYRO_RANGE
Definition: imu_mpu60x0_i2c.h:37
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