Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
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 #if !USE_AIRSPEED
35 // Just a Warning --> We do't use it.
36 //#ifndef SENSOR_SYNC_SEND
37 //#warning either set USE_AIRSPEED or SENSOR_SYNC_SEND to use amsys_airspeed
38 //#endif
39 #endif
40 
41 #define AIRSPEED_AMSYS_ADDR 0xE8 // original F0
42 #ifndef AIRSPEED_AMSYS_SCALE
43 #define AIRSPEED_AMSYS_SCALE 1
44 #endif
45 #ifndef AIRSPEED_AMSYS_OFFSET
46 #define AIRSPEED_AMSYS_OFFSET 0
47 #endif
48 #define AIRSPEED_AMSYS_OFFSET_MAX 29491
49 #define AIRSPEED_AMSYS_OFFSET_MIN 3277
50 #define AIRSPEED_AMSYS_OFFSET_NBSAMPLES_INIT 40
51 #define AIRSPEED_AMSYS_OFFSET_NBSAMPLES_AVRG 60
52 #define AIRSPEED_AMSYS_NBSAMPLES_AVRG 10
53 #ifndef AIRSPEED_AMSYS_MAXPRESURE
54 #define AIRSPEED_AMSYS_MAXPRESURE 2068//2073 //Pascal
55 #endif
56 #ifndef AIRSPEED_AMSYS_I2C_DEV
57 #define AIRSPEED_AMSYS_I2C_DEV i2c0
58 #endif
59 #ifdef MEASURE_AMSYS_TEMPERATURE
60 #define TEMPERATURE_AMSYS_OFFSET_MAX 29491
61 #define TEMPERATURE_AMSYS_OFFSET_MIN 3277
62 #define TEMPERATURE_AMSYS_MAX 110
63 #define TEMPERATURE_AMSYS_MIN -25
64 #endif
65 
66 #ifndef DOWNLINK_DEVICE
67 #define DOWNLINK_DEVICE DOWNLINK_AP_DEVICE
68 #endif
69 
70 // Global variables
75 float pressure_amsys; //Pascal
76 float airspeed_amsys; //mps
80 
81 // Local variables
82 volatile bool_t airspeed_amsys_i2c_done;
84 float airspeed_old = 0.0;
85 
86 
87 void airspeed_amsys_init( void ) {
89  airspeed_amsys = 0.0;
90  pressure_amsys = 0.0;
93  airspeed_scale = AIRSPEED_SCALE;
94  airspeed_filter = AIRSPEED_FILTER;
96 }
97 
99 #ifndef SITL
101 #ifndef MEASURE_AMSYS_TEMPERATURE
103 #else
105 #endif
106 
107 #else // SITL
108  extern float sim_air_speed;
109  stateSetAirspeed_f(&sim_air_speed);
110 #endif //SITL
111 }
112 
114 
115  // Get raw airspeed from buffer
116  airspeed_amsys_raw = 0;
118 #ifdef MEASURE_AMSYS_TEMPERATURE
120  airspeed_temperature = (float)((float)(tempAS_amsys_raw-TEMPERATURE_AMSYS_OFFSET_MIN)/((float)(TEMPERATURE_AMSYS_OFFSET_MAX-TEMPERATURE_AMSYS_OFFSET_MIN)/TEMPERATURE_AMSYS_MAX)+TEMPERATURE_AMSYS_MIN);// Tmin=-25, Tmax=85
121 #endif
122 
123  // Check if this is valid airspeed
124  if (airspeed_amsys_raw == 0)
126  else
128 
129  // Continue only if a new airspeed value was received
130  if (airspeed_amsys_valid) {
131 
132  // raw not under offest min
135  // raw not over offest max
138 
139  // calculate raw to pressure
141 
142  airspeed_tmp = sqrtf(2*(pressure_amsys)*airspeed_scale/1.2041); //without offset
143 
144  // Lowpass filter
147 
148 #if USE_AIRSPEED
150 #endif
151 #ifdef SENSOR_SYNC_SEND
153 #else
154  RunOnceEvery(10, DOWNLINK_SEND_AMSYS_AIRSPEED(DefaultChannel, DefaultDevice, &airspeed_amsys_raw, &pressure_amsys, &airspeed_tmp, &airspeed_amsys, &airspeed_temperature));
155 #endif
156  }
157 
158  // Transaction has been read
160 }
161 
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
float airspeed_tmp
#define AIRSPEED_AMSYS_ADDR
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
float pressure_amsys
static void stateSetAirspeed_f(float *airspeed)
Set airspeed (float).
Definition: state.h:1152
enum I2CTransactionStatus status
Definition: i2c.h:83
float airspeed_scale
#define TRUE
Definition: imu_chimu.h:144
volatile uint8_t buf[I2C_BUF_LEN]
Definition: i2c.h:82
API to get/set the generic vehicle states.
uint16_t airspeed_amsys_raw
void airspeed_amsys_read_event(void)
void airspeed_amsys_init(void)
float airspeed_old
#define AIRSPEED_AMSYS_I2C_DEV
#define AIRSPEED_AMSYS_OFFSET_MAX
uint16_t tempAS_amsys_raw
void airspeed_amsys_read_periodic(void)
bool_t i2c_receive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint16_t len)
Definition: i2c.c:95
#define AIRSPEED_AMSYS_MAXPRESURE
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
volatile bool_t airspeed_amsys_i2c_done