Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
sys_id_chirp.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) Joost Meulenbeld
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, see
18  * <http://www.gnu.org/licenses/>.
19  */
26 #include "std.h"
27 
28 #include "sys_id_chirp.h"
29 #include "pprz_chirp.h"
30 
32 #include "generated/airframe.h"
33 #include "mcu_periph/sys_time.h"
35 #include "math/pprz_random.h"
36 
37 
38 #ifndef SYS_ID_CHIRP_AXES
39 #define SYS_ID_CHIRP_AXES {COMMAND_ROLL,COMMAND_PITCH,COMMAND_YAW,COMMAND_THRUST}
40 #endif
41 
42 #ifndef SYS_ID_CHIRP_ENABLED
43 #define SYS_ID_CHIRP_ENABLED TRUE
44 #endif
45 
46 #ifndef SYS_ID_CHIRP_USE_NOISE
47 #define SYS_ID_CHIRP_USE_NOISE TRUE
48 #endif
49 
50 #ifdef SYS_ID_CHIRP_RADIO_CHANNEL
52 pprz_t previous_radio_value_chirp = 0;
53 #endif
54 
55 // #ifndef SYS_ID_CHIRP_EXPONENTIAL
56 // #define SYS_ID_CHIRP_EXPONENTIAL TRUE
57 // #endif
58 
59 // #ifndef SYS_ID_CHIRP_FADEIN
60 // #define SYS_ID_CHIRP_FADEIN TRUE
61 // #endif
62 
63 
64 static struct chirp_t chirp;
70 float chirp_fstart_hz = 1.0f;
71 float chirp_fstop_hz = 5.0f;
72 float chirp_length_s = 20;
73 
76 
77 // The axes on which noise and chirp values can be applied
79 #define SYS_ID_CHIRP_NB_AXES sizeof SYS_ID_ACTIVE_CHIRP_AXES / sizeof SYS_ID_ACTIVE_CHIRP_AXES[0] // Number of items in ACTIVE_CHIRP_AXES
80 
81 // Filters used to cut-off the gaussian noise fed into the actuator channels
83 
84 // Chirp and noise values for all axes (indices correspond to the axes given in CHIRP_AXES)
86 
87 static void set_current_chirp_values(void)
88 {
89  // initializing at zero the chirp input for every axis
90  for (uint8_t i = 0; i < SYS_ID_CHIRP_NB_AXES; i++) {
91  current_chirp_values[i] = 0;
92  }
93  // adding values if the chirp is active
94  if (chirp_active) {
95  // adding extra on the chirp signal (both on-axis and off axis)
96  #if SYS_ID_CHIRP_USE_NOISE
97 
98  float amplitude, noise;
99  for (uint8_t i = 0; i < SYS_ID_CHIRP_NB_AXES; i++) {
102  current_chirp_values[i] += (int32_t)(noise * amplitude);
103  }
104 
105  #endif
106  // adding nominal chirp value
108  } else {
109  for (uint8_t i = 0; i < SYS_ID_CHIRP_NB_AXES; i++) {
110  current_chirp_values[i] = 0;
111  }
112  }
113 }
114 
115 static void send_chirp(struct transport_tx *trans, struct link_device *dev)
116 {
117  pprz_msg_send_CHIRP(trans, dev, AC_ID, &chirp_active, &chirp.percentage_done, &chirp.current_frequency_hz,
120 
121 }
122 
123 static void start_chirp(void)
124 {
126  chirp_active = TRUE;
128 }
129 
130 static void stop_chirp(void)
131 {
135 }
136 
138 {
139  chirp_active = activate;
140  #ifdef SYS_ID_CHIRP_RADIO_CHANNEL
141  // Don't activate chirp when radio signal is low
142  if (radio_control.values[SYS_ID_CHIRP_RADIO_CHANNEL] < 1750)
143  {
144  chirp_active = 0;
145  }
146  #endif
147  if (chirp_active) {
149  chirp_fade_in);
150  start_chirp();
151  } else {
152  stop_chirp();
153  }
154 }
155 
157 {
158  return chirp_active;
159 }
160 
162 {
163  if (axis < SYS_ID_CHIRP_NB_AXES) {
164  chirp_axis = axis;
165  }
166 }
167 
168 extern void sys_id_chirp_fstart_handler(float fstart)
169 {
170  if (fstart < chirp_fstop_hz) {
171  chirp_fstart_hz = fstart;
172  }
173 }
174 
175 extern void sys_id_chirp_fstop_handler(float fstop)
176 {
177  if (fstop > chirp_fstart_hz) {
178  chirp_fstop_hz = fstop;
179  }
180 }
181 
183 {
184  chirp_fade_in = fade_in;
185 }
186 
188 {
189  chirp_exponential = exponential;
190 }
191 
193 {
194 #if SYS_ID_CHIRP_USE_NOISE
195 
196  init_random();
197 
198 #endif
199 
201  chirp_fade_in);
204 
205  // Filter cutoff frequency is the chirp maximum frequency
206  float tau = 1 / (chirp_fstop_hz * 2 * M_PI);
207  for (uint8_t i = 0; i < SYS_ID_CHIRP_NB_AXES; i++) {
208  init_first_order_low_pass(&filters[i], tau, SYS_ID_CHIRP_RUN_PERIOD, 0);
209  current_chirp_values[i] = 0;
210  }
211 }
212 
214 {
215 #if SYS_ID_CHIRP_ENABLED
216 
217  #ifdef SYS_ID_CHIRP_RADIO_CHANNEL
218  // Check if chirp switched on when off before
219  if (previous_radio_value_chirp < 1750)
220  {
221  if (radio_control.values[SYS_ID_CHIRP_RADIO_CHANNEL] > 1750)
222  {
223  // Activate chirp
225  }
226  }
227  // Check if chirp switched off when on before
228  if (previous_radio_value_chirp > 1750)
229  {
230  if (radio_control.values[SYS_ID_CHIRP_RADIO_CHANNEL] < 1750)
231  {
232  // Deactivate chirp
234  }
235  }
236  previous_radio_value_chirp = radio_control.values[SYS_ID_CHIRP_RADIO_CHANNEL];
237  #endif
238 
239  if (chirp_active) {
241  stop_chirp();
242  } else {
245  }
246  }
247 
248 #endif
249 }
250 
251 void sys_id_chirp_add_values(bool UNUSED motors_on, bool UNUSED override_on, pprz_t UNUSED in_cmd[])
252 {
253 
254 #if SYS_ID_CHIRP_ENABLED
255 
256  if (motors_on) {
257  for (uint8_t i = 0; i < SYS_ID_CHIRP_NB_AXES; i++) {
259  BoundAbs(in_cmd[SYS_ID_ACTIVE_CHIRP_AXES[i]], MAX_PPRZ);
260  }
261  }
262 
263 #endif
264 }
uint8_t last_wp UNUSED
Simple first order low pass filter with bilinear transform.
static float update_first_order_low_pass(struct FirstOrderLowPass *filter, float value)
Update first order low pass filter state with a new value.
static void init_first_order_low_pass(struct FirstOrderLowPass *filter, float tau, float sample_time, float value)
Init first order low pass filter.
First order low pass filter structure.
int16_t pprz_t
Definition: paparazzi.h:6
#define MAX_PPRZ
Definition: paparazzi.h:8
bool chirp_is_running(struct chirp_t *chirp, float current_time_s)
Return if the current_time is within the chirp manoeuvre.
Definition: pprz_chirp.c:65
void chirp_init(struct chirp_t *chirp, float f0_hz, float f1_hz, float length_s, float current_time_s, bool exponential_chirp, bool fade_in)
Allocate and initialize a new chirp struct.
Definition: pprz_chirp.c:34
float chirp_update(struct chirp_t *chirp, float current_time_s)
Calculate the value at current_time_s and update the struct with current frequency and value.
Definition: pprz_chirp.c:71
void chirp_reset(struct chirp_t *chirp, float current_time_s)
Reset the time of the chirp.
Definition: pprz_chirp.c:56
float current_frequency_hz
Definition: pprz_chirp.h:66
float percentage_done
Definition: pprz_chirp.h:69
float current_value
Definition: pprz_chirp.h:67
Initialize with chirp_init.
Definition: pprz_chirp.h:59
void init_random(void)
Definition: pprz_random.c:35
double rand_gaussian(void)
Definition: pprz_random.c:53
struct RadioControl radio_control
Definition: radio_control.c:33
Generic interface for radio control modules.
pprz_t values[RADIO_CONTROL_NB_CHANNEL]
Definition: radio_control.h:67
#define TRUE
Definition: std.h:4
#define FALSE
Definition: std.h:5
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:74
void sys_id_chirp_fstart_handler(float fstart)
Definition: sys_id_chirp.c:168
static struct chirp_t chirp
Definition: sys_id_chirp.c:64
float chirp_noise_stdv_onaxis_ratio
Definition: sys_id_chirp.c:68
float chirp_length_s
Definition: sys_id_chirp.c:72
#define SYS_ID_CHIRP_AXES
Definition: sys_id_chirp.c:39
void sys_id_chirp_add_values(bool UNUSED motors_on, bool UNUSED override_on, pprz_t UNUSED in_cmd[])
Definition: sys_id_chirp.c:251
void sys_id_chirp_activate_handler(uint8_t activate)
Definition: sys_id_chirp.c:137
void sys_id_chirp_exponential_activate_handler(uint8_t exponential)
Definition: sys_id_chirp.c:187
uint8_t chirp_active
Definition: sys_id_chirp.c:65
void sys_id_chirp_run(void)
Definition: sys_id_chirp.c:213
float chirp_noise_stdv_offaxis
Definition: sys_id_chirp.c:69
static void stop_chirp(void)
Definition: sys_id_chirp.c:130
float chirp_fstop_hz
Definition: sys_id_chirp.c:71
pprz_t chirp_amplitude
Definition: sys_id_chirp.c:67
float chirp_fstart_hz
Definition: sys_id_chirp.c:70
uint8_t chirp_axis
Definition: sys_id_chirp.c:66
void sys_id_chirp_axis_handler(uint8_t axis)
Definition: sys_id_chirp.c:161
static void send_chirp(struct transport_tx *trans, struct link_device *dev)
Definition: sys_id_chirp.c:115
uint8_t chirp_exponential
Definition: sys_id_chirp.c:75
static pprz_t current_chirp_values[SYS_ID_CHIRP_NB_AXES]
Definition: sys_id_chirp.c:85
uint8_t chirp_fade_in
Definition: sys_id_chirp.c:74
static void set_current_chirp_values(void)
Definition: sys_id_chirp.c:87
static struct FirstOrderLowPass filters[SYS_ID_CHIRP_NB_AXES]
Definition: sys_id_chirp.c:82
uint8_t sys_id_chirp_running(void)
Definition: sys_id_chirp.c:156
static const int8_t SYS_ID_ACTIVE_CHIRP_AXES[]
Definition: sys_id_chirp.c:78
static void start_chirp(void)
Definition: sys_id_chirp.c:123
#define SYS_ID_CHIRP_NB_AXES
Definition: sys_id_chirp.c:79
void sys_id_chirp_fstop_handler(float fstop)
Definition: sys_id_chirp.c:175
void sys_id_chirp_init(void)
Definition: sys_id_chirp.c:192
void sys_id_chirp_fade_in_activate_handler(uint8_t fade_in)
Definition: sys_id_chirp.c:182
Architecture independent timing functions.
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition: sys_time.h:138
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:51
Periodic telemetry system header (includes downlink utility and generated code).
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98
signed char int8_t
Typedef defining 8 bit char type.
Definition: vl53l1_types.h:103