Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ahrs_aligner.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2009 Antoine Drouin <poinix@gmail.com>
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 
29 #include "ahrs_aligner.h"
30 
31 #include <stdlib.h> /* for abs() */
32 #include "subsystems/imu.h"
33 #include "led.h"
34 
36 
37 #define SAMPLES_NB PERIODIC_FREQUENCY
38 
39 static struct Int32Rates gyro_sum;
40 static struct Int32Vect3 accel_sum;
41 static struct Int32Vect3 mag_sum;
44 
45 #if PERIODIC_TELEMETRY
47 
48 static void send_aligner(void) {
49  DOWNLINK_SEND_FILTER_ALIGNER(DefaultChannel, DefaultDevice,
53  &imu.gyro.p,
54  &imu.gyro.q,
55  &imu.gyro.r,
59 }
60 #endif
61 
62 void ahrs_aligner_init(void) {
63 
68  samples_idx = 0;
69  ahrs_aligner.noise = 0;
71 
72 #if PERIODIC_TELEMETRY
73  register_periodic_telemetry(DefaultPeriodic, "FILTER_ALIGNER", send_aligner);
74 #endif
75 }
76 
77 #ifndef LOW_NOISE_THRESHOLD
78 #define LOW_NOISE_THRESHOLD 90000
79 #endif
80 #ifndef LOW_NOISE_TIME
81 #define LOW_NOISE_TIME 5
82 #endif
83 
84 void ahrs_aligner_run(void) {
85 
89 
91  samples_idx++;
92 
93 #ifdef AHRS_ALIGNER_LED
94  RunOnceEvery(50, {LED_TOGGLE(AHRS_ALIGNER_LED);});
95 #endif
96 
97  if (samples_idx >= SAMPLES_NB) {
98  int32_t avg_ref_sensor = accel_sum.z;
99  if ( avg_ref_sensor >= 0)
100  avg_ref_sensor += SAMPLES_NB / 2;
101  else
102  avg_ref_sensor -= SAMPLES_NB / 2;
103  avg_ref_sensor /= SAMPLES_NB;
104 
105  ahrs_aligner.noise = 0;
106  int i;
107  for (i=0; i<SAMPLES_NB; i++) {
108  int32_t diff = ref_sensor_samples[i] - avg_ref_sensor;
109  ahrs_aligner.noise += abs(diff);
110  }
111 
112  RATES_SDIV(ahrs_aligner.lp_gyro, gyro_sum, SAMPLES_NB);
114  VECT3_SDIV(ahrs_aligner.lp_mag, mag_sum, SAMPLES_NB);
115 
119  samples_idx = 0;
120 
123  else
124  if ( ahrs_aligner.low_noise_cnt > 0)
126 
129 #ifdef AHRS_ALIGNER_LED
130  LED_ON(AHRS_ALIGNER_LED);
131 #endif
132  }
133  }
134 
135 }
#define SAMPLES_NB
Definition: ahrs_aligner.c:37
Interface to align the AHRS via low-passed measurements at startup.
#define LOW_NOISE_THRESHOLD
Definition: ahrs_aligner.c:78
static struct Int32Vect3 mag_sum
Definition: ahrs_aligner.c:41
int32_t p
in rad/s with INT32_RATE_FRAC
Periodic telemetry system header (includes downlink utility and generated code).
int32_t low_noise_cnt
Definition: ahrs_aligner.h:44
struct Int32Vect3 lp_accel
Definition: ahrs_aligner.h:41
struct Int32Vect3 accel
accelerometer measurements in m/s^2 in BFP with INT32_ACCEL_FRAC
Definition: imu.h:42
#define LED_ON(i)
Definition: led_hw.h:28
#define INT_RATES_ZERO(_e)
uint8_t status
Definition: ahrs_aligner.h:45
int32_t noise
Definition: ahrs_aligner.h:43
bool_t register_periodic_telemetry(struct pprz_telemetry *_pt, const char *_msg, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:38
struct Int32Vect3 lp_mag
Definition: ahrs_aligner.h:42
#define AHRS_ALIGNER_RUNNING
Definition: ahrs_aligner.h:36
#define RATES_SDIV(_ro, _ri, _s)
Definition: pprz_algebra.h:375
static struct Int32Rates gyro_sum
Definition: ahrs_aligner.c:39
struct AhrsAligner ahrs_aligner
Definition: ahrs_aligner.c:35
struct Imu imu
global IMU state
Definition: imu_aspirin2.c:47
unsigned long uint32_t
Definition: types.h:18
#define AHRS_ALIGNER_LOCKED
Definition: ahrs_aligner.h:37
#define RATES_ADD(_a, _b)
Definition: pprz_algebra.h:333
#define INT_VECT3_ZERO(_v)
Inertial Measurement Unit interface.
angular rates
#define LED_TOGGLE(i)
Definition: led_hw.h:30
#define LOW_NOISE_TIME
Definition: ahrs_aligner.c:81
signed long int32_t
Definition: types.h:19
struct Int32Vect3 mag
magnetometer measurements scaled to 1 in BFP with INT32_MAG_FRAC
Definition: imu.h:43
#define VECT3_ADD(_a, _b)
Definition: pprz_algebra.h:136
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:41
arch independent LED (Light Emitting Diodes) API
static int32_t ref_sensor_samples[SAMPLES_NB]
Definition: ahrs_aligner.c:42
int32_t r
in rad/s with INT32_RATE_FRAC
#define VECT3_SDIV(_vo, _vi, _s)
Definition: pprz_algebra.h:185
void ahrs_aligner_init(void)
Definition: ahrs_aligner.c:62
void ahrs_aligner_run(void)
Definition: ahrs_aligner.c:84
static uint32_t samples_idx
Definition: ahrs_aligner.c:43
struct Int32Rates lp_gyro
Definition: ahrs_aligner.h:40
static struct Int32Vect3 accel_sum
Definition: ahrs_aligner.c:40