Paparazzi UAS  v5.14.0_stable-0-g3f680d1
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces 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 "pprzlink/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 85
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)
98 {
100  pBaroRaw = 0;
101  tBaroRaw = 0;
102  baro_amsys_altitude = 0.0;
103  baro_amsys_p = 0.0;
104  baro_amsys_offset = 0;
106  baro_amsys_valid = true;
107  baro_amsys_offset_init = false;
108  baro_amsys_enabled = true;
113  // baro_head=0;
114  ref_alt_init = 0;
116 }
117 
119 {
120  // Initiate next read
122 #ifndef MEASURE_AMSYS_TEMPERATURE
124 #else
126 #endif
127  }
128 
129 #ifdef BARO_AMSYS_SYNC_SEND
132 #else
133  RunOnceEvery(10, DOWNLINK_SEND_AMSYS_BARO(DefaultChannel, DefaultDevice, &pBaroRaw, &baro_amsys_p, &baro_amsys_offset,
135 #endif
136 }
137 
139 {
140  pBaroRaw = 0;
141  // Get raw altimeter from buffer
143 #ifdef MEASURE_AMSYS_TEMPERATURE
145  const float temp_off_scale = (float)(TEMPERATURE_AMSYS_MAX - TEMPERATURE_AMSYS_MIN) /
146  (TEMPERATURE_AMSYS_OFFSET_MAX - TEMPERATURE_AMSYS_OFFSET_MIN);
147  // Tmin=-25, Tmax=85
148  baro_amsys_temp = temp_off_scale * (tBaroRaw - TEMPERATURE_AMSYS_OFFSET_MIN) +
149  TEMPERATURE_AMSYS_MIN;
150 #endif
151  // Check if this is valid altimeter
152  if (pBaroRaw == 0) {
153  baro_amsys_valid = false;
154  } else {
155  baro_amsys_valid = true;
156  }
157 
159 
160  // Continue only if a new altimeter value was received
161  //if (baro_amsys_valid && GpsFixValid()) {
162  if (baro_amsys_valid) {
163  //Cut RAW Min and Max
166  }
169  }
170 
171  //Convert to pressure
174  // Send pressure over ABI
175  AbiSendMsgBARO_ABS(BARO_AMSYS_SENDER_ID, baro_amsys_p);
176  // compute altitude localy
177  if (!baro_amsys_offset_init) {
178  --baro_amsys_cnt;
179  // Check if averaging completed
180  if (baro_amsys_cnt == 0) {
181  // Calculate average
183  ref_alt_init = GROUND_ALT;
184  baro_amsys_offset_init = true;
185 
186  // hight over Sea level at init point
187  //baro_amsys_offset_altitude = 288.15 / 0.0065 * (1 - pow((baro_amsys_p)/1013.25 , 1/5.255));
188  }
189  // Check if averaging needs to continue
192  }
193 
194  baro_amsys_altitude = 0.0;
195 
196  } else {
197  // Lowpassfiltering and convert pressure to altitude
199  (1.2041 * 9.81);
201  //New value available
202  }
204  } /*else {
205  baro_amsys_abs_altitude = 0.0;
206  }*/
207 
208 
209  // Transaction has been read
211 }
#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
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
double baro_amsys_offset_tmp
Definition: baro_amsys.c:94
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
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
bool baro_amsys_enabled
Definition: baro_amsys.c:77
#define BARO_AMSYS_OFFSET_NBSAMPLES_INIT
Definition: baro_amsys.c:49
void baro_amsys_init(void)
Definition: baro_amsys.c:97
#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
bool baro_amsys_offset_init
Definition: baro_amsys.c:93
float baro_old
Definition: baro_amsys.c:87
I2C transaction structure.
Definition: i2c.h:93
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
float baro_amsys_offset_altitude
Definition: baro_amsys.c:82
float baro_amsys_offset
Definition: baro_amsys.c:74
API to get/set the generic vehicle states.
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:270
void baro_amsys_read_event(void)
Definition: baro_amsys.c:138
uint16_t baro_amsys_adc
Definition: baro_amsys.c:73
bool baro_amsys_valid
Definition: baro_amsys.c:75
#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:118
float ref_alt_init
Definition: baro_amsys.c:84
uint16_t pBaroRaw
Definition: baro_amsys.c:71
Architecture independent I2C (Inter-Integrated Circuit Bus) API.