Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
imu_navgo.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 Gautier Hattenberger
3  * Derived from Aspirin and ppzuavimu drivers
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 
33 #include <math.h>
34 #include "imu_navgo.h"
35 #include "mcu_periph/i2c.h"
36 #include "led.h"
37 #include "subsystems/abi.h"
38 
39 // Downlink
40 #include "mcu_periph/uart.h"
41 #include "pprzlink/messages.h"
43 
44 
45 #ifndef NAVGO_ACCEL_RATE
46 #define NAVGO_ACCEL_RATE ADXL345_RATE_25HZ
47 #endif
48 PRINT_CONFIG_VAR(NAVGO_ACCEL_RATE)
49 
50 /* default gyro internal lowpass frequency and sample rate divider */
51 #if !defined NAVGO_GYRO_LOWPASS && !defined NAVGO_GYRO_SMPLRT_DIV
52 #define NAVGO_GYRO_LOWPASS ITG3200_DLPF_10HZ
53 #define NAVGO_GYRO_SMPLRT_DIV 1
54 PRINT_CONFIG_MSG("Gyro output rate is 500Hz")
55 #endif
56 PRINT_CONFIG_VAR(NAVGO_GYRO_LOWPASS)
57 PRINT_CONFIG_VAR(NAVGO_GYRO_SMPLRT_DIV)
58 
59 #if NAVGO_USE_MEDIAN_FILTER
60 #include "filters/median_filter.h"
61 struct MedianFilter3Int median_gyro, median_accel, median_mag;
62 #endif
63 
65 
66 void imu_navgo_init(void)
67 {
69  // ITG3200
70  itg3200_init(&imu_navgo.itg, &(IMU_NAVGO_I2C_DEV), ITG3200_ADDR_ALT);
71  // change the default configuration
72  imu_navgo.itg.config.smplrt_div = NAVGO_GYRO_SMPLRT_DIV; // 500Hz sample rate since internal is 1kHz
74 
76  // ADXL345
77  adxl345_i2c_init(&imu_navgo.adxl, &(IMU_NAVGO_I2C_DEV), ADXL345_ADDR_ALT);
78  // change the default data rate
80 
82  // HMC58XX
83  hmc58xx_init(&imu_navgo.hmc, &(IMU_NAVGO_I2C_DEV), HMC58XX_ADDR);
84 
85 #if NAVGO_USE_MEDIAN_FILTER
86  // Init median filters
87  InitMedianFilterRatesInt(median_gyro);
88  InitMedianFilterVect3Int(median_accel);
90 #endif
91 }
92 
94 {
95  // Start reading the latest gyroscope data
97 
98  // Start reading the latest accelerometer data
99  // Periodicity is automatically adapted
100  // 3200 is the maximum output freq corresponding to the parameter 0xF
101  // A factor 2 is applied to reduice the delay without overloading the i2c
102  RunOnceEvery((PERIODIC_FREQUENCY / (2 * 3200 >> (0xf - NAVGO_ACCEL_RATE))), adxl345_i2c_periodic(&imu_navgo.adxl));
103 
104  // Read HMC58XX at 100Hz (main loop for rotorcraft: 512Hz)
105  RunOnceEvery(5, hmc58xx_periodic(&imu_navgo.hmc));
106 
107  //RunOnceEvery(20,imu_navgo_downlink_raw());
108 }
109 
110 
112 {
113  DOWNLINK_SEND_IMU_GYRO_RAW(DefaultChannel, DefaultDevice, &imu.gyro_unscaled.p, &imu.gyro_unscaled.q,
114  &imu.gyro_unscaled.r);
115  DOWNLINK_SEND_IMU_ACCEL_RAW(DefaultChannel, DefaultDevice, &imu.accel_unscaled.x, &imu.accel_unscaled.y,
116  &imu.accel_unscaled.z);
117  DOWNLINK_SEND_IMU_MAG_RAW(DefaultChannel, DefaultDevice, &imu.mag_unscaled.x, &imu.mag_unscaled.y, &imu.mag_unscaled.z);
118 }
119 
120 void imu_navgo_event(void)
121 {
122  uint32_t now_ts = get_sys_time_usec();
123 
124  // If the itg3200 I2C transaction has succeeded: convert the data
128 #if NAVGO_USE_MEDIAN_FILTER
130 #endif
131  imu_navgo.itg.data_available = false;
133  AbiSendMsgIMU_GYRO_INT32(IMU_BOARD_ID, now_ts, &imu.gyro);
134  }
135 
136  // If the adxl345 I2C transaction has succeeded: convert the data
140 #if NAVGO_USE_MEDIAN_FILTER
142 #endif
143  imu_navgo.adxl.data_available = false;
145  AbiSendMsgIMU_ACCEL_INT32(IMU_BOARD_ID, now_ts, &imu.accel);
146  }
147 
148  // HMC58XX event task
152 #if NAVGO_USE_MEDIAN_FILTER
154 #endif
155  imu_navgo.hmc.data_available = false;
156  imu_scale_mag(&imu);
157  AbiSendMsgIMU_MAG_INT32(IMU_BOARD_ID, now_ts, &imu.mag);
158  }
159 
160 }
void imu_navgo_event(void)
Definition: imu_navgo.c:120
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
volatile bool data_available
data ready flag
Definition: hmc58xx.h:65
uint8_t smplrt_div
Sample rate divider.
Definition: itg3200.h:53
union Hmc58xx::@301 data
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
union Itg3200::@302 data
#define NAVGO_GYRO_SMPLRT_DIV
Definition: imu_navgo.c:53
void imu_scale_gyro(struct Imu *_imu)
Definition: ahrs_gx3.c:349
#define IMU_BOARD_ID
void imu_scale_accel(struct Imu *_imu)
Definition: ahrs_gx3.c:350
enum Adxl345Rates rate
Data Output Rate.
Definition: adxl345.h:53
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:139
#define VECT3_ASSIGN(_a, _x, _y, _z)
Definition: pprz_algebra.h:124
#define RATES_ASSIGN(_ra, _p, _q, _r)
Definition: pprz_algebra.h:329
Main include for ABI (AirBorneInterface).
#define ADXL345_ADDR_ALT
Definition: adxl345_regs.h:33
void imu_navgo_downlink_raw(void)
Definition: imu_navgo.c:111
#define InitMedianFilterVect3Int(_f)
Definition: median_filter.h:88
struct Imu imu
global IMU state
Definition: imu.c:108
void imu_navgo_init(void)
Definition: imu_navgo.c:66
#define UpdateMedianFilterRatesInt(_f, _v)
#define HMC58XX_ADDR
Definition: hmc58xx_regs.h:31
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:48
int32_t r
in rad/s with INT32_RATE_FRAC
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
void imu_navgo_periodic(void)
Definition: imu_navgo.c:93
struct Adxl345Config config
Definition: adxl345_i2c.h:49
Interface for the IMU on the NavGo board.
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
struct Itg3200 itg
Definition: imu_navgo.h:63
unsigned long uint32_t
Definition: types.h:18
#define NAVGO_ACCEL_RATE
Definition: imu_navgo.c:46
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
void adxl345_i2c_init(struct Adxl345_I2c *adxl, struct i2c_periph *i2c_p, uint8_t addr)
Definition: adxl345_i2c.c:36
#define UpdateMedianFilterVect3Int(_f, _v)
Definition: median_filter.h:97
union Adxl345_I2c::@291 data
enum Itg3200DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: itg3200.h:55
struct MedianFilter3Int median_mag
Definition: imu_krooz.c:66
void imu_scale_mag(struct Imu *_imu)
Definition: ahrs_gx3.c:351
struct ImuNavgo imu_navgo
Definition: imu_navgo.c:64
static void hmc58xx_periodic(struct Hmc58xx *hmc)
convenience function: read or start configuration if not already initialized
Definition: hmc58xx.h:85
struct Hmc58xx hmc
Definition: imu_navgo.h:65
#define NAVGO_GYRO_LOWPASS
Definition: imu_navgo.c:52
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
arch independent LED (Light Emitting Diodes) API
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
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 Adxl345_I2c adxl
Definition: imu_navgo.h:64
#define InitMedianFilterRatesInt(_f)
Definition: median_filter.h:95
struct Itg3200Config config
Definition: itg3200.h:80
void hmc58xx_event(struct Hmc58xx *hmc)
Definition: hmc58xx.c:152
#define ITG3200_ADDR_ALT
Definition: itg3200_regs.h:32
int32_t q
in rad/s with INT32_RATE_FRAC
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.