Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 "messages.h"
49 #include <math.h>
50 
51 #ifndef USE_AIRSPEED_ETS
52 #if USE_AIRSPEED
53 #define USE_AIRSPEED_ETS TRUE
54 PRINT_CONFIG_MSG("USE_AIRSPEED_ETS automatically set to TRUE")
55 #endif
56 #endif
57 
58 #if !USE_AIRSPEED_ETS && !AIRSPEED_ETS_SYNC_SEND
59 #warning either set USE_AIRSPEED_ETS or AIRSPEED_ETS_SYNC_SEND to use airspeed_ets
60 #endif
61 
62 #define AIRSPEED_ETS_ADDR 0xEA
63 #ifndef AIRSPEED_ETS_SCALE
64 #define AIRSPEED_ETS_SCALE 1.8
65 #endif
66 #ifndef AIRSPEED_ETS_OFFSET
67 #define AIRSPEED_ETS_OFFSET 0
68 #endif
69 #define AIRSPEED_ETS_OFFSET_MAX 1750
70 #define AIRSPEED_ETS_OFFSET_MIN 1450
71 #define AIRSPEED_ETS_OFFSET_NBSAMPLES_INIT 40
72 #define AIRSPEED_ETS_OFFSET_NBSAMPLES_AVRG 60
73 #define AIRSPEED_ETS_NBSAMPLES_AVRG 10
74 
75 #ifndef AIRSPEED_ETS_I2C_DEV
76 #define AIRSPEED_ETS_I2C_DEV i2c0
77 #endif
78 PRINT_CONFIG_VAR(AIRSPEED_ETS_I2C_DEV)
79 
80 
81 #ifndef AIRSPEED_ETS_START_DELAY
82 #define AIRSPEED_ETS_START_DELAY 0.2
83 #endif
84 PRINT_CONFIG_VAR(AIRSPEED_ETS_START_DELAY)
85 
86 
87 // Global variables
94 
96 
97 // Local variables
98 volatile bool_t airspeed_ets_i2c_done;
104 
106 {
107  int n;
108  airspeed_ets_raw = 0;
109  airspeed_ets = 0.0;
110  airspeed_ets_offset = 0;
111  airspeed_ets_offset_tmp = 0;
112  airspeed_ets_i2c_done = TRUE;
113  airspeed_ets_valid = FALSE;
114  airspeed_ets_offset_init = FALSE;
116 
117  airspeed_ets_buffer_idx = 0;
118  for (n = 0; n < AIRSPEED_ETS_NBSAMPLES_AVRG; ++n) {
119  airspeed_ets_buffer[n] = 0.0;
120  }
121 
122  airspeed_ets_i2c_trans.status = I2CTransDone;
123 
124  airspeed_ets_delay_done = FALSE;
125  SysTimeTimerStart(airspeed_ets_delay_time);
126 }
127 
129 {
130 #ifndef SITL
133  else { airspeed_ets_delay_done = TRUE; }
134  }
137  }
138 #elif !defined USE_NPS
139  extern float sim_air_speed;
140  stateSetAirspeed_f(sim_air_speed);
141 #endif //SITL
142 }
143 
145 {
146  int n;
147  float airspeed_tmp = 0.0;
148 
149  // Get raw airspeed from buffer
151  // Check if this is valid airspeed
152  if (airspeed_ets_raw == 0) {
154  } else {
156  }
157 
158  // Continue only if a new airspeed value was received
159  if (airspeed_ets_valid) {
160 #if !AIRSPEED_ETS_3RD_PARTY_MODE
161  // Calculate offset average if not done already
164  // Check if averaging completed
165  if (airspeed_ets_cnt == 0) {
166  // Calculate average
168  // Limit offset
171  }
174  }
176  }
177  // Check if averaging needs to continue
180  }
181  }
182  // Convert raw to m/s
183 #ifdef AIRSPEED_ETS_REVERSE
185  airspeed_tmp = AIRSPEED_ETS_SCALE * sqrtf((float)(airspeed_ets_offset - airspeed_ets_raw)) - AIRSPEED_ETS_OFFSET;
186  }
187 #else
189  airspeed_tmp = AIRSPEED_ETS_SCALE * sqrtf((float)(airspeed_ets_raw - airspeed_ets_offset)) - AIRSPEED_ETS_OFFSET;
190  }
191 #endif
192  else {
193  airspeed_tmp = 0.0;
194  }
195 //use raw value for sensor set to third-party mode
196 #else
197  airspeed_tmp = airspeed_ets_raw;
198 #endif //AIRSPEED_ETS_3RD_PARTY_MODE
199 
200  // Airspeed should always be positive
201  if (airspeed_tmp < 0.0) {
202  airspeed_tmp = 0.0;
203  }
204  // Moving average
208  }
209  airspeed_ets = 0.0;
210  for (n = 0; n < AIRSPEED_ETS_NBSAMPLES_AVRG; ++n) {
212  }
213  airspeed_ets = airspeed_ets / (float)AIRSPEED_ETS_NBSAMPLES_AVRG;
214 #if USE_AIRSPEED_ETS
216 #endif
217 #if AIRSPEED_ETS_SYNC_SEND
219 #endif
220  } else {
221  airspeed_ets = 0.0;
222  }
223 
224  // Transaction has been read
226 }
#define AIRSPEED_ETS_OFFSET_NBSAMPLES_INIT
Definition: airspeed_ets.c:71
unsigned short uint16_t
Definition: types.h:16
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
#define AIRSPEED_ETS_NBSAMPLES_AVRG
Definition: airspeed_ets.c:73
float airspeed_ets
Definition: airspeed_ets.c:91
void airspeed_ets_init(void)
Definition: airspeed_ets.c:105
#define AIRSPEED_ETS_SCALE
Definition: airspeed_ets.c:64
#define AIRSPEED_ETS_OFFSET_MIN
Definition: airspeed_ets.c:70
#define FALSE
Definition: std.h:5
uint16_t airspeed_ets_cnt
Definition: airspeed_ets.c:101
void airspeed_ets_read_periodic(void)
Definition: airspeed_ets.c:128
#define TRUE
Definition: std.h:4
float sim_air_speed
Definition: sim_ir.c:14
#define AIRSPEED_ETS_OFFSET_NBSAMPLES_AVRG
Definition: airspeed_ets.c:72
transaction set to done by user level
Definition: i2c.h:59
Driver for the EagleTree Systems Airspeed Sensor.
Architecture independent timing functions.
#define AIRSPEED_ETS_I2C_DEV
Definition: airspeed_ets.c:76
volatile bool_t airspeed_ets_i2c_done
Definition: airspeed_ets.c:98
#define AIRSPEED_ETS_ADDR
Definition: airspeed_ets.c:62
unsigned long uint32_t
Definition: types.h:18
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
#define AIRSPEED_ETS_START_DELAY
delay in seconds until sensor is read after startup
Definition: airspeed_ets.c:82
#define AIRSPEED_ETS_OFFSET
Definition: airspeed_ets.c:67
int airspeed_ets_buffer_idx
Definition: airspeed_ets.c:92
I2C transaction structure.
Definition: i2c.h:93
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
#define USEC_OF_SEC(sec)
Definition: sys_time.h:205
bool_t airspeed_ets_delay_done
Definition: airspeed_ets.c:103
#define SysTimeTimer(_t)
Definition: sys_time.h:214
API to get/set the generic vehicle states.
static void stateSetAirspeed_f(float airspeed)
Set airspeed (float).
Definition: state.h:1254
struct i2c_transaction airspeed_ets_i2c_trans
Definition: airspeed_ets.c:95
uint16_t airspeed_ets_offset
Definition: airspeed_ets.c:89
bool_t airspeed_ets_valid
Definition: airspeed_ets.c:90
void airspeed_ets_read_event(void)
Definition: airspeed_ets.c:144
float airspeed_ets_buffer[AIRSPEED_ETS_NBSAMPLES_AVRG]
Definition: airspeed_ets.c:93
#define SysTimeTimerStart(_t)
Definition: sys_time.h:213
#define AIRSPEED_ETS_OFFSET_MAX
Definition: airspeed_ets.c:69
uint32_t airspeed_ets_delay_time
Definition: airspeed_ets.c:102
bool_t airspeed_ets_offset_init
Definition: airspeed_ets.c:99
uint16_t airspeed_ets_raw
Definition: airspeed_ets.c:88
uint32_t airspeed_ets_offset_tmp
Definition: airspeed_ets.c:100
bool_t 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:268
Architecture independent I2C (Inter-Integrated Circuit Bus) API.