Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
imu_analog.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 The Paparazzi Team
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 
22 #include "imu_analog.h"
23 #include "mcu_periph/adc.h"
24 #include "subsystems/abi.h"
25 
27 
29 
30 void imu_impl_init(void)
31 {
32 
33  imu_overrun = 0;
34 
35 #ifdef ADC_CHANNEL_GYRO_P
36  adc_buf_channel(ADC_CHANNEL_GYRO_P, &analog_imu_adc_buf[0], ADC_CHANNEL_GYRO_NB_SAMPLES);
37 #endif
38 #ifdef ADC_CHANNEL_GYRO_Q
39  adc_buf_channel(ADC_CHANNEL_GYRO_Q, &analog_imu_adc_buf[1], ADC_CHANNEL_GYRO_NB_SAMPLES);
40 #endif
41 #ifdef ADC_CHANNEL_GYRO_R
42  adc_buf_channel(ADC_CHANNEL_GYRO_R, &analog_imu_adc_buf[2], ADC_CHANNEL_GYRO_NB_SAMPLES);
43 #endif
44 #ifdef ADC_CHANNEL_ACCEL_X
45  adc_buf_channel(ADC_CHANNEL_ACCEL_X, &analog_imu_adc_buf[3], ADC_CHANNEL_ACCEL_NB_SAMPLES);
46 #endif
47 #ifdef ADC_CHANNEL_ACCEL_Y
48  adc_buf_channel(ADC_CHANNEL_ACCEL_Y, &analog_imu_adc_buf[4], ADC_CHANNEL_ACCEL_NB_SAMPLES);
49 #endif
50 #ifdef ADC_CHANNEL_ACCEL_Z
51  adc_buf_channel(ADC_CHANNEL_ACCEL_Z, &analog_imu_adc_buf[5], ADC_CHANNEL_ACCEL_NB_SAMPLES);
52 #endif
53 
54 }
55 
56 void imu_periodic(void)
57 {
58  // Actual Nr of ADC measurements per channel per periodic loop
59  static int last_head = 0;
60 
61  uint32_t now_ts = get_sys_time_usec();
62 
63  imu_overrun = analog_imu_adc_buf[0].head - last_head;
64  if (imu_overrun < 0) {
65  imu_overrun += ADC_CHANNEL_GYRO_NB_SAMPLES;
66  }
67  last_head = analog_imu_adc_buf[0].head;
68 
69  // Read All Measurements
70 #ifdef ADC_CHANNEL_GYRO_P
71  imu.gyro_unscaled.p = analog_imu_adc_buf[0].sum / ADC_CHANNEL_GYRO_NB_SAMPLES;
72 #endif
73 #ifdef ADC_CHANNEL_GYRO_Q
74  imu.gyro_unscaled.q = analog_imu_adc_buf[1].sum / ADC_CHANNEL_GYRO_NB_SAMPLES;
75 #endif
76 #ifdef ADC_CHANNEL_GYRO_R
77  imu.gyro_unscaled.r = analog_imu_adc_buf[2].sum / ADC_CHANNEL_GYRO_NB_SAMPLES;
78 #endif
79 #ifdef ADC_CHANNEL_ACCEL_X
80  imu.accel_unscaled.x = analog_imu_adc_buf[3].sum / ADC_CHANNEL_ACCEL_NB_SAMPLES;
81 #endif
82 #ifdef ADC_CHANNEL_ACCEL_Y
83  imu.accel_unscaled.y = analog_imu_adc_buf[4].sum / ADC_CHANNEL_ACCEL_NB_SAMPLES;
84 #endif
85 #ifdef ADC_CHANNEL_ACCEL_Z
86  imu.accel_unscaled.z = analog_imu_adc_buf[5].sum / ADC_CHANNEL_ACCEL_NB_SAMPLES;
87 #endif
88 
91  AbiSendMsgIMU_GYRO_INT32(IMU_ANALOG_ID, now_ts, &imu.gyro);
92  AbiSendMsgIMU_ACCEL_INT32(IMU_ANALOG_ID, now_ts, &imu.accel);
93 }
94 
95 // if not all gyros are used, override the imu_scale_gyro handler
96 #if defined ADC_CHANNEL_GYRO_P && defined ADC_CHANNEL_GYRO_Q && ! defined ADC_CHANNEL_GYRO_R
97 
98 void imu_scale_gyro(struct Imu *_imu)
99 {
100  _imu->gyro.p = ((_imu->gyro_unscaled.p - _imu->gyro_neutral.p) * IMU_GYRO_P_SIGN * IMU_GYRO_P_SENS_NUM) /
102  _imu->gyro.q = ((_imu->gyro_unscaled.q - _imu->gyro_neutral.q) * IMU_GYRO_Q_SIGN * IMU_GYRO_Q_SENS_NUM) /
104 }
105 
106 #elif defined ADC_CHANNEL_GYRO_P && ! defined ADC_CHANNEL_GYRO_Q && ! defined ADC_CHANNEL_GYRO_R
107 
108 void imu_scale_gyro(struct Imu *_imu)
109 {
110  _imu->gyro.p = ((_imu->gyro_unscaled.p - _imu->gyro_neutral.p) * IMU_GYRO_P_SIGN * IMU_GYRO_P_SENS_NUM) /
112 }
113 
114 #endif
115 
116 // if we don't have any accelerometers, set an empty imu_scale_accel handler
117 #if ! defined ADC_CHANNEL_ACCEL_X && ! defined ADC_CHANNEL_ACCEL_Z && ! defined ADC_CHANNEL_ACCEL_Z
118 void imu_scale_accel(struct Imu *_imu __attribute__((unused))) {}
119 #endif
#define IMU_GYRO_Q_SENS_DEN
Definition: imu_apogee.h:63
void imu_scale_gyro(struct Imu *_imu)
Definition: ahrs_gx3.c:349
int imu_overrun
Definition: imu_analog.c:26
#define IMU_GYRO_P_SIGN
Definition: imu_apogee.h:41
Main include for ABI (AirBorneInterface).
#define IMU_GYRO_Q_SENS_NUM
Definition: imu_apogee.h:62
uint32_t sum
Definition: adc.h:54
#define IMU_GYRO_P_SENS_NUM
Definition: imu_apogee.h:59
#define IMU_ANALOG_ID
uint8_t head
Definition: adc.h:56
arch independent ADC (Analog to Digital Converter) API
static struct adc_buf analog_imu_adc_buf[NB_ANALOG_IMU_ADC]
Definition: imu_analog.c:28
static uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.h:39
struct Int32Rates gyro_neutral
gyroscope bias
Definition: imu.h:47
int32_t r
in rad/s with INT32_RATE_FRAC
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:50
Generic interface for all ADC hardware drivers, independent from microcontroller architecture.
Definition: adc.h:53
void imu_scale_accel(struct Imu *_imu)
Definition: imu_analog.c:118
struct Int32Vect3 accel
accelerometer measurements in m/s^2 in BFP with INT32_ACCEL_FRAC
Definition: imu.h:43
#define IMU_GYRO_P_SENS_DEN
Definition: imu_apogee.h:60
struct Imu imu
global IMU state
Definition: imu_aspirin2.c:43
unsigned long uint32_t
Definition: types.h:18
void adc_buf_channel(uint8_t adc_channel, struct adc_buf *s, uint8_t av_nb_sample)
Registers a buffer to be used to store the specified converted channel Usage:
Definition: adc_arch.c:91
void imu_impl_init(void)
must be defined by underlying hardware
Definition: imu_analog.c:30
#define NB_ANALOG_IMU_ADC
Definition: imu_analog.h:31
void imu_periodic(void)
optional.
Definition: imu_analog.c:56
Inertial Measurement Unit using onboard ADCs.
int32_t p
in rad/s with INT32_RATE_FRAC
struct Int32Vect3 accel_unscaled
unscaled accelerometer measurements
Definition: imu.h:51
abstract IMU interface providing fixed point interface
Definition: imu.h:41
#define IMU_GYRO_Q_SIGN
Definition: imu_apogee.h:42
int32_t q
in rad/s with INT32_RATE_FRAC
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:42