Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
imu_disco.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Freek van Tienen <freek.v.tienen@gmail.com>
3  * Copyright (C) 2017 Gautier Hattenberger <gautier.hattenberger@enac.fr>
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, see
19  * <http://www.gnu.org/licenses/>.
20  */
21 
27 #include "modules/imu/imu_disco.h"
28 #include "modules/imu/imu.h"
29 #include "modules/core/abi.h"
30 #include "mcu_periph/i2c.h"
31 #include "generated/modules.h"
32 
33 
34 /* I2C is hardwired on Disco autopilot */
35 #define DISCO_MAG_I2C_DEV i2c1
37 #define DISCO_MPU_I2C_DEV i2c2
39 
40 #if !defined AK8963_HZ
41 #define AK8963_HZ 50
42 #endif
43 
44 #if !defined DISCO_LOWPASS_FILTER && !defined DISCO_SMPLRT_DIV
45 #if (PERIODIC_FREQUENCY == 60) || (PERIODIC_FREQUENCY == 120)
46 /* Accelerometer: Bandwidth 44Hz, Delay 4.9ms
47  * Gyroscope: Bandwidth 42Hz, Delay 4.8ms sampling 1kHz
48  */
49 #define DISCO_LOWPASS_FILTER MPU60X0_DLPF_42HZ
50 #define DISCO_SMPLRT_DIV 9
51 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
52 #elif PERIODIC_FREQUENCY == 512
53 /* Accelerometer: Bandwidth 260Hz, Delay 0ms
54  * Gyroscope: Bandwidth 256Hz, Delay 0.98ms sampling 8kHz
55  */
56 #define DISCO_LOWPASS_FILTER MPU60X0_DLPF_256HZ
57 #define DISCO_SMPLRT_DIV 3
58 PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
59 #endif
60 #endif
61 
62 PRINT_CONFIG_VAR(DISCO_SMPLRT_DIV)
63 PRINT_CONFIG_VAR(DISCO_LOWPASS_FILTER)
64 
67 
68 
69 struct ImuDisco imu_disco;
70 
74 void imu_disco_init(void)
75 {
76  /* MPU-60X0 */
78  imu_disco.mpu.config.smplrt_div = DISCO_SMPLRT_DIV;
79  imu_disco.mpu.config.dlpf_cfg = DISCO_LOWPASS_FILTER;
82 
83  // Set the default scaling
86 
87  /* AKM8963 */
89 }
90 
96 {
97  static uint32_t cntr = 0;
98 
99  // Start reading the latest gyroscope data
101 
102  // AKM8963 if read faster thant datasheet 100Hz over I2C crashes it once in a while
103  if (cntr%((uint32_t)(PERIODIC_FREQUENCY/AK8963_HZ))==0) {
105  cntr=0;
106  }
107  cntr++;
108 }
109 
114 void imu_disco_event(void)
115 {
116  uint32_t now_ts = get_sys_time_usec();
117 
118  /* MPU-60x0 event taks */
120 
122  /* set correct orientation here */
123  struct Int32Rates gyro;
124  struct Int32Vect3 accel;
125  RATES_ASSIGN(gyro,
126  -imu_disco.mpu.data_rates.rates.p,
127  -imu_disco.mpu.data_rates.rates.q,
128  imu_disco.mpu.data_rates.rates.r);
129  VECT3_ASSIGN(accel,
130  -imu_disco.mpu.data_accel.vect.x,
131  -imu_disco.mpu.data_accel.vect.y,
132  imu_disco.mpu.data_accel.vect.z);
133 
134  imu_disco.mpu.data_available = false;
135  AbiSendMsgIMU_GYRO_RAW(IMU_BOARD_ID, now_ts, &gyro, 1, IMU_DISCO_PERIODIC_FREQ, imu_disco.mpu.temp);
136  AbiSendMsgIMU_ACCEL_RAW(IMU_BOARD_ID, now_ts, &accel, 1, IMU_DISCO_PERIODIC_FREQ, imu_disco.mpu.temp);
137  }
138 
139  /* AKM8963 event task */
141 
143  struct Int32Vect3 mag;
144  VECT3_ASSIGN(mag,
145  imu_disco.ak.data.vect.x,
146  imu_disco.ak.data.vect.y,
147  imu_disco.ak.data.vect.z);
148 
149  AbiSendMsgIMU_MAG_RAW(IMU_BOARD_ID, now_ts, &mag);
150  imu_disco.ak.data_available = false;
151  }
152 }
Main include for ABI (AirBorneInterface).
#define IMU_BOARD_ID
void ak8963_event(struct Ak8963 *ak)
Definition: ak8963.c:95
void ak8963_init(struct Ak8963 *ak, struct i2c_periph *i2c_p, uint8_t addr)
Initialize AK8963 struct.
Definition: ak8963.c:34
volatile bool data_available
data ready flag
Definition: ak8963.h:61
union Ak8963::@310 data
static void ak8963_periodic(struct Ak8963 *ak)
convenience function: read or start configuration if not already initialized
Definition: ak8963.h:75
#define AK8963_ADDR
Definition: ak8963_regs.h:31
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:71
#define RATES_ASSIGN(_ra, _p, _q, _r)
Definition: pprz_algebra.h:330
#define VECT3_ASSIGN(_a, _x, _y, _z)
Definition: pprz_algebra.h:125
angular rates
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.
void imu_disco_periodic(void)
Handle all the periodic tasks of the Disco IMU components.
Definition: imu_disco.c:95
struct ImuDisco imu_disco
Disco IMU data.
Definition: imu_disco.c:69
#define AK8963_HZ
Definition: imu_disco.c:41
#define DISCO_MPU_I2C_DEV
Definition: imu_disco.c:37
void imu_disco_init(void)
Disco IMU initializtion of the MPU-60x0 and HMC58xx.
Definition: imu_disco.c:74
#define DISCO_MAG_I2C_DEV
Definition: imu_disco.c:35
void imu_disco_event(void)
Handle all the events of the Disco IMU components.
Definition: imu_disco.c:114
Interface for the Disco magnetometer, accelerometer and gyroscope.
#define DISCO_GYRO_RANGE
Definition: imu_disco.h:38
#define DISCO_ACCEL_RANGE
Definition: imu_disco.h:42
struct Ak8963 ak
The AK8963 mag.
Definition: imu_disco.h:48
struct Mpu60x0_I2c mpu
The MPU gyro/accel device.
Definition: imu_disco.h:47
Everything that is in the disco IMU.
Definition: imu_disco.h:46
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
const struct Int32Rates MPU60X0_GYRO_SENS_FRAC[4][2]
Definition: mpu60x0.c:38
const struct Int32Vect3 MPU60X0_ACCEL_SENS_FRAC[4][2]
Definition: mpu60x0.c:56
uint8_t smplrt_div
Sample rate divider.
Definition: mpu60x0.h:140
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:141
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:144
enum Mpu60x0GyroRanges gyro_range
deg/s Range
Definition: mpu60x0.h:143
void mpu60x0_i2c_init(struct Mpu60x0_I2c *mpu, struct i2c_periph *i2c_p, uint8_t addr)
Definition: mpu60x0_i2c.c:31
void mpu60x0_i2c_event(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:82
volatile bool data_available
data ready flag
Definition: mpu60x0_i2c.h:57
union Mpu60x0_I2c::@340 data_rates
union Mpu60x0_I2c::@339 data_accel
static void mpu60x0_i2c_periodic(struct Mpu60x0_I2c *mpu)
convenience function: read or start configuration if not already initialized
Definition: mpu60x0_i2c.h:79
float temp
temperature in degrees Celcius
Definition: mpu60x0_i2c.h:66
struct Mpu60x0Config config
Definition: mpu60x0_i2c.h:68
#define MPU60X0_ADDR
Definition: mpu60x0_regs.h:32
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78