Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
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
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 
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
96  }
97 
98  if (!imu_krooz.hmc.initialized) {
100  }
101 
102  if (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) {
143  imu_krooz.mpu_eoc = false;
144  }
145 
146  // If the MPU6050 I2C transaction has succeeded: convert the data
151  imu_krooz.meas_nb++;
152  imu_krooz.mpu.data_available = false;
153  }
154 
155  if (imu_krooz.hmc_eoc) {
157  imu_krooz.hmc_eoc = false;
158  }
159 
160  // If the HMC5883 I2C transaction has succeeded: convert the data
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 }
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
ImuKrooz::accel_sum
struct Int32Vect3 accel_sum
Definition: imu_krooz.h:86
hmc58xx_start_configure
void hmc58xx_start_configure(struct Hmc58xx *hmc)
Definition: hmc58xx.c:126
hmc58xx_event
void hmc58xx_event(struct Hmc58xx *hmc)
Definition: hmc58xx.c:152
Hmc58xx::data
union Hmc58xx::@317 data
imu_krooz_periodic
void imu_krooz_periodic(void)
Definition: imu_krooz.c:91
InitMedianFilterVect3Int
#define InitMedianFilterVect3Int(_f, _n)
Definition: median_filter.h:97
VECT3_SMUL
#define VECT3_SMUL(_vo, _vi, _s)
Definition: pprz_algebra.h:189
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
Mpu60x0Config::drdy_int_enable
bool drdy_int_enable
Enable Data Ready Interrupt.
Definition: mpu60x0.h:145
Int32Rates::q
int32_t q
in rad/s with INT32_RATE_FRAC
Definition: pprz_algebra_int.h:181
imu_krooz_sd_arch.h
IMU_KROOZ_ACCEL_AVG_FILTER
#define IMU_KROOZ_ACCEL_AVG_FILTER
Definition: imu_krooz.h:77
Int32Vect3::z
int32_t z
Definition: pprz_algebra_int.h:91
VECT3_SDIV
#define VECT3_SDIV(_vo, _vi, _s)
Definition: pprz_algebra.h:196
uint32_t
unsigned long uint32_t
Definition: types.h:18
ImuKrooz
Definition: imu_krooz.h:80
mpu60x0_i2c_start_configure
void mpu60x0_i2c_start_configure(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:61
imu_krooz_sd_arch_init
void imu_krooz_sd_arch_init(void)
Definition: imu_krooz_sd_arch.c:10
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
KROOZ_GYRO_RANGE
#define KROOZ_GYRO_RANGE
Definition: imu_krooz.h:41
ImuKrooz::hmc
struct Hmc58xx hmc
Definition: imu_krooz.h:84
VECT3_ADD
#define VECT3_ADD(_a, _b)
Definition: pprz_algebra.h:147
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
Int32Rates::p
int32_t p
in rad/s with INT32_RATE_FRAC
Definition: pprz_algebra_int.h:180
HMC58XX_ADDR
#define HMC58XX_ADDR
Definition: hmc58xx_regs.h:31
MedianFilter3Int
Definition: median_filter.h:93
RATES_ASSIGN
#define RATES_ASSIGN(_ra, _p, _q, _r)
Definition: pprz_algebra.h:330
uart.h
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
KROOZ_ACCEL_RANGE
#define KROOZ_ACCEL_RANGE
Definition: imu_krooz.h:45
ImuKrooz::mpu_eoc
volatile bool mpu_eoc
Definition: imu_krooz.h:81
KROOZ_SMPLRT_DIV
#define KROOZ_SMPLRT_DIV
Definition: imu_krooz.c:46
mpu60x0_i2c_init
void mpu60x0_i2c_init(struct Mpu60x0_I2c *mpu, struct i2c_periph *i2c_p, uint8_t addr)
Definition: mpu60x0_i2c.c:31
ImuKrooz::mpu
struct Mpu60x0_I2c mpu
Definition: imu_krooz.h:83
imu_krooz
struct ImuKrooz imu_krooz
Definition: imu_krooz.c:54
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
median_mag
struct MedianFilter3Int median_mag
Definition: imu_krooz.c:59
hmc58xx_read
void hmc58xx_read(struct Hmc58xx *hmc)
Definition: hmc58xx.c:139
led.h
arch independent LED (Light Emitting Diodes) API
median_filter.h
Mpu60x0_I2c::config
struct Mpu60x0Config config
Definition: mpu60x0_i2c.h:68
mpu60x0_i2c_read
void mpu60x0_i2c_read(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:71
RATES_ADD
#define RATES_ADD(_a, _b)
Definition: pprz_algebra.h:344
imu
struct Imu imu
global IMU state
Definition: imu.c:108
imu_krooz_event
void imu_krooz_event(void)
Definition: imu_krooz.c:139
ImuKrooz::meas_nb
volatile uint8_t meas_nb
Definition: imu_krooz.h:87
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
imu_krooz_init
void imu_krooz_init(void)
Definition: imu_krooz.c:61
MEDIAN_DEFAULT_SIZE
#define MEDIAN_DEFAULT_SIZE
Definition: median_filter.h:28
imu_krooz_downlink_raw
void imu_krooz_downlink_raw(void)
Definition: imu_krooz.c:130
Mpu60x0_I2c::data_accel
union Mpu60x0_I2c::@331 data_accel
ImuKrooz::accel_filtered
struct Int32Vect3 accel_filtered
Definition: imu_krooz.h:88
imu_scale_mag
void imu_scale_mag(struct Imu *_imu)
Definition: ahrs_gx3.c:352
KROOZ_LOWPASS_FILTER
#define KROOZ_LOWPASS_FILTER
Definition: imu_krooz.c:45
MPU60X0_ADDR
#define MPU60X0_ADDR
Definition: mpu60x0_regs.h:32
Mpu60x0_I2c::data_rates
union Mpu60x0_I2c::@332 data_rates
Int32Vect3::x
int32_t x
Definition: pprz_algebra_int.h:89
ImuKrooz::hmc_eoc
volatile bool hmc_eoc
Definition: imu_krooz.h:82
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
imu_krooz.h
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
Hmc58xx::data_available
volatile bool data_available
data ready flag
Definition: hmc58xx.h:65
i2c.h
Int32Rates::r
int32_t r
in rad/s with INT32_RATE_FRAC
Definition: pprz_algebra_int.h:182
imu_scale_accel
void imu_scale_accel(struct Imu *_imu)
Definition: imu_vectornav.c:44
Mpu60x0Config::initialized
bool initialized
config done flag
Definition: mpu60x0.h:149
VECT3_COPY
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:140
UpdateMedianFilterVect3Int
#define UpdateMedianFilterVect3Int(_f, _v)
Definition: median_filter.h:106
Imu::gyro
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:38
Hmc58xx::initialized
bool initialized
config done flag
Definition: hmc58xx.h:63
ImuKrooz::rates_sum
struct Int32Rates rates_sum
Definition: imu_krooz.h:85