Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
imu_krooz.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Sergey Krukowski <softsr@yahoo.de>
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 
31 #include <math.h>
32 #include "boards/krooz/imu_krooz.h"
34 #include "mcu_periph/i2c.h"
35 #include "led.h"
36 #include "filters/median_filter.h"
37 #include "subsystems/abi.h"
38 
39 // Downlink
40 #include "mcu_periph/uart.h"
41 #include "messages.h"
43 
44 #if !defined KROOZ_LOWPASS_FILTER && !defined KROOZ_SMPLRT_DIV
45 #define KROOZ_LOWPASS_FILTER MPU60X0_DLPF_256HZ
46 #define KROOZ_SMPLRT_DIV 1
47 #endif
48 PRINT_CONFIG_VAR(KROOZ_SMPLRT_DIV)
49 PRINT_CONFIG_VAR(KROOZ_LOWPASS_FILTER)
50 
51 #ifndef KROOZ_GYRO_RANGE
52 #define KROOZ_GYRO_RANGE MPU60X0_GYRO_RANGE_250
53 #endif
54 PRINT_CONFIG_VAR(KROOZ_GYRO_RANGE)
55 
56 #ifndef KROOZ_ACCEL_RANGE
57 #define KROOZ_ACCEL_RANGE MPU60X0_ACCEL_RANGE_2G
58 #endif
59 PRINT_CONFIG_VAR(KROOZ_ACCEL_RANGE)
60 
62 
63 #if IMU_KROOZ_USE_ACCEL_MEDIAN_FILTER
64 struct MedianFilter3Int median_accel;
65 #endif
67 
68 void imu_impl_init(void)
69 {
71  // MPU-60X0
72  mpu60x0_i2c_init(&imu_krooz.mpu, &(IMU_KROOZ_I2C_DEV), MPU60X0_ADDR);
73  // change the default configuration
78  imu_krooz.mpu.config.drdy_int_enable = TRUE;
79 
80  hmc58xx_init(&imu_krooz.hmc, &(IMU_KROOZ_I2C_DEV), HMC58XX_ADDR);
81 
82  // Init median filters
83 #if IMU_KROOZ_USE_ACCEL_MEDIAN_FILTER
84  InitMedianFilterVect3Int(median_accel);
85 #endif
87 
88  RATES_ASSIGN(imu_krooz.rates_sum, 0, 0, 0);
89  VECT3_ASSIGN(imu_krooz.accel_sum, 0, 0, 0);
90  imu_krooz.meas_nb = 0;
91 
92  imu_krooz.hmc_eoc = FALSE;
93  imu_krooz.mpu_eoc = FALSE;
94 
96 }
97 
98 void imu_periodic(void)
99 {
100  // Start reading the latest gyroscope data
101  if (!imu_krooz.mpu.config.initialized) {
102  mpu60x0_i2c_start_configure(&imu_krooz.mpu);
103  }
104 
105  if (!imu_krooz.hmc.initialized) {
106  hmc58xx_start_configure(&imu_krooz.hmc);
107  }
108 
109  if (imu_krooz.meas_nb) {
110  RATES_ASSIGN(imu.gyro_unscaled, -imu_krooz.rates_sum.q / imu_krooz.meas_nb, imu_krooz.rates_sum.p / imu_krooz.meas_nb,
111  imu_krooz.rates_sum.r / imu_krooz.meas_nb);
112  VECT3_ASSIGN(imu.accel_unscaled, -imu_krooz.accel_sum.y / imu_krooz.meas_nb, imu_krooz.accel_sum.x / imu_krooz.meas_nb,
113  imu_krooz.accel_sum.z / imu_krooz.meas_nb);
114 
115 #if IMU_KROOZ_USE_ACCEL_MEDIAN_FILTER
117 #endif
122 
123  RATES_ASSIGN(imu_krooz.rates_sum, 0, 0, 0);
124  VECT3_ASSIGN(imu_krooz.accel_sum, 0, 0, 0);
125  imu_krooz.meas_nb = 0;
126 
127  uint32_t now_ts = get_sys_time_usec();
130  AbiSendMsgIMU_GYRO_INT32(IMU_BOARD_ID, now_ts, &imu.gyro);
131  AbiSendMsgIMU_ACCEL_INT32(IMU_BOARD_ID, now_ts, &imu.accel);
132  }
133 
134  //RunOnceEvery(10,imu_krooz_downlink_raw());
135 }
136 
138 {
139  DOWNLINK_SEND_IMU_GYRO_RAW(DefaultChannel, DefaultDevice, &imu.gyro_unscaled.p, &imu.gyro_unscaled.q,
140  &imu.gyro_unscaled.r);
141  DOWNLINK_SEND_IMU_ACCEL_RAW(DefaultChannel, DefaultDevice, &imu.accel_unscaled.x, &imu.accel_unscaled.y,
142  &imu.accel_unscaled.z);
143 }
144 
145 
146 void imu_krooz_event(void)
147 {
148  if (imu_krooz.mpu_eoc) {
149  mpu60x0_i2c_read(&imu_krooz.mpu);
150  imu_krooz.mpu_eoc = FALSE;
151  }
152 
153  // If the MPU6050 I2C transaction has succeeded: convert the data
154  mpu60x0_i2c_event(&imu_krooz.mpu);
155  if (imu_krooz.mpu.data_available) {
156  RATES_ADD(imu_krooz.rates_sum, imu_krooz.mpu.data_rates.rates);
157  VECT3_ADD(imu_krooz.accel_sum, imu_krooz.mpu.data_accel.vect);
158  imu_krooz.meas_nb++;
159  imu_krooz.mpu.data_available = FALSE;
160  }
161 
162  if (imu_krooz.hmc_eoc) {
163  hmc58xx_read(&imu_krooz.hmc);
164  imu_krooz.hmc_eoc = FALSE;
165  }
166 
167  // If the HMC5883 I2C transaction has succeeded: convert the data
168  hmc58xx_event(&imu_krooz.hmc);
169  if (imu_krooz.hmc.data_available) {
170  VECT3_ASSIGN(imu.mag_unscaled, imu_krooz.hmc.data.vect.y, -imu_krooz.hmc.data.vect.x, imu_krooz.hmc.data.vect.z);
172  imu_krooz.hmc.data_available = FALSE;
173  imu_scale_mag(&imu);
174  AbiSendMsgIMU_MAG_INT32(IMU_BOARD_ID, get_sys_time_usec(), &imu.mag);
175  }
176 }
struct Int32Vect3 accel_filtered
Definition: imu_krooz.h:115
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
#define VECT3_ADD(_a, _b)
Definition: pprz_algebra.h:146
void mpu60x0_i2c_event(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:82
#define RATES_ADD(_a, _b)
Definition: pprz_algebra.h:343
void imu_scale_gyro(struct Imu *_imu)
Definition: ahrs_gx3.c:349
#define IMU_BOARD_ID
void hmc58xx_start_configure(struct Hmc58xx *hmc)
Definition: hmc58xx.c:126
void mpu60x0_i2c_init(struct Mpu60x0_I2c *mpu, struct i2c_periph *i2c_p, uint8_t addr)
Definition: mpu60x0_i2c.c:31
void imu_scale_accel(struct Imu *_imu)
Definition: ahrs_gx3.c:350
struct Mpu60x0Config config
Definition: mpu60x0_i2c.h:68
union Mpu60x0_I2c::@41 data_accel
#define IMU_KROOZ_ACCEL_AVG_FILTER
Definition: imu_krooz.h:104
#define KROOZ_SMPLRT_DIV
Definition: imu_krooz.c:46
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:139
void imu_krooz_sd_arch_init(void)
#define VECT3_ASSIGN(_a, _x, _y, _z)
Definition: pprz_algebra.h:124
#define RATES_ASSIGN(_ra, _p, _q, _r)
Definition: pprz_algebra.h:329
Main include for ABI (AirBorneInterface).
uint8_t smplrt_div
Sample rate divider.
Definition: mpu60x0.h:74
#define InitMedianFilterVect3Int(_f)
Definition: median_filter.h:88
struct Int32Rates rates_sum
Definition: imu_krooz.h:112
volatile uint8_t meas_nb
Definition: imu_krooz.h:114
enum Mpu60x0GyroRanges gyro_range
deg/s Range
Definition: mpu60x0.h:76
#define KROOZ_LOWPASS_FILTER
Definition: imu_krooz.c:45
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:77
union Mpu60x0_I2c::@42 data_rates
#define HMC58XX_ADDR
Definition: hmc58xx_regs.h:31
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:75
void mpu60x0_i2c_read(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:71
#define FALSE
Definition: std.h:5
void imu_krooz_event(void)
Definition: imu_krooz.c:146
static uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.h:39
bool_t initialized
config done flag
Definition: hmc58xx.h:63
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:52
void imu_krooz_downlink_raw(void)
Definition: imu_krooz.c:137
int32_t r
in rad/s with INT32_RATE_FRAC
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:50
#define TRUE
Definition: std.h:4
struct Int32Vect3 accel
accelerometer measurements in m/s^2 in BFP with INT32_ACCEL_FRAC
Definition: imu.h:43
void hmc58xx_read(struct Hmc58xx *hmc)
Definition: hmc58xx.c:139
struct Imu imu
global IMU state
Definition: imu_aspirin2.c:43
unsigned long uint32_t
Definition: types.h:18
#define KROOZ_GYRO_RANGE
Definition: imu_krooz.c:52
#define VECT3_SDIV(_vo, _vi, _s)
Definition: pprz_algebra.h:195
void imu_impl_init(void)
must be defined by underlying hardware
Definition: imu_krooz.c:68
bool_t initialized
config done flag
Definition: mpu60x0.h:82
struct Int32Vect3 mag
magnetometer measurements scaled to 1 in BFP with INT32_MAG_FRAC
Definition: imu.h:44
#define KROOZ_ACCEL_RANGE
Definition: imu_krooz.c:57
volatile bool_t data_available
data ready flag
Definition: hmc58xx.h:65
#define MPU60X0_ADDR
Definition: mpu60x0_regs.h:32
struct Hmc58xx hmc
Definition: imu_krooz.h:111
#define UpdateMedianFilterVect3Int(_f, _v)
Definition: median_filter.h:97
union Hmc58xx::@33 data
Driver for the IMU on the KroozSD board.
struct Int32Vect3 accel_sum
Definition: imu_krooz.h:113
void imu_periodic(void)
optional.
Definition: imu_krooz.c:98
#define VECT3_SMUL(_vo, _vi, _s)
Definition: pprz_algebra.h:188
struct ImuKrooz imu_krooz
Definition: imu_krooz.c:61
struct MedianFilter3Int median_mag
Definition: imu_krooz.c:66
void imu_scale_mag(struct Imu *_imu)
Definition: ahrs_gx3.c:351
struct Mpu60x0_I2c mpu
Definition: imu_krooz.h:110
void mpu60x0_i2c_start_configure(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:61
int32_t p
in rad/s with INT32_RATE_FRAC
volatile bool_t hmc_eoc
Definition: imu_krooz.h:109
arch independent LED (Light Emitting Diodes) API
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
struct Int32Vect3 accel_unscaled
unscaled accelerometer measurements
Definition: imu.h:51
void hmc58xx_event(struct Hmc58xx *hmc)
Definition: hmc58xx.c:152
int32_t q
in rad/s with INT32_RATE_FRAC
volatile bool_t data_available
data ready flag
Definition: mpu60x0_i2c.h:57
volatile bool_t mpu_eoc
Definition: imu_krooz.h:108
bool_t drdy_int_enable
Enable Data Ready Interrupt.
Definition: mpu60x0.h:78
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:42
Architecture independent I2C (Inter-Integrated Circuit Bus) API.