Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
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 
42 #include "sensors/airspeed_ets.h"
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
59 PRINT_CONFIG_MSG("USE_AIRSPEED_ETS automatically set to TRUE")
60 #endif
61 #endif
62 
63 #if !USE_AIRSPEED_ETS
64 PRINT_CONFIG_MSG("AIRSPEED_ETS not used")
65 #endif
66 
67 #define AIRSPEED_ETS_ADDR 0xEA
68 #ifndef AIRSPEED_ETS_SCALE
69 #define AIRSPEED_ETS_SCALE 1.8
70 #endif
71 #ifndef AIRSPEED_ETS_OFFSET
72 #define AIRSPEED_ETS_OFFSET 0
73 #endif
74 #define AIRSPEED_ETS_OFFSET_MAX 1750
75 #define AIRSPEED_ETS_OFFSET_MIN 1450
76 #define AIRSPEED_ETS_OFFSET_NBSAMPLES_INIT 40
77 #define AIRSPEED_ETS_OFFSET_NBSAMPLES_AVRG 60
78 #define AIRSPEED_ETS_NBSAMPLES_AVRG 10
79 
80 #ifndef AIRSPEED_ETS_I2C_DEV
81 #define AIRSPEED_ETS_I2C_DEV i2c0
82 #endif
84 
85 
86 #ifndef AIRSPEED_ETS_START_DELAY
87 #define AIRSPEED_ETS_START_DELAY 0.2
88 #endif
90 
91 #ifndef SITL
92 #if AIRSPEED_ETS_SDLOG
94 #include "modules/gps/gps.h"
95 bool log_airspeed_ets_started;
96 #endif
97 #endif
98 
99 
100 
101 // Global variables
108 
110 
111 // Local variables
112 volatile bool airspeed_ets_i2c_done;
118 
119 static void airspeed_ets_downlink(struct transport_tx *trans, struct link_device *dev)
120 {
121  uint8_t dev_id = AIRSPEED_ETS_ID;
122  float press = 0;
123  float temp = 0;
124  float offset = airspeed_ets_offset;
125  pprz_msg_send_AIRSPEED_RAW(trans,dev,AC_ID,
126  &dev_id,
128  &offset,
129  &press,
130  &temp,
131  &airspeed_ets);
132 }
133 
135 {
136  int n;
137  airspeed_ets_raw = 0;
138  airspeed_ets = 0.0;
141  airspeed_ets_i2c_done = true;
142  airspeed_ets_valid = false;
143  airspeed_ets_offset_init = false;
145 
147  for (n = 0; n < AIRSPEED_ETS_NBSAMPLES_AVRG; ++n) {
148  airspeed_ets_buffer[n] = 0.0;
149  }
150 
152 
153  airspeed_ets_delay_done = false;
155 
156 
157 #if PERIODIC_TELEMETRY
159 #endif
160 
161 #ifndef SITL
162 #if AIRSPEED_ETS_SDLOG
163  log_airspeed_ets_started = false;
164 #endif
165 #endif
166 }
167 
169 {
170 #ifndef SITL
173  else { airspeed_ets_delay_done = true; }
174  }
177  }
178 #elif !defined USE_NPS
179  extern float sim_air_speed;
180  stateSetAirspeed_f(MODULE_AIRSPEED_ETS_ID, sim_air_speed);
181 #endif //SITL
182 }
183 
185 {
186  int n;
187  float airspeed_tmp = 0.0;
188 
189  // Get raw airspeed from buffer
191  // Check if this is valid airspeed
192  if (airspeed_ets_raw == 0) {
193  airspeed_ets_valid = false;
194  } else {
195  airspeed_ets_valid = true;
196  }
197 
198  // Continue only if a new airspeed value was received
199  if (airspeed_ets_valid) {
200 #if !AIRSPEED_ETS_3RD_PARTY_MODE
201  // Calculate offset average if not done already
204  // Check if averaging completed
205  if (airspeed_ets_cnt == 0) {
206  // Calculate average
208  // Limit offset
211  }
214  }
216  }
217  // Check if averaging needs to continue
220  }
221  }
222  // Convert raw to m/s
223 #ifdef AIRSPEED_ETS_REVERSE
225  airspeed_tmp = AIRSPEED_ETS_SCALE * sqrtf((float)(airspeed_ets_offset - airspeed_ets_raw)) - AIRSPEED_ETS_OFFSET;
226  }
227 #else
229  airspeed_tmp = AIRSPEED_ETS_SCALE * sqrtf((float)(airspeed_ets_raw - airspeed_ets_offset)) - AIRSPEED_ETS_OFFSET;
230  }
231 #endif
232  else {
233  airspeed_tmp = 0.0;
234  }
235 //use raw value for sensor set to third-party mode
236 #else
237  airspeed_tmp = airspeed_ets_raw;
238 #endif //AIRSPEED_ETS_3RD_PARTY_MODE
239 
240  // Airspeed should always be positive
241  if (airspeed_tmp < 0.0) {
242  airspeed_tmp = 0.0;
243  }
244  // Moving average
248  }
249  airspeed_ets = 0.0;
250  for (n = 0; n < AIRSPEED_ETS_NBSAMPLES_AVRG; ++n) {
252  }
254 
255  // Publish airspeed sensor
256  AbiSendMsgAIRSPEED(AIRSPEED_ETS_ID, airspeed_ets);
257 
258 #if USE_AIRSPEED_ETS
259  stateSetAirspeed_f(MODULE_AIRSPEED_ETS_ID, airspeed_ets);
260 #endif
261  } else {
262  airspeed_ets = 0.0;
263  }
264 
265 
266 #if AIRSPEED_ETS_SDLOG
267 #ifndef SITL
268  if (pprzLogFile != -1) {
269  if (!log_airspeed_ets_started) {
270  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");
271  log_airspeed_ets_started = true;
272  }
273  sdLogWriteLog(pprzLogFile, "airspeed_ets: %d %d %8.4f %d %d %d %d %d %d %d %d %d\n",
275  gps.fix, gps.tow, gps.week,
278  }
279 #endif
280 #endif
281 
282 
283 
284  // Transaction has been read
286 }
Main include for ABI (AirBorneInterface).
#define AIRSPEED_ETS_ID
float airspeed_ets_buffer[AIRSPEED_ETS_NBSAMPLES_AVRG]
Definition: airspeed_ets.c:107
static void airspeed_ets_downlink(struct transport_tx *trans, struct link_device *dev)
Definition: airspeed_ets.c:119
#define AIRSPEED_ETS_OFFSET_MIN
Definition: airspeed_ets.c:75
bool airspeed_ets_delay_done
Definition: airspeed_ets.c:117
#define AIRSPEED_ETS_SCALE
Definition: airspeed_ets.c:69
bool airspeed_ets_offset_init
Definition: airspeed_ets.c:113
volatile bool airspeed_ets_i2c_done
Definition: airspeed_ets.c:112
uint32_t airspeed_ets_offset_tmp
Definition: airspeed_ets.c:114
uint16_t airspeed_ets_cnt
Definition: airspeed_ets.c:115
float airspeed_ets
Definition: airspeed_ets.c:105
#define AIRSPEED_ETS_OFFSET_NBSAMPLES_INIT
Definition: airspeed_ets.c:76
#define AIRSPEED_ETS_OFFSET_NBSAMPLES_AVRG
Definition: airspeed_ets.c:77
uint16_t airspeed_ets_raw
Definition: airspeed_ets.c:102
#define AIRSPEED_ETS_START_DELAY
delay in seconds until sensor is read after startup
Definition: airspeed_ets.c:87
#define AIRSPEED_ETS_OFFSET_MAX
Definition: airspeed_ets.c:74
void airspeed_ets_read_event(void)
Definition: airspeed_ets.c:184
uint32_t airspeed_ets_delay_time
Definition: airspeed_ets.c:116
bool airspeed_ets_valid
Definition: airspeed_ets.c:104
#define AIRSPEED_ETS_NBSAMPLES_AVRG
Definition: airspeed_ets.c:78
uint16_t airspeed_ets_offset
Definition: airspeed_ets.c:103
void airspeed_ets_init(void)
Definition: airspeed_ets.c:134
void airspeed_ets_read_periodic(void)
Definition: airspeed_ets.c:168
#define AIRSPEED_ETS_OFFSET
Definition: airspeed_ets.c:72
struct i2c_transaction airspeed_ets_i2c_trans
Definition: airspeed_ets.c:109
int airspeed_ets_buffer_idx
Definition: airspeed_ets.c:106
#define AIRSPEED_ETS_I2C_DEV
Definition: airspeed_ets.c:81
#define AIRSPEED_ETS_ADDR
Definition: airspeed_ets.c:67
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:109
int32_t hmsl
height above mean sea level (MSL) in mm
Definition: gps.h:94
struct LlaCoor_i lla_pos
position in LLA (lat,lon: deg*1e7; alt: mm over ellipsoid)
Definition: gps.h:92
int32_t course
GPS course over ground in rad*1e7, [0, 2*Pi]*1e7 (CW/north)
Definition: gps.h:99
struct NedCoor_i ned_vel
speed NED in cm/s
Definition: gps.h:96
uint16_t gspeed
norm of 2d ground speed in cm/s
Definition: gps.h:97
uint16_t week
GPS week.
Definition: gps.h:108
uint8_t fix
status of fix
Definition: gps.h:107
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
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:334
@ I2CTransDone
transaction set to done by user level
Definition: i2c.h:59
I2C transaction structure.
Definition: i2c.h:93
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")
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
FileDes pprzLogFile
Definition: sdlog_chibios.c:75
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
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
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98