Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
baro_ets.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 Vassilis Varveropoulos
3  * Copyright (C) 2010 The Paparazzi Team
4  *
5  * This file is part of paparazzi.
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, write to
19  * the Free Software Foundation, 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 
42 #include "sensors/baro_ets.h"
43 #include "mcu_periph/i2c.h"
44 #include "state.h"
45 #include "modules/core/abi.h"
46 #include <math.h>
47 #include "mcu_periph/sys_time.h"
48 
50 
51 #ifdef BARO_ETS_SYNC_SEND
52 
53 #include "mcu_periph/uart.h"
54 #include "pprzlink/messages.h"
56 #endif //BARO_ETS_SYNC_SEND
57 
58 #define BARO_ETS_ADDR 0xE8
59 #define BARO_ETS_REG 0x07
60 #define BARO_ETS_OFFSET_MAX 30000
61 #define BARO_ETS_OFFSET_MIN 10
62 #define BARO_ETS_OFFSET_NBSAMPLES_INIT 20
63 #define BARO_ETS_OFFSET_NBSAMPLES_AVRG 40
64 #define BARO_ETS_R 0.5
65 #define BARO_ETS_SIGMA2 0.1
66 
74 #ifndef BARO_ETS_SCALE
75 #define BARO_ETS_SCALE 37.5
76 #endif
77 
79 #ifndef BARO_ETS_ALT_SCALE
80 #define BARO_ETS_ALT_SCALE 0.32
81 #endif
82 
84 #ifndef BARO_ETS_PRESSURE_OFFSET
85 #define BARO_ETS_PRESSURE_OFFSET 101325.0
86 #endif
87 
88 #ifndef BARO_ETS_I2C_DEV
89 #define BARO_ETS_I2C_DEV i2c0
90 #endif
91 PRINT_CONFIG_VAR(BARO_ETS_I2C_DEV)
92 
93 
94 PRINT_CONFIG_VAR(BARO_ETS_SENDER_ID)
95 
96 
97 #ifndef BARO_ETS_START_DELAY
98 #define BARO_ETS_START_DELAY 0.2
99 #endif
100 PRINT_CONFIG_VAR(BARO_ETS_START_DELAY)
101 
102 // Global variables
110 
112 
113 // Local variables
119 
120 void baro_ets_init(void)
121 {
122  baro_ets_adc = 0;
123  baro_ets_altitude = 0.0;
124  baro_ets_offset = 0;
126  baro_ets_valid = false;
127  baro_ets_offset_init = false;
128  baro_ets_enabled = true;
132 
134 
135  baro_ets_delay_done = false;
137 }
138 
140 {
141  // Initiate next read
142  if (!baro_ets_delay_done) {
144  else { baro_ets_delay_done = true; }
145  }
148  }
149 }
150 
152 {
153  // Get raw altimeter from buffer
155  // Check if this is valid altimeter
156  if (baro_ets_adc == 0) {
157  baro_ets_valid = false;
158  } else {
159  baro_ets_valid = true;
160  }
161 
162  // Continue only if a new altimeter value was received
163  if (baro_ets_valid) {
164  // Calculate offset average if not done already
165  if (!baro_ets_offset_init) {
166  --baro_ets_cnt;
167  // Check if averaging completed
168  if (baro_ets_cnt == 0) {
169  // Calculate average
171  // Limit offset
174  }
177  }
178  baro_ets_offset_init = true;
179  }
180  // Check if averaging needs to continue
183  }
184  }
185  // Convert raw to m/s
186  if (baro_ets_offset_init) {
188  // New value available
189  uint32_t now_ts = get_sys_time_usec();
190  float pressure = BARO_ETS_SCALE * (float) baro_ets_adc + BARO_ETS_PRESSURE_OFFSET;
191  AbiSendMsgBARO_ABS(BARO_ETS_SENDER_ID, now_ts, pressure);
192 #ifdef BARO_ETS_SYNC_SEND
194 #endif
195  } else {
196  baro_ets_altitude = 0.0;
197  }
198  } else {
199  baro_ets_altitude = 0.0;
200  }
201 
202  // Transaction has been read
204 }
Main include for ABI (AirBorneInterface).
#define BARO_ETS_SENDER_ID
void baro_ets_read_periodic(void)
Definition: baro_ets.c:139
float baro_ets_r
Definition: baro_ets.c:108
uint32_t baro_ets_delay_time
Definition: baro_ets.c:117
bool baro_ets_valid
Definition: baro_ets.c:105
#define BARO_ETS_ALT_SCALE
scale factor to convert raw ADC measurement to altitude change in meters
Definition: baro_ets.c:80
#define BARO_ETS_ADDR
Definition: baro_ets.c:58
#define BARO_ETS_SCALE
scale factor to convert raw ADC measurement to pressure in Pascal.
Definition: baro_ets.c:75
#define BARO_ETS_I2C_DEV
Definition: baro_ets.c:89
struct i2c_transaction baro_ets_i2c_trans
Definition: baro_ets.c:111
#define BARO_ETS_START_DELAY
delay in seconds until sensor is read after startup
Definition: baro_ets.c:98
void baro_ets_init(void)
Definition: baro_ets.c:120
bool baro_ets_offset_init
Definition: baro_ets.c:114
bool baro_ets_enabled
Definition: baro_ets.c:107
#define BARO_ETS_OFFSET_MIN
Definition: baro_ets.c:61
uint16_t baro_ets_adc
Definition: baro_ets.c:103
#define BARO_ETS_SIGMA2
Definition: baro_ets.c:65
void baro_ets_read_event(void)
Definition: baro_ets.c:151
uint16_t baro_ets_cnt
Definition: baro_ets.c:116
#define BARO_ETS_PRESSURE_OFFSET
Pressure offset in Pascal when converting raw adc to real pressure (.
Definition: baro_ets.c:85
uint32_t baro_ets_offset_tmp
Definition: baro_ets.c:115
uint16_t baro_ets_offset
Definition: baro_ets.c:104
float baro_ets_sigma2
Definition: baro_ets.c:109
float baro_ets_altitude
Definition: baro_ets.c:106
#define BARO_ETS_R
Definition: baro_ets.c:64
#define BARO_ETS_OFFSET_MAX
Definition: baro_ets.c:60
#define BARO_ETS_OFFSET_NBSAMPLES_AVRG
Definition: baro_ets.c:63
#define BARO_ETS_OFFSET_NBSAMPLES_INIT
Definition: baro_ets.c:62
bool baro_ets_delay_done
Definition: baro_ets.c:118
Driver for the EagleTree Systems Altitude Sensor.
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:71
float ground_alt
size == nb_waypoint, waypoint 0 is a dummy waypoint
Definition: common_nav.c:41
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
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
Fixedwing Navigation library.
API to get/set the generic vehicle states.
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
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