Paparazzi UAS  v5.15_devel-230-gc96ce27
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
imu_aspirin_i2c.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 
32 // Set SPI_CS High to enable I2C mode of ADXL345
33 #include "mcu_periph/gpio.h"
34 
35 
36 /* i2c default suitable for Lisa */
37 #ifndef ASPIRIN_I2C_DEV
38 #define ASPIRIN_I2C_DEV i2c2
39 #endif
40 PRINT_CONFIG_VAR(ASPIRIN_I2C_DEV)
41 
42 
43 #ifndef ASPIRIN_ACCEL_RATE
44 # if PERIODIC_FREQUENCY <= 60
45 # define ASPIRIN_ACCEL_RATE ADXL345_RATE_50HZ
46 # elif PERIODIC_FREQUENCY <= 120
47 # define ASPIRIN_ACCEL_RATE ADXL345_RATE_100HZ
48 # else
49 # define ASPIRIN_ACCEL_RATE ADXL345_RATE_200HZ
50 # endif
51 #endif
52 PRINT_CONFIG_VAR(ASPIRIN_ACCEL_RATE)
53 
54 
55 
56 #ifndef ASPIRIN_GYRO_LOWPASS
57 # if PERIODIC_FREQUENCY <= 60
58 # define ASPIRIN_GYRO_LOWPASS ITG3200_DLPF_20HZ
59 # elif PERIODIC_FREQUENCY <= 120
60 # define ASPIRIN_GYRO_LOWPASS ITG3200_DLPF_42HZ
61 # else
62 # define ASPIRIN_GYRO_LOWPASS ITG3200_DLPF_98HZ
63 # endif
64 #endif
65 PRINT_CONFIG_VAR(ASPIRIN_GYRO_LOWPASS)
66 
67 
68 
69 #ifndef ASPIRIN_GYRO_SMPLRT_DIV
70 # if PERIODIC_FREQUENCY <= 60
71 # define ASPIRIN_GYRO_SMPLRT_DIV 19
72 PRINT_CONFIG_MSG("Gyro output rate is 50Hz")
73 # else
74 # define ASPIRIN_GYRO_SMPLRT_DIV 9
75 PRINT_CONFIG_MSG("Gyro output rate is 100Hz")
76 # endif
77 #endif
78 PRINT_CONFIG_VAR(ASPIRIN_GYRO_SMPLRT_DIV)
79 
80 
82 
84 {
85  /* Set accel configuration */
87  // set the data rate
88  imu_aspirin.acc_adxl.config.rate = ASPIRIN_ACCEL_RATE;
90  //imu_aspirin.acc_adxl.config.drdy_int_enable = true;
91 
92  // With CS tied high to VDD I/O, the ADXL345 is in I2C mode
93 #ifdef ASPIRIN_I2C_CS_PORT
94  gpio_setup_output(ASPIRIN_I2C_CS_PORT, ASPIRIN_I2C_CS_PIN);
95  gpio_set(ASPIRIN_I2C_CS_PORT, ASPIRIN_I2C_CS_PIN);
96 #endif
97 
98  /* Gyro configuration and initalization */
100  /* change the default config */
101  // Aspirin sample rate divider
103  // digital low pass filter
105 
107  /* interrupt on data ready, idle high, latch until read any register */
108  //itg_conf.int_cfg = (0x01 | (0x1<<4) | (0x1<<5) | 0x01<<7);
109 
110  /* initialize mag and set default options */
112 #ifdef IMU_ASPIRIN_VERSION_1_0
113  imu_aspirin.mag_hmc.type = HMC_TYPE_5843;
114 #endif
115 }
116 
117 
119 {
121 
122  // Start reading the latest gyroscope data
124 
125  // Read HMC58XX at 50Hz (main loop for rotorcraft: 512Hz)
126  RunOnceEvery(10, hmc58xx_periodic(&imu_aspirin.mag_hmc));
127 }
128 
130 {
131  uint32_t now_ts = get_sys_time_usec();
132 
138  AbiSendMsgIMU_ACCEL_INT32(IMU_ASPIRIN_ID, now_ts, &imu.accel);
139  }
140 
141  /* If the itg3200 I2C transaction has succeeded: convert the data */
147  AbiSendMsgIMU_GYRO_INT32(IMU_ASPIRIN_ID, now_ts, &imu.gyro);
148  }
149 
150  /* HMC58XX event task */
153 #ifdef IMU_ASPIRIN_VERSION_1_0
155 #else // aspirin 1.5 with hmc5883
159 #endif
161  imu_scale_mag(&imu);
162  AbiSendMsgIMU_MAG_INT32(IMU_ASPIRIN_ID, now_ts, &imu.mag);
163  }
164 }
void imu_aspirin_i2c_event(void)
volatile bool data_available
data ready flag
Definition: hmc58xx.h:65
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
struct Itg3200 gyro_itg
struct Hmc58xx mag_hmc
Some architecture independent helper functions for GPIOs.
enum Adxl345Rates rate
Data Output Rate.
Definition: adxl345.h:53
#define ASPIRIN_I2C_DEV
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:140
Main include for ABI (AirBorneInterface).
#define ASPIRIN_ACCEL_RATE
adxl345 accelerometer output rate, lowpass is set to half of rate
struct Imu imu
global IMU state
Definition: imu.c:108
#define IMU_ASPIRIN_ID
void gpio_setup_output(ioportid_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as outputs.
Definition: gpio_arch.c:33
#define ASPIRIN_GYRO_LOWPASS
gyro internal lowpass frequency
#define HMC58XX_ADDR
Definition: hmc58xx_regs.h:31
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:48
struct ImuAspirinI2c imu_aspirin
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
struct Adxl345Config config
Definition: adxl345_i2c.h:49
static void adxl345_i2c_periodic(struct Adxl345_I2c *adxl)
convenience function: read or start configuration if not already initialized
Definition: adxl345_i2c.h:59
void adxl345_i2c_event(struct Adxl345_I2c *adxl)
Definition: adxl345_i2c.c:114
unsigned long uint32_t
Definition: types.h:18
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
struct Int32Vect3 mag
magnetometer measurements scaled to 1 in BFP with INT32_MAG_FRAC
Definition: imu.h:40
Inertial Measurement Unit interface.
void imu_scale_gyro(struct Imu *_imu)
Definition: imu_vectornav.c:43
union Adxl345_I2c::@303 data
void adxl345_i2c_init(struct Adxl345_I2c *adxl, struct i2c_periph *i2c_p, uint8_t addr)
Definition: adxl345_i2c.c:36
#define ITG3200_ADDR
Definition: itg3200_regs.h:31
#define ASPIRIN_GYRO_SMPLRT_DIV
gyro sample rate divider
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
void imu_aspirin_i2c_periodic(void)
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
#define ADXL345_ADDR
default I2C address
Definition: adxl345_regs.h:32
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
union Hmc58xx::@315 data
void imu_scale_accel(struct Imu *_imu)
Definition: imu_vectornav.c:44
union Itg3200::@317 data
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:337
void imu_aspirin_i2c_init(void)
volatile bool data_available
data ready flag
Definition: itg3200.h:75
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
struct Itg3200Config config
Definition: itg3200.h:80
void hmc58xx_event(struct Hmc58xx *hmc)
Definition: hmc58xx.c:152
struct Adxl345_I2c acc_adxl
static void gpio_set(ioportid_t port, uint16_t pin)
Set a gpio output to high level.
Definition: gpio_arch.h:98
volatile bool data_available
data ready flag
Definition: adxl345_i2c.h:44
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:38
Architecture independent I2C (Inter-Integrated Circuit Bus) API.