Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
airspeed_uavcan.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2023 Freek van Tienen <freek.v.tienen@gmail.com>
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, write to
18 * the Free Software Foundation, 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
25
26#include "airspeed_uavcan.h"
27#include "uavcan/uavcan.h"
28#include "core/abi.h"
29#include "mcu_periph/sys_time.h"
30#include "math/pprz_isa.h"
31#include "uavcan.equipment.air_data.RawAirData.h"
32
33/* Default pressure scaling */
34#ifndef AIRSPEED_UAVCAN_DIFF_P_SCALE
35#define AIRSPEED_UAVCAN_DIFF_P_SCALE 1.0f
36#endif
37
38/* The UAVCAN temperature is in Kelvin, but some nodes send Celsius: above this it is Kelvin (no air is this hot in Celsius or this cold in Kelvin) */
39#ifndef AIRSPEED_UAVCAN_KELVIN_MIN
40#define AIRSPEED_UAVCAN_KELVIN_MIN 150.f
41#endif
42
43/* Local variables */
46
47static void airspeed_uavcan_cb(struct uavcan_iface_t *iface __attribute__((unused)), CanardRxTransfer *transfer) {
48
49 struct uavcan_equipment_air_data_RawAirData msg;
50
51 if(uavcan_equipment_air_data_RawAirData_decode(transfer, &msg)) {
52 return; // decode error
53 }
54
55 uint32_t now_ts = get_sys_time_usec();
56 float diff_p = msg.differential_pressure;
57 float static_air_temp = msg.static_air_temperature;
58
59
60 if(!isnan(diff_p)) {
61 // Apply a scaling factor, air_data filters and zeroes it and computes the airspeed
62 airspeed_uavcan.diff_p = diff_p * airspeed_uavcan.diff_p_scale;
63
64 AbiSendMsgBARO_DIFF_RAW(UAVCAN_SENDER_ID, now_ts, airspeed_uavcan.diff_p);
65 }
66
67 if(!isnan(static_air_temp)) {
68 // Kelvin to Celsius, if the node sends Kelvin
69 if (static_air_temp > AIRSPEED_UAVCAN_KELVIN_MIN) {
70 static_air_temp += PPRZ_ISA_ABS_NULL;
71 }
72 airspeed_uavcan.temperature = static_air_temp;
73 AbiSendMsgTEMPERATURE_RAW(UAVCAN_SENDER_ID, now_ts, airspeed_uavcan.temperature, TEMPERATURE_TYPE_AIR);
74 }
75}
76
78{
79 // Set the default values
81
82 // Bind uavcan RAWAIRDATA message from EQUIPMENT.AIR_DATA
83 uavcan_bind(UAVCAN_EQUIPMENT_AIR_DATA_RAWAIRDATA_ID, UAVCAN_EQUIPMENT_AIR_DATA_RAWAIRDATA_SIGNATURE, &airspeed_uavcan_ev, &airspeed_uavcan_cb);
84}
Main include for ABI (AirBorneInterface).
#define TEMPERATURE_TYPE_AIR
Types of the TEMPERATURE_RAW message, what the temperature measures.
Definition abi_common.h:89
#define UAVCAN_SENDER_ID
static void airspeed_uavcan_cb(struct uavcan_iface_t *iface, CanardRxTransfer *transfer)
#define AIRSPEED_UAVCAN_DIFF_P_SCALE
struct airspeed_uavcan_t airspeed_uavcan
#define AIRSPEED_UAVCAN_KELVIN_MIN
static uavcan_event airspeed_uavcan_ev
void airspeed_uavcan_init(void)
Airspeed sensor on the uavcan bus.
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
#define PPRZ_ISA_ABS_NULL
absolute null in celcius
Definition pprz_isa.h:66
Paparazzi atmospheric pressure conversion utilities.
Architecture independent timing functions.
void uavcan_bind(uint16_t data_type_id, uint64_t data_type_signature, uavcan_event *ev, uavcan_callback cb)
Bind to a receiving message from uavcan.
Definition uavcan.c:272
struct uavcan_event_t uavcan_event
Definition uavcan.h:98
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
int transfer(const Mat *from, const image_t *to)