Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
airspeed_ets.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2009 Vassilis Varveropoulos
3 * Modified by Mark Griffin on 8 September 2010 to work with new i2c transaction routines.
4 * Converted by Gautier Hattenberger to modules (10/2010)
5 *
6 * This file is part of paparazzi.
7 *
8 * paparazzi is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * paparazzi is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with paparazzi; see the file COPYING. If not, write to
20 * the Free Software Foundation, 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 */
23
43#include "state.h"
44#include "mcu_periph/i2c.h"
45#include "mcu_periph/uart.h"
46#include "mcu_periph/sys_time.h"
47#include "pprzlink/messages.h"
48#include "modules/core/abi.h"
50#include <math.h>
51
52#if PERIODIC_TELEMETRY
54#endif
55
56#ifndef USE_AIRSPEED_ETS
57#if USE_AIRSPEED
58#define USE_AIRSPEED_ETS TRUE
59PRINT_CONFIG_MSG("USE_AIRSPEED_ETS automatically set to TRUE")
60#endif
61#endif
62
63#if !USE_AIRSPEED_ETS
64PRINT_CONFIG_MSG("AIRSPEED_ETS not used")
65#endif
66
67#define AIRSPEED_ETS_ADDR 0xEA
68
69#ifndef AIRSPEED_ETS_SCALE
70#define AIRSPEED_ETS_SCALE 1.8
71#endif
72#ifndef AIRSPEED_ETS_OFFSET
73#define AIRSPEED_ETS_OFFSET 0
74#endif
75#define AIRSPEED_ETS_OFFSET_MAX 1750
76#define AIRSPEED_ETS_OFFSET_MIN 1450
77#define AIRSPEED_ETS_OFFSET_NBSAMPLES_INIT 40
78#define AIRSPEED_ETS_OFFSET_NBSAMPLES_AVRG 60
79
80#ifndef AIRSPEED_ETS_USE_FILTER
81#define AIRSPEED_ETS_USE_FILTER FALSE
82#endif
83
84#ifdef AIRSPEED_ETS_USE_FILTER
85#define AIRSPEED_ETS_NBSAMPLES_AVRG 20
86#else
87#define AIRSPEED_ETS_NBSAMPLES_AVRG 10
88#endif
89
93#ifndef AIRSPEED_ETS_LOWPASS_TAU
94#define AIRSPEED_ETS_LOWPASS_TAU 0.15
95#endif
96
97#ifndef AIRSPEED_ETS_I2C_DEV
98#define AIRSPEED_ETS_I2C_DEV i2c0
99#endif
101
102
103#ifndef AIRSPEED_ETS_START_DELAY
104#define AIRSPEED_ETS_START_DELAY 0.2
105#endif
107
108#ifdef AIRSPEED_ETS_USE_FILTER
110#endif
111
112#ifndef SITL
113#if AIRSPEED_ETS_SDLOG
115#include "modules/gps/gps.h"
117#endif
118#endif
119
120
121
122// Global variables
129
131
132#ifdef AIRSPEED_ETS_USE_FILTER
134#endif
135
136// Local variables
143
144static void airspeed_ets_downlink(struct transport_tx *trans, struct link_device *dev)
145{
146 uint8_t dev_id = AIRSPEED_ETS_ID;
147 float press = 0;
148 float temp = 0;
151 &dev_id,
153 &offset,
154 &press,
155 &temp,
156 &airspeed_ets);
157}
158
160{
161 int n;
163 airspeed_ets = 0.0;
167 airspeed_ets_valid = false;
170
172 for (n = 0; n < AIRSPEED_ETS_NBSAMPLES_AVRG; ++n) {
173 airspeed_ets_buffer[n] = 0.0;
174 }
175
176#ifdef AIRSPEED_ETS_USE_FILTER
179#endif
180
182
185
186
187#if PERIODIC_TELEMETRY
189#endif
190
191#ifndef SITL
192#if AIRSPEED_ETS_SDLOG
194#endif
195#endif
196}
197
199{
200#ifndef SITL
203 else { airspeed_ets_delay_done = true; }
204 }
207 }
208#elif !defined USE_NPS
209 extern float sim_air_speed;
211#endif //SITL
212}
213
215{
216 int n;
217 float airspeed_tmp = 0.0;
218
219 // Get raw airspeed from buffer
221 // Check if this is valid airspeed
222 if (airspeed_ets_raw == 0) {
223 airspeed_ets_valid = false;
224 } else {
225 airspeed_ets_valid = true;
226 }
227
228 // Continue only if a new airspeed value was received
229 if (airspeed_ets_valid) {
230#if !AIRSPEED_ETS_3RD_PARTY_MODE
231
232 // Calculate offset average if not done already
235 // Check if averaging completed
236 if (airspeed_ets_cnt == 0) {
237 // Calculate average
239 // Limit offset
242 }
245 }
247 }
248 // Check if averaging needs to continue
251 }
252 }
253
254 // Convert raw to m/s
255#ifdef AIRSPEED_ETS_REVERSE
258 }
259#else
262 }
263#endif
264 else {
265 airspeed_tmp = 0.0;
266 }
267//use raw value for sensor set to third-party mode
268#else
270#endif //AIRSPEED_ETS_3RD_PARTY_MODE
271
272 // Airspeed should always be positive
273 if (airspeed_tmp < 0.0) {
274 airspeed_tmp = 0.0;
275 }
276 // Moving average
280 }
281 airspeed_ets = 0.0;
282 for (n = 0; n < AIRSPEED_ETS_NBSAMPLES_AVRG; ++n) {
284 }
286
287#ifdef AIRSPEED_ETS_USE_FILTER
288 float a_out;
291#endif
292
293 //Sometimes offset is not correct enough calculated dynamically and yields to a small negative airspeed once in a while at certain samples
294 //Also with steep airspeed decline and a butterworth_2_low_pass filter on it could overshoot to a negative value this is also capped
295 //so force cap it to zero
296 if ((airspeed_ets < 0.0) || (airspeed_tmp < 0.0)) {
297 airspeed_tmp = 0.0; //for filter also
298 airspeed_ets = 0.0;
299 }
300
301 // Publish airspeed sensor
303
304#if USE_AIRSPEED_ETS
306#endif
307 } else {
308 airspeed_ets = 0.0;
309 }
310
311
312#if AIRSPEED_ETS_SDLOG
313#ifndef SITL
314 if (pprzLogFile != -1) {
316 sdLogWriteLog(pprzLogFile, "AIRSPEED_ETS: raw offset airspeed(m/s) GPS_fix TOW(ms) Week Lat(1e7deg) Lon(1e7deg) HMSL(mm) gpseed(cm/s) course(1e7rad) climb(cm/s)\n");
318 }
319 sdLogWriteLog(pprzLogFile, "airspeed_ets: %d %d %8.4f %d %d %d %d %d %d %d %d %d\n",
321 gps.fix, gps.tow, gps.week,
324 }
325#endif
326#endif
327
328
329
330 // Transaction has been read
332}
Main include for ABI (AirBorneInterface).
#define AIRSPEED_ETS_ID
float airspeed_ets_buffer[AIRSPEED_ETS_NBSAMPLES_AVRG]
#define AIRSPEED_ETS_LOWPASS_TAU
Time constant for second order Butterworth low pass filter Default of 0.15 should give cut-off freq o...
static void airspeed_ets_downlink(struct transport_tx *trans, struct link_device *dev)
#define AIRSPEED_ETS_OFFSET_MIN
bool airspeed_ets_delay_done
#define AIRSPEED_ETS_SCALE
bool airspeed_ets_offset_init
volatile bool airspeed_ets_i2c_done
uint32_t airspeed_ets_offset_tmp
uint16_t airspeed_ets_cnt
float airspeed_ets
#define AIRSPEED_ETS_OFFSET_NBSAMPLES_INIT
#define AIRSPEED_ETS_OFFSET_NBSAMPLES_AVRG
uint16_t airspeed_ets_raw
#define AIRSPEED_ETS_START_DELAY
delay in seconds until sensor is read after startup
static Butterworth2LowPass airspeed_filter
#define AIRSPEED_ETS_OFFSET_MAX
void airspeed_ets_read_event(void)
uint32_t airspeed_ets_delay_time
bool airspeed_ets_valid
#define AIRSPEED_ETS_NBSAMPLES_AVRG
uint16_t airspeed_ets_offset
void airspeed_ets_init(void)
void airspeed_ets_read_periodic(void)
#define AIRSPEED_ETS_OFFSET
struct i2c_transaction airspeed_ets_i2c_trans
int airspeed_ets_buffer_idx
#define AIRSPEED_ETS_I2C_DEV
#define AIRSPEED_ETS_ADDR
Driver for the EagleTree Systems Airspeed Sensor.
static const float offset[]
struct GpsState gps
global GPS state
Definition gps.c:74
Device independent GPS code (interface)
uint32_t tow
GPS time of week in ms.
Definition gps.h:112
int32_t hmsl
height above mean sea level (MSL) in mm
Definition gps.h:97
struct LlaCoor_i lla_pos
position in LLA (lat,lon: deg*1e7; alt: mm over ellipsoid)
Definition gps.h:95
int32_t course
GPS course over ground in rad*1e7, [0, 2*Pi]*1e7 (CW/north)
Definition gps.h:102
struct NedCoor_i ned_vel
speed NED in cm/s
Definition gps.h:99
uint16_t gspeed
norm of 2d ground speed in cm/s
Definition gps.h:100
uint16_t week
GPS week.
Definition gps.h:111
uint8_t fix
status of fix
Definition gps.h:110
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition i2c.h:123
enum I2CTransactionStatus status
Transaction status.
Definition i2c.h:127
bool i2c_receive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint16_t len)
Submit a read only transaction.
Definition i2c.c:212
@ I2CTransDone
transaction set to done by user level
Definition i2c.h:60
I2C transaction structure.
Definition i2c.h:94
int32_t lat
in degrees*1e7
int32_t z
Down.
int32_t lon
in degrees*1e7
static void stateSetAirspeed_f(uint16_t id, float airspeed)
Set airspeed (float).
Definition state.h:1486
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
Simple first order low pass filter with bilinear transform.
static void init_butterworth_2_low_pass(Butterworth2LowPass *filter, float tau, float sample_time, float value)
Init a second order Butterworth filter.
static float update_butterworth_2_low_pass(Butterworth2LowPass *filter, float value)
Update second order Butterworth low pass filter state with a new value.
Second order low pass filter structure.
uint16_t foo
Definition main_demo5.c:58
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
FileDes pprzLogFile
API to get/set the generic vehicle states.
static const struct usb_device_descriptor dev
Definition usb_ser_hw.c:74
Architecture independent timing functions.
#define SysTimeTimerStart(_t)
Definition sys_time.h:227
#define USEC_OF_SEC(sec)
Definition sys_time.h:219
#define SysTimeTimer(_t)
Definition sys_time.h:228
int16_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint16_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
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.