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_ppzuav.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 
31 #include "subsystems/imu.h"
32 #include "subsystems/abi.h"
33 #include "mcu_periph/i2c.h"
34 
35 /* i2c default suitable for Tiny/Twog */
36 #ifndef IMU_PPZUAV_I2C_DEV
37 #define IMU_PPZUAV_I2C_DEV i2c0
38 #endif
39 
41 #ifndef IMU_PPZUAV_ACCEL_RATE
42 # if PERIODIC_FREQUENCY <= 60
43 # define IMU_PPZUAV_ACCEL_RATE ADXL345_RATE_50HZ
44 # elif PERIODIC_FREQUENCY <= 120
45 # define IMU_PPZUAV_ACCEL_RATE ADXL345_RATE_100HZ
46 # else
47 # define IMU_PPZUAV_ACCEL_RATE ADXL345_RATE_200HZ
48 # endif
49 #endif
50 PRINT_CONFIG_VAR(IMU_PPZUAV_ACCEL_RATE)
51 
52 
53 
54 #ifndef IMU_PPZUAV_GYRO_LOWPASS
55 # if PERIODIC_FREQUENCY <= 60
56 # define IMU_PPZUAV_GYRO_LOWPASS ITG3200_DLPF_20HZ
57 # elif PERIODIC_FREQUENCY <= 120
58 # define IMU_PPZUAV_GYRO_LOWPASS ITG3200_DLPF_42HZ
59 # else
60 # define IMU_PPZUAV_GYRO_LOWPASS ITG3200_DLPF_98HZ
61 # endif
62 #endif
63 PRINT_CONFIG_VAR(IMU_PPZUAV_GYRO_LOWPASS)
64 
65 
66 
67 #ifndef IMU_PPZUAV_GYRO_SMPLRT_DIV
68 # if PERIODIC_FREQUENCY <= 60
69 # define IMU_PPZUAV_GYRO_SMPLRT_DIV 19
70 PRINT_CONFIG_MSG("Gyro output rate is 50Hz")
71 # else
72 # define IMU_PPZUAV_GYRO_SMPLRT_DIV 9
73 PRINT_CONFIG_MSG("Gyro output rate is 100Hz")
74 # endif
75 #endif
76 PRINT_CONFIG_VAR(IMU_PPZUAV_GYRO_SMPLRT_DIV)
77 
78 
80 
81 void imu_impl_init(void)
82 {
83  /* Set accel configuration */
85  /* set the data rate */
87 
88  /* Gyro configuration and initalization */
90  /* change the default config */
93 
94  /* initialize mag and set default options */
96  /* set the type to the old HMC5843 */
97  imu_ppzuav.mag_hmc.type = HMC_TYPE_5843;
98 }
99 
100 
101 void imu_periodic(void)
102 {
104 
105  // Start reading the latest gyroscope data
107 
108  // Read HMC58XX at 50Hz (main loop for rotorcraft: 512Hz)
109  RunOnceEvery(10, hmc58xx_periodic(&imu_ppzuav.mag_hmc));
110 }
111 
113 {
114  uint32_t now_ts = get_sys_time_usec();
115 
123  AbiSendMsgIMU_ACCEL_INT32(IMU_PPZUAV_ID, now_ts, &imu.accel);
124  }
125 
126  /* If the itg3200 I2C transaction has succeeded: convert the data */
134  AbiSendMsgIMU_GYRO_INT32(IMU_PPZUAV_ID, now_ts, &imu.gyro);
135  }
136 
137  /* HMC58XX event task */
144  imu_scale_mag(&imu);
145  AbiSendMsgIMU_MAG_INT32(IMU_PPZUAV_ID, now_ts, &imu.mag);
146  }
147 }
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 Adxl345_I2c acc_adxl
Definition: imu_ppzuav.h:83
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
Main include for ABI (AirBorneInterface).
#define IMU_PPZUAV_GYRO_LOWPASS
gyro internal lowpass frequency
Definition: imu_ppzuav.c:56
struct Hmc58xx mag_hmc
Definition: imu_ppzuav.h:85
#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
#define IMU_PPZUAV_ACCEL_RATE
adxl345 accelerometer output rate, lowpass is set to half of rate
Definition: imu_ppzuav.c:43
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:52
int32_t r
in rad/s with INT32_RATE_FRAC
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:50
struct Int32Vect3 accel
accelerometer measurements in m/s^2 in BFP with INT32_ACCEL_FRAC
Definition: imu.h:43
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 imu_ppzuav_event(void)
Definition: imu_ppzuav.c:112
void adxl345_i2c_event(struct Adxl345_I2c *adxl)
Definition: adxl345_i2c.c:114
#define IMU_PPZUAV_I2C_DEV
Definition: imu_ppzuav.c:37
struct Imu imu
global IMU state
Definition: imu_aspirin2.c:43
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:44
Inertial Measurement Unit interface.
volatile bool_t data_available
data ready flag
Definition: hmc58xx.h:65
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
union Hmc58xx::@33 data
void imu_periodic(void)
optional.
Definition: imu_ppzuav.c:101
volatile bool_t data_available
data ready flag
Definition: adxl345_i2c.h:44
enum Itg3200DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: itg3200.h:55
void imu_scale_mag(struct Imu *_imu)
Definition: ahrs_gx3.c:351
#define IMU_PPZUAV_ID
static void hmc58xx_periodic(struct Hmc58xx *hmc)
convenience function: read or start configuration if not already initialized
Definition: hmc58xx.h:85
#define IMU_PPZUAV_GYRO_SMPLRT_DIV
gyro sample rate divider
Definition: imu_ppzuav.c:69
struct ImuPpzuav imu_ppzuav
Definition: imu_ppzuav.c:79
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
int32_t p
in rad/s with INT32_RATE_FRAC
#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:51
struct Itg3200 gyro_itg
Definition: imu_ppzuav.h:84
struct Itg3200Config config
Definition: itg3200.h:80
void hmc58xx_event(struct Hmc58xx *hmc)
Definition: hmc58xx.c:152
int32_t q
in rad/s with INT32_RATE_FRAC
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.
union Adxl345_I2c::@24 data
void imu_impl_init(void)
must be defined by underlying hardware
Definition: imu_ppzuav.c:81