Paparazzi UAS  v5.15_devel-88-gb3ad7fe
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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
76  imu_krooz.mpu.config.drdy_int_enable = true;
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 
86  RATES_ASSIGN(imu_krooz.rates_sum, 0, 0, 0);
87  VECT3_ASSIGN(imu_krooz.accel_sum, 0, 0, 0);
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;
95  imu_krooz.ad7689_trans.cpol = SPICpolIdleLow;
96  imu_krooz.ad7689_trans.cpha = SPICphaEdge1;
97  imu_krooz.ad7689_trans.dss = SPIDss8bit;
98  imu_krooz.ad7689_trans.bitorder = SPIMSBFirst;
99  imu_krooz.ad7689_trans.cdiv = SPIDiv16;
100  imu_krooz.ad7689_trans.output_length = sizeof(imu_krooz.ad7689_spi_tx_buffer);
101  imu_krooz.ad7689_trans.output_buf = (uint8_t *) imu_krooz.ad7689_spi_tx_buffer;
102  imu_krooz.ad7689_trans.input_length = sizeof(imu_krooz.ad7689_spi_rx_buffer);
103  imu_krooz.ad7689_trans.input_buf = (uint8_t *) imu_krooz.ad7689_spi_rx_buffer;
104  imu_krooz.ad7689_trans.before_cb = NULL;
105  imu_krooz.ad7689_trans.after_cb = NULL;
106  axis_cnt = 0;
107  axis_nb = 2;
108 
110 }
111 
113 {
114  // Start reading the latest gyroscope data
115  if (!imu_krooz.mpu.config.initialized) {
116  mpu60x0_i2c_start_configure(&imu_krooz.mpu);
117  }
118 
119  if (!imu_krooz.hmc.initialized) {
120  hmc58xx_start_configure(&imu_krooz.hmc);
121  }
122 
123  uint32_t now_ts = get_sys_time_usec();
124 
125  if (imu_krooz.meas_nb) {
126  RATES_ASSIGN(imu.gyro_unscaled, -imu_krooz.rates_sum.q / imu_krooz.meas_nb,
127  imu_krooz.rates_sum.p / imu_krooz.meas_nb,
128  imu_krooz.rates_sum.r / 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 
136  if (imu_krooz.meas_nb_acc.x && imu_krooz.meas_nb_acc.y && imu_krooz.meas_nb_acc.z) {
137  imu.accel_unscaled.x = 65536 - imu_krooz.accel_sum.x / imu_krooz.meas_nb_acc.x;
138  imu.accel_unscaled.y = 65536 - imu_krooz.accel_sum.y / imu_krooz.meas_nb_acc.y;
139  imu.accel_unscaled.z = imu_krooz.accel_sum.z / imu_krooz.meas_nb_acc.z;
140 
141 #if IMU_KROOZ_USE_ACCEL_MEDIAN_FILTER
143 #endif
148 
149  INT_VECT3_ZERO(imu_krooz.accel_sum);
150  INT_VECT3_ZERO(imu_krooz.meas_nb_acc);
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) {
161  mpu60x0_i2c_read(&imu_krooz.mpu);
162  imu_krooz.mpu_eoc = false;
163  }
164 
165  // If the MPU6050 I2C transaction has succeeded: convert the data
166  mpu60x0_i2c_event(&imu_krooz.mpu);
167  if (imu_krooz.mpu.data_available) {
168  RATES_ADD(imu_krooz.rates_sum, imu_krooz.mpu.data_rates.rates);
169  imu_krooz.meas_nb++;
170  imu_krooz.mpu.data_available = false;
171  }
172 
173  if (SysTimeTimer(ad7689_event_timer) > 215) {
174  SysTimeTimerStart(ad7689_event_timer);
175  if (axis_cnt < axis_nb) {
176  axis_cnt++;
177  } else {
178  axis_cnt = 0;
179  }
180  imu_krooz.ad7689_trans.output_buf[0] =
181  axis_cnt <= 2 ? 0xF0 | (axis_cnt << 1) : (axis_cnt >= 4 ? 0xF0 | ((axis_cnt - 3) << 1) : 0xB0);
182  imu_krooz.ad7689_trans.output_buf[1] = 0x44;
183  spi_submit(&(IMU_KROOZ_SPI_DEV), &imu_krooz.ad7689_trans);
184  }
185  if (imu_krooz.ad7689_trans.status == SPITransSuccess) {
186  imu_krooz.ad7689_trans.status = SPITransDone;
187  uint16_t buf = (imu_krooz.ad7689_trans.input_buf[0] << 8) | imu_krooz.ad7689_trans.input_buf[1];
188  switch (axis_cnt) {
189  case 0:
190  case 3:
191  imu_krooz.accel_sum.x += (int32_t)buf;
192  imu_krooz.meas_nb_acc.x++;
193  break;
194  case 1:
195  case 4:
196  imu_krooz.accel_sum.y += (int32_t)buf;
197  imu_krooz.meas_nb_acc.y++;
198  break;
199  case 2:
200  imu_krooz.accel_sum.z += (int32_t)buf;
201  imu_krooz.meas_nb_acc.z++;
202  break;
203  case 5:
204  imu_krooz.temperature = (imu_krooz.temperature * 4 + (int32_t)buf) / 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) {
215  hmc58xx_read(&imu_krooz.hmc);
216  imu_krooz.hmc_eoc = false;
217  }
218 
219  // If the HMC5883 I2C transaction has succeeded: convert the data
220  hmc58xx_event(&imu_krooz.hmc);
221  if (imu_krooz.hmc.data_available) {
222  VECT3_ASSIGN(imu.mag_unscaled, imu_krooz.hmc.data.vect.y, -imu_krooz.hmc.data.vect.x, imu_krooz.hmc.data.vect.z);
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 }
struct Int32Vect3 accel_filtered
Definition: imu_krooz.h:88
unsigned short uint16_t
Definition: types.h:16
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:149
bool initialized
config done flag
Definition: hmc58xx.h:63
#define VECT3_ADD(_a, _b)
Definition: pprz_algebra.h:147
volatile bool data_available
data ready flag
Definition: hmc58xx.h:65
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:153
uint16_t output_length
number of data words to write
Definition: spi.h:146
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
static uint8_t axis_cnt
#define KROOZ_LOWPASS_FILTER
#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_ACCEL_RANGE
#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
struct spi_transaction ad7689_trans
Main include for ABI (AirBorneInterface).
uint8_t smplrt_div
Sample rate divider.
Definition: mpu60x0.h:140
Driver for the IMU on the KroozSD Big Rotorcraft Edition board.
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
Definition: spi_arch.c:505
struct Int32Rates rates_sum
Definition: imu_krooz.h:85
union Hmc58xx::@309 data
struct ImuKrooz imu_krooz
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:152
void imu_krooz_periodic(void)
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
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:144
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:144
bool drdy_int_enable
Enable Data Ready Interrupt.
Definition: mpu60x0.h:145
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:148
#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_init(void)
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:48
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:150
int32_t r
in rad/s with INT32_RATE_FRAC
volatile uint8_t ad7689_spi_rx_buffer[2]
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
static uint32_t ad7689_event_timer
Definition: spi.h:84
void hmc58xx_read(struct Hmc58xx *hmc)
Definition: hmc58xx.c:139
Architecture independent timing functions.
struct Uint8Vect3 meas_nb_acc
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:155
unsigned long uint32_t
Definition: types.h:18
#define KROOZ_SMPLRT_DIV
#define VECT3_SDIV(_vo, _vi, _s)
Definition: pprz_algebra.h:196
CPOL = 0.
Definition: spi.h:77
struct Int32Vect3 mag
magnetometer measurements scaled to 1 in BFP with INT32_MAG_FRAC
Definition: imu.h:40
void imu_scale_gyro(struct Imu *_imu)
Definition: imu_vectornav.c:43
int32_t temperature
Definition: imu_krooz.h:89
#define MPU60X0_ADDR
Definition: mpu60x0_regs.h:32
uint16_t input_length
number of data words to read
Definition: spi.h:145
signed long int32_t
Definition: types.h:19
#define SysTimeTimer(_t)
Definition: sys_time.h:219
struct Hmc58xx hmc
Definition: imu_krooz.h:84
#define UpdateMedianFilterVect3Int(_f, _v)
#define INT_VECT3_ZERO(_v)
union Mpu60x0_I2c::@323 data_rates
struct Int32Vect3 accel_sum
Definition: imu_krooz.h:86
#define VECT3_SMUL(_vo, _vi, _s)
Definition: pprz_algebra.h:189
unsigned char uint8_t
Definition: types.h:14
void imu_scale_mag(struct Imu *_imu)
Definition: ahrs_gx3.c:351
CPHA = 0.
Definition: spi.h:68
struct Mpu60x0_I2c mpu
Definition: imu_krooz.h:83
Definition: spi.h:117
slave is selected before transaction and unselected after
Definition: spi.h:57
static uint8_t axis_nb
#define MEDIAN_DEFAULT_SIZE
Definition: median_filter.h:28
void mpu60x0_i2c_start_configure(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:61
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:151
struct MedianFilter3Int median_mag
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:147
volatile uint8_t ad7689_spi_tx_buffer[2]
int32_t p
in rad/s with INT32_RATE_FRAC
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:143
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
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:154
void imu_scale_accel(struct Imu *_imu)
Definition: imu_vectornav.c:44
void imu_krooz_event(void)
#define SysTimeTimerStart(_t)
Definition: sys_time.h:218
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
enum SPITransactionStatus status
Definition: spi.h:156
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.