Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
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 
26 #include "airspeed_uavcan.h"
27 #include "uavcan/uavcan.h"
28 #include "core/abi.h"
29 
30 /* Enable ABI sending */
31 #ifndef AIRSPEED_UAVCAN_SEND_ABI
32 #define AIRSPEED_UAVCAN_SEND_ABI true
33 #endif
34 
35 /* Default pressure scaling */
36 #ifndef AIRSPEED_UAVCAN_DIFF_P_SCALE
37 #define AIRSPEED_UAVCAN_DIFF_P_SCALE 1.0f
38 #endif
39 
40 /* Airspeed lowpass filter*/
41 #ifdef USE_AIRSPEED_UAVCAN_LOWPASS_FILTER
43 
44 #ifndef AIRSPEED_UAVCAN_LOWPASS_TAU
45 #define AIRSPEED_UAVCAN_LOWPASS_TAU 0.15
46 #endif
47 
48 #ifndef AIRSPEED_UAVCAN_LOWPASS_PERIOD
49 #define AIRSPEED_UAVCAN_LOWPASS_PERIOD 0.1
50 #endif
51 
53 #endif /* USE_AIRSPEED_UAVCAN_LOWPASS_FILTER */
54 
55 /* uavcan EQUIPMENT_ESC_STATUS message definition */
56 #define UAVCAN_EQUIPMENT_AIR_DATA_RAWAIRDATA_ID 1027
57 #define UAVCAN_EQUIPMENT_AIR_DATA_RAWAIRDATA_SIGNATURE (0xC77DF38BA122F5DAULL)
58 #define UAVCAN_EQUIPMENT_AIR_DATA_RAWAIRDATA_MAX_SIZE ((397 + 7)/8)
59 
60 /* Local variables */
63 
64 #if PERIODIC_TELEMETRY
66 
67 static void airspeed_uavcan_downlink(struct transport_tx *trans, struct link_device *dev)
68 {
69  uint8_t dev_id = UAVCAN_SENDER_ID;
70  uint16_t raw = 0;
71  float offset = 0;
72  float sign = 1.0f;
73  if (airspeed_uavcan.diff_p < 0) {
74  sign = -1.0f;
75  }
76  float airspeed = sqrt(airspeed_uavcan.diff_p * sign * 2.0f / 1.225f) * sign;
77  pprz_msg_send_AIRSPEED_RAW(trans,dev,AC_ID,
78  &dev_id,
79  &raw,
80  &offset,
83  &airspeed);
84 }
85 #endif /* PERIODIC_TELEMETRY */
86 
87 static void airspeed_uavcan_cb(struct uavcan_iface_t *iface __attribute__((unused)), CanardRxTransfer *transfer) {
88  uint16_t tmp_float = 0;
89  float diff_p;
90 
91  /* Decode the message */
92  //canardDecodeScalar(transfer, (uint32_t)0, 8, false, (void*)&dest->flags);
93  //canardDecodeScalar(transfer, (uint32_t)8, 32, false, (void*)&static_p);
94  canardDecodeScalar(transfer, (uint32_t)40, 32, false, (void*)&diff_p);
95  //canardDecodeScalar(transfer, (uint32_t)72, 16, false, (void*)&tmp_float);
96  //float static_temp = canardConvertFloat16ToNativeFloat(tmp_float);
97  //canardDecodeScalar(transfer, (uint32_t)88, 16, false, (void*)&tmp_float);
98  //float diff_temp = canardConvertFloat16ToNativeFloat(tmp_float);
99  canardDecodeScalar(transfer, (uint32_t)104, 16, false, (void*)&tmp_float);
100  float static_air_temp = canardConvertFloat16ToNativeFloat(tmp_float);
101  //canardDecodeScalar(transfer, (uint32_t)120, 16, false, (void*)&tmp_float);
102  //float pitot_temp = canardConvertFloat16ToNativeFloat(tmp_float);
103 
104  if(!isnan(diff_p)) {
105  // Remove the offset and apply a scaling factor
108 
109  // Filtering
110 #ifdef USE_AIRSPEED_UAVCAN_LOWPASS_FILTER
112  airspeed_uavcan.diff_p = diff_p_filt;
113 #else
115 #endif
116 
117  // Send the ABI message
118 #if AIRSPEED_UAVCAN_SEND_ABI
119  AbiSendMsgBARO_DIFF(UAVCAN_SENDER_ID, airspeed_uavcan.diff_p);
120 #endif
121  }
122 
123  if(!isnan(static_air_temp)) {
124  airspeed_uavcan.temperature = static_air_temp;
125 #if AIRSPEED_UAVCAN_SEND_ABI
126  AbiSendMsgTEMPERATURE(UAVCAN_SENDER_ID, airspeed_uavcan.temperature);
127 #endif
128  }
129 }
130 
132 {
133  // Set the default values
135 
136  // Setup the low pass filter
137 #ifdef USE_AIRSPEED_UAVCAN_LOWPASS_FILTER
138  init_butterworth_2_low_pass(&airspeed_filter, AIRSPEED_UAVCAN_LOWPASS_TAU, AIRSPEED_UAVCAN_LOWPASS_PERIOD, 0);
139 #endif
140 
141  // Bind uavcan RAWAIRDATA message from EQUIPMENT.AIR_DATA
143 
144 #if PERIODIC_TELEMETRY
146 #endif
147 }
148 
150  if(set) {
152  }
153 }
Main include for ABI (AirBorneInterface).
#define UAVCAN_SENDER_ID
float airspeed_filter
static void airspeed_uavcan_cb(struct uavcan_iface_t *iface, CanardRxTransfer *transfer)
#define AIRSPEED_UAVCAN_DIFF_P_SCALE
static void airspeed_uavcan_downlink(struct transport_tx *trans, struct link_device *dev)
void airspeed_uavcan_autoset_offset(bool set)
struct airspeed_uavcan_t airspeed_uavcan
static uavcan_event airspeed_uavcan_ev
#define UAVCAN_EQUIPMENT_AIR_DATA_RAWAIRDATA_SIGNATURE
void airspeed_uavcan_init(void)
#define UAVCAN_EQUIPMENT_AIR_DATA_RAWAIRDATA_ID
Airspeed sensor on the uavcan bus.
float temperature
Temperature in Celsius.
float diff_p_offset
Differential pressure offset.
float diff_p
Differential pressure.
float diff_p_scale
Differential pressure scale.
Main uavcan event structure for registering/calling callbacks.
Definition: uavcan.h:60
static const float offset[]
Simple first order low pass filter with bilinear transform.
static void init_butterworth_2_low_pass(Butterworth2LowPass *filter, float tau, float sample_time, float value)
Init a second order Butterworth filter.
static float update_butterworth_2_low_pass(Butterworth2LowPass *filter, float value)
Update second order Butterworth low pass filter state with a new value.
Second order low pass filter structure.
static float sign(float x)
sign function
Definition: nav_fish.c:232
uavcan interface structure
Definition: uavcan.h:34
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:74
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:51
Periodic telemetry system header (includes downlink utility and generated code).
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
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:412
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
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98
int transfer(const Mat *from, const image_t *to)