Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
imu_b2.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2009 Antoine Drouin <poinix@gmail.com>
3  * Copyright (C) 2012 Gautier Hattenberger
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 
32 #include "subsystems/imu.h"
33 
35 
36 PRINT_CONFIG_VAR(IMU_B2_MAG_TYPE)
37 
38 void imu_b2_init(void)
39 {
40 
41  max1168_init();
42 #if defined IMU_B2_MAG_TYPE && IMU_B2_MAG_TYPE == IMU_B2_MAG_MS2100
43  ms2100_init(&ms2100, &(MS2100_SPI_DEV), MS2100_SLAVE_IDX);
44 #elif defined IMU_B2_MAG_TYPE && IMU_B2_MAG_TYPE == IMU_B2_MAG_AMI601
45  ami601_init();
46 #elif defined IMU_B2_MAG_TYPE && IMU_B2_MAG_TYPE == IMU_B2_MAG_HMC5843
47  hmc5843_init();
48 #elif defined IMU_B2_MAG_TYPE && IMU_B2_MAG_TYPE == IMU_B2_MAG_HMC58XX
49  hmc58xx_init(&imu_b2.mag_hmc, &(IMU_B2_I2C_DEV), HMC58XX_ADDR);
50  // Booz2 v1.2 has HMC5843
51  imu_b2.mag_hmc.type = HMC_TYPE_5843;
52 #endif
53 
54 }
55 
56 #include "led.h"
57 void imu_b2_periodic(void)
58 {
59 
60  // read adc
62  // read mag
63 #if defined IMU_B2_MAG_TYPE && IMU_B2_MAG_TYPE == IMU_B2_MAG_MS2100
65 #elif defined IMU_B2_MAG_TYPE && IMU_B2_MAG_TYPE == IMU_B2_MAG_AMI601
66  RunOnceEvery(10, { ami601_read(); });
67 #elif defined IMU_B2_MAG_TYPE && IMU_B2_MAG_TYPE == IMU_B2_MAG_HMC58XX
68  RunOnceEvery(5, hmc58xx_periodic(&imu_b2.mag_hmc));
69 #endif
70 
71 }
72 
73 #if defined IMU_MAG_45_HACK
74 void imu_scale_mag(struct Imu *_imu)
75 {
78  _imu->mag.x = msx - msy;
79  _imu->mag.y = msx + msy;
81 }
82 #elif defined IMU_B2_MAG_TYPE && IMU_B2_MAG_TYPE == IMU_B2_MAG_NONE
83 void imu_scale_mag(struct Imu *_imu __attribute__((unused))) {}
84 #endif
85 
86 
87 
90 #include "subsystems/abi.h"
91 #include "mcu_periph/sys_time.h"
92 
93 #if defined IMU_B2_MAG_TYPE && IMU_B2_MAG_TYPE == IMU_B2_MAG_MS2100
94 static inline void ImuMagEvent(void)
95 {
102  imu_scale_mag(&imu);
103  AbiSendMsgIMU_MAG_INT32(IMU_B2_ID, get_sys_time_usec(), &imu.mag);
104  }
105 }
106 #elif defined IMU_B2_MAG_TYPE && IMU_B2_MAG_TYPE == IMU_B2_MAG_AMI601
107 static inline void foo_handler(void) {}
108 static inline void ImuMagEvent(void)
109 {
110  AMI601Event(foo_handler);
116  imu_scale_mag(&imu);
117  AbiSendMsgIMU_MAG_INT32(IMU_B2_ID, get_sys_time_usec(), &imu.mag);
118  }
119 }
120 #elif defined IMU_B2_MAG_TYPE && IMU_B2_MAG_TYPE == IMU_B2_MAG_HMC5843
121 static inline void foo_handler(void) {}
122 static inline void ImuMagEvent(void)
123 {
125  if (hmc5843.data_available) {
129  imu_scale_mag(&imu);
130  AbiSendMsgIMU_MAG_INT32(IMU_B2_ID, get_sys_time_usec(), &imu.mag);
131  hmc5843.data_available = false;
132  }
133 }
134 #elif defined IMU_B2_MAG_TYPE && IMU_B2_MAG_TYPE == IMU_B2_MAG_HMC58XX
135 static inline void ImuMagEvent(void)
136 {
137  hmc58xx_event(&imu_b2.mag_hmc);
138  if (imu_b2.mag_hmc.data_available) {
139  imu.mag_unscaled.x = imu_b2.mag_hmc.data.value[IMU_MAG_X_CHAN];
140  imu.mag_unscaled.y = imu_b2.mag_hmc.data.value[IMU_MAG_Y_CHAN];
141  imu.mag_unscaled.z = imu_b2.mag_hmc.data.value[IMU_MAG_Z_CHAN];
142  imu_scale_mag(&imu);
143  AbiSendMsgIMU_MAG_INT32(IMU_B2_ID, get_sys_time_usec(), &imu.mag);
144  imu_b2.mag_hmc.data_available = false;
145  }
146 }
147 #else
148 #define ImuMagEvent() {}
149 #endif
150 
151 
152 void imu_b2_event(void)
153 {
154  max1168_event();
156  uint32_t now_ts = get_sys_time_usec();
166  AbiSendMsgIMU_GYRO_INT32(IMU_ASPIRIN2_ID, now_ts, &imu.gyro);
167  AbiSendMsgIMU_ACCEL_INT32(IMU_ASPIRIN2_ID, now_ts, &imu.accel);
168  }
169  ImuMagEvent();
170 }
Imu::gyro_unscaled
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:46
IMU_MAG_Z_SIGN
#define IMU_MAG_Z_SIGN
Definition: imu_hbmini.h:57
hmc5843_idle_task
void hmc5843_idle_task(void)
Definition: hmc5843.c:60
hmc58xx_event
void hmc58xx_event(struct Hmc58xx *hmc)
Definition: hmc58xx.c:152
IMU_GYRO_P_CHAN
#define IMU_GYRO_P_CHAN
Definition: imu_hbmini.c:46
IMU_ASPIRIN2_ID
#define IMU_ASPIRIN2_ID
Definition: abi_sender_ids.h:287
max1168_values
uint16_t max1168_values[MAX1168_NB_CHAN]
Definition: max1168.c:28
abi.h
IMU_MAG_Y_CHAN
#define IMU_MAG_Y_CHAN
Definition: imu_hbmini.c:57
Imu::accel
struct Int32Vect3 accel
accelerometer measurements in m/s^2 in BFP with INT32_ACCEL_FRAC
Definition: imu.h:39
max1168_event
void max1168_event(void)
Definition: max1168.c:101
IMU_MAG_Y_SENS_DEN
#define IMU_MAG_Y_SENS_DEN
Definition: imu_ardrone2.h:76
Int32Rates::q
int32_t q
in rad/s with INT32_RATE_FRAC
Definition: pprz_algebra_int.h:181
Hmc5843::data_available
uint8_t data_available
Definition: hmc5843.h:34
ami601_values
uint16_t ami601_values[AMI601_NB_CHAN]
Definition: ami601.c:6
Int32Vect3::z
int32_t z
Definition: pprz_algebra_int.h:91
MAX1168_IDLE
#define MAX1168_IDLE
Definition: max1168.h:59
imu_b2
struct ImuBooz2 imu_b2
Definition: imu_b2.c:34
Imu::mag_neutral
struct Int32Vect3 mag_neutral
magnetometer neutral readings (bias) in raw/unscaled units
Definition: imu.h:45
uint32_t
unsigned long uint32_t
Definition: types.h:18
IMU_MAG_Y_SENS_NUM
#define IMU_MAG_Y_SENS_NUM
Definition: imu_ardrone2.h:75
IMU_MAG_X_SENS_NUM
#define IMU_MAG_X_SENS_NUM
Definition: imu_ardrone2.h:72
MS2100_IDLE
@ MS2100_IDLE
Definition: ms2100.h:37
IMU_MAG_Z_SENS_DEN
#define IMU_MAG_Z_SENS_DEN
Definition: imu_ardrone2.h:79
ami601_init
void ami601_init(void)
Definition: ami601.c:12
IMU_B2_ID
#define IMU_B2_ID
Definition: abi_sender_ids.h:291
hmc5843_init
void hmc5843_init(void)
Definition: hmc5843.c:16
IMU_ACCEL_Y_CHAN
#define IMU_ACCEL_Y_CHAN
Definition: imu_hbmini.c:52
ImuBooz2
Definition: imu_b2.h:168
Imu::accel_unscaled
struct Int32Vect3 accel_unscaled
unscaled accelerometer measurements
Definition: imu.h:47
ms2100
struct Ms2100 ms2100
Definition: ms2100.c:44
AMI601Event
#define AMI601Event(_handler)
Definition: ami601.h:38
ami601_read
void ami601_read(void)
Definition: ami601.c:26
IMU_GYRO_Q_CHAN
#define IMU_GYRO_Q_CHAN
Definition: imu_hbmini.c: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
HMC58XX_ADDR
#define HMC58XX_ADDR
Definition: hmc58xx_regs.h:31
imu.h
Imu
abstract IMU interface providing fixed point interface
Definition: imu.h:37
Hmc5843::data
union Hmc5843::@316 data
AMI601_DATA_AVAILABLE
#define AMI601_DATA_AVAILABLE
Definition: ami601.h:23
IMU_MAG_X_CHAN
#define IMU_MAG_X_CHAN
Definition: imu_hbmini.c:56
Max1168Periodic
#define Max1168Periodic()
Definition: max1168.h:68
Ms2100::status
enum Ms2100Status status
Definition: ms2100.h:50
MS2100_DATA_AVAILABLE
@ MS2100_DATA_AVAILABLE
Definition: ms2100.h:41
max1168_status
volatile uint8_t max1168_status
Definition: max1168.c:27
sys_time.h
Architecture independent timing functions.
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
MAX1168_DATA_AVAILABLE
#define MAX1168_DATA_AVAILABLE
Definition: max1168.h:63
IMU_MAG_Y_SIGN
#define IMU_MAG_Y_SIGN
Definition: imu_hbmini.h:56
IMU_MAG_X_SENS_DEN
#define IMU_MAG_X_SENS_DEN
Definition: imu_ardrone2.h:73
ami601_status
volatile uint8_t ami601_status
Definition: ami601.c:8
hmc58xx_periodic
static void hmc58xx_periodic(struct Hmc58xx *hmc)
convenience function: read or start configuration if not already initialized
Definition: hmc58xx.h:85
led.h
arch independent LED (Light Emitting Diodes) API
IMU_MAG_Z_SENS_NUM
#define IMU_MAG_Z_SENS_NUM
Definition: imu_ardrone2.h:78
IMU_ACCEL_X_CHAN
#define IMU_ACCEL_X_CHAN
Definition: imu_hbmini.c:51
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
Ms2100::data
union Ms2100::@339 data
IMU_GYRO_R_CHAN
#define IMU_GYRO_R_CHAN
Definition: imu_hbmini.c:48
IMU_MAG_Z_CHAN
#define IMU_MAG_Z_CHAN
Definition: imu_hbmini.c:58
IMU_ACCEL_Z_CHAN
#define IMU_ACCEL_Z_CHAN
Definition: imu_hbmini.c:53
int32_t
signed long int32_t
Definition: types.h:19
HMC_TYPE_5843
@ HMC_TYPE_5843
Definition: hmc58xx.h:56
imu_b2_event
void imu_b2_event(void)
Definition: imu_b2.c:152
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_X_SIGN
#define IMU_MAG_X_SIGN
Definition: imu_hbmini.h:55
Imu::mag_unscaled
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:48
AMI601_IDLE
#define AMI601_IDLE
Definition: ami601.h:19
max1168_init
void max1168_init(void)
Definition: max1168.c:40
ms2100_periodic
static void ms2100_periodic(struct Ms2100 *ms)
Definition: ms2100.h:65
ImuMagEvent
#define ImuMagEvent()
Event functions for imu_b2.
Definition: imu_b2.c:148
ms2100_init
void ms2100_init(struct Ms2100 *ms, struct spi_periph *spi_p, uint8_t slave_idx)
Definition: ms2100.c:47
hmc5843
struct Hmc5843 hmc5843
Definition: hmc5843.c:9
imu_b2_periodic
void imu_b2_periodic(void)
Definition: imu_b2.c:57
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
ms2100_event
void ms2100_event(struct Ms2100 *ms)
Definition: ms2100.c:105
imu_b2_init
void imu_b2_init(void)
Definition: imu_b2.c:38
Imu::gyro
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:38