Paparazzi UAS  v5.15_devel-230-gc96ce27
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 "pprzlink/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 PRINT_CONFIG_VAR(KROOZ_GYRO_RANGE)
52 PRINT_CONFIG_VAR(KROOZ_ACCEL_RANGE)
53 
55 
56 #if IMU_KROOZ_USE_ACCEL_MEDIAN_FILTER
57 struct MedianFilter3Int median_accel;
58 #endif
60 
61 void imu_krooz_init(void)
62 {
64  // MPU-60X0
65  mpu60x0_i2c_init(&imu_krooz.mpu, &(IMU_KROOZ_I2C_DEV), MPU60X0_ADDR);
66  // change the default configuration
71  imu_krooz.mpu.config.drdy_int_enable = true;
72 
73  hmc58xx_init(&imu_krooz.hmc, &(IMU_KROOZ_I2C_DEV), HMC58XX_ADDR);
74 
75  // Init median filters
76 #if IMU_KROOZ_USE_ACCEL_MEDIAN_FILTER
78 #endif
80 
81  RATES_ASSIGN(imu_krooz.rates_sum, 0, 0, 0);
82  VECT3_ASSIGN(imu_krooz.accel_sum, 0, 0, 0);
83  imu_krooz.meas_nb = 0;
84 
85  imu_krooz.hmc_eoc = false;
86  imu_krooz.mpu_eoc = false;
87 
89 }
90 
92 {
93  // Start reading the latest gyroscope data
94  if (!imu_krooz.mpu.config.initialized) {
95  mpu60x0_i2c_start_configure(&imu_krooz.mpu);
96  }
97 
98  if (!imu_krooz.hmc.initialized) {
99  hmc58xx_start_configure(&imu_krooz.hmc);
100  }
101 
102  if (imu_krooz.meas_nb) {
103  RATES_ASSIGN(imu.gyro_unscaled, -imu_krooz.rates_sum.q / imu_krooz.meas_nb, imu_krooz.rates_sum.p / imu_krooz.meas_nb,
104  imu_krooz.rates_sum.r / imu_krooz.meas_nb);
105  VECT3_ASSIGN(imu.accel_unscaled, -imu_krooz.accel_sum.y / imu_krooz.meas_nb, imu_krooz.accel_sum.x / imu_krooz.meas_nb,
106  imu_krooz.accel_sum.z / imu_krooz.meas_nb);
107 
108 #if IMU_KROOZ_USE_ACCEL_MEDIAN_FILTER
110 #endif
115 
116  RATES_ASSIGN(imu_krooz.rates_sum, 0, 0, 0);
117  VECT3_ASSIGN(imu_krooz.accel_sum, 0, 0, 0);
118  imu_krooz.meas_nb = 0;
119 
120  uint32_t now_ts = get_sys_time_usec();
123  AbiSendMsgIMU_GYRO_INT32(IMU_BOARD_ID, now_ts, &imu.gyro);
124  AbiSendMsgIMU_ACCEL_INT32(IMU_BOARD_ID, now_ts, &imu.accel);
125  }
126 
127  //RunOnceEvery(10,imu_krooz_downlink_raw());
128 }
129 
131 {
132  DOWNLINK_SEND_IMU_GYRO_RAW(DefaultChannel, DefaultDevice, &imu.gyro_unscaled.p, &imu.gyro_unscaled.q,
133  &imu.gyro_unscaled.r);
134  DOWNLINK_SEND_IMU_ACCEL_RAW(DefaultChannel, DefaultDevice, &imu.accel_unscaled.x, &imu.accel_unscaled.y,
135  &imu.accel_unscaled.z);
136 }
137 
138 
139 void imu_krooz_event(void)
140 {
141  if (imu_krooz.mpu_eoc) {
142  mpu60x0_i2c_read(&imu_krooz.mpu);
143  imu_krooz.mpu_eoc = false;
144  }
145 
146  // If the MPU6050 I2C transaction has succeeded: convert the data
147  mpu60x0_i2c_event(&imu_krooz.mpu);
148  if (imu_krooz.mpu.data_available) {
149  RATES_ADD(imu_krooz.rates_sum, imu_krooz.mpu.data_rates.rates);
150  VECT3_ADD(imu_krooz.accel_sum, imu_krooz.mpu.data_accel.vect);
151  imu_krooz.meas_nb++;
152  imu_krooz.mpu.data_available = false;
153  }
154 
155  if (imu_krooz.hmc_eoc) {
156  hmc58xx_read(&imu_krooz.hmc);
157  imu_krooz.hmc_eoc = false;
158  }
159 
160  // If the HMC5883 I2C transaction has succeeded: convert the data
161  hmc58xx_event(&imu_krooz.hmc);
162  if (imu_krooz.hmc.data_available) {
163  VECT3_ASSIGN(imu.mag_unscaled, imu_krooz.hmc.data.vect.y, -imu_krooz.hmc.data.vect.x, imu_krooz.hmc.data.vect.z);
165  imu_krooz.hmc.data_available = false;
166  imu_scale_mag(&imu);
167  AbiSendMsgIMU_MAG_INT32(IMU_BOARD_ID, get_sys_time_usec(), &imu.mag);
168  }
169 }
struct Int32Vect3 accel_filtered
Definition: imu_krooz.h:88
#define KROOZ_ACCEL_RANGE
Definition: imu_krooz.h:45
bool initialized
config done flag
Definition: hmc58xx.h:63
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
#define VECT3_ADD(_a, _b)
Definition: pprz_algebra.h:147
volatile bool data_available
data ready flag
Definition: hmc58xx.h:65
void mpu60x0_i2c_event(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:82
volatile bool mpu_eoc
Definition: imu_krooz.h:81
#define RATES_ADD(_a, _b)
Definition: pprz_algebra.h:344
#define IMU_BOARD_ID
void hmc58xx_start_configure(struct Hmc58xx *hmc)
Definition: hmc58xx.c:126
#define KROOZ_GYRO_RANGE
Definition: imu_krooz.h:41
bool initialized
config done flag
Definition: mpu60x0.h:149
void mpu60x0_i2c_init(struct Mpu60x0_I2c *mpu, struct i2c_periph *i2c_p, uint8_t addr)
Definition: mpu60x0_i2c.c:31
struct Mpu60x0Config config
Definition: mpu60x0_i2c.h:68
volatile bool hmc_eoc
Definition: imu_krooz.h:82
#define IMU_KROOZ_ACCEL_AVG_FILTER
Definition: imu_krooz.h:77
#define KROOZ_SMPLRT_DIV
Definition: imu_krooz.c:46
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:140
void imu_krooz_sd_arch_init(void)
#define InitMedianFilterVect3Int(_f, _n)
Definition: median_filter.h:97
#define VECT3_ASSIGN(_a, _x, _y, _z)
Definition: pprz_algebra.h:125
#define RATES_ASSIGN(_ra, _p, _q, _r)
Definition: pprz_algebra.h:330
Main include for ABI (AirBorneInterface).
uint8_t smplrt_div
Sample rate divider.
Definition: mpu60x0.h:140
struct Int32Rates rates_sum
Definition: imu_krooz.h:85
struct Imu imu
global IMU state
Definition: imu.c:108
volatile uint8_t meas_nb
Definition: imu_krooz.h:87
enum Mpu60x0GyroRanges gyro_range
deg/s Range
Definition: mpu60x0.h:143
#define KROOZ_LOWPASS_FILTER
Definition: imu_krooz.c:45
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:144
bool drdy_int_enable
Enable Data Ready Interrupt.
Definition: mpu60x0.h:145
#define HMC58XX_ADDR
Definition: hmc58xx_regs.h:31
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:141
void mpu60x0_i2c_read(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:71
void imu_krooz_event(void)
Definition: imu_krooz.c:139
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:48
void imu_krooz_downlink_raw(void)
Definition: imu_krooz.c:130
int32_t r
in rad/s with INT32_RATE_FRAC
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:46
struct Int32Vect3 accel
accelerometer measurements in m/s^2 in BFP with INT32_ACCEL_FRAC
Definition: imu.h:39
void imu_krooz_periodic(void)
Definition: imu_krooz.c:91
void hmc58xx_read(struct Hmc58xx *hmc)
Definition: hmc58xx.c:139
unsigned long uint32_t
Definition: types.h:18
#define VECT3_SDIV(_vo, _vi, _s)
Definition: pprz_algebra.h:196
struct Int32Vect3 mag
magnetometer measurements scaled to 1 in BFP with INT32_MAG_FRAC
Definition: imu.h:40
union Mpu60x0_I2c::@330 data_rates
void imu_scale_gyro(struct Imu *_imu)
Definition: imu_vectornav.c:43
#define MPU60X0_ADDR
Definition: mpu60x0_regs.h:32
struct Hmc58xx hmc
Definition: imu_krooz.h:84
void imu_krooz_init(void)
Definition: imu_krooz.c:61
#define UpdateMedianFilterVect3Int(_f, _v)
union Mpu60x0_I2c::@329 data_accel
Driver for the IMU on the KroozSD board.
struct Int32Vect3 accel_sum
Definition: imu_krooz.h:86
#define VECT3_SMUL(_vo, _vi, _s)
Definition: pprz_algebra.h:189
struct ImuKrooz imu_krooz
Definition: imu_krooz.c:54
struct MedianFilter3Int median_mag
Definition: imu_krooz.c:59
void imu_scale_mag(struct Imu *_imu)
Definition: ahrs_gx3.c:351
struct Mpu60x0_I2c mpu
Definition: imu_krooz.h:83
#define MEDIAN_DEFAULT_SIZE
Definition: median_filter.h:28
void mpu60x0_i2c_start_configure(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:61
int32_t p
in rad/s with INT32_RATE_FRAC
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:47
union Hmc58xx::@315 data
void imu_scale_accel(struct Imu *_imu)
Definition: imu_vectornav.c:44
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
void hmc58xx_event(struct Hmc58xx *hmc)
Definition: hmc58xx.c:152
int32_t q
in rad/s with INT32_RATE_FRAC
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:38
volatile bool data_available
data ready flag
Definition: mpu60x0_i2c.h:57
Architecture independent I2C (Inter-Integrated Circuit Bus) API.