Paparazzi UAS  v5.12_stable-4-g9b43e9b
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces 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 #include "subsystems/abi.h"
35 #include "mcu_periph/sys_time.h"
36 
38 
39 #define SAMPLES_NB 100
40 
41 static struct Int32Rates gyro_sum;
42 static struct Int32Vect3 accel_sum;
43 static struct Int32Vect3 mag_sum;
46 
47 #ifndef AHRS_ALIGNER_IMU_ID
48 #define AHRS_ALIGNER_IMU_ID ABI_BROADCAST
49 #endif
51 
52 static void gyro_cb(uint8_t sender_id __attribute__((unused)),
53  uint32_t stamp __attribute__((unused)),
54  struct Int32Rates *gyro __attribute__((unused)))
55 {
58  }
59 }
60 
61 #if PERIODIC_TELEMETRY
63 
64 static void send_aligner(struct transport_tx *trans, struct link_device *dev)
65 {
66  pprz_msg_send_FILTER_ALIGNER(trans, dev, AC_ID,
70  &imu.gyro.p,
71  &imu.gyro.q,
72  &imu.gyro.r,
76 }
77 #endif
78 
80 {
81 
86  samples_idx = 0;
87  ahrs_aligner.noise = 0;
89 
90  // for now: only bind to gyro message and still read from global imu struct
91  AbiBindMsgIMU_GYRO_INT32(AHRS_ALIGNER_IMU_ID, &gyro_ev, gyro_cb);
92 
93 #if PERIODIC_TELEMETRY
94  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_FILTER_ALIGNER, send_aligner);
95 #endif
96 }
97 
98 #ifndef LOW_NOISE_THRESHOLD
99 #define LOW_NOISE_THRESHOLD 90000
100 #endif
101 
102 #ifndef LOW_NOISE_TIME
103 #define LOW_NOISE_TIME 5
104 #endif
105 
107 {
108 
112 
114  samples_idx++;
115 
116 #ifdef AHRS_ALIGNER_LED
117  RunOnceEvery(50, {LED_TOGGLE(AHRS_ALIGNER_LED);});
118 #endif
119 
120  if (samples_idx >= SAMPLES_NB) {
121  int32_t avg_ref_sensor = accel_sum.z;
122  if (avg_ref_sensor >= 0) {
123  avg_ref_sensor += SAMPLES_NB / 2;
124  } else {
125  avg_ref_sensor -= SAMPLES_NB / 2;
126  }
127  avg_ref_sensor /= SAMPLES_NB;
128 
129  ahrs_aligner.noise = 0;
130  int i;
131  for (i = 0; i < SAMPLES_NB; i++) {
132  int32_t diff = ref_sensor_samples[i] - avg_ref_sensor;
133  ahrs_aligner.noise += abs(diff);
134  }
135 
136  RATES_SDIV(ahrs_aligner.lp_gyro, gyro_sum, SAMPLES_NB);
138  VECT3_SDIV(ahrs_aligner.lp_mag, mag_sum, SAMPLES_NB);
139 
143  samples_idx = 0;
144 
147  } else if (ahrs_aligner.low_noise_cnt > 0) {
149  }
150 
153 #ifdef AHRS_ALIGNER_LED
154  LED_ON(AHRS_ALIGNER_LED);
155 #endif
156  uint32_t now_ts = get_sys_time_usec();
157  AbiSendMsgIMU_LOWPASSED(ABI_BROADCAST, now_ts, &ahrs_aligner.lp_gyro,
159  }
160  }
161 
162 }
#define SAMPLES_NB
Definition: ahrs_aligner.c:39
Event structure to store callbacks in a linked list.
Definition: abi_common.h:65
#define RATES_SDIV(_ro, _ri, _s)
Definition: pprz_algebra.h:385
Interface to align the AHRS via low-passed measurements at startup.
angular rates
#define VECT3_ADD(_a, _b)
Definition: pprz_algebra.h:146
#define LOW_NOISE_THRESHOLD
Definition: ahrs_aligner.c:99
#define RATES_ADD(_a, _b)
Definition: pprz_algebra.h:343
static struct Int32Vect3 mag_sum
Definition: ahrs_aligner.c:43
Periodic telemetry system header (includes downlink utility and generated code).
static void send_aligner(struct transport_tx *trans, struct link_device *dev)
Definition: ahrs_aligner.c:64
#define INT_RATES_ZERO(_e)
Main include for ABI (AirBorneInterface).
struct Imu imu
global IMU state
Definition: imu.c:108
#define AHRS_ALIGNER_RUNNING
Definition: ahrs_aligner.h:36
static void gyro_cb(uint8_t sender_id, uint32_t stamp, struct Int32Rates *gyro)
Definition: ahrs_aligner.c:52
struct Int32Rates lp_gyro
Definition: ahrs_aligner.h:40
int32_t r
in rad/s with INT32_RATE_FRAC
struct Int32Vect3 lp_mag
Definition: ahrs_aligner.h:42
int32_t low_noise_cnt
Definition: ahrs_aligner.h:44
struct Int32Vect3 accel
accelerometer measurements in m/s^2 in BFP with INT32_ACCEL_FRAC
Definition: imu.h:39
static struct Int32Rates gyro_sum
Definition: ahrs_aligner.c:41
struct AhrsAligner ahrs_aligner
Definition: ahrs_aligner.c:37
uint8_t status
Definition: ahrs_aligner.h:45
Architecture independent timing functions.
unsigned long uint32_t
Definition: types.h:18
#define AHRS_ALIGNER_LOCKED
Definition: ahrs_aligner.h:37
#define VECT3_SDIV(_vo, _vi, _s)
Definition: pprz_algebra.h:195
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
#define AHRS_ALIGNER_IMU_ID
Definition: ahrs_aligner.c:48
struct Int32Vect3 lp_accel
Definition: ahrs_aligner.h:41
struct Int32Vect3 mag
magnetometer measurements scaled to 1 in BFP with INT32_MAG_FRAC
Definition: imu.h:40
Inertial Measurement Unit interface.
#define LED_TOGGLE(i)
Definition: led_hw.h:52
#define LOW_NOISE_TIME
Number of cycles (100 samples each) with low noise.
Definition: ahrs_aligner.c:103
signed long int32_t
Definition: types.h:19
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
static abi_event gyro_ev
Definition: ahrs_aligner.c:50
#define INT_VECT3_ZERO(_v)
int32_t noise
Definition: ahrs_aligner.h:43
unsigned char uint8_t
Definition: types.h:14
int32_t p
in rad/s with INT32_RATE_FRAC
arch independent LED (Light Emitting Diodes) API
#define ABI_BROADCAST
Broadcast address.
Definition: abi_common.h:56
#define LED_ON(i)
Definition: led_hw.h:50
static int32_t ref_sensor_samples[SAMPLES_NB]
Definition: ahrs_aligner.c:44
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
void ahrs_aligner_init(void)
Definition: ahrs_aligner.c:79
int32_t q
in rad/s with INT32_RATE_FRAC
void ahrs_aligner_run(void)
Definition: ahrs_aligner.c:106
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46
static uint32_t samples_idx
Definition: ahrs_aligner.c:45
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:38
static struct Int32Vect3 accel_sum
Definition: ahrs_aligner.c:42