Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
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 #ifndef KROOZ_GYRO_RANGE
49 #define KROOZ_GYRO_RANGE MPU60X0_GYRO_RANGE_250
50 #endif
51 PRINT_CONFIG_VAR(KROOZ_GYRO_RANGE)
52 
53 #ifndef KROOZ_ACCEL_RANGE
54 #define KROOZ_ACCEL_RANGE MPU60X0_ACCEL_RANGE_2G
55 #endif
56 PRINT_CONFIG_VAR(KROOZ_ACCEL_RANGE)
57 
59 
60 #if IMU_KROOZ_USE_ACCEL_MEDIAN_FILTER
61 struct MedianFilter3Int median_accel;
62 #endif
64 
68 
69 void imu_krooz_init(void)
70 {
72  // MPU-60X0
73  mpu60x0_i2c_init(&imu_krooz.mpu, &(IMU_KROOZ_I2C_DEV), MPU60X0_ADDR);
74  // change the default configuration
79  imu_krooz.mpu.config.drdy_int_enable = true;
80 
81  hmc58xx_init(&imu_krooz.hmc, &(IMU_KROOZ_I2C_DEV), HMC58XX_ADDR);
82 
83  // Init median filters
84 #if IMU_KROOZ_USE_ACCEL_MEDIAN_FILTER
85  InitMedianFilterVect3Int(median_accel);
86 #endif
88 
89  RATES_ASSIGN(imu_krooz.rates_sum, 0, 0, 0);
90  VECT3_ASSIGN(imu_krooz.accel_sum, 0, 0, 0);
91  imu_krooz.meas_nb = 0;
92 
93  imu_krooz.hmc_eoc = false;
94  imu_krooz.mpu_eoc = false;
95 
96  imu_krooz.ad7689_trans.slave_idx = IMU_KROOZ_SPI_SLAVE_IDX;
98  imu_krooz.ad7689_trans.cpol = SPICpolIdleLow;
99  imu_krooz.ad7689_trans.cpha = SPICphaEdge1;
100  imu_krooz.ad7689_trans.dss = SPIDss8bit;
101  imu_krooz.ad7689_trans.bitorder = SPIMSBFirst;
102  imu_krooz.ad7689_trans.cdiv = SPIDiv16;
103  imu_krooz.ad7689_trans.output_length = sizeof(imu_krooz.ad7689_spi_tx_buffer);
104  imu_krooz.ad7689_trans.output_buf = (uint8_t *) imu_krooz.ad7689_spi_tx_buffer;
105  imu_krooz.ad7689_trans.input_length = sizeof(imu_krooz.ad7689_spi_rx_buffer);
106  imu_krooz.ad7689_trans.input_buf = (uint8_t *) imu_krooz.ad7689_spi_rx_buffer;
107  imu_krooz.ad7689_trans.before_cb = NULL;
108  imu_krooz.ad7689_trans.after_cb = NULL;
109  axis_cnt = 0;
110  axis_nb = 2;
111 
113 }
114 
116 {
117  // Start reading the latest gyroscope data
118  if (!imu_krooz.mpu.config.initialized) {
119  mpu60x0_i2c_start_configure(&imu_krooz.mpu);
120  }
121 
122  if (!imu_krooz.hmc.initialized) {
123  hmc58xx_start_configure(&imu_krooz.hmc);
124  }
125 
126  uint32_t now_ts = get_sys_time_usec();
127 
128  if (imu_krooz.meas_nb) {
129  RATES_ASSIGN(imu.gyro_unscaled, -imu_krooz.rates_sum.q / imu_krooz.meas_nb,
130  imu_krooz.rates_sum.p / imu_krooz.meas_nb,
131  imu_krooz.rates_sum.r / imu_krooz.meas_nb);
132 
133  RATES_ASSIGN(imu_krooz.rates_sum, 0, 0, 0);
134  imu_krooz.meas_nb = 0;
136  AbiSendMsgIMU_GYRO_INT32(IMU_BOARD_ID, now_ts, &imu.gyro);
137  }
138 
139  if (imu_krooz.meas_nb_acc.x && imu_krooz.meas_nb_acc.y && imu_krooz.meas_nb_acc.z) {
140  imu.accel_unscaled.x = 65536 - imu_krooz.accel_sum.x / imu_krooz.meas_nb_acc.x;
141  imu.accel_unscaled.y = 65536 - imu_krooz.accel_sum.y / imu_krooz.meas_nb_acc.y;
142  imu.accel_unscaled.z = imu_krooz.accel_sum.z / imu_krooz.meas_nb_acc.z;
143 
144 #if IMU_KROOZ_USE_ACCEL_MEDIAN_FILTER
146 #endif
151 
152  INT_VECT3_ZERO(imu_krooz.accel_sum);
153  INT_VECT3_ZERO(imu_krooz.meas_nb_acc);
155  AbiSendMsgIMU_ACCEL_INT32(IMU_BOARD_ID, now_ts, &imu.accel);
156  }
157 
158  RunOnceEvery(128, {axis_nb = 5;});
159 }
160 
161 void imu_krooz_event(void)
162 {
163  if (imu_krooz.mpu_eoc) {
164  mpu60x0_i2c_read(&imu_krooz.mpu);
165  imu_krooz.mpu_eoc = false;
166  }
167 
168  // If the MPU6050 I2C transaction has succeeded: convert the data
169  mpu60x0_i2c_event(&imu_krooz.mpu);
170  if (imu_krooz.mpu.data_available) {
171  RATES_ADD(imu_krooz.rates_sum, imu_krooz.mpu.data_rates.rates);
172  imu_krooz.meas_nb++;
173  imu_krooz.mpu.data_available = false;
174  }
175 
176  if (SysTimeTimer(ad7689_event_timer) > 215) {
177  SysTimeTimerStart(ad7689_event_timer);
178  if (axis_cnt < axis_nb) {
179  axis_cnt++;
180  } else {
181  axis_cnt = 0;
182  }
183  imu_krooz.ad7689_trans.output_buf[0] =
184  axis_cnt <= 2 ? 0xF0 | (axis_cnt << 1) : (axis_cnt >= 4 ? 0xF0 | ((axis_cnt - 3) << 1) : 0xB0);
185  imu_krooz.ad7689_trans.output_buf[1] = 0x44;
186  spi_submit(&(IMU_KROOZ_SPI_DEV), &imu_krooz.ad7689_trans);
187  }
188  if (imu_krooz.ad7689_trans.status == SPITransSuccess) {
189  imu_krooz.ad7689_trans.status = SPITransDone;
190  uint16_t buf = (imu_krooz.ad7689_trans.input_buf[0] << 8) | imu_krooz.ad7689_trans.input_buf[1];
191  switch (axis_cnt) {
192  case 0:
193  case 3:
194  imu_krooz.accel_sum.x += (int32_t)buf;
195  imu_krooz.meas_nb_acc.x++;
196  break;
197  case 1:
198  case 4:
199  imu_krooz.accel_sum.y += (int32_t)buf;
200  imu_krooz.meas_nb_acc.y++;
201  break;
202  case 2:
203  imu_krooz.accel_sum.z += (int32_t)buf;
204  imu_krooz.meas_nb_acc.z++;
205  break;
206  case 5:
207  imu_krooz.temperature = (imu_krooz.temperature * 4 + (int32_t)buf) / 5;
208  //imu.temperature = 33000 * imu_krooz.temp / 65536 - 2400;
209  axis_nb = 2;
210  break;
211  default:
212  axis_cnt = 0;
213  break;
214  }
215  }
216 
217  if (imu_krooz.hmc_eoc) {
218  hmc58xx_read(&imu_krooz.hmc);
219  imu_krooz.hmc_eoc = false;
220  }
221 
222  // If the HMC5883 I2C transaction has succeeded: convert the data
223  hmc58xx_event(&imu_krooz.hmc);
224  if (imu_krooz.hmc.data_available) {
225  VECT3_ASSIGN(imu.mag_unscaled, imu_krooz.hmc.data.vect.y, -imu_krooz.hmc.data.vect.x, imu_krooz.hmc.data.vect.z);
227  imu_krooz.hmc.data_available = false;
228  imu_scale_mag(&imu);
229  AbiSendMsgIMU_MAG_INT32(IMU_BOARD_ID, get_sys_time_usec(), &imu.mag);
230  }
231 }
struct Int32Vect3 accel_filtered
Definition: imu_krooz.h:115
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:146
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
union Hmc58xx::@301 data
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:108
#define RATES_ADD(_a, _b)
Definition: pprz_algebra.h:343
void imu_scale_gyro(struct Imu *_imu)
Definition: ahrs_gx3.c:349
static uint8_t axis_cnt
#define KROOZ_LOWPASS_FILTER
#define IMU_BOARD_ID
void hmc58xx_start_configure(struct Hmc58xx *hmc)
Definition: hmc58xx.c:126
bool initialized
config done flag
Definition: mpu60x0.h:87
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
volatile bool hmc_eoc
Definition: imu_krooz.h:109
#define IMU_KROOZ_ACCEL_AVG_FILTER
Definition: imu_krooz.h:104
#define KROOZ_ACCEL_RANGE
#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
struct spi_transaction ad7689_trans
Main include for ABI (AirBorneInterface).
uint8_t smplrt_div
Sample rate divider.
Definition: mpu60x0.h:79
Driver for the IMU on the KroozSD Big Rotorcraft Edition board.
#define InitMedianFilterVect3Int(_f)
Definition: median_filter.h:88
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
Definition: spi_arch.c:364
struct Int32Rates rates_sum
Definition: imu_krooz.h:112
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:114
enum Mpu60x0GyroRanges gyro_range
deg/s Range
Definition: mpu60x0.h:81
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:144
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:82
bool drdy_int_enable
Enable Data Ready Interrupt.
Definition: mpu60x0.h:83
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:80
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
union Mpu60x0_I2c::@313 data_rates
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:195
CPOL = 0.
Definition: spi.h:77
struct Int32Vect3 mag
magnetometer measurements scaled to 1 in BFP with INT32_MAG_FRAC
Definition: imu.h:40
int32_t temperature
Definition: imu_krooz.h:116
#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:111
#define UpdateMedianFilterVect3Int(_f, _v)
Definition: median_filter.h:97
#define INT_VECT3_ZERO(_v)
struct Int32Vect3 accel_sum
Definition: imu_krooz.h:113
#define VECT3_SMUL(_vo, _vi, _s)
Definition: pprz_algebra.h:188
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:110
Definition: spi.h:117
slave is selected before transaction and unselected after
Definition: spi.h:57
static uint8_t axis_nb
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_krooz_event(void)
#define SysTimeTimerStart(_t)
Definition: sys_time.h:218
#define KROOZ_GYRO_RANGE
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.