Paparazzi UAS  v6.2.0_stable
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 
29 #include "modules/imu/imu.h"
30 #include "modules/core/abi.h"
31 #include "mcu_periph/i2c.h"
32 
33 // Set SPI_CS High to enable I2C mode of ADXL345
34 #include "mcu_periph/gpio.h"
35 
36 
37 /* i2c default suitable for Lisa */
38 #ifndef ASPIRIN_I2C_DEV
39 #define ASPIRIN_I2C_DEV i2c2
40 #endif
41 PRINT_CONFIG_VAR(ASPIRIN_I2C_DEV)
42 
43 
44 #ifndef ASPIRIN_ACCEL_RATE
45 # if PERIODIC_FREQUENCY <= 60
46 # define ASPIRIN_ACCEL_RATE ADXL345_RATE_50HZ
47 # elif PERIODIC_FREQUENCY <= 120
48 # define ASPIRIN_ACCEL_RATE ADXL345_RATE_100HZ
49 # else
50 # define ASPIRIN_ACCEL_RATE ADXL345_RATE_200HZ
51 # endif
52 #endif
53 PRINT_CONFIG_VAR(ASPIRIN_ACCEL_RATE)
54 
55 
56 
57 #ifndef ASPIRIN_GYRO_LOWPASS
58 # if PERIODIC_FREQUENCY <= 60
59 # define ASPIRIN_GYRO_LOWPASS ITG3200_DLPF_20HZ
60 # elif PERIODIC_FREQUENCY <= 120
61 # define ASPIRIN_GYRO_LOWPASS ITG3200_DLPF_42HZ
62 # else
63 # define ASPIRIN_GYRO_LOWPASS ITG3200_DLPF_98HZ
64 # endif
65 #endif
66 PRINT_CONFIG_VAR(ASPIRIN_GYRO_LOWPASS)
67 
68 
69 
70 #ifndef ASPIRIN_GYRO_SMPLRT_DIV
71 # if PERIODIC_FREQUENCY <= 60
72 # define ASPIRIN_GYRO_SMPLRT_DIV 19
73 PRINT_CONFIG_MSG("Gyro output rate is 50Hz")
74 # else
75 # define ASPIRIN_GYRO_SMPLRT_DIV 9
76 PRINT_CONFIG_MSG("Gyro output rate is 100Hz")
77 # endif
78 #endif
79 PRINT_CONFIG_VAR(ASPIRIN_GYRO_SMPLRT_DIV)
80 
81 
83 
85 {
86  /* Set accel configuration */
88  // set the data rate
91  //imu_aspirin.acc_adxl.config.drdy_int_enable = true;
92 
93  // With CS tied high to VDD I/O, the ADXL345 is in I2C mode
94 #ifdef ASPIRIN_I2C_CS_PORT
95  gpio_setup_output(ASPIRIN_I2C_CS_PORT, ASPIRIN_I2C_CS_PIN);
96  gpio_set(ASPIRIN_I2C_CS_PORT, ASPIRIN_I2C_CS_PIN);
97 #endif
98 
99  /* Gyro configuration and initalization */
101  /* change the default config */
102  // Aspirin sample rate divider
104  // digital low pass filter
106 
108  /* interrupt on data ready, idle high, latch until read any register */
109  //itg_conf.int_cfg = (0x01 | (0x1<<4) | (0x1<<5) | 0x01<<7);
110 
111  // Default scaling values
112 #ifdef IMU_ASPIRIN_VERSION_1_5
113  const struct Int32Rates gyro_scale[2] = {
114  {4359, 4359, 4359},
115  {1000, 1000, 1000}
116  };
117 #else
118  const struct Int32Rates gyro_scale[2] = {
119  {4973, 4973, 4973},
120  {1000, 1000, 1000}
121  };
122 #endif
123  const struct Int32Vect3 accel_scale[2] = {
124  {3791, 3791, 3791},
125  {100, 100, 100}
126  };
127 
128  // Set the default scaling
131 
132  /* initialize mag and set default options */
134 #ifdef IMU_ASPIRIN_VERSION_1_0
136 #endif
137 }
138 
139 
141 {
143 
144  // Start reading the latest gyroscope data
146 
147  // Read HMC58XX at 50Hz (main loop for rotorcraft: 512Hz)
148  RunOnceEvery(10, hmc58xx_periodic(&imu_aspirin.mag_hmc));
149 }
150 
152 {
153  uint32_t now_ts = get_sys_time_usec();
154 
157  AbiSendMsgIMU_ACCEL_RAW(IMU_ASPIRIN_ID, now_ts, &imu_aspirin.acc_adxl.data.vect, 1, NAN);
159  }
160 
161  /* If the itg3200 I2C transaction has succeeded: convert the data */
164  AbiSendMsgIMU_GYRO_RAW(IMU_ASPIRIN_ID, now_ts, &imu_aspirin.gyro_itg.data.rates, 1, NAN);
166  }
167 
168  /* HMC58XX event task */
171  struct Int32Vect3 mag;
172 #ifdef IMU_ASPIRIN_VERSION_1_0
173  VECT3_COPY(mag, imu_aspirin.mag_hmc.data.vect);
174 #else // aspirin 1.5 with hmc5883
175  mag.x = imu_aspirin.mag_hmc.data.vect.y;
176  mag.y = -imu_aspirin.mag_hmc.data.vect.x;
177  mag.z = imu_aspirin.mag_hmc.data.vect.z;
178 #endif
180  AbiSendMsgIMU_MAG_RAW(IMU_ASPIRIN_ID, now_ts, &mag);
181  }
182 }
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
union Adxl345_I2c::@295 data
struct Adxl345Config config
Definition: adxl345_i2c.h:49
volatile bool data_available
data ready flag
Definition: adxl345_i2c.h:44
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:98
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:75
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
union Hmc58xx::@308 data
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
#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:440
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:410
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::@312 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