Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
imu_apogee.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Gautier Hattenberger
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
30 #include <math.h>
32 #include "mcu_periph/i2c.h"
33 #include "led.h"
34 #include "subsystems/abi.h"
35 
36 // Downlink
37 #include "mcu_periph/uart.h"
38 #include "pprzlink/messages.h"
40 
41 #ifndef IMU_APOGEE_CHAN_X
42 #define IMU_APOGEE_CHAN_X 0
43 #endif
44 #ifndef IMU_APOGEE_CHAN_Y
45 #define IMU_APOGEE_CHAN_Y 1
46 #endif
47 #ifndef IMU_APOGEE_CHAN_Z
48 #define IMU_APOGEE_CHAN_Z 2
49 #endif
50 
51 #if !defined APOGEE_LOWPASS_FILTER && !defined APOGEE_SMPLRT_DIV
52 #define APOGEE_LOWPASS_FILTER MPU60X0_DLPF_42HZ
53 #define APOGEE_SMPLRT_DIV 9
54 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz")
55 #endif
56 PRINT_CONFIG_VAR(APOGEE_SMPLRT_DIV)
57 PRINT_CONFIG_VAR(APOGEE_LOWPASS_FILTER)
58 
59 PRINT_CONFIG_VAR(APOGEE_GYRO_RANGE)
60 PRINT_CONFIG_VAR(APOGEE_ACCEL_RANGE)
61 
62 #if APOGEE_USE_MPU9150
63 
67 #ifndef APOGEE_MAG_FREQ
68 #define APOGEE_MAG_FREQ 50
69 #endif
70 PRINT_CONFIG_VAR(APOGEE_MAG_FREQ)
73 #define MAG_PRESCALER Max(1,((PERIODIC_FREQUENCY)/APOGEE_MAG_FREQ))
74 PRINT_CONFIG_VAR(MAG_PRESCALER)
75 
76 // mag config will be done later in bypass mode
77 bool configure_mag_slave(Mpu60x0ConfigSet mpu_set, void *mpu);
78 bool configure_mag_slave(Mpu60x0ConfigSet mpu_set __attribute__((unused)), void *mpu __attribute__((unused)))
79 {
80  return true;
81 }
82 
83 #endif
84 
86 
87 // baro config will be done later in bypass mode
88 bool configure_baro_slave(Mpu60x0ConfigSet mpu_set, void *mpu);
89 bool configure_baro_slave(Mpu60x0ConfigSet mpu_set __attribute__((unused)), void *mpu __attribute__((unused)))
90 {
91  return true;
92 }
93 
94 void imu_apogee_init(void)
95 {
97  // MPU-60X0
98  mpu60x0_i2c_init(&imu_apogee.mpu, &(IMU_APOGEE_I2C_DEV), MPU60X0_ADDR_ALT);
99  // change the default configuration
104  // set MPU in bypass mode for the baro
108 #if APOGEE_USE_MPU9150
109  // if using MPU9150, internal mag needs to be configured
110  ak8975_init(&imu_apogee.ak, &(IMU_APOGEE_I2C_DEV), AK8975_I2C_SLV_ADDR);
112  imu_apogee.mpu.config.slaves[1].configure = &configure_mag_slave;
113 #endif
114 }
115 
117 {
118  // Start reading the latest gyroscope data
120 
121 #if APOGEE_USE_MPU9150
122  // Start reading internal mag if available
123  RunOnceEvery(MAG_PRESCALER, ak8975_periodic(&imu_apogee.ak));
124 #endif
125 
126  //RunOnceEvery(10,imu_apogee_downlink_raw());
127 }
128 
130 {
131  DOWNLINK_SEND_IMU_GYRO_RAW(DefaultChannel, DefaultDevice, &imu.gyro_unscaled.p, &imu.gyro_unscaled.q,
132  &imu.gyro_unscaled.r);
133  DOWNLINK_SEND_IMU_ACCEL_RAW(DefaultChannel, DefaultDevice, &imu.accel_unscaled.x, &imu.accel_unscaled.y,
134  &imu.accel_unscaled.z);
135 }
136 
137 
139 {
140  uint32_t now_ts = get_sys_time_usec();
141 
142  // If the itg3200 I2C transaction has succeeded: convert the data
145  struct Int32Rates rates = {
149  };
150  RATES_COPY(imu.gyro_unscaled, rates);
151  struct Int32Vect3 accel = {
155  };
156  VECT3_COPY(imu.accel_unscaled, accel);
157  imu_apogee.mpu.data_available = false;
160  AbiSendMsgIMU_GYRO_INT32(IMU_BOARD_ID, now_ts, &imu.gyro);
161  AbiSendMsgIMU_ACCEL_INT32(IMU_BOARD_ID, now_ts, &imu.accel);
162  }
163 
164 #if APOGEE_USE_MPU9150
166  if (imu_apogee.ak.data_available) {
167  struct Int32Vect3 mag = {
168  (int32_t)( imu_apogee.ak.data.value[IMU_APOGEE_CHAN_Y]),
169  (int32_t)(-imu_apogee.ak.data.value[IMU_APOGEE_CHAN_X]),
170  (int32_t)( imu_apogee.ak.data.value[IMU_APOGEE_CHAN_Z])
171  };
173  imu_apogee.ak.data_available = false;
174  imu_scale_mag(&imu);
175  AbiSendMsgIMU_MAG_INT32(IMU_BOARD_ID, now_ts, &imu.mag);
176  }
177 #endif
178 }
179 
Imu::gyro_unscaled
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:46
Mpu60x0Config::smplrt_div
uint8_t smplrt_div
Sample rate divider.
Definition: mpu60x0.h:140
mpu60x0_i2c_periodic
static void mpu60x0_i2c_periodic(struct Mpu60x0_I2c *mpu)
convenience function: read or start configuration if not already initialized
Definition: mpu60x0_i2c.h:79
IMU_APOGEE_CHAN_Y
#define IMU_APOGEE_CHAN_Y
Definition: imu_apogee.c:45
configure_baro_slave
bool configure_baro_slave(Mpu60x0ConfigSet mpu_set, void *mpu)
Definition: imu_apogee.c:89
Int32Rates
angular rates
Definition: pprz_algebra_int.h:179
Mpu60x0Config::gyro_range
enum Mpu60x0GyroRanges gyro_range
deg/s Range
Definition: mpu60x0.h:143
imu_apogee_periodic
void imu_apogee_periodic(void)
Definition: imu_apogee.c:116
abi.h
Imu::accel
struct Int32Vect3 accel
accelerometer measurements in m/s^2 in BFP with INT32_ACCEL_FRAC
Definition: imu.h:39
MPU60X0_ADDR_ALT
#define MPU60X0_ADDR_ALT
Definition: mpu60x0_regs.h:33
APOGEE_SMPLRT_DIV
#define APOGEE_SMPLRT_DIV
Definition: imu_apogee.c:53
ImuApogee::mpu
struct Mpu60x0_I2c mpu
Definition: imu_apogee.h:99
ak8975_init
void ak8975_init(struct Ak8975 *ak, struct i2c_periph *i2c_p, uint8_t addr)
Definition: ak8975.c:47
Int32Rates::q
int32_t q
in rad/s with INT32_RATE_FRAC
Definition: pprz_algebra_int.h:181
Int32Vect3::z
int32_t z
Definition: pprz_algebra_int.h:91
imu_apogee_event
void imu_apogee_event(void)
Definition: imu_apogee.c:138
ak8975_event
void ak8975_event(struct Ak8975 *ak)
Definition: ak8975.c:131
IMU_APOGEE_CHAN_Z
#define IMU_APOGEE_CHAN_Z
Definition: imu_apogee.c:48
imu_apogee.h
IMU_APOGEE_CHAN_X
#define IMU_APOGEE_CHAN_X
Definition: imu_apogee.c:42
uint32_t
unsigned long uint32_t
Definition: types.h:18
Mpu60x0Config::accel_range
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:144
IMU_BOARD_ID
#define IMU_BOARD_ID
Definition: abi_sender_ids.h:275
Mpu60x0Config::dlpf_cfg
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:141
ImuApogee
Definition: imu_apogee.h:98
Mpu60x0ConfigSet
void(* Mpu60x0ConfigSet)(void *mpu, uint8_t _reg, uint8_t _val)
Configuration function prototype.
Definition: mpu60x0.h:129
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
Int32Rates::p
int32_t p
in rad/s with INT32_RATE_FRAC
Definition: pprz_algebra_int.h:180
uart.h
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
APOGEE_LOWPASS_FILTER
#define APOGEE_LOWPASS_FILTER
Definition: imu_apogee.c:52
Mpu60x0I2cSlave::configure
Mpu60x0I2cSlaveConfigure configure
Definition: mpu60x0.h:135
mpu60x0_i2c_init
void mpu60x0_i2c_init(struct Mpu60x0_I2c *mpu, struct i2c_periph *i2c_p, uint8_t addr)
Definition: mpu60x0_i2c.c:31
Int32Vect3
Definition: pprz_algebra_int.h:88
imu_scale_gyro
void imu_scale_gyro(struct Imu *_imu)
Definition: imu_vectornav.c:43
APOGEE_ACCEL_RANGE
#define APOGEE_ACCEL_RANGE
Definition: imu_apogee.h:58
Int32Vect3::y
int32_t y
Definition: pprz_algebra_int.h:90
mpu60x0_i2c_event
void mpu60x0_i2c_event(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:82
AK8975_I2C_SLV_ADDR
#define AK8975_I2C_SLV_ADDR
Definition: ak8975.h:35
ak8975_periodic
static void ak8975_periodic(struct Ak8975 *ak)
convenience function: read or start configuration if not already initialized
Definition: ak8975.h:83
imu_apogee
struct ImuApogee imu_apogee
Definition: imu_apogee.c:85
led.h
arch independent LED (Light Emitting Diodes) API
Mpu60x0_I2c::config
struct Mpu60x0Config config
Definition: mpu60x0_i2c.h:68
imu
struct Imu imu
global IMU state
Definition: imu.c:108
Mpu60x0Config::nb_slaves
uint8_t nb_slaves
number of used I2C slaves
Definition: mpu60x0.h:156
PRINT_CONFIG_MSG
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
Mpu60x0Config::i2c_bypass
bool i2c_bypass
Bypass MPU I2C.
Definition: mpu60x0.h:154
Mpu60x0_I2c::data_accel
union Mpu60x0_I2c::@331 data_accel
imu_apogee_downlink_raw
void imu_apogee_downlink_raw(void)
Definition: imu_apogee.c:129
int32_t
signed long int32_t
Definition: types.h:19
imu_scale_mag
void imu_scale_mag(struct Imu *_imu)
Definition: ahrs_gx3.c:352
Mpu60x0Config::slaves
struct Mpu60x0I2cSlave slaves[MPU60X0_I2C_NB_SLAVES]
I2C slaves.
Definition: mpu60x0.h:158
Mpu60x0_I2c::data_rates
union Mpu60x0_I2c::@332 data_rates
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
Mpu60x0_I2c::data_available
volatile bool data_available
data ready flag
Definition: mpu60x0_i2c.h:57
RATES_COPY
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:337
i2c.h
imu_apogee_init
void imu_apogee_init(void)
Definition: imu_apogee.c:94
APOGEE_GYRO_RANGE
#define APOGEE_GYRO_RANGE
Definition: imu_apogee.h:55
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
VECT3_COPY
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:140
Imu::gyro
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:38