Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
baro_amsys.c
Go to the documentation of this file.
1/*
2 * Driver for a Amsys Barometric Sensor I2C
3 * AMS 5812-0150-A
4 * ----measuring only---
5 *
6 * Copyright (C) 2010 The Paparazzi Team
7 *
8 * This file is part of paparazzi.
9 *
10 * paparazzi is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
14 *
15 * paparazzi is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with paparazzi; see the file COPYING. If not, write to
22 * the Free Software Foundation, 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
24 */
25
26#include "sensors/baro_amsys.h"
27#include "mcu_periph/i2c.h"
28#include "modules/core/abi.h"
29#include "mcu_periph/sys_time.h"
30#include "state.h"
31#include <math.h>
32#include "generated/flight_plan.h" // for ground alt
33
34//Messages
35#include "mcu_periph/uart.h"
36#include "pprzlink/messages.h"
38//#include "gps.h"
39
40#define BARO_AMSYS_ADDR 0xE4
41#define BARO_AMSYS_REG 0x07
42#ifndef BARO_AMSYS_SCALE
43#define BARO_AMSYS_SCALE 1
44#endif
45#ifndef BARO_AMSYS_MAX_PRESSURE
46#define BARO_AMSYS_MAX_PRESSURE 103400 // Pascal
47#endif
48#define BARO_AMSYS_OFFSET_MAX 29491
49#define BARO_AMSYS_OFFSET_MIN 3277
50#define BARO_AMSYS_OFFSET_NBSAMPLES_INIT 40
51#define BARO_AMSYS_OFFSET_NBSAMPLES_AVRG 60
52#ifndef BARO_AMSYS_FILTER
53#define BARO_AMSYS_FILTER 0
54#endif
55#define BARO_AMSYS_R 0.5
56#define BARO_AMSYS_SIGMA2 0.1
57
58#ifdef MEASURE_AMSYS_TEMPERATURE
59#define TEMPERATURE_AMSYS_OFFSET_MAX 29491
60#define TEMPERATURE_AMSYS_OFFSET_MIN 3277
61#define TEMPERATURE_AMSYS_MAX 85
62#define TEMPERATURE_AMSYS_MIN -25
63#endif
64
65//#define BARO_ALT_CORRECTION 0.0
66#ifndef BARO_AMSYS_I2C_DEV
67#define BARO_AMSYS_I2C_DEV i2c0
68#endif
69
70
71// Global variables
85float ref_alt_init; //Altitude by initialising
89
90
92
93// Local variables
97
118
120{
121 // Initiate next read
122 if (baro_amsys_i2c_trans.status == I2CTransDone) {
123#ifndef MEASURE_AMSYS_TEMPERATURE
125#else
127#endif
128 }
129
130}
131
133{
134 pBaroRaw = 0;
135 // Get raw altimeter from buffer
136 pBaroRaw = (baro_amsys_i2c_trans.buf[0] << 8) | baro_amsys_i2c_trans.buf[1];
137#ifdef MEASURE_AMSYS_TEMPERATURE
138 tBaroRaw = (baro_amsys_i2c_trans.buf[2] << 8) | baro_amsys_i2c_trans.buf[3];
139 const float temp_off_scale = (float)(TEMPERATURE_AMSYS_MAX - TEMPERATURE_AMSYS_MIN) /
140 (TEMPERATURE_AMSYS_OFFSET_MAX - TEMPERATURE_AMSYS_OFFSET_MIN);
141 // Tmin=-25, Tmax=85
142 baro_amsys_temp = temp_off_scale * (tBaroRaw - TEMPERATURE_AMSYS_OFFSET_MIN) +
143 TEMPERATURE_AMSYS_MIN;
144#endif
145 // Check if this is valid altimeter
146 if (pBaroRaw == 0) {
147 baro_amsys_valid = false;
148 } else {
149 baro_amsys_valid = true;
150 }
151
153
154 // Continue only if a new altimeter value was received
155 //if (baro_amsys_valid && GpsFixValid()) {
156 if (baro_amsys_valid) {
157 uint32_t now_ts = get_sys_time_usec();
158
159 //Cut RAW Min and Max
162 }
165 }
166
167 //Convert to pressure
170 // Send pressure over ABI
171 AbiSendMsgBARO_ABS_RAW(BARO_AMSYS_SENDER_ID, now_ts, baro_amsys_p);
172 // compute altitude localy
175 // Check if averaging completed
176 if (baro_amsys_cnt == 0) {
177 // Calculate average
179 ref_alt_init = GROUND_ALT;
181
182 // hight over Sea level at init point
183 //baro_amsys_offset_altitude = 288.15 / 0.0065 * (1 - pow((baro_amsys_p)/1013.25 , 1/5.255));
184 }
185 // Check if averaging needs to continue
188 }
189
191
192 } else {
193 // Lowpassfiltering and convert pressure to altitude
195 (1.2041 * 9.81);
197 //New value available
198 }
200 } /*else {
201 baro_amsys_abs_altitude = 0.0;
202 }*/
203
204
205 // Transaction has been read
207}
Main include for ABI (AirBorneInterface).
#define BARO_AMSYS_SENDER_ID
float baro_scale
Definition baro_amsys.c:86
float ref_alt_init
Definition baro_amsys.c:85
bool baro_amsys_offset_init
Definition baro_amsys.c:94
uint16_t baro_amsys_cnt
Definition baro_amsys.c:96
void baro_amsys_read_event(void)
Definition baro_amsys.c:132
#define BARO_AMSYS_SIGMA2
Definition baro_amsys.c:56
#define BARO_AMSYS_OFFSET_MAX
Definition baro_amsys.c:48
#define BARO_AMSYS_SCALE
Definition baro_amsys.c:43
void baro_amsys_init(void)
Definition baro_amsys.c:98
float baro_amsys_temp
Definition baro_amsys.c:81
#define BARO_AMSYS_R
Definition baro_amsys.c:55
float baro_amsys_abs_altitude
Definition baro_amsys.c:84
void baro_amsys_read_periodic(void)
Definition baro_amsys.c:119
float baro_amsys_offset
Definition baro_amsys.c:75
#define BARO_AMSYS_OFFSET_NBSAMPLES_INIT
Definition baro_amsys.c:50
double baro_amsys_offset_tmp
Definition baro_amsys.c:95
float baro_amsys_r
Definition baro_amsys.c:79
#define BARO_AMSYS_OFFSET_NBSAMPLES_AVRG
Definition baro_amsys.c:51
float baro_amsys_p
Definition baro_amsys.c:82
float baro_amsys_altitude
Definition baro_amsys.c:77
float baro_old
Definition baro_amsys.c:88
#define BARO_AMSYS_OFFSET_MIN
Definition baro_amsys.c:49
#define BARO_AMSYS_MAX_PRESSURE
Definition baro_amsys.c:46
bool baro_amsys_valid
Definition baro_amsys.c:76
float baro_amsys_offset_altitude
Definition baro_amsys.c:83
float baro_amsys_sigma2
Definition baro_amsys.c:80
uint16_t tBaroRaw
Definition baro_amsys.c:73
static uint16_t pBaroRaw
Definition baro_amsys.c:72
bool baro_amsys_enabled
Definition baro_amsys.c:78
float baro_filter
Definition baro_amsys.c:87
#define BARO_AMSYS_I2C_DEV
Definition baro_amsys.c:67
#define BARO_AMSYS_ADDR
Definition baro_amsys.c:40
uint16_t baro_amsys_adc
Definition baro_amsys.c:74
struct i2c_transaction baro_amsys_i2c_trans
Definition baro_amsys.c:91
#define BARO_AMSYS_FILTER
Definition baro_amsys.c:53
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
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:212
@ I2CTransDone
transaction set to done by user level
Definition i2c.h:60
I2C transaction structure.
Definition i2c.h:94
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
API to get/set the generic vehicle states.
Architecture independent timing functions.
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.