Paparazzi UAS  v6.3_unstable
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 "modules/core/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
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 
85 struct ImuApogee imu_apogee;
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  // set the default values
111 
112 #if APOGEE_USE_MPU9150
113  // if using MPU9150, internal mag needs to be configured
114  ak8975_init(&imu_apogee.ak, &(IMU_APOGEE_I2C_DEV), AK8975_I2C_SLV_ADDR);
116  imu_apogee.mpu.config.slaves[1].configure = &configure_mag_slave;
117 #endif
118 }
119 
121 {
122  // Start reading the latest gyroscope data
124 
125 #if APOGEE_USE_MPU9150
126  // Start reading internal mag if available
127  RunOnceEvery(MAG_PRESCALER, ak8975_periodic(&imu_apogee.ak));
128 #endif
129 }
130 
132 {
133  uint32_t now_ts = get_sys_time_usec();
134 
135  // If the itg3200 I2C transaction has succeeded: convert the data
138  struct Int32Rates rates = {
142  };
143  AbiSendMsgIMU_GYRO_RAW(IMU_BOARD_ID, now_ts, &rates, 1, imu_apogee.mpu.temp);
144  struct Int32Vect3 accel = {
148  };
149  AbiSendMsgIMU_ACCEL_RAW(IMU_BOARD_ID, now_ts, &accel, 1, imu_apogee.mpu.temp);
150  imu_apogee.mpu.data_available = false;
151  }
152 
153 #if APOGEE_USE_MPU9150
155  if (imu_apogee.ak.data_available) {
156  struct Int32Vect3 mag = {
157  (int32_t)( imu_apogee.ak.data.value[IMU_APOGEE_CHAN_Y]),
158  (int32_t)(-imu_apogee.ak.data.value[IMU_APOGEE_CHAN_X]),
159  (int32_t)( imu_apogee.ak.data.value[IMU_APOGEE_CHAN_Z])
160  };
161  AbiSendMsgIMU_MAG_RAW(IMU_BOARD_ID, now_ts, &mag);
162  imu_apogee.ak.data_available = false;
163  }
164 #endif
165 }
166 
Main include for ABI (AirBorneInterface).
#define IMU_BOARD_ID
void ak8975_init(struct Ak8975 *ak, struct i2c_periph *i2c_p, uint8_t addr)
Definition: ak8975.c:47
void ak8975_event(struct Ak8975 *ak)
Definition: ak8975.c:131
#define AK8975_I2C_SLV_ADDR
Definition: ak8975.h:35
static void ak8975_periodic(struct Ak8975 *ak)
convenience function: read or start configuration if not already initialized
Definition: ak8975.h:83
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:71
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:440
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:410
struct ImuApogee imu_apogee
Definition: imu_apogee.c:85
#define APOGEE_SMPLRT_DIV
Definition: imu_apogee.c:53
#define IMU_APOGEE_CHAN_X
Definition: imu_apogee.c:42
void imu_apogee_periodic(void)
Definition: imu_apogee.c:120
bool configure_baro_slave(Mpu60x0ConfigSet mpu_set, void *mpu)
Definition: imu_apogee.c:89
void imu_apogee_init(void)
Definition: imu_apogee.c:94
void imu_apogee_event(void)
Definition: imu_apogee.c:131
#define IMU_APOGEE_CHAN_Z
Definition: imu_apogee.c:48
#define APOGEE_LOWPASS_FILTER
Definition: imu_apogee.c:52
#define IMU_APOGEE_CHAN_Y
Definition: imu_apogee.c:45
Driver for the IMU on the Apogee board.
#define APOGEE_GYRO_RANGE
Definition: imu_apogee.h:44
struct Mpu60x0_I2c mpu
Definition: imu_apogee.h:51
#define APOGEE_ACCEL_RANGE
Definition: imu_apogee.h:47
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
arch independent LED (Light Emitting Diodes) API
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
Mpu60x0I2cSlaveConfigure configure
Definition: mpu60x0.h:135
uint8_t smplrt_div
Sample rate divider.
Definition: mpu60x0.h:140
uint8_t nb_slaves
number of used I2C slaves
Definition: mpu60x0.h:156
struct Mpu60x0I2cSlave slaves[MPU60X0_I2C_NB_SLAVES]
I2C slaves.
Definition: mpu60x0.h:158
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:141
bool i2c_bypass
Bypass MPU I2C.
Definition: mpu60x0.h:154
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:144
void(* Mpu60x0ConfigSet)(void *mpu, uint8_t _reg, uint8_t _val)
Configuration function prototype.
Definition: mpu60x0.h:129
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::@325 data_rates
static void mpu60x0_i2c_periodic(struct Mpu60x0_I2c *mpu)
convenience function: read or start configuration if not already initialized
Definition: mpu60x0_i2c.h:79
union Mpu60x0_I2c::@324 data_accel
float temp
temperature in degrees Celcius
Definition: mpu60x0_i2c.h:66
struct Mpu60x0Config config
Definition: mpu60x0_i2c.h:68
#define MPU60X0_ADDR_ALT
Definition: mpu60x0_regs.h:33
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78