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
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 "subsystems/abi.h"
29 #include "state.h"
30 #include <math.h>
31 #include "generated/flight_plan.h" // for ground alt
32 
33 //Messages
34 #include "mcu_periph/uart.h"
35 #include "messages.h"
37 //#include "gps.h"
38 
39 #define BARO_AMSYS_ADDR 0xE4
40 #define BARO_AMSYS_REG 0x07
41 #ifndef BARO_AMSYS_SCALE
42 #define BARO_AMSYS_SCALE 1
43 #endif
44 #ifndef BARO_AMSYS_MAX_PRESSURE
45 #define BARO_AMSYS_MAX_PRESSURE 103400 // Pascal
46 #endif
47 #define BARO_AMSYS_OFFSET_MAX 29491
48 #define BARO_AMSYS_OFFSET_MIN 3277
49 #define BARO_AMSYS_OFFSET_NBSAMPLES_INIT 40
50 #define BARO_AMSYS_OFFSET_NBSAMPLES_AVRG 60
51 #ifndef BARO_AMSYS_FILTER
52 #define BARO_AMSYS_FILTER 0
53 #endif
54 #define BARO_AMSYS_R 0.5
55 #define BARO_AMSYS_SIGMA2 0.1
56 
57 #ifdef MEASURE_AMSYS_TEMPERATURE
58 #define TEMPERATURE_AMSYS_OFFSET_MAX 29491
59 #define TEMPERATURE_AMSYS_OFFSET_MIN 3277
60 #define TEMPERATURE_AMSYS_MAX 110
61 #define TEMPERATURE_AMSYS_MIN -25
62 #endif
63 
64 //#define BARO_ALT_CORRECTION 0.0
65 #ifndef BARO_AMSYS_I2C_DEV
66 #define BARO_AMSYS_I2C_DEV i2c0
67 #endif
68 
69 
70 // Global variables
84 float ref_alt_init; //Altitude by initialising
85 float baro_scale;
87 float baro_old;
88 
89 
91 
92 // Local variables
96 
97 void baro_amsys_init( void ) {
99  pBaroRaw = 0;
100  tBaroRaw = 0;
101  baro_amsys_altitude = 0.0;
102  baro_amsys_p=0.0;
103  baro_amsys_offset = 0;
112  // baro_head=0;
113  ref_alt_init = 0;
115 }
116 
118  // Initiate next read
120 #ifndef MEASURE_AMSYS_TEMPERATURE
122 #else
124 #endif
125  }
126 
127 #ifdef BARO_AMSYS_SYNC_SEND
129 #else
131 #endif
132 }
133 
134 void baro_amsys_read_event( void ) {
135  pBaroRaw = 0;
136  // Get raw altimeter from buffer
138 #ifdef MEASURE_AMSYS_TEMPERATURE
140  baro_amsys_temp = (float)(tBaroRaw-TEMPERATURE_AMSYS_OFFSET_MIN)*TEMPERATURE_AMSYS_MAX/(float)(TEMPERATURE_AMSYS_OFFSET_MAX-TEMPERATURE_AMSYS_OFFSET_MIN)+(float)TEMPERATURE_AMSYS_MIN;
141 #endif
142  // Check if this is valid altimeter
143  if (pBaroRaw == 0)
145  else
147 
149 
150  // Continue only if a new altimeter value was received
151  //if (baro_amsys_valid && GpsFixValid()) {
152  if (baro_amsys_valid) {
153  //Cut RAW Min and Max
158 
159  //Convert to pressure
161  // Send pressure over ABI
162  AbiSendMsgBARO_ABS(BARO_AMSYS_SENDER_ID, &baro_amsys_p);
163  // compute altitude localy
164  if (!baro_amsys_offset_init) {
165  --baro_amsys_cnt;
166  // Check if averaging completed
167  if (baro_amsys_cnt == 0) {
168  // Calculate average
170  ref_alt_init = GROUND_ALT;
172 
173  // hight over Sea level at init point
174  //baro_amsys_offset_altitude = 288.15 / 0.0065 * (1 - pow((baro_amsys_p)/1013.25 , 1/5.255));
175  }
176  // Check if averaging needs to continue
179 
180  baro_amsys_altitude = 0.0;
181 
182  }
183  else {
184  // Lowpassfiltering and convert pressure to altitude
187  //New value available
188  }
190  } /*else {
191  baro_amsys_abs_altitude = 0.0;
192  }*/
193 
194 
195  // Transaction has been read
197 }
#define BARO_AMSYS_SCALE
Definition: baro_amsys.c:42
unsigned short uint16_t
Definition: types.h:16
float baro_amsys_temp
Definition: baro_amsys.c:80
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
struct i2c_transaction baro_amsys_i2c_trans
Definition: baro_amsys.c:90
uint16_t tBaroRaw
Definition: baro_amsys.c:72
double baro_amsys_offset_tmp
Definition: baro_amsys.c:94
I2C transaction structure.
Definition: i2c.h:93
Main include for ABI (AirBorneInterface).
float baro_amsys_p
Definition: baro_amsys.c:81
#define BARO_AMSYS_R
Definition: baro_amsys.c:54
#define BARO_AMSYS_SENDER_ID
float baro_filter
Definition: baro_amsys.c:86
#define FALSE
Definition: imu_chimu.h:141
float baro_amsys_abs_altitude
Definition: baro_amsys.c:83
float baro_scale
Definition: baro_amsys.c:85
float baro_amsys_r
Definition: baro_amsys.c:78
#define BARO_AMSYS_FILTER
Definition: baro_amsys.c:52
#define BARO_AMSYS_SIGMA2
Definition: baro_amsys.c:55
#define BARO_AMSYS_OFFSET_NBSAMPLES_AVRG
Definition: baro_amsys.c:50
transaction set to done by user level
Definition: i2c.h:59
#define BARO_AMSYS_MAX_PRESSURE
Definition: baro_amsys.c:45
#define BARO_AMSYS_ADDR
Definition: baro_amsys.c:39
#define BARO_AMSYS_I2C_DEV
Definition: baro_amsys.c:66
#define BARO_AMSYS_OFFSET_NBSAMPLES_INIT
Definition: baro_amsys.c:49
bool_t baro_amsys_valid
Definition: baro_amsys.c:75
void baro_amsys_init(void)
Definition: baro_amsys.c:97
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
#define BARO_AMSYS_OFFSET_MAX
Definition: baro_amsys.c:47
float baro_amsys_altitude
Definition: baro_amsys.c:76
float baro_amsys_sigma2
Definition: baro_amsys.c:79
float baro_old
Definition: baro_amsys.c:87
#define TRUE
Definition: imu_chimu.h:144
bool_t baro_amsys_enabled
Definition: baro_amsys.c:77
float baro_amsys_offset_altitude
Definition: baro_amsys.c:82
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
float baro_amsys_offset
Definition: baro_amsys.c:74
API to get/set the generic vehicle states.
void baro_amsys_read_event(void)
Definition: baro_amsys.c:134
uint16_t baro_amsys_adc
Definition: baro_amsys.c:73
bool_t baro_amsys_offset_init
Definition: baro_amsys.c:93
#define BARO_AMSYS_OFFSET_MIN
Definition: baro_amsys.c:48
uint16_t baro_amsys_cnt
Definition: baro_amsys.c:95
void baro_amsys_read_periodic(void)
Definition: baro_amsys.c:117
float ref_alt_init
Definition: baro_amsys.c:84
uint16_t pBaroRaw
Definition: baro_amsys.c:71
bool_t 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:248
Architecture independent I2C (Inter-Integrated Circuit Bus) API.