Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
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
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 }
Itg3200Config::smplrt_div
uint8_t smplrt_div
Sample rate divider.
Definition: itg3200.h:53
Imu::gyro_unscaled
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:46
Itg3200::config
struct Itg3200Config config
Definition: itg3200.h:80
ITG3200_ADDR_ALT
#define ITG3200_ADDR_ALT
Definition: itg3200_regs.h:32
adxl345_i2c_event
void adxl345_i2c_event(struct Adxl345_I2c *adxl)
Definition: adxl345_i2c.c:114
hmc58xx_event
void hmc58xx_event(struct Hmc58xx *hmc)
Definition: hmc58xx.c:152
Hmc58xx::data
union Hmc58xx::@317 data
imu_navgo
struct ImuNavgo imu_navgo
Definition: imu_navgo.c:64
InitMedianFilterVect3Int
#define InitMedianFilterVect3Int(_f, _n)
Definition: median_filter.h:97
itg3200_init
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
abi.h
Imu::accel
struct Int32Vect3 accel
accelerometer measurements in m/s^2 in BFP with INT32_ACCEL_FRAC
Definition: imu.h:39
Int32Rates::q
int32_t q
in rad/s with INT32_RATE_FRAC
Definition: pprz_algebra_int.h:181
ADXL345_ADDR_ALT
#define ADXL345_ADDR_ALT
Definition: adxl345_regs.h:33
Int32Vect3::z
int32_t z
Definition: pprz_algebra_int.h:91
ImuNavgo
Definition: imu_navgo.h:62
NAVGO_GYRO_LOWPASS
#define NAVGO_GYRO_LOWPASS
Definition: imu_navgo.c:52
Adxl345Config::rate
enum Adxl345Rates rate
Data Output Rate.
Definition: adxl345.h:53
Adxl345_I2c::config
struct Adxl345Config config
Definition: adxl345_i2c.h:49
uint32_t
unsigned long uint32_t
Definition: types.h:18
UpdateMedianFilterRatesInt
#define UpdateMedianFilterRatesInt(_f, _v)
Definition: median_filter.h:118
adxl345_i2c_init
void adxl345_i2c_init(struct Adxl345_I2c *adxl, struct i2c_periph *i2c_p, uint8_t addr)
Definition: adxl345_i2c.c:36
IMU_BOARD_ID
#define IMU_BOARD_ID
Definition: abi_sender_ids.h:275
adxl345_i2c_periodic
static void adxl345_i2c_periodic(struct Adxl345_I2c *adxl)
convenience function: read or start configuration if not already initialized
Definition: adxl345_i2c.h:59
imu_navgo_init
void imu_navgo_init(void)
Definition: imu_navgo.c:66
Imu::accel_unscaled
struct Int32Vect3 accel_unscaled
unscaled accelerometer measurements
Definition: imu.h:47
get_sys_time_usec
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
imu_navgo_event
void imu_navgo_event(void)
Definition: imu_navgo.c:120
Int32Rates::p
int32_t p
in rad/s with INT32_RATE_FRAC
Definition: pprz_algebra_int.h:180
HMC58XX_ADDR
#define HMC58XX_ADDR
Definition: hmc58xx_regs.h:31
imu_navgo_periodic
void imu_navgo_periodic(void)
Definition: imu_navgo.c:93
MedianFilter3Int
Definition: median_filter.h:93
RATES_ASSIGN
#define RATES_ASSIGN(_ra, _p, _q, _r)
Definition: pprz_algebra.h:330
uart.h
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
ImuNavgo::itg
struct Itg3200 itg
Definition: imu_navgo.h:63
InitMedianFilterRatesInt
#define InitMedianFilterRatesInt(_f, _n)
Definition: median_filter.h:104
imu_navgo.h
Itg3200Config::dlpf_cfg
enum Itg3200DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: itg3200.h:55
imu_scale_gyro
void imu_scale_gyro(struct Imu *_imu)
Definition: imu_vectornav.c:43
Int32Vect3::y
int32_t y
Definition: pprz_algebra_int.h:90
Itg3200::data
union Itg3200::@319 data
hmc58xx_periodic
static void hmc58xx_periodic(struct Hmc58xx *hmc)
convenience function: read or start configuration if not already initialized
Definition: hmc58xx.h:85
ImuNavgo::adxl
struct Adxl345_I2c adxl
Definition: imu_navgo.h:64
imu_navgo_downlink_raw
void imu_navgo_downlink_raw(void)
Definition: imu_navgo.c:111
median_mag
struct MedianFilter3Int median_mag
Definition: imu_krooz.c:59
led.h
arch independent LED (Light Emitting Diodes) API
median_filter.h
imu
struct Imu imu
global IMU state
Definition: imu.c:108
hmc58xx_init
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
PRINT_CONFIG_MSG
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
MEDIAN_DEFAULT_SIZE
#define MEDIAN_DEFAULT_SIZE
Definition: median_filter.h:28
Adxl345_I2c::data
union Adxl345_I2c::@304 data
NAVGO_ACCEL_RATE
#define NAVGO_ACCEL_RATE
Definition: imu_navgo.c:46
Adxl345_I2c::data_available
volatile bool data_available
data ready flag
Definition: adxl345_i2c.h:44
imu_scale_mag
void imu_scale_mag(struct Imu *_imu)
Definition: ahrs_gx3.c:352
Int32Vect3::x
int32_t x
Definition: pprz_algebra_int.h:89
Imu::mag
struct Int32Vect3 mag
magnetometer measurements scaled to 1 in BFP with INT32_MAG_FRAC
Definition: imu.h:40
Imu::mag_unscaled
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:48
NAVGO_GYRO_SMPLRT_DIV
#define NAVGO_GYRO_SMPLRT_DIV
Definition: imu_navgo.c:53
VECT3_ASSIGN
#define VECT3_ASSIGN(_a, _x, _y, _z)
Definition: pprz_algebra.h:125
Hmc58xx::data_available
volatile bool data_available
data ready flag
Definition: hmc58xx.h:65
ImuNavgo::hmc
struct Hmc58xx hmc
Definition: imu_navgo.h:65
i2c.h
Itg3200::data_available
volatile bool data_available
data ready flag
Definition: itg3200.h:75
Int32Rates::r
int32_t r
in rad/s with INT32_RATE_FRAC
Definition: pprz_algebra_int.h:182
imu_scale_accel
void imu_scale_accel(struct Imu *_imu)
Definition: imu_vectornav.c:44
itg3200_event
void itg3200_event(struct Itg3200 *itg)
Definition: itg3200.c:126
VECT3_COPY
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:140
UpdateMedianFilterVect3Int
#define UpdateMedianFilterVect3Int(_f, _v)
Definition: median_filter.h:106
itg3200_periodic
static void itg3200_periodic(struct Itg3200 *itg)
convenience function: read or start configuration if not already initialized
Definition: itg3200.h:93
Imu::gyro
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:38