Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
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 
29 #include "modules/imu/imu.h"
30 #include "modules/core/abi.h"
31 #include "mcu_periph/i2c.h"
32 #include "mcu_periph/spi.h"
33 #include "generated/modules.h"
34 
35 
36 /* defaults suitable for Lisa */
37 #ifndef ASPIRIN_SPI_SLAVE_IDX
38 #define ASPIRIN_SPI_SLAVE_IDX SPI_SLAVE2
39 #endif
41 
42 #ifndef ASPIRIN_SPI_DEV
43 #define ASPIRIN_SPI_DEV spi2
44 #endif
46 
47 #ifndef ASPIRIN_I2C_DEV
48 #define ASPIRIN_I2C_DEV i2c2
49 #endif
51 
52 #ifndef ASPIRIN_ACCEL_RATE
53 #define ASPIRIN_ACCEL_RATE ADXL345_RATE_800HZ
54 #endif
56 
57 
58 /* gyro internal lowpass frequency */
59 #if !defined ASPIRIN_GYRO_LOWPASS && !defined ASPIRIN_GYRO_SMPLRT_DIV
60 #define ASPIRIN_GYRO_LOWPASS ITG3200_DLPF_256HZ
61 #define ASPIRIN_GYRO_SMPLRT_DIV 14
62 PRINT_CONFIG_MSG("Gyro output rate is 533Hz")
63 #endif
66 
67 
68 struct ImuAspirin imu_aspirin;
69 
70 void imu_aspirin_init(void)
71 {
72  /* Set accel configuration */
74  // set the data rate
77  //imu_aspirin.acc_adxl.config.drdy_int_enable = true;
78 
79  /* Gyro configuration and initalization */
81  /* change the default config */
82  // Aspirin sample rate divider defaults to 533Hz
84  // aspirin defaults to 8kHz internal with 256Hz low pass
86 
88  /* interrupt on data ready, idle high, latch until read any register */
89  //itg_conf.int_cfg = (0x01 | (0x1<<4) | (0x1<<5) | 0x01<<7);
90 
91  // Default scaling values
92 #ifdef IMU_ASPIRIN_VERSION_1_5
93  const struct Int32Rates gyro_scale[2] = {
94  {4359, 4359, 4359},
95  {1000, 1000, 1000}
96  };
97 #else
98  const struct Int32Rates gyro_scale[2] = {
99  {4973, 4973, 4973},
100  {1000, 1000, 1000}
101  };
102 #endif
103  const struct Int32Vect3 accel_scale[2] = {
104  {3791, 3791, 3791},
105  {100, 100, 100}
106  };
107 
108  // Set the default scaling
111 
112  /* initialize mag and set default options */
114 #ifdef IMU_ASPIRIN_VERSION_1_0
116 #endif
117 
118 #if ASPIRIN_ARCH_INDEP
119  TODO("Arch dependent functions (accel and gyro eoc interrupt) not used for aspirin!")
120 #else
122 #endif
123 }
124 
125 
127 {
129 
130  // Start reading the latest gyroscope data
132 
133  // Read HMC58XX at 50Hz (main loop for rotorcraft: 512Hz)
134  RunOnceEvery(10, hmc58xx_periodic(&imu_aspirin.mag_hmc));
135 }
136 
138 {
139  uint32_t now_ts = get_sys_time_usec();
140 
143  struct Int32Vect3 accel;
144  VECT3_COPY(accel, imu_aspirin.acc_adxl.data.vect);
145  AbiSendMsgIMU_ACCEL_RAW(IMU_ASPIRIN_ID, now_ts, &accel, 1, IMU_ASPIRIN_PERIODIC_FREQ, NAN);
147  }
148 
149  /* If the itg3200 I2C transaction has succeeded: convert the data */
152  AbiSendMsgIMU_GYRO_RAW(IMU_ASPIRIN_ID, now_ts, &imu_aspirin.gyro_itg.data.rates, 1, IMU_ASPIRIN_PERIODIC_FREQ, NAN);
154  }
155 
156  /* HMC58XX event task */
159  struct Int32Vect3 mag;
160 #ifdef IMU_ASPIRIN_VERSION_1_0
161  VECT3_COPY(mag, imu_aspirin.mag_hmc.data.vect);
162 #else // aspirin 1.5 with hmc5883
163  mag.x = imu_aspirin.mag_hmc.data.vect.y;
164  mag.y = -imu_aspirin.mag_hmc.data.vect.x;
165  mag.z = imu_aspirin.mag_hmc.data.vect.z;
166 #endif
168  AbiSendMsgIMU_MAG_RAW(IMU_ASPIRIN_ID, now_ts, &mag);
169  }
170 }
Main include for ABI (AirBorneInterface).
#define IMU_ASPIRIN_ID
enum Adxl345Rates rate
Data Output Rate.
Definition: adxl345.h:53
void adxl345_spi_init(struct Adxl345_Spi *adxl, struct spi_periph *spi_p, uint8_t slave_idx)
Definition: adxl345_spi.c:35
void adxl345_spi_event(struct Adxl345_Spi *adxl)
Definition: adxl345_spi.c:131
struct Adxl345Config config
Definition: adxl345_spi.h:51
union Adxl345_Spi::@309 data
volatile bool data_available
data ready flag
Definition: adxl345_spi.h:46
static void adxl345_spi_periodic(struct Adxl345_Spi *adxl)
convenience function: read or start configuration if not already initialized
Definition: adxl345_spi.h:61
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:71
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:140
angular rates
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:83
void hmc58xx_event(struct Hmc58xx *hmc)
Definition: hmc58xx.c:160
static void hmc58xx_periodic(struct Hmc58xx *hmc)
convenience function: read or start configuration if not already initialized
Definition: hmc58xx.h:87
union Hmc58xx::@323 data
enum Hmc58xxType type
Definition: hmc58xx.h:73
@ HMC_TYPE_5843
Definition: hmc58xx.h:58
volatile bool data_available
data ready flag
Definition: hmc58xx.h:67
#define HMC58XX_ADDR
Definition: hmc58xx_regs.h:31
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
void imu_set_defaults_accel(uint8_t abi_id, const struct Int32RMat *imu_to_sensor, const struct Int32Vect3 *neutral, const struct Int32Vect3 *scale)
Set the defaults for a accel sensor WARNING: Should be called before sensor is publishing messages to...
Definition: imu.c:610
void imu_set_defaults_gyro(uint8_t abi_id, const struct Int32RMat *imu_to_sensor, const struct Int32Rates *neutral, const struct Int32Rates *scale)
Set the defaults for a gyro sensor WARNING: Should be called before sensor is publishing messages to ...
Definition: imu.c:580
Inertial Measurement Unit interface.
#define ASPIRIN_GYRO_LOWPASS
Definition: imu_aspirin.c:60
#define ASPIRIN_GYRO_SMPLRT_DIV
Definition: imu_aspirin.c:61
#define ASPIRIN_SPI_DEV
Definition: imu_aspirin.c:43
void imu_aspirin_init(void)
Definition: imu_aspirin.c:70
void imu_aspirin_periodic(void)
Definition: imu_aspirin.c:126
#define ASPIRIN_SPI_SLAVE_IDX
Definition: imu_aspirin.c:38
void imu_aspirin_event(void)
Definition: imu_aspirin.c:137
#define ASPIRIN_ACCEL_RATE
Definition: imu_aspirin.c:53
struct ImuAspirin imu_aspirin
Definition: imu_aspirin.c:68
#define ASPIRIN_I2C_DEV
Definition: imu_aspirin.c:48
Interface for the Aspirin v1.x IMU using SPI for the accelerometer.
struct Adxl345_Spi acc_adxl
Definition: imu_aspirin.h:41
struct Itg3200 gyro_itg
Definition: imu_aspirin.h:42
struct Hmc58xx mag_hmc
Definition: imu_aspirin.h:43
void imu_aspirin_arch_init(void)
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
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 itg3200_event(struct Itg3200 *itg)
Definition: itg3200.c:126
enum Itg3200DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: itg3200.h:55
static void itg3200_periodic(struct Itg3200 *itg)
convenience function: read or start configuration if not already initialized
Definition: itg3200.h:93
uint8_t smplrt_div
Sample rate divider.
Definition: itg3200.h:53
union Itg3200::@329 data
struct Itg3200Config config
Definition: itg3200.h:80
volatile bool data_available
data ready flag
Definition: itg3200.h:75
#define ITG3200_ADDR
Definition: itg3200_regs.h:31
static const struct Int32Rates gyro_scale[2]
Default gyro scale.
Definition: navdata.c:92
static const struct Int32Vect3 accel_scale[2]
Default accel scale/neutral.
Definition: navdata.c:100
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
Architecture independent SPI (Serial Peripheral Interface) API.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78