Paparazzi UAS  v5.18.0_stable
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 "subsystems/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();
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 }
uint16_t
unsigned short uint16_t
Definition: types.h:16
i2c_transaction::buf
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
baro_ets_sigma2
float baro_ets_sigma2
Definition: baro_ets.c:109
BARO_ETS_OFFSET_MIN
#define BARO_ETS_OFFSET_MIN
Definition: baro_ets.c:61
abi.h
baro_ets_offset_tmp
uint32_t baro_ets_offset_tmp
Definition: baro_ets.c:115
baro_ets_r
float baro_ets_r
Definition: baro_ets.c:108
uint32_t
unsigned long uint32_t
Definition: types.h:18
baro_ets_altitude
float baro_ets_altitude
Definition: baro_ets.c:106
BARO_ETS_OFFSET_NBSAMPLES_INIT
#define BARO_ETS_OFFSET_NBSAMPLES_INIT
Definition: baro_ets.c:62
baro_ets_read_periodic
void baro_ets_read_periodic(void)
Definition: baro_ets.c:139
baro_ets_adc
uint16_t baro_ets_adc
Definition: baro_ets.c:103
BARO_ETS_OFFSET_MAX
#define BARO_ETS_OFFSET_MAX
Definition: baro_ets.c:60
SysTimeTimer
#define SysTimeTimer(_t)
Definition: sys_time.h:219
BARO_ETS_OFFSET_NBSAMPLES_AVRG
#define BARO_ETS_OFFSET_NBSAMPLES_AVRG
Definition: baro_ets.c:63
baro_ets.h
get_sys_time_usec
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
USEC_OF_SEC
#define USEC_OF_SEC(sec)
Definition: sys_time.h:210
uart.h
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
BARO_ETS_SCALE
#define BARO_ETS_SCALE
scale factor to convert raw ADC measurement to pressure in Pascal.
Definition: baro_ets.c:75
BARO_ETS_SENDER_ID
#define BARO_ETS_SENDER_ID
Definition: abi_sender_ids.h:52
baro_ets_read_event
void baro_ets_read_event(void)
Definition: baro_ets.c:151
sys_time.h
Architecture independent timing functions.
i2c_transaction::status
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
i2c_transaction
I2C transaction structure.
Definition: i2c.h:93
baro_ets_offset
uint16_t baro_ets_offset
Definition: baro_ets.c:104
baro_ets_init
void baro_ets_init(void)
Definition: baro_ets.c:120
BARO_ETS_ADDR
#define BARO_ETS_ADDR
Definition: baro_ets.c:58
baro_ets_i2c_trans
struct i2c_transaction baro_ets_i2c_trans
Definition: baro_ets.c:111
nav.h
BARO_ETS_ALT_SCALE
#define BARO_ETS_ALT_SCALE
scale factor to convert raw ADC measurement to altitude change in meters
Definition: baro_ets.c:80
BARO_ETS_PRESSURE_OFFSET
#define BARO_ETS_PRESSURE_OFFSET
Pressure offset in Pascal when converting raw adc to real pressure (.
Definition: baro_ets.c:85
ground_alt
float ground_alt
size == nb_waypoint, waypoint 0 is a dummy waypoint
Definition: common_nav.c:40
baro_ets_delay_time
uint32_t baro_ets_delay_time
Definition: baro_ets.c:117
BARO_ETS_R
#define BARO_ETS_R
Definition: baro_ets.c:64
baro_ets_cnt
uint16_t baro_ets_cnt
Definition: baro_ets.c:116
BARO_ETS_SIGMA2
#define BARO_ETS_SIGMA2
Definition: baro_ets.c:65
I2CTransDone
@ I2CTransDone
transaction set to done by user level
Definition: i2c.h:59
BARO_ETS_I2C_DEV
#define BARO_ETS_I2C_DEV
Definition: baro_ets.c:89
baro_ets_offset_init
bool baro_ets_offset_init
Definition: baro_ets.c:114
BARO_ETS_START_DELAY
#define BARO_ETS_START_DELAY
delay in seconds until sensor is read after startup
Definition: baro_ets.c:98
state.h
baro_ets_delay_done
bool baro_ets_delay_done
Definition: baro_ets.c:118
i2c.h
SysTimeTimerStart
#define SysTimeTimerStart(_t)
Definition: sys_time.h:218
baro_ets_valid
bool baro_ets_valid
Definition: baro_ets.c:105
baro_ets_enabled
bool baro_ets_enabled
Definition: baro_ets.c:107
i2c_receive
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
Bmp3_I2c::pressure
float pressure
pressure in Pascal
Definition: bmp3_i2c.h:50