Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
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 #ifndef AHRS_ALIGNER_SAMPLES_NB
40 #define AHRS_ALIGNER_SAMPLES_NB 100
41 #endif
42 
43 static struct Int32Rates gyro_sum;
44 static struct Int32Vect3 accel_sum;
45 static struct Int32Vect3 mag_sum;
48 
49 #ifndef AHRS_ALIGNER_IMU_ID
50 #define AHRS_ALIGNER_IMU_ID ABI_BROADCAST
51 #endif
53 
54 static void gyro_cb(uint8_t sender_id __attribute__((unused)),
55  uint32_t stamp __attribute__((unused)),
56  struct Int32Rates *gyro __attribute__((unused)))
57 {
60  }
61 }
62 
63 #if PERIODIC_TELEMETRY
65 
66 static void send_aligner(struct transport_tx *trans, struct link_device *dev)
67 {
68  pprz_msg_send_FILTER_ALIGNER(trans, dev, AC_ID,
72  &imu.gyro.p,
73  &imu.gyro.q,
74  &imu.gyro.r,
78 }
79 #endif
80 
82 {
83 
88  samples_idx = 0;
89  ahrs_aligner.noise = 0;
91 
92  // for now: only bind to gyro message and still read from global imu struct
93  AbiBindMsgIMU_GYRO_INT32(AHRS_ALIGNER_IMU_ID, &gyro_ev, gyro_cb);
94 
95 #if PERIODIC_TELEMETRY
96  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_FILTER_ALIGNER, send_aligner);
97 #endif
98 }
99 
100 #ifndef LOW_NOISE_THRESHOLD
101 #define LOW_NOISE_THRESHOLD 90000
102 #endif
103 
104 #ifndef LOW_NOISE_TIME
105 #define LOW_NOISE_TIME 5
106 #endif
107 
109 {
110 
114 
116  samples_idx++;
117 
118 #ifdef AHRS_ALIGNER_LED
119  RunOnceEvery(50, {LED_TOGGLE(AHRS_ALIGNER_LED);});
120 #endif
121 
123  int32_t avg_ref_sensor = accel_sum.z;
124  if (avg_ref_sensor >= 0) {
125  avg_ref_sensor += AHRS_ALIGNER_SAMPLES_NB / 2;
126  } else {
127  avg_ref_sensor -= AHRS_ALIGNER_SAMPLES_NB / 2;
128  }
129  avg_ref_sensor /= AHRS_ALIGNER_SAMPLES_NB;
130 
131  ahrs_aligner.noise = 0;
132  int i;
133  for (i = 0; i < AHRS_ALIGNER_SAMPLES_NB; i++) {
134  int32_t diff = ref_sensor_samples[i] - avg_ref_sensor;
135  ahrs_aligner.noise += abs(diff);
136  }
137 
141 
145  samples_idx = 0;
146 
149  } else if (ahrs_aligner.low_noise_cnt > 0) {
151  }
152 
155 #ifdef AHRS_ALIGNER_LED
156  LED_ON(AHRS_ALIGNER_LED);
157 #endif
158  uint32_t now_ts = get_sys_time_usec();
159  AbiSendMsgIMU_LOWPASSED(ABI_BROADCAST, now_ts, &ahrs_aligner.lp_gyro,
161  }
162  }
163 
164 }
LOW_NOISE_THRESHOLD
#define LOW_NOISE_THRESHOLD
Definition: ahrs_aligner.c:101
AHRS_ALIGNER_SAMPLES_NB
#define AHRS_ALIGNER_SAMPLES_NB
Definition: ahrs_aligner.c:40
Int32Rates
angular rates
Definition: pprz_algebra_int.h:179
samples_idx
static uint32_t samples_idx
Definition: ahrs_aligner.c:47
INT_RATES_ZERO
#define INT_RATES_ZERO(_e)
Definition: pprz_algebra_int.h:575
abi.h
Imu::accel
struct Int32Vect3 accel
accelerometer measurements in m/s^2 in BFP with INT32_ACCEL_FRAC
Definition: imu.h:39
Int32Rates::q
int32_t q
in rad/s with INT32_RATE_FRAC
Definition: pprz_algebra_int.h:181
mag_sum
static struct Int32Vect3 mag_sum
Definition: ahrs_aligner.c:45
Int32Vect3::z
int32_t z
Definition: pprz_algebra_int.h:91
abi_struct
Event structure to store callbacks in a linked list.
Definition: abi_common.h:65
send_aligner
static void send_aligner(struct transport_tx *trans, struct link_device *dev)
Definition: ahrs_aligner.c:66
VECT3_SDIV
#define VECT3_SDIV(_vo, _vi, _s)
Definition: pprz_algebra.h:196
gyro_cb
static void gyro_cb(uint8_t sender_id, uint32_t stamp, struct Int32Rates *gyro)
Definition: ahrs_aligner.c:54
uint32_t
unsigned long uint32_t
Definition: types.h:18
AhrsAligner::low_noise_cnt
int32_t low_noise_cnt
Definition: ahrs_aligner.h:44
AhrsAligner
Definition: ahrs_aligner.h:39
LED_ON
#define LED_ON(i)
Definition: led_hw.h:51
VECT3_ADD
#define VECT3_ADD(_a, _b)
Definition: pprz_algebra.h:147
LED_TOGGLE
#define LED_TOGGLE(i)
Definition: led_hw.h:53
ahrs_aligner.h
AhrsAligner::lp_gyro
struct Int32Rates lp_gyro
Definition: ahrs_aligner.h:40
AHRS_ALIGNER_LOCKED
#define AHRS_ALIGNER_LOCKED
Definition: ahrs_aligner.h:37
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
telemetry.h
imu.h
AHRS_ALIGNER_IMU_ID
#define AHRS_ALIGNER_IMU_ID
Definition: ahrs_aligner.c:50
AhrsAligner::lp_mag
struct Int32Vect3 lp_mag
Definition: ahrs_aligner.h:42
dev
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:74
AHRS_ALIGNER_RUNNING
#define AHRS_ALIGNER_RUNNING
Definition: ahrs_aligner.h:36
ahrs_aligner
struct AhrsAligner ahrs_aligner
Definition: ahrs_aligner.c:37
AhrsAligner::status
uint8_t status
Definition: ahrs_aligner.h:45
Int32Vect3
Definition: pprz_algebra_int.h:88
sys_time.h
Architecture independent timing functions.
uint8_t
unsigned char uint8_t
Definition: types.h:14
register_periodic_telemetry
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46
AhrsAligner::noise
int32_t noise
Definition: ahrs_aligner.h:43
led.h
arch independent LED (Light Emitting Diodes) API
gyro_sum
static struct Int32Rates gyro_sum
Definition: ahrs_aligner.c:43
RATES_ADD
#define RATES_ADD(_a, _b)
Definition: pprz_algebra.h:344
imu
struct Imu imu
global IMU state
Definition: imu.c:108
AhrsAligner::lp_accel
struct Int32Vect3 lp_accel
Definition: ahrs_aligner.h:41
LOW_NOISE_TIME
#define LOW_NOISE_TIME
Number of cycles (100 samples each) with low noise.
Definition: ahrs_aligner.c:105
RATES_SDIV
#define RATES_SDIV(_ro, _ri, _s)
Definition: pprz_algebra.h:386
int32_t
signed long int32_t
Definition: types.h:19
gyro_ev
static abi_event gyro_ev
Definition: ahrs_aligner.c:52
Imu::mag
struct Int32Vect3 mag
magnetometer measurements scaled to 1 in BFP with INT32_MAG_FRAC
Definition: imu.h:40
INT_VECT3_ZERO
#define INT_VECT3_ZERO(_v)
Definition: pprz_algebra_int.h:287
ahrs_aligner_run
void ahrs_aligner_run(void)
Definition: ahrs_aligner.c:108
ABI_BROADCAST
#define ABI_BROADCAST
Broadcast address.
Definition: abi_common.h:56
ref_sensor_samples
static int32_t ref_sensor_samples[AHRS_ALIGNER_SAMPLES_NB]
Definition: ahrs_aligner.c:46
Int32Rates::r
int32_t r
in rad/s with INT32_RATE_FRAC
Definition: pprz_algebra_int.h:182
accel_sum
static struct Int32Vect3 accel_sum
Definition: ahrs_aligner.c:44
DefaultPeriodic
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
ahrs_aligner_init
void ahrs_aligner_init(void)
Definition: ahrs_aligner.c:81
Imu::gyro
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:38