Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
imu_krooz_memsic.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 
32 #include <math.h>
35 #include "mcu_periph/i2c.h"
36 #include "led.h"
37 #include "filters/median_filter.h"
38 #include "mcu_periph/sys_time.h"
39 #include "subsystems/abi.h"
40 
41 #if !defined KROOZ_LOWPASS_FILTER && !defined KROOZ_SMPLRT_DIV
42 #define KROOZ_LOWPASS_FILTER MPU60X0_DLPF_256HZ
43 #define KROOZ_SMPLRT_DIV 1
44 #endif
45 PRINT_CONFIG_VAR(KROOZ_SMPLRT_DIV)
46 PRINT_CONFIG_VAR(KROOZ_LOWPASS_FILTER)
47 
48 PRINT_CONFIG_VAR(KROOZ_GYRO_RANGE)
49 
50 #ifndef KROOZ_ACCEL_RANGE
51 #define KROOZ_ACCEL_RANGE MPU60X0_ACCEL_RANGE_2G
52 #endif
53 PRINT_CONFIG_VAR(KROOZ_ACCEL_RANGE)
54 
56 
57 #if IMU_KROOZ_USE_ACCEL_MEDIAN_FILTER
58 struct MedianFilter3Int median_accel;
59 #endif
61 
65 
66 void imu_krooz_init(void)
67 {
69  // MPU-60X0
70  mpu60x0_i2c_init(&imu_krooz.mpu, &(IMU_KROOZ_I2C_DEV), MPU60X0_ADDR);
71  // change the default configuration
77 
78  hmc58xx_init(&imu_krooz.hmc, &(IMU_KROOZ_I2C_DEV), HMC58XX_ADDR);
79 
80  // Init median filters
81 #if IMU_KROOZ_USE_ACCEL_MEDIAN_FILTER
83 #endif
85 
88  imu_krooz.meas_nb = 0;
89 
90  imu_krooz.hmc_eoc = false;
91  imu_krooz.mpu_eoc = false;
92 
93  imu_krooz.ad7689_trans.slave_idx = IMU_KROOZ_SPI_SLAVE_IDX;
106  axis_cnt = 0;
107  axis_nb = 2;
108 
110 }
111 
113 {
114  // Start reading the latest gyroscope data
117  }
118 
119  if (!imu_krooz.hmc.initialized) {
121  }
122 
123  uint32_t now_ts = get_sys_time_usec();
124 
125  if (imu_krooz.meas_nb) {
129 
130  RATES_ASSIGN(imu_krooz.rates_sum, 0, 0, 0);
131  imu_krooz.meas_nb = 0;
133  AbiSendMsgIMU_GYRO_INT32(IMU_BOARD_ID, now_ts, &imu.gyro);
134  }
135 
140 
141 #if IMU_KROOZ_USE_ACCEL_MEDIAN_FILTER
143 #endif
148 
152  AbiSendMsgIMU_ACCEL_INT32(IMU_BOARD_ID, now_ts, &imu.accel);
153  }
154 
155  RunOnceEvery(128, {axis_nb = 5;});
156 }
157 
158 void imu_krooz_event(void)
159 {
160  if (imu_krooz.mpu_eoc) {
162  imu_krooz.mpu_eoc = false;
163  }
164 
165  // If the MPU6050 I2C transaction has succeeded: convert the data
169  imu_krooz.meas_nb++;
170  imu_krooz.mpu.data_available = false;
171  }
172 
173  if (SysTimeTimer(ad7689_event_timer) > 215) {
175  if (axis_cnt < axis_nb) {
176  axis_cnt++;
177  } else {
178  axis_cnt = 0;
179  }
181  axis_cnt <= 2 ? 0xF0 | (axis_cnt << 1) : (axis_cnt >= 4 ? 0xF0 | ((axis_cnt - 3) << 1) : 0xB0);
183  spi_submit(&(IMU_KROOZ_SPI_DEV), &imu_krooz.ad7689_trans);
184  }
188  switch (axis_cnt) {
189  case 0:
190  case 3:
191  imu_krooz.accel_sum.x += (int32_t)buf;
193  break;
194  case 1:
195  case 4:
196  imu_krooz.accel_sum.y += (int32_t)buf;
198  break;
199  case 2:
200  imu_krooz.accel_sum.z += (int32_t)buf;
202  break;
203  case 5:
205  //imu.temperature = 33000 * imu_krooz.temp / 65536 - 2400;
206  axis_nb = 2;
207  break;
208  default:
209  axis_cnt = 0;
210  break;
211  }
212  }
213 
214  if (imu_krooz.hmc_eoc) {
216  imu_krooz.hmc_eoc = false;
217  }
218 
219  // If the HMC5883 I2C transaction has succeeded: convert the data
224  imu_krooz.hmc.data_available = false;
225  imu_scale_mag(&imu);
226  AbiSendMsgIMU_MAG_INT32(IMU_BOARD_ID, get_sys_time_usec(), &imu.mag);
227  }
228 }
Imu::gyro_unscaled
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:46
imu_krooz_periodic
void imu_krooz_periodic(void)
Definition: imu_krooz_memsic.c:112
uint16_t
unsigned short uint16_t
Definition: types.h:16
spi_transaction::cdiv
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:159
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
ImuKrooz::ad7689_spi_tx_buffer
volatile uint8_t ad7689_spi_tx_buffer[2]
Definition: imu_krooz_memsic.h:92
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
spi_transaction::cpol
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:155
InitMedianFilterVect3Int
#define InitMedianFilterVect3Int(_f, _n)
Definition: median_filter.h:97
VECT3_SMUL
#define VECT3_SMUL(_vo, _vi, _s)
Definition: pprz_algebra.h:189
imu_krooz_init
void imu_krooz_init(void)
Definition: imu_krooz_memsic.c:66
ImuKrooz::meas_nb_acc
struct Uint8Vect3 meas_nb_acc
Definition: imu_krooz_memsic.h:98
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
spi_transaction::output_length
uint16_t output_length
number of data words to write
Definition: spi.h:152
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
ImuKrooz::temperature
int32_t temperature
Definition: imu_krooz.h:89
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
SPISelectUnselect
@ SPISelectUnselect
slave is selected before transaction and unselected after
Definition: spi.h:63
SysTimeTimer
#define SysTimeTimer(_t)
Definition: sys_time.h:219
SPITransSuccess
@ SPITransSuccess
Definition: spi.h:99
spi_transaction::bitorder
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:158
Uint8Vect3::z
uint8_t z
Definition: pprz_algebra_int.h:47
Imu::accel_unscaled
struct Int32Vect3 accel_unscaled
unscaled accelerometer measurements
Definition: imu.h:47
spi_transaction::output_buf
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:150
spi_transaction::select
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:154
axis_cnt
static uint8_t axis_cnt
Definition: imu_krooz_memsic.c:63
get_sys_time_usec
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
KROOZ_LOWPASS_FILTER
#define KROOZ_LOWPASS_FILTER
Definition: imu_krooz_memsic.c:42
SPICphaEdge1
@ SPICphaEdge1
CPHA = 0.
Definition: spi.h:74
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
SPIMSBFirst
@ SPIMSBFirst
Definition: spi.h:112
Uint8Vect3::y
uint8_t y
Definition: pprz_algebra_int.h:46
SPICpolIdleLow
@ SPICpolIdleLow
CPOL = 0.
Definition: spi.h:83
Uint8Vect3::x
uint8_t x
Definition: pprz_algebra_int.h:45
KROOZ_ACCEL_RANGE
#define KROOZ_ACCEL_RANGE
Definition: imu_krooz_memsic.c:51
spi_transaction::cpha
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:156
spi_transaction::after_cb
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:161
spi_submit
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
Definition: spi_arch.c:511
ImuKrooz::mpu_eoc
volatile bool mpu_eoc
Definition: imu_krooz.h:81
ImuKrooz::ad7689_trans
struct spi_transaction ad7689_trans
Definition: imu_krooz_memsic.h:91
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
axis_nb
static uint8_t axis_nb
Definition: imu_krooz_memsic.c:64
sys_time.h
Architecture independent timing functions.
imu_krooz
struct ImuKrooz imu_krooz
Definition: imu_krooz_memsic.c:55
median_mag
struct MedianFilter3Int median_mag
Definition: imu_krooz_memsic.c:60
imu_scale_gyro
void imu_scale_gyro(struct Imu *_imu)
Definition: imu_vectornav.c:43
uint8_t
unsigned char uint8_t
Definition: types.h:14
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
imu_krooz_event
void imu_krooz_event(void)
Definition: imu_krooz_memsic.c:158
hmc58xx_read
void hmc58xx_read(struct Hmc58xx *hmc)
Definition: hmc58xx.c:139
led.h
arch independent LED (Light Emitting Diodes) API
median_filter.h
SPIDss8bit
@ SPIDss8bit
Definition: spi.h:90
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
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
ImuKrooz::ad7689_spi_rx_buffer
volatile uint8_t ad7689_spi_rx_buffer[2]
Definition: imu_krooz_memsic.h:93
MEDIAN_DEFAULT_SIZE
#define MEDIAN_DEFAULT_SIZE
Definition: median_filter.h:28
ad7689_event_timer
static uint32_t ad7689_event_timer
Definition: imu_krooz_memsic.c:62
spi_transaction::input_length
uint16_t input_length
number of data words to read
Definition: spi.h:151
spi_transaction::dss
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:157
spi_transaction::slave_idx
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:153
int32_t
signed long int32_t
Definition: types.h:19
SPITransDone
@ SPITransDone
Definition: spi.h:101
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
spi_transaction::input_buf
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:149
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
Mpu60x0_I2c::data_available
volatile bool data_available
data ready flag
Definition: mpu60x0_i2c.h:57
INT_VECT3_ZERO
#define INT_VECT3_ZERO(_v)
Definition: pprz_algebra_int.h:287
spi_transaction::before_cb
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:160
VECT3_ASSIGN
#define VECT3_ASSIGN(_a, _x, _y, _z)
Definition: pprz_algebra.h:125
KROOZ_SMPLRT_DIV
#define KROOZ_SMPLRT_DIV
Definition: imu_krooz_memsic.c:43
Hmc58xx::data_available
volatile bool data_available
data ready flag
Definition: hmc58xx.h:65
imu_krooz_memsic.h
SPIDiv16
@ SPIDiv16
Definition: spi.h:123
i2c.h
SysTimeTimerStart
#define SysTimeTimerStart(_t)
Definition: sys_time.h:218
spi_transaction::status
enum SPITransactionStatus status
Definition: spi.h:162
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