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
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 <math.h>
46 
47 #include "subsystems/nav.h"
48 
49 #ifdef SITL
50 #include "subsystems/gps.h"
51 #endif
52 
53 #ifdef BARO_ETS_TELEMETRY
54 #ifndef DOWNLINK_DEVICE
55 #define DOWNLINK_DEVICE DOWNLINK_AP_DEVICE
56 #endif
57 
58 #include "mcu_periph/uart.h"
59 #include "messages.h"
61 #endif //BARO_ETS_TELEMETRY
62 
63 #define BARO_ETS_ADDR 0xE8
64 #define BARO_ETS_REG 0x07
65 #ifndef BARO_ETS_SCALE
66 #define BARO_ETS_SCALE 0.32
67 #endif
68 #define BARO_ETS_OFFSET_MAX 30000
69 #define BARO_ETS_OFFSET_MIN 10
70 #define BARO_ETS_OFFSET_NBSAMPLES_INIT 20
71 #define BARO_ETS_OFFSET_NBSAMPLES_AVRG 40
72 #define BARO_ETS_R 0.5
73 #define BARO_ETS_SIGMA2 0.1
74 
75 #ifndef BARO_ETS_I2C_DEV
76 #define BARO_ETS_I2C_DEV i2c0
77 #endif
78 
79 // Global variables
85 float baro_ets_r;
87 
89 
90 // Local variables
94 
95 void baro_ets_init( void ) {
96  baro_ets_adc = 0;
97  baro_ets_altitude = 0.0;
98  baro_ets_offset = 0;
106 
108 }
109 
111  // Initiate next read
112 #ifndef SITL
115 #else // SITL
116  /* fake an offset so sim works for under hmsl as well */
117  if (!baro_ets_offset_init) {
118  baro_ets_offset = 12400;
120  }
121  baro_ets_altitude = gps.hmsl / 1000.0;
124 #endif
125 
126 #ifdef BARO_ETS_TELEMETRY
128 #endif
129 }
130 
131 void baro_ets_read_event( void ) {
132  // Get raw altimeter from buffer
134  // Check if this is valid altimeter
135  if (baro_ets_adc == 0)
137  else
139 
140  // Continue only if a new altimeter value was received
141  if (baro_ets_valid) {
142  // Calculate offset average if not done already
143  if (!baro_ets_offset_init) {
144  --baro_ets_cnt;
145  // Check if averaging completed
146  if (baro_ets_cnt == 0) {
147  // Calculate average
149  // Limit offset
155  }
156  // Check if averaging needs to continue
159  }
160  // Convert raw to m/s
161  if (baro_ets_offset_init) {
163  // New value available
164 #ifdef BARO_ETS_TELEMETRY
166 #endif
167  } else {
168  baro_ets_altitude = 0.0;
169  }
170  } else {
171  baro_ets_altitude = 0.0;
172  }
173 
174  // Transaction has been read
176 }
unsigned short uint16_t
Definition: types.h:16
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
struct i2c_transaction baro_ets_i2c_trans
Definition: baro_ets.c:88
#define BARO_ETS_SCALE
Definition: baro_ets.c:66
int32_t hmsl
height above mean sea level in mm
Definition: gps.h:67
float baro_ets_r
Definition: baro_ets.c:85
#define FALSE
Definition: imu_chimu.h:141
#define BARO_ETS_ADDR
Definition: baro_ets.c:63
#define BARO_ETS_SIGMA2
Definition: baro_ets.c:73
bool_t baro_ets_valid
Definition: baro_ets.c:82
#define BARO_ETS_R
Definition: baro_ets.c:72
Driver for the EagleTree Systems Altitude Sensor.
Device independent GPS code (interface)
unsigned long uint32_t
Definition: types.h:18
float baro_ets_altitude
Definition: baro_ets.c:83
uint16_t baro_ets_cnt
Definition: baro_ets.c:93
void baro_ets_init(void)
Definition: baro_ets.c:95
enum I2CTransactionStatus status
Definition: i2c.h:83
#define BARO_ETS_OFFSET_MAX
Definition: baro_ets.c:68
#define TRUE
Definition: imu_chimu.h:144
bool_t baro_ets_enabled
Definition: baro_ets.c:84
volatile uint8_t buf[I2C_BUF_LEN]
Definition: i2c.h:82
bool_t baro_ets_offset_init
Definition: baro_ets.c:91
API to get/set the generic vehicle states.
float baro_ets_sigma2
Definition: baro_ets.c:86
#define BARO_ETS_OFFSET_MIN
Definition: baro_ets.c:69
void baro_ets_read_periodic(void)
Definition: baro_ets.c:110
uint16_t baro_ets_offset
Definition: baro_ets.c:81
#define BARO_ETS_OFFSET_NBSAMPLES_INIT
Definition: baro_ets.c:70
#define BARO_ETS_I2C_DEV
Definition: baro_ets.c:76
#define BARO_ETS_OFFSET_NBSAMPLES_AVRG
Definition: baro_ets.c:71
void baro_ets_read_event(void)
Definition: baro_ets.c:131
struct GpsState gps
global GPS state
Definition: gps.c:33
uint16_t baro_ets_adc
Definition: baro_ets.c:80
bool_t i2c_receive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint16_t len)
Definition: i2c.c:95
uint32_t baro_ets_offset_tmp
Definition: baro_ets.c:92
Architecture independent I2C (Inter-Integrated Circuit Bus) API.