Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
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 
29 #include "modules/imu/imu.h"
30 #include "modules/core/abi.h"
31 #include "mcu_periph/i2c.h"
32 #include "generated/modules.h"
33 
34 // Set SPI_CS High to enable I2C mode of ADXL345
35 #include "mcu_periph/gpio.h"
36 
37 
38 /* i2c default suitable for Lisa */
39 #ifndef ASPIRIN_I2C_DEV
40 #define ASPIRIN_I2C_DEV i2c2
41 #endif
42 PRINT_CONFIG_VAR(ASPIRIN_I2C_DEV)
43 
44 
45 #ifndef ASPIRIN_ACCEL_RATE
46 # if PERIODIC_FREQUENCY <= 60
47 # define ASPIRIN_ACCEL_RATE ADXL345_RATE_50HZ
48 # elif PERIODIC_FREQUENCY <= 120
49 # define ASPIRIN_ACCEL_RATE ADXL345_RATE_100HZ
50 # else
51 # define ASPIRIN_ACCEL_RATE ADXL345_RATE_200HZ
52 # endif
53 #endif
54 PRINT_CONFIG_VAR(ASPIRIN_ACCEL_RATE)
55 
56 
57 
58 #ifndef ASPIRIN_GYRO_LOWPASS
59 # if PERIODIC_FREQUENCY <= 60
60 # define ASPIRIN_GYRO_LOWPASS ITG3200_DLPF_20HZ
61 # elif PERIODIC_FREQUENCY <= 120
62 # define ASPIRIN_GYRO_LOWPASS ITG3200_DLPF_42HZ
63 # else
64 # define ASPIRIN_GYRO_LOWPASS ITG3200_DLPF_98HZ
65 # endif
66 #endif
67 PRINT_CONFIG_VAR(ASPIRIN_GYRO_LOWPASS)
68 
69 
70 
71 #ifndef ASPIRIN_GYRO_SMPLRT_DIV
72 # if PERIODIC_FREQUENCY <= 60
73 # define ASPIRIN_GYRO_SMPLRT_DIV 19
74 PRINT_CONFIG_MSG("Gyro output rate is 50Hz")
75 # else
76 # define ASPIRIN_GYRO_SMPLRT_DIV 9
77 PRINT_CONFIG_MSG("Gyro output rate is 100Hz")
78 # endif
79 #endif
80 PRINT_CONFIG_VAR(ASPIRIN_GYRO_SMPLRT_DIV)
81 
82 
84 
86 {
87  /* Set accel configuration */
89  // set the data rate
92  //imu_aspirin.acc_adxl.config.drdy_int_enable = true;
93 
94  // With CS tied high to VDD I/O, the ADXL345 is in I2C mode
95 #ifdef ASPIRIN_I2C_CS_PORT
96  gpio_setup_output(ASPIRIN_I2C_CS_PORT, ASPIRIN_I2C_CS_PIN);
97  gpio_set(ASPIRIN_I2C_CS_PORT, ASPIRIN_I2C_CS_PIN);
98 #endif
99 
100  /* Gyro configuration and initalization */
102  /* change the default config */
103  // Aspirin sample rate divider
105  // digital low pass filter
107 
109  /* interrupt on data ready, idle high, latch until read any register */
110  //itg_conf.int_cfg = (0x01 | (0x1<<4) | (0x1<<5) | 0x01<<7);
111 
112  // Default scaling values
113 #ifdef IMU_ASPIRIN_VERSION_1_5
114  const struct Int32Rates gyro_scale[2] = {
115  {4359, 4359, 4359},
116  {1000, 1000, 1000}
117  };
118 #else
119  const struct Int32Rates gyro_scale[2] = {
120  {4973, 4973, 4973},
121  {1000, 1000, 1000}
122  };
123 #endif
124  const struct Int32Vect3 accel_scale[2] = {
125  {3791, 3791, 3791},
126  {100, 100, 100}
127  };
128 
129  // Set the default scaling
132 
133  /* initialize mag and set default options */
135 #ifdef IMU_ASPIRIN_VERSION_1_0
137 #endif
138 }
139 
140 
142 {
144 
145  // Start reading the latest gyroscope data
147 
148  // Read HMC58XX at 50Hz (main loop for rotorcraft: 512Hz)
149  RunOnceEvery(10, hmc58xx_periodic(&imu_aspirin.mag_hmc));
150 }
151 
153 {
154  uint32_t now_ts = get_sys_time_usec();
155 
158  AbiSendMsgIMU_ACCEL_RAW(IMU_ASPIRIN_ID, now_ts, &imu_aspirin.acc_adxl.data.vect, 1, IMU_ASPIRIN_I2C_PERIODIC_FREQ, NAN);
160  }
161 
162  /* If the itg3200 I2C transaction has succeeded: convert the data */
165  AbiSendMsgIMU_GYRO_RAW(IMU_ASPIRIN_ID, now_ts, &imu_aspirin.gyro_itg.data.rates, 1, IMU_ASPIRIN_I2C_PERIODIC_FREQ, NAN);
167  }
168 
169  /* HMC58XX event task */
172  struct Int32Vect3 mag;
173 #ifdef IMU_ASPIRIN_VERSION_1_0
174  VECT3_COPY(mag, imu_aspirin.mag_hmc.data.vect);
175 #else // aspirin 1.5 with hmc5883
176  mag.x = imu_aspirin.mag_hmc.data.vect.y;
177  mag.y = -imu_aspirin.mag_hmc.data.vect.x;
178  mag.z = imu_aspirin.mag_hmc.data.vect.z;
179 #endif
181  AbiSendMsgIMU_MAG_RAW(IMU_ASPIRIN_ID, now_ts, &mag);
182  }
183 }
Main include for ABI (AirBorneInterface).
#define IMU_ASPIRIN_ID
enum Adxl345Rates rate
Data Output Rate.
Definition: adxl345.h:53
void adxl345_i2c_init(struct Adxl345_I2c *adxl, struct i2c_periph *i2c_p, uint8_t addr)
Definition: adxl345_i2c.c:36
void adxl345_i2c_event(struct Adxl345_I2c *adxl)
Definition: adxl345_i2c.c:114
struct Adxl345Config config
Definition: adxl345_i2c.h:49
volatile bool data_available
data ready flag
Definition: adxl345_i2c.h:44
union Adxl345_I2c::@303 data
static void adxl345_i2c_periodic(struct Adxl345_I2c *adxl)
convenience function: read or start configuration if not already initialized
Definition: adxl345_i2c.h:59
#define ADXL345_ADDR
default I2C address
Definition: adxl345_regs.h:32
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
static void gpio_set(ioportid_t port, uint16_t pin)
Set a gpio output to high level.
Definition: gpio_arch.h:104
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:71
Some architecture independent helper functions for GPIOs.
#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:75
void hmc58xx_event(struct Hmc58xx *hmc)
Definition: hmc58xx.c:152
static void hmc58xx_periodic(struct Hmc58xx *hmc)
convenience function: read or start configuration if not already initialized
Definition: hmc58xx.h:85
enum Hmc58xxType type
Definition: hmc58xx.h:71
@ HMC_TYPE_5843
Definition: hmc58xx.h:56
volatile bool data_available
data ready flag
Definition: hmc58xx.h:65
union Hmc58xx::@316 data
#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:521
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:491
Inertial Measurement Unit interface.
#define ASPIRIN_GYRO_LOWPASS
gyro internal lowpass frequency
#define ASPIRIN_GYRO_SMPLRT_DIV
gyro sample rate divider
void imu_aspirin_i2c_init(void)
void imu_aspirin_i2c_event(void)
void imu_aspirin_i2c_periodic(void)
#define ASPIRIN_ACCEL_RATE
adxl345 accelerometer output rate, lowpass is set to half of rate
#define ASPIRIN_I2C_DEV
struct ImuAspirinI2c imu_aspirin
Interface for the Aspirin v1.x IMU using I2C for the accelerometer.
struct Adxl345_I2c acc_adxl
struct Itg3200 gyro_itg
struct Hmc58xx mag_hmc
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::@322 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
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78