Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
airspeed_ms45xx_i2c.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2014 Freek van Tienen <freek.v.tienen@gmail.com>
3 * 2014 Felix Ruess <felix.ruess@gmail.com>
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
27
28#include "std.h"
29#include "mcu_periph/i2c.h"
31#include "modules/core/abi.h"
32#include "mcu_periph/sys_time.h"
33
34#include "mcu_periph/uart.h"
35#include "pprzlink/messages.h"
37
38
41#ifndef MS45XX_I2C_DEV
42#define MS45XX_I2C_DEV i2c2
43#endif
44
47#ifndef MS45XX_I2C_ADDR
48#define MS45XX_I2C_ADDR 0x50
49#endif
50
54#ifndef MS45XX_PRESSURE_OUTPUT_TYPE_InH2O
55#define MS45XX_PRESSURE_OUTPUT_TYPE_InH2O 0
56#endif
57
62#ifndef MS45XX_PRESSURE_RANGE
63#define MS45XX_PRESSURE_RANGE 1
64#endif
65
68#ifndef MS45XX_PRESSURE_TYPE
69#define MS45XX_PRESSURE_TYPE 0
70#endif
71
76#ifndef MS45XX_OUTPUT_TYPE
77#define MS45XX_OUTPUT_TYPE 0
78#endif
79
81#define InH2O_TO_PA 249.08891
82
84#define PSI_TO_PA 6894.75729
85
86#if MS45XX_PRESSURE_OUTPUT_TYPE_InH2O
87#define OutputPressureToPa InH2O_TO_PA
88#else
89#define OutputPressureToPa PSI_TO_PA
90#endif
91
92#if MS45XX_OUTPUT_TYPE == 0
93/* Offset and scaling for OUTPUT TYPE A:
94 * p_raw = (0.8*16383)/ (Pmax - Pmin) * (pressure - Pmin) + 0.1*16383
95 * For differential sensors Pmax = MS45XX_PRESSURE_RANGE = -Pmin.
96 *
97 * p_diff = (p_raw - 0.1*16383) * 2*RANGE/(0.8*16383) - RANGE
98 * p_diff = p_raw * 2*RANGE/(0.8*16383) - (RANGE + (0.1 * 16383) * 2*RANGE/(0.8*16383)
99 * p_diff = p_raw * 2*RANGE/(0.8*16383) - (1.25 * RANGE)
100 * p_diff = (p_raw - offset) * scale
101 * then convert to Pascal
102 */
103#ifndef MS45XX_PRESSURE_SCALE
104#define MS45XX_PRESSURE_SCALE (2 * MS45XX_PRESSURE_RANGE / (0.8 * 16383) * OutputPressureToPa)
105#endif
106#ifndef MS45XX_PRESSURE_OFFSET
107#define MS45XX_PRESSURE_OFFSET (16383 / 2)
108#endif
109#else /* Can still be improved using another if statment with MS45XX_PRESSURE_TYPE etc. */
110/* Offset and scaling for OUTPUT TYPE B:
111 * p_raw = (0.9*16383)/ (Pmax - Pmin) * (pressure - Pmin) + 0.05*16383
112 */
113#ifndef MS45XX_PRESSURE_SCALE
114#define MS45XX_PRESSURE_SCALE (MS45XX_PRESSURE_RANGE/(0.9*16383)*OutputPressureToPa)
115#endif
116#ifndef MS45XX_PRESSURE_OFFSET
117#define MS45XX_PRESSURE_OFFSET (0.05*16383)
118#endif
119#endif
120
126
127
128
133#ifdef MS45XX_AIRSPEED_SCALE
134PRINT_CONFIG_MSG("MS45XX changed air density. PS: Use MS45XX_PRESSURE_SCALE to calibrate the MS45XX.");
135#endif
136
137#define MS45XX_RHO_DIV_2 1.6327
138
139
142
144{
145 ms45xx.pressure = 0.;
146 ms45xx.temperature = 0;
147 ms45xx.airspeed = 0.;
148 ms45xx.raw_p = 0;
149 ms45xx.pressure_type = MS45XX_PRESSURE_TYPE;
150 ms45xx.pressure_scale = MS45XX_PRESSURE_SCALE;
151 ms45xx.pressure_offset = MS45XX_PRESSURE_OFFSET;
152
153 ms45xx_trans.status = I2CTransDone;
154}
155
156
158{
159 // Initiate next read
160 if (ms45xx_trans.status == I2CTransDone) {
162 }
163}
164
166{
167 /* Check if transaction is succesfull */
168 if (ms45xx_trans.status == I2CTransSuccess) {
169
170 /* 2 MSB of data are status bits, 0 = good data, 2 = already fetched, 3 = fault */
171 uint8_t status = (0xC0 & ms45xx_trans.buf[0]) >> 6;
172
173 if (status == 0) {
174 /* 14bit raw pressure */
175 uint16_t p_raw = 0x3FFF & (((uint16_t)(ms45xx_trans.buf[0]) << 8) | (uint16_t)(ms45xx_trans.buf[1]));
176 ms45xx.raw_p = p_raw;
177
178 /* 11bit raw temperature, 5 LSB bits not used */
179 uint16_t temp_raw = 0xFFE0 & (((uint16_t)(ms45xx_trans.buf[2]) << 8) |
180 (uint16_t)(ms45xx_trans.buf[3]));
181 temp_raw = temp_raw >> 5;
182
183 /* Reject any values that are the absolute minimum or maximums these
184 can happen due to gnd lifts or communication errors on the bus */
185 if(p_raw == 0x3FFF || p_raw == 0 || temp_raw == 0x7FF || temp_raw == 0) {
186 ms45xx_trans.status = I2CTransDone;
187 return;
188 }
189
190 /* For type Diff
191 * Output is proportional to the difference between Port 1 and Port 2. Output
192 * swings positive when Port 1> Port 2. Output is 50% of total counts
193 * when Port 1=Port 2.
194 * For type Gauge
195 * p_out = (p_raw - offset) * scale
196 */
197
198 float p_out = (p_raw - ms45xx.pressure_offset) * ms45xx.pressure_scale;
199
200 /* 0 = -50degC, 20147 = 150degC
201 * ms45xx_temperature in 0.1 deg Celcius
202 */
203 ms45xx.temperature = ((uint32_t)temp_raw * 2000) / 2047 - 500;
204
205 // if(electrical.vboard != 0) {
206 // float volt_diff = electrical.vboard - 5.0f;
207 // Bound(volt_diff, -0.7f, 0.5f);
208
209 // p_out -= 65.0f * volt_diff;
210 // ms45xx.temperature -= 8.87f * volt_diff;
211 // }
212 ms45xx.pressure = p_out;
213
214 // Send (differential) pressure via ABI, air_data filters and zeroes it and computes the airspeed
215 uint32_t now_ts = get_sys_time_usec();
216 AbiSendMsgBARO_DIFF_RAW(MS45XX_SENDER_ID, now_ts, ms45xx.pressure);
217
218 // Send temperature as float in deg Celcius via ABI
219 float temp = ms45xx.temperature / 10.0f;
220 AbiSendMsgTEMPERATURE_RAW(MS45XX_SENDER_ID, now_ts, temp, TEMPERATURE_TYPE_DIFF);
221
222 // Compute airspeed
223 float sign = 1.0f;
224 if (ms45xx.pressure < 0.0f) {
225 sign = -1.0f;
226 }
227 ms45xx.airspeed = sqrtf(ms45xx.pressure * sign * MS45XX_RHO_DIV_2) * sign;
228
229 }
230
231 // Set to done
232 ms45xx_trans.status = I2CTransDone;
233 } else if (ms45xx_trans.status == I2CTransFailed) {
234 // Just retry if failed
235 ms45xx_trans.status = I2CTransDone;
236 }
237}
Main include for ABI (AirBorneInterface).
#define TEMPERATURE_TYPE_DIFF
differential pressure (pitot) sensor
Definition abi_common.h:90
#define MS45XX_SENDER_ID
#define MS45XX_RHO_DIV_2
Quadratic scale factor for indicated airspeed.
void ms45xx_i2c_event(void)
void ms45xx_i2c_periodic(void)
#define MS45XX_I2C_ADDR
Sensor I2C slave address (existing defaults 0x50, 0x6C and 0x8C).
struct AirspeedMs45xx ms45xx
#define MS45XX_PRESSURE_OFFSET
static struct i2c_transaction ms45xx_trans
#define MS45XX_PRESSURE_SCALE
void ms45xx_i2c_init(void)
Airspeed driver for the MS45xx pressure sensor via I2C.
static uint8_t status
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
#define MS45XX_PRESSURE_RANGE
Definition disco.h:94
#define MS45XX_OUTPUT_TYPE
Definition disco.h:96
#define MS45XX_I2C_DEV
Definition disco.h:93
#define MS45XX_PRESSURE_TYPE
Definition disco.h:95
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
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition i2c.h:58
@ I2CTransFailed
transaction failed
Definition i2c.h:59
@ 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.
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
static float sign(float x)
sign function
Definition nav_fish.c:232
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
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.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.