Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
airspeed_sdp3x.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019 Gautier Hattenberger <gautier.hattenberger@enac.fr>
3 *
4 * This file is part of paparazzi
5 *
6 * paparazzi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * paparazzi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with paparazzi; see the file COPYING. If not, see
18 * <http://www.gnu.org/licenses/>.
19 */
20
24
25#include "std.h"
26#include "mcu_periph/i2c.h"
28#include "modules/core/abi.h"
29#include "mcu_periph/sys_time.h"
30
33#define SDP3X_SCALE_TEMPERATURE 200.0f
34#define SDP3X_RESET_ADDR 0x00
35#define SDP3X_RESET_CMD 0x06
36
37#define SDP3X_CONT_MEAS_AVG_MODE 0x3615
38#define SDP3X_CONT_NONE_MODE 0x361E
39
40#define SDP3X_SCALE_PRESSURE_SDP31 60
41#define SDP3X_SCALE_PRESSURE_SDP32 240
42#define SDP3X_SCALE_PRESSURE_SDP33 20
43
46#ifndef SDP3X_I2C_ADDR
47#define SDP3X_I2C_ADDR 0x42
48#endif
49
52#ifndef SDP3X_MODE
53#define SDP3X_MODE SDP3X_CONT_MEAS_AVG_MODE
54#endif
55
58#ifndef SDP3X_PRESSURE_SCALE
59#define SDP3X_PRESSURE_SCALE SDP3X_SCALE_PRESSURE_SDP31
60#endif
61
63
66
67static bool sdp3x_crc(const uint8_t data[], unsigned size, uint8_t checksum)
68{
69 uint8_t crc_value = 0xff;
70
71 // calculate 8-bit checksum with polynomial 0x31 (x^8 + x^5 + x^4 + 1)
72 for (unsigned i = 0; i < size; i++) {
73 crc_value ^= (data[i]);
74
75 for (int bit = 8; bit > 0; --bit) {
76 if (crc_value & 0x80) {
77 crc_value = (crc_value << 1) ^ 0x31;
78
79 } else {
80 crc_value = (crc_value << 1);
81 }
82 }
83 }
84
85 // verify checksum
86 return (crc_value == checksum);
87}
88
89void sdp3x_init(void)
90{
91 sdp3x.pressure = 0.f;
92 sdp3x.temperature = 0.f;
93 sdp3x.pressure_scale = SDP3X_PRESSURE_SCALE;
94 sdp3x.initialized = false;
95
97}
98
100{
101 if (sdp3x_trans.status != I2CTransDone) {
102 return; // not ready
103 }
104
105 if (sdp3x.initialized) {
106 // Initiate next read
107 i2c_receive(&SDP3X_I2C_DEV, &sdp3x_trans, SDP3X_I2C_ADDR, 6);
108 }
109 else {
110 // Init sensor in continuous mode
111 sdp3x_trans.buf[0] = SDP3X_MODE >> 8;
112 sdp3x_trans.buf[1] = SDP3X_MODE & 0xFF;
113 i2c_transmit(&SDP3X_I2C_DEV, &sdp3x_trans, SDP3X_I2C_ADDR, 2);
114 sdp3x.initialized = true;
115 }
116}
117
118void sdp3x_event(void)
119{
120 /* Check if transaction is succesfull */
121 if (sdp3x_trans.status == I2CTransSuccess) {
122
123 if (sdp3x.initialized) {
124 uint8_t buf[6];
125 for (uint8_t i = 0; i < 6; i++) {
126 buf[i] = sdp3x_trans.buf[i];
127 }
128
129 // Check the CRC
130 if (!sdp3x_crc(&buf[0], 2, buf[2]) || !sdp3x_crc(&buf[3], 2, buf[5])) {
131 // error
132 sdp3x_trans.status = I2CTransDone;
133 return;
134 }
135
136 int16_t p_raw = ((int16_t)(buf[0]) << 8) | (int16_t)(buf[1]);
137
138 float p_out = (float)p_raw / sdp3x.pressure_scale;
139
140 sdp3x.pressure = p_out;
141
142 int16_t t_raw = ((int16_t)(buf[3]) << 8) | (int16_t)(buf[4]);
143 sdp3x.temperature = (float)t_raw / SDP3X_SCALE_TEMPERATURE;
144
145 // Send (differential) pressure via ABI, air_data filters and zeroes it and computes the airspeed
146 uint32_t now_ts = get_sys_time_usec();
147 AbiSendMsgBARO_DIFF_RAW(SDP3X_SENDER_ID, now_ts, sdp3x.pressure);
148 // Send temperature as float in deg Celcius via ABI
149 AbiSendMsgTEMPERATURE_RAW(SDP3X_SENDER_ID, now_ts, sdp3x.temperature, TEMPERATURE_TYPE_DIFF);
150 }
151
152 // Set to done
153 sdp3x_trans.status = I2CTransDone;
154 } else if (sdp3x_trans.status == I2CTransFailed) {
155 // Just retry if failed
156 sdp3x_trans.status = I2CTransDone;
157 }
158}
Main include for ABI (AirBorneInterface).
#define TEMPERATURE_TYPE_DIFF
differential pressure (pitot) sensor
Definition abi_common.h:90
#define SDP3X_SENDER_ID
#define SDP3X_SCALE_TEMPERATURE
Commands and scales.
struct AirspeedSdp3x sdp3x
#define SDP3X_MODE
Default operation mode.
#define SDP3X_PRESSURE_SCALE
Default scale for SDP31.
void sdp3x_init(void)
void sdp3x_periodic(void)
static struct i2c_transaction sdp3x_trans
#define SDP3X_I2C_ADDR
Sensor I2C slave address (existing defaults 0x42, 0x44 and 0x46).
void sdp3x_event(void)
static bool sdp3x_crc(const uint8_t data[], unsigned size, uint8_t checksum)
Airspeed driver for the SDP3X pressure sensor via I2C.
static uint8_t checksum
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
bool i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Submit a write only transaction.
Definition i2c.c:202
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_VAR(ONELOOP_ANDI_FILT_CUTOFF)
Architecture independent timing functions.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
short int16_t
Typedef defining 16 bit short type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.