Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
imu_aspirin.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 Antoine Drouin <poinix@gmail.com>
3  * 2013 Felix Ruess <felix.ruess@gmail.com>
4  *
5  * This file is part of paparazzi.
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, write to
19  * the Free Software Foundation, 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 
28 #include "subsystems/imu.h"
29 #include "subsystems/abi.h"
30 #include "mcu_periph/i2c.h"
31 #include "mcu_periph/spi.h"
32 
33 
34 /* defaults suitable for Lisa */
35 #ifndef ASPIRIN_SPI_SLAVE_IDX
36 #define ASPIRIN_SPI_SLAVE_IDX SPI_SLAVE2
37 #endif
38 PRINT_CONFIG_VAR(ASPIRIN_SPI_SLAVE_IDX)
39 
40 #ifndef ASPIRIN_SPI_DEV
41 #define ASPIRIN_SPI_DEV spi2
42 #endif
43 PRINT_CONFIG_VAR(ASPIRIN_SPI_DEV)
44 
45 #ifndef ASPIRIN_I2C_DEV
46 #define ASPIRIN_I2C_DEV i2c2
47 #endif
48 PRINT_CONFIG_VAR(ASPIRIN_I2C_DEV)
49 
50 #ifndef ASPIRIN_ACCEL_RATE
51 #define ASPIRIN_ACCEL_RATE ADXL345_RATE_800HZ
52 #endif
53 PRINT_CONFIG_VAR(ASPIRIN_ACCEL_RATE)
54 
55 
56 /* gyro internal lowpass frequency */
57 #if !defined ASPIRIN_GYRO_LOWPASS && !defined ASPIRIN_GYRO_SMPLRT_DIV
58 #define ASPIRIN_GYRO_LOWPASS ITG3200_DLPF_256HZ
59 #define ASPIRIN_GYRO_SMPLRT_DIV 14
60 PRINT_CONFIG_MSG("Gyro output rate is 533Hz")
61 #endif
62 PRINT_CONFIG_VAR(ASPIRIN_GYRO_LOWPASS)
63 PRINT_CONFIG_VAR(ASPIRIN_GYRO_SMPLRT_DIV)
64 
65 
67 
68 void imu_impl_init(void)
69 {
70  imu_aspirin.accel_valid = FALSE;
71  imu_aspirin.gyro_valid = FALSE;
72  imu_aspirin.mag_valid = FALSE;
73 
74  /* Set accel configuration */
76  // set the data rate
77  imu_aspirin.acc_adxl.config.rate = ASPIRIN_ACCEL_RATE;
79  //imu_aspirin.acc_adxl.config.drdy_int_enable = TRUE;
80 
81  /* Gyro configuration and initalization */
83  /* change the default config */
84  // Aspirin sample rate divider defaults to 533Hz
86  // aspirin defaults to 8kHz internal with 256Hz low pass
88 
90  /* interrupt on data ready, idle high, latch until read any register */
91  //itg_conf.int_cfg = (0x01 | (0x1<<4) | (0x1<<5) | 0x01<<7);
92 
93  /* initialize mag and set default options */
95 #ifdef IMU_ASPIRIN_VERSION_1_0
96  imu_aspirin.mag_hmc.type = HMC_TYPE_5843;
97 #endif
98 
99 #if ASPIRIN_ARCH_INDEP
100  TODO("Arch dependent functions (accel and gyro eoc interrupt) not used for aspirin!")
101 #else
103 #endif
104 }
105 
106 
107 void imu_periodic(void)
108 {
110 
111  // Start reading the latest gyroscope data
113 
114  // Read HMC58XX at 50Hz (main loop for rotorcraft: 512Hz)
115  RunOnceEvery(10, hmc58xx_periodic(&imu_aspirin.mag_hmc));
116 }
117 
119 {
120  uint32_t now_ts = get_sys_time_usec();
121 
127  }
128 
129  /* If the itg3200 I2C transaction has succeeded: convert the data */
135  }
136 
137  /* HMC58XX event task */
140 #ifdef IMU_ASPIRIN_VERSION_1_0
142 #else // aspirin 1.5 with hmc5883
146 #endif
149  }
150 
151  if (imu_aspirin.gyro_valid) {
154  AbiSendMsgIMU_GYRO_INT32(IMU_ASPIRIN_ID, now_ts, &imu.gyro);
155  }
156  if (imu_aspirin.accel_valid) {
159  AbiSendMsgIMU_ACCEL_INT32(IMU_ASPIRIN_ID, now_ts, &imu.accel);
160  }
161  if (imu_aspirin.mag_valid) {
163  imu_scale_mag(&imu);
164  AbiSendMsgIMU_MAG_INT32(IMU_ASPIRIN_ID, now_ts, &imu.mag);
165  }
166 }
struct ImuAspirin imu_aspirin
Definition: imu_aspirin.c:66
struct Adxl345Config config
Definition: adxl345_spi.h:51
void imu_aspirin_arch_init(void)
void imu_aspirin_event(void)
Definition: imu_aspirin.c:118
enum Hmc58xxType type
Definition: hmc58xx.h:71
uint8_t smplrt_div
Sample rate divider.
Definition: itg3200.h:53
void itg3200_init(struct Itg3200 *itg, struct i2c_periph *i2c_p, uint8_t addr)
Initialize Itg3200 struct and set default config options.
Definition: itg3200.c:49
void imu_scale_gyro(struct Imu *_imu)
Definition: ahrs_gx3.c:349
void imu_scale_accel(struct Imu *_imu)
Definition: ahrs_gx3.c:350
enum Adxl345Rates rate
Data Output Rate.
Definition: adxl345.h:53
volatile bool_t data_available
data ready flag
Definition: itg3200.h:75
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:139
volatile uint8_t mag_valid
Definition: imu_aspirin.h:45
Main include for ABI (AirBorneInterface).
#define ASPIRIN_SPI_DEV
Definition: imu_aspirin.c:41
void imu_impl_init(void)
must be defined by underlying hardware
Definition: imu_aspirin.c:68
void imu_periodic(void)
optional.
Definition: imu_aspirin.c:107
union Adxl345_Spi::@25 data
#define IMU_ASPIRIN_ID
#define HMC58XX_ADDR
Definition: hmc58xx_regs.h:31
#define FALSE
Definition: std.h:5
static uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.h:39
Architecture independent SPI (Serial Peripheral Interface) API.
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:52
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:50
#define TRUE
Definition: std.h:4
volatile bool_t data_available
data ready flag
Definition: adxl345_spi.h:46
struct Int32Vect3 accel
accelerometer measurements in m/s^2 in BFP with INT32_ACCEL_FRAC
Definition: imu.h:43
void adxl345_spi_event(struct Adxl345_Spi *adxl)
Definition: adxl345_spi.c:131
struct Hmc58xx mag_hmc
Definition: imu_aspirin.h:48
#define ASPIRIN_ACCEL_RATE
Definition: imu_aspirin.c:51
struct Imu imu
global IMU state
Definition: imu_aspirin2.c:43
unsigned long uint32_t
Definition: types.h:18
struct Adxl345_Spi acc_adxl
Definition: imu_aspirin.h:46
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
#define ASPIRIN_I2C_DEV
Definition: imu_aspirin.c:46
#define ASPIRIN_GYRO_LOWPASS
Definition: imu_aspirin.c:58
struct Int32Vect3 mag
magnetometer measurements scaled to 1 in BFP with INT32_MAG_FRAC
Definition: imu.h:44
Inertial Measurement Unit interface.
volatile bool_t data_available
data ready flag
Definition: hmc58xx.h:65
void adxl345_spi_init(struct Adxl345_Spi *adxl, struct spi_periph *spi_p, uint8_t slave_idx)
Definition: adxl345_spi.c:35
#define ITG3200_ADDR
Definition: itg3200_regs.h:31
union Hmc58xx::@33 data
enum Itg3200DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: itg3200.h:55
void imu_scale_mag(struct Imu *_imu)
Definition: ahrs_gx3.c:351
static void hmc58xx_periodic(struct Hmc58xx *hmc)
convenience function: read or start configuration if not already initialized
Definition: hmc58xx.h:85
volatile uint8_t accel_valid
Definition: imu_aspirin.h:43
void itg3200_event(struct Itg3200 *itg)
Definition: itg3200.c:126
static void itg3200_periodic(struct Itg3200 *itg)
convenience function: read or start configuration if not already initialized
Definition: itg3200.h:93
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:51
struct Itg3200 gyro_itg
Definition: imu_aspirin.h:47
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:336
struct Itg3200Config config
Definition: itg3200.h:80
void hmc58xx_event(struct Hmc58xx *hmc)
Definition: hmc58xx.c:152
static void adxl345_spi_periodic(struct Adxl345_Spi *adxl)
convenience function: read or start configuration if not already initialized
Definition: adxl345_spi.h:61
#define ASPIRIN_SPI_SLAVE_IDX
Definition: imu_aspirin.c:36
volatile uint8_t gyro_valid
Definition: imu_aspirin.h:44
union Itg3200::@34 data
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:42
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
#define ASPIRIN_GYRO_SMPLRT_DIV
Definition: imu_aspirin.c:59