Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
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 "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  uint32_t now_ts = get_sys_time_usec();
164 
165  //Cut RAW Min and Max
168  }
171  }
172 
173  //Convert to pressure
176  // Send pressure over ABI
177  AbiSendMsgBARO_ABS(BARO_AMSYS_SENDER_ID, now_ts, baro_amsys_p);
178  // compute altitude localy
179  if (!baro_amsys_offset_init) {
180  --baro_amsys_cnt;
181  // Check if averaging completed
182  if (baro_amsys_cnt == 0) {
183  // Calculate average
185  ref_alt_init = GROUND_ALT;
186  baro_amsys_offset_init = true;
187 
188  // hight over Sea level at init point
189  //baro_amsys_offset_altitude = 288.15 / 0.0065 * (1 - pow((baro_amsys_p)/1013.25 , 1/5.255));
190  }
191  // Check if averaging needs to continue
194  }
195 
196  baro_amsys_altitude = 0.0;
197 
198  } else {
199  // Lowpassfiltering and convert pressure to altitude
201  (1.2041 * 9.81);
203  //New value available
204  }
206  } /*else {
207  baro_amsys_abs_altitude = 0.0;
208  }*/
209 
210 
211  // Transaction has been read
213 }
Main include for ABI (AirBorneInterface).
#define BARO_AMSYS_SENDER_ID
float baro_scale
Definition: baro_amsys.c:85
float ref_alt_init
Definition: baro_amsys.c:84
bool baro_amsys_offset_init
Definition: baro_amsys.c:93
uint16_t baro_amsys_cnt
Definition: baro_amsys.c:95
void baro_amsys_read_event(void)
Definition: baro_amsys.c:138
#define BARO_AMSYS_SIGMA2
Definition: baro_amsys.c:55
#define BARO_AMSYS_OFFSET_MAX
Definition: baro_amsys.c:47
#define BARO_AMSYS_SCALE
Definition: baro_amsys.c:42
void baro_amsys_init(void)
Definition: baro_amsys.c:97
float baro_amsys_temp
Definition: baro_amsys.c:80
#define BARO_AMSYS_R
Definition: baro_amsys.c:54
float baro_amsys_abs_altitude
Definition: baro_amsys.c:83
void baro_amsys_read_periodic(void)
Definition: baro_amsys.c:118
float baro_amsys_offset
Definition: baro_amsys.c:74
#define BARO_AMSYS_OFFSET_NBSAMPLES_INIT
Definition: baro_amsys.c:49
double baro_amsys_offset_tmp
Definition: baro_amsys.c:94
float baro_amsys_r
Definition: baro_amsys.c:78
#define BARO_AMSYS_OFFSET_NBSAMPLES_AVRG
Definition: baro_amsys.c:50
float baro_amsys_p
Definition: baro_amsys.c:81
float baro_amsys_altitude
Definition: baro_amsys.c:76
float baro_old
Definition: baro_amsys.c:87
#define BARO_AMSYS_OFFSET_MIN
Definition: baro_amsys.c:48
#define BARO_AMSYS_MAX_PRESSURE
Definition: baro_amsys.c:45
bool baro_amsys_valid
Definition: baro_amsys.c:75
float baro_amsys_offset_altitude
Definition: baro_amsys.c:82
float baro_amsys_sigma2
Definition: baro_amsys.c:79
uint16_t tBaroRaw
Definition: baro_amsys.c:72
uint16_t pBaroRaw
Definition: baro_amsys.c:71
bool baro_amsys_enabled
Definition: baro_amsys.c:77
float baro_filter
Definition: baro_amsys.c:86
#define BARO_AMSYS_I2C_DEV
Definition: baro_amsys.c:66
#define BARO_AMSYS_ADDR
Definition: baro_amsys.c:39
uint16_t baro_amsys_adc
Definition: baro_amsys.c:73
struct i2c_transaction baro_amsys_i2c_trans
Definition: baro_amsys.c:90
#define BARO_AMSYS_FILTER
Definition: baro_amsys.c:52
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:71
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
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
@ I2CTransDone
transaction set to done by user level
Definition: i2c.h:59
I2C transaction structure.
Definition: i2c.h:93
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
API to get/set the generic vehicle states.
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78