Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
airspeed_amsys.c
Go to the documentation of this file.
1 /*
2  * Driver for a Amsys Differential Presure Sensor I2C
3  * AMS 5812-0003-D
4  *
5  * Copyright (C) 2010 The Paparazzi Team
6  *
7  * This file is part of paparazzi.
8  *
9  * paparazzi is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2, or (at your option)
12  * any later version.
13  *
14  * paparazzi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with paparazzi; see the file COPYING. If not, write to
21  * the Free Software Foundation, 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24 
25 #include "sensors/airspeed_amsys.h"
26 #include "state.h"
27 #include "mcu_periph/i2c.h"
28 #include "mcu_periph/uart.h"
29 #include "messages.h"
31 #include <math.h>
32 //#include <stdlib.h>
33 
34 
35 #define AIRSPEED_AMSYS_ADDR 0xE8 // original F0
36 #ifndef AIRSPEED_AMSYS_SCALE
37 #define AIRSPEED_AMSYS_SCALE 1
38 #endif
39 #define AIRSPEED_AMSYS_OFFSET_MAX 29491
40 #define AIRSPEED_AMSYS_OFFSET_MIN 3277
41 #define AIRSPEED_AMSYS_OFFSET_NBSAMPLES_INIT 40
42 #define AIRSPEED_AMSYS_OFFSET_NBSAMPLES_AVRG 60
43 #define AIRSPEED_AMSYS_NBSAMPLES_AVRG 10
44 #ifndef AIRSPEED_AMSYS_MAXPRESURE
45 #define AIRSPEED_AMSYS_MAXPRESURE 2068 //003-2068, 001-689 //Pascal
46 #endif
47 #ifndef AIRSPEED_AMSYS_FILTER
48 #define AIRSPEED_AMSYS_FILTER 0
49 #endif
50 #ifndef AIRSPEED_AMSYS_I2C_DEV
51 #define AIRSPEED_AMSYS_I2C_DEV i2c0
52 #endif
53 #ifdef MEASURE_AMSYS_TEMPERATURE
54 #define TEMPERATURE_AMSYS_OFFSET_MAX 29491
55 #define TEMPERATURE_AMSYS_OFFSET_MIN 3277
56 #define TEMPERATURE_AMSYS_MAX 110
57 #define TEMPERATURE_AMSYS_MIN -25
58 #endif
59 
60 
61 // Global variables
67 float airspeed_amsys_p; //Pascal
68 float airspeed_amsys; //mps
72 
73 // Local variables
74 volatile bool_t airspeed_amsys_i2c_done;
76 float airspeed_old = 0.0;
80 
81 void airspeed_amsys_downlink(void);
82 
83 void airspeed_amsys_init( void ) {
85  airspeed_amsys = 0.0;
86  airspeed_amsys_p = 0.0;
97 }
98 
100 #ifndef SITL
102 #ifndef MEASURE_AMSYS_TEMPERATURE
104 #else
106 #endif
107  }
108 
109 #if USE_AIRSPEED
111 #endif
112 
113 #elif !defined USE_NPS
114  extern float sim_air_speed;
115  stateSetAirspeed_f(&sim_air_speed);
116 #endif //SITL
117 
118 
119 #ifndef AIRSPEED_AMSYS_SYNC_SEND
120  RunOnceEvery(10, airspeed_amsys_downlink());
121 #endif
122 }
123 
125  DOWNLINK_SEND_AMSYS_AIRSPEED(DefaultChannel, DefaultDevice,
129 }
130 
132 
133  // Get raw airspeed from buffer
134  airspeed_amsys_raw = 0;
136 #ifdef MEASURE_AMSYS_TEMPERATURE
138  const float temp_off_scale = (float)(TEMPERATURE_AMSYS_MAX) /
139  (TEMPERATURE_AMSYS_OFFSET_MAX - TEMPERATURE_AMSYS_OFFSET_MIN);
140  // Tmin=-25, Tmax=85
141  airspeed_temperature = temp_off_scale * (tempAS_amsys_raw - TEMPERATURE_AMSYS_OFFSET_MIN) +
142  TEMPERATURE_AMSYS_MIN;
143 #endif
144 
145  // Check if this is valid airspeed
146  if (airspeed_amsys_raw == 0)
148  else
150 
151  // Continue only if a new airspeed value was received
152  if (airspeed_amsys_valid) {
153 
154  // raw not under offest min
157  // raw not over offest max
160 
161  // calculate raw to pressure
162  const float p_off_scale = (float)(AIRSPEED_AMSYS_MAXPRESURE) /
165 
168  // Check if averaging completed
169  if (airspeed_amsys_cnt == 0) {
170  // Calculate average
173  }
174  // Check if averaging needs to continue
177  }
178 
179  airspeed_amsys = 0.;
180 
181  }
182  else {
184  if (airspeed_amsys_p <= 0)
185  airspeed_amsys_p = 0.000000001;
186  // convert pressure to airspeed
187  airspeed_amsys_tmp = sqrtf(2 * airspeed_amsys_p * airspeed_scale / 1.2041);
188  // Lowpassfiltering
192 
193  //New value available
194 #if USE_AIRSPEED
196 #endif
197 #ifdef AIRSPEED_AMSYS_SYNC_SEND
199 #endif
200  }
201 
202  }
203  /*else {
204  airspeed_amsys = 0.0;
205  }*/
206 
207 
208  // Transaction has been read
210 }
211 
unsigned short uint16_t
Definition: types.h:16
float airspeed_amsys
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
float airspeed_temperature
bool_t airspeed_amsys_valid
I2C transaction structure.
Definition: i2c.h:93
#define AIRSPEED_AMSYS_ADDR
#define AIRSPEED_AMSYS_OFFSET_NBSAMPLES_INIT
float airspeed_filter
#define FALSE
Definition: imu_chimu.h:141
float sim_air_speed
Definition: sim_ir.c:14
#define AIRSPEED_AMSYS_OFFSET_MIN
struct i2c_transaction airspeed_amsys_i2c_trans
transaction set to done by user level
Definition: i2c.h:59
#define AIRSPEED_AMSYS_FILTER
float airspeed_amsys_p
static void stateSetAirspeed_f(float *airspeed)
Set airspeed (float).
Definition: state.h:1152
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
float airspeed_scale
#define TRUE
Definition: imu_chimu.h:144
bool_t airspeed_amsys_offset_init
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
#define AIRSPEED_AMSYS_OFFSET_NBSAMPLES_AVRG
API to get/set the generic vehicle states.
uint16_t airspeed_amsys_raw
float airspeed_amsys_offset
uint16_t airspeed_amsys_cnt
void airspeed_amsys_read_event(void)
void airspeed_amsys_init(void)
double airspeed_amsys_offset_tmp
float airspeed_old
#define AIRSPEED_AMSYS_I2C_DEV
float airspeed_amsys_tmp
#define AIRSPEED_AMSYS_OFFSET_MAX
void airspeed_amsys_downlink(void)
uint16_t tempAS_amsys_raw
void airspeed_amsys_read_periodic(void)
#define AIRSPEED_AMSYS_SCALE
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:248
#define AIRSPEED_AMSYS_MAXPRESURE
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
volatile bool_t airspeed_amsys_i2c_done