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
pressure_board_navarro.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 ENAC
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22 
23 /*
24  * Pressure Board Navarro
25  */
26 
27 
28 #include "pressure_board_navarro.h"
29 #include "state.h"
30 #include "subsystems/abi.h"
31 
32 /* Default I2C device on tiny is i2c0
33  */
34 #ifndef PBN_I2C_DEV
35 #define PBN_I2C_DEV i2c0
36 #endif
37 
38 /* Sensor I2C slave address */
39 #define PBN_I2C_ADDR 0x28
40 
41 /* Number of values to compute an offset at startup */
42 #define OFFSET_NBSAMPLES_AVRG 100
43 
44 /* Number of loops before starting to store data */
45 #define PBN_START_DELAY 30
46 
47 /* Weight for offset IIR filter */
48 #define PBN_OFFSET_FILTER 7
49 
50 /* Quadratic scale factor for airspeed */
51 #ifndef PBN_AIRSPEED_SCALE
52 #define PBN_AIRSPEED_SCALE (1./0.54)
53 #endif
54 
55 /* Linear scale factor for altitude */
56 #ifndef PBN_ALTITUDE_SCALE
57 #define PBN_ALTITUDE_SCALE 0.32
58 #endif
59 
60 #ifndef PBN_PRESSURE_OFFSET
61 #define PBN_PRESSURE_OFFSET 101325.0
62 #endif
63 
64 
65 // Global variables
68 bool_t data_valid;
70 
71 
81 
82 void pbn_init( void ) {
84  altitude_offset = 0;
85  airspeed_offset = 0;
86  airspeed_adc = 0;
87  altitude_adc = 0;
88  data_valid = TRUE;
90  pbn_airspeed = 0.;
91  pbn_altitude = 0.;
93 }
94 
95 
96 void pbn_periodic( void ) {
97 
98  if ( startup_delay > 0 ) {
99  --startup_delay;
100  return;
101  }
102 
103  // Initiate next read
104  pbn_trans.buf[0] = 0;
106 
107 }
108 
109 void pbn_read_event( void ) {
110 
112 
113  // Get raw values from buffer
114  airspeed_adc = ((uint16_t)(pbn_trans.buf[0]) << 8) | (uint16_t)(pbn_trans.buf[1]);
115  altitude_adc = ((uint16_t)(pbn_trans.buf[2]) << 8) | (uint16_t)(pbn_trans.buf[3]);
116 
117  // Consider 0 as a wrong value
118  if (airspeed_adc == 0 || altitude_adc == 0) {
119  data_valid = FALSE;
120  }
121  else {
122  data_valid = TRUE;
123 
124  if (offset_cnt > 0) {
125  // IIR filter to compute an initial offset
126 #ifndef PBN_AIRSPEED_OFFSET
128 #else
129  airspeed_offset = PBN_AIRSPEED_OFFSET;
130 #endif
131 #ifndef PBN_ALTITUDE_OFFSET
133 #else
134  altitude_offset = PBN_ALTITUDE_OFFSET;
135 #endif
136 
137  // decrease init counter
138  --offset_cnt;
139  }
140  else {
141  // Compute pressure
142  float pressure = PBN_ALTITUDE_SCALE * (float) altitude_adc + PBN_PRESSURE_OFFSET;
143  AbiSendMsgBARO_ABS(BARO_PBN_SENDER_ID, &pressure);
144  // Compute airspeed and altitude
145  //pbn_airspeed = (-4.45 + sqrtf(19.84-0.57*(float)(airspeed_offset-airspeed_adc)))/0.28;
147  float tmp_airspeed = sqrtf((float)diff * PBN_AIRSPEED_SCALE);
149 
150  pbn_airspeed = (airspeed_filter*pbn_airspeed + tmp_airspeed) / (airspeed_filter + 1.);
151 #if USE_AIRSPEED
153 #endif
154  }
155 
156  }
157 }
unsigned short uint16_t
Definition: types.h:16
uint16_t altitude_adc
uint16_t startup_delay
#define PBN_START_DELAY
#define OFFSET_NBSAMPLES_AVRG
I2C transaction structure.
Definition: i2c.h:93
#define PBN_OFFSET_FILTER
Main include for ABI (AirBorneInterface).
bool_t i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r)
Submit a write/read transaction.
Definition: i2c.c:258
#define PBN_I2C_DEV
#define FALSE
Definition: imu_chimu.h:141
float pbn_altitude
uint16_t airspeed_offset
uint32_t altitude_offset_tmp
#define PBN_ALTITUDE_SCALE
#define Max(x, y)
float airspeed_filter
uint16_t airspeed_adc
transaction set to done by user level
Definition: i2c.h:59
bool_t data_valid
void pbn_read_event(void)
unsigned long uint32_t
Definition: types.h:18
#define BARO_PBN_SENDER_ID
static void stateSetAirspeed_f(float *airspeed)
Set airspeed (float).
Definition: state.h:1152
#define PBN_PRESSURE_OFFSET
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
uint16_t offset_cnt
#define TRUE
Definition: imu_chimu.h:144
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
void pbn_init(void)
API to get/set the generic vehicle states.
struct i2c_transaction pbn_trans
void pbn_periodic(void)
#define PBN_AIRSPEED_SCALE
uint16_t altitude_offset
uint32_t airspeed_offset_tmp
float pbn_airspeed
#define PBN_I2C_ADDR