2013-08-29 02:34:34 -03:00
/*
This program is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2018-12-20 19:41:15 -04:00
2013-08-29 02:34:34 -03:00
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
2018-12-20 19:41:15 -04:00
2013-08-29 02:34:34 -03:00
You should have received a copy of the GNU General Public License
along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
2012-07-15 22:21:20 -03:00
/*
2018-07-23 20:47:40 -03:00
* AP_Airspeed . cpp - airspeed ( pitot ) driver
2012-08-17 03:08:43 -03:00
*/
2012-07-15 22:21:20 -03:00
2022-01-02 03:41:01 -04:00
# include "AP_Airspeed.h"
2022-09-29 20:10:39 -03:00
# include <AP_Vehicle/AP_Vehicle_Type.h>
2022-01-02 03:41:01 -04:00
// Dummy the AP_Airspeed class to allow building Airspeed only for plane, rover, sub, and copter & heli 2MB boards
// This could be removed once the build system allows for APM_BUILD_TYPE in header files
2022-12-13 07:40:08 -04:00
// Note that this is also defined in AP_Airspeed_Params.cpp
2022-01-02 03:41:01 -04:00
# ifndef AP_AIRSPEED_DUMMY_METHODS_ENABLED
# define AP_AIRSPEED_DUMMY_METHODS_ENABLED ((APM_BUILD_COPTER_OR_HELI && BOARD_FLASH_SIZE <= 1024) || \
APM_BUILD_TYPE ( APM_BUILD_AntennaTracker ) | | APM_BUILD_TYPE ( APM_BUILD_Blimp ) )
# endif
# if !AP_AIRSPEED_DUMMY_METHODS_ENABLED
2016-01-29 07:38:33 -04:00
# include <AP_Common/AP_Common.h>
2015-08-11 03:28:42 -03:00
# include <AP_HAL/AP_HAL.h>
2016-06-08 09:13:53 -03:00
# include <AP_HAL/I2CDevice.h>
2015-08-11 03:28:42 -03:00
# include <AP_Math/AP_Math.h>
2016-05-24 01:22:40 -03:00
# include <GCS_MAVLink/GCS.h>
2017-04-29 03:53:39 -03:00
# include <SRV_Channel/SRV_Channel.h>
2019-01-18 00:23:42 -04:00
# include <AP_Logger/AP_Logger.h>
2016-06-08 09:13:53 -03:00
# include <utility>
2016-11-30 00:59:20 -04:00
# include "AP_Airspeed_MS4525.h"
2016-11-30 02:21:48 -04:00
# include "AP_Airspeed_MS5525.h"
2017-11-03 01:47:32 -03:00
# include "AP_Airspeed_SDP3X.h"
2018-07-11 23:05:12 -03:00
# include "AP_Airspeed_DLVR.h"
2016-11-30 00:49:19 -04:00
# include "AP_Airspeed_analog.h"
2021-03-23 11:18:41 -03:00
# include "AP_Airspeed_ASP5033.h"
2017-11-03 01:47:32 -03:00
# include "AP_Airspeed_Backend.h"
2018-09-05 03:45:01 -03:00
# include "AP_Airspeed_UAVCAN.h"
2020-07-18 16:49:08 -03:00
# include "AP_Airspeed_NMEA.h"
2020-11-16 06:35:15 -04:00
# include "AP_Airspeed_MSP.h"
2022-05-24 01:14:25 -03:00
# include "AP_Airspeed_SITL.h"
2016-01-29 07:38:33 -04:00
extern const AP_HAL : : HAL & hal ;
2022-12-06 16:36:30 -04:00
# include <AP_Vehicle/AP_FixedWing.h>
2020-08-23 03:01:07 -03:00
# ifdef HAL_AIRSPEED_TYPE_DEFAULT
# define ARSPD_DEFAULT_TYPE HAL_AIRSPEED_TYPE_DEFAULT
# ifndef ARSPD_DEFAULT_PIN
# define ARSPD_DEFAULT_PIN 1
# endif
2021-11-02 23:32:05 -03:00
# elif APM_BUILD_TYPE(APM_BUILD_ArduPlane)
// The HAL_BOARD_SITL setting is required because of current probe process for MS4525 will
// connect and find the SIM_DLVR sensors & fault as there is no way to tell them apart
# if CONFIG_HAL_BOARD == HAL_BOARD_SITL
# define ARSPD_DEFAULT_TYPE TYPE_ANALOG
# define ARSPD_DEFAULT_PIN 1
# else
# define ARSPD_DEFAULT_TYPE TYPE_I2C_MS4525
# ifdef HAL_DEFAULT_AIRSPEED_PIN
# define ARSPD_DEFAULT_PIN HAL_DEFAULT_AIRSPEED_PIN
# else
# define ARSPD_DEFAULT_PIN 15
# endif
# endif //CONFIG_HAL_BOARD
# else // All Other Vehicle Types
2019-02-06 21:01:33 -04:00
# define ARSPD_DEFAULT_TYPE TYPE_NONE
# define ARSPD_DEFAULT_PIN 15
2019-07-11 22:38:39 -03:00
# endif
2016-10-31 06:48:22 -03:00
2019-10-03 08:02:07 -03:00
# ifndef HAL_AIRSPEED_BUS_DEFAULT
# define HAL_AIRSPEED_BUS_DEFAULT 1
# endif
2022-06-25 20:21:18 -03:00
# define OPTIONS_DEFAULT AP_Airspeed::OptionsMask::ON_FAILURE_AHRS_WIND_MAX_DO_DISABLE | AP_Airspeed::OptionsMask::ON_FAILURE_AHRS_WIND_MAX_RECOVERY_DO_REENABLE | AP_Airspeed::OptionsMask::USE_EKF_CONSISTENCY
2020-10-09 09:38:25 -03:00
2022-11-26 11:53:55 -04:00
# define ENABLE_PARAMETER !(APM_BUILD_TYPE(APM_BUILD_ArduPlane) || defined(HAL_BUILD_AP_PERIPH))
2022-12-06 16:36:30 -04:00
2012-07-15 22:21:20 -03:00
// table of user settable parameters
2015-10-25 14:03:46 -03:00
const AP_Param : : GroupInfo AP_Airspeed : : var_info [ ] = {
2012-07-15 22:21:20 -03:00
2022-11-26 11:53:55 -04:00
# if ENABLE_PARAMETER
// @Param: _ENABLE
// @DisplayName: Airspeed Enable
// @Description: Enable airspeed sensor support
// @Values: 0:Disable, 1:Enable
// @User: Standard
AP_GROUPINFO_FLAGS ( " _ENABLE " , 30 , AP_Airspeed , _enable , 0 , AP_PARAM_FLAG_ENABLE ) ,
# endif
2022-04-07 23:45:58 -03:00
// slots 0-9 (and 63) were previously used by params before being refactored into AP_Airspeed_Params
2012-07-15 22:21:20 -03:00
2017-11-03 03:17:34 -03:00
// @Param: _TUBE_ORDER
2014-02-14 07:07:12 -04:00
// @DisplayName: Control pitot tube order
2022-02-28 17:06:36 -04:00
// @Description: This parameter allows you to control whether the order in which the tubes are attached to your pitot tube matters. If you set this to 0 then the first (often the top) connector on the sensor needs to be the stagnation pressure (the pressure at the tip of the pitot tube). If set to 1 then the second (often the bottom) connector needs to be the stagnation pressure. If set to 2 (the default) then the airspeed driver will accept either order. The reason you may wish to specify the order is it will allow your airspeed sensor to detect if the aircraft is receiving excessive pressure on the static port compared to the stagnation port such as during a stall, which would otherwise be seen as a positive airspeed.
2014-02-14 07:07:12 -04:00
// @User: Advanced
2022-02-28 17:06:36 -04:00
// @Values: 0:Normal,1:Swapped,2:Auto Detect
2014-02-14 07:07:12 -04:00
2022-04-07 23:45:58 -03:00
// tube order param had to be shortened so is not preserved in per group descriptions
2017-11-03 03:17:34 -03:00
2019-10-03 08:02:07 -03:00
# if AIRSPEED_MAX_SENSORS > 1
2017-11-03 03:17:34 -03:00
// @Param: _PRIMARY
// @DisplayName: Primary airspeed sensor
// @Description: This selects which airspeed sensor will be the primary if multiple sensors are found
// @Values: 0:FirstSensor,1:2ndSensor
// @User: Advanced
AP_GROUPINFO ( " _PRIMARY " , 10 , AP_Airspeed , primary_sensor , 0 ) ,
2019-10-03 08:02:07 -03:00
# endif
2017-11-03 03:17:34 -03:00
2022-04-07 23:45:58 -03:00
// 11-20 were previously used by second sensor params before being refactored into AP_Airspeed_Params
2020-02-04 22:18:45 -04:00
# ifndef HAL_BUILD_AP_PERIPH
2019-01-29 19:03:35 -04:00
// @Param: _OPTIONS
// @DisplayName: Airspeed options bitmask
2022-06-25 20:21:18 -03:00
// @Description: Bitmask of options to use with airspeed. 0:Disable use based on airspeed/groundspeed mismatch (see ARSPD_WIND_MAX), 1:Automatically reenable use based on airspeed/groundspeed mismatch recovery (see ARSPD_WIND_MAX) 2:Disable voltage correction, 3:Check that the airspeed is statistically consistent with the navigation EKF vehicle and wind velocity estimates using EKF3 (requires AHRS_EKF_TYPE = 3)
2022-04-01 12:55:42 -03:00
// @Description{Copter, Blimp, Rover, Sub}: This parameter and function is not used by this vehicle. Always set to 0.
2022-06-25 20:21:18 -03:00
// @Bitmask: 0:SpeedMismatchDisable, 1:AllowSpeedMismatchRecovery, 2:DisableVoltageCorrection, 3:UseEkf3Consistency
2020-10-09 09:38:25 -03:00
// @User: Advanced
AP_GROUPINFO ( " _OPTIONS " , 21 , AP_Airspeed , _options , OPTIONS_DEFAULT ) ,
// @Param: _WIND_MAX
// @DisplayName: Maximum airspeed and ground speed difference
// @Description: If the difference between airspeed and ground speed is greater than this value the sensor will be marked unhealthy. Using ARSPD_OPTION this health value can be used to disable the sensor.
2022-04-01 12:55:42 -03:00
// @Description{Copter, Blimp, Rover, Sub}: This parameter and function is not used by this vehicle. Always set to 0.
2020-10-09 09:38:25 -03:00
// @Units: m/s
// @User: Advanced
AP_GROUPINFO ( " _WIND_MAX " , 22 , AP_Airspeed , _wind_max , 0 ) ,
// @Param: _WIND_WARN
// @DisplayName: Airspeed and ground speed difference that gives a warning
// @Description: If the difference between airspeed and ground speed is greater than this value the sensor will issue a warning. If 0 ARSPD_WIND_MAX is used.
2022-04-01 12:55:42 -03:00
// @Description{Copter, Blimp, Rover, Sub}: This parameter and function is not used by this vehicle. Always set to 0.
2020-10-09 09:38:25 -03:00
// @Units: m/s
2019-01-29 19:03:35 -04:00
// @User: Advanced
2020-10-09 09:38:25 -03:00
AP_GROUPINFO ( " _WIND_WARN " , 23 , AP_Airspeed , _wind_warn , 0 ) ,
2022-06-25 20:21:18 -03:00
// @Param: _WIND_GATE
// @DisplayName: Re-enable Consistency Check Gate Size
2022-07-04 06:15:20 -03:00
// @Description: Number of standard deviations applied to the re-enable EKF consistency check that is used when ARSPD_OPTIONS bit position 3 is set. Larger values will make the re-enabling of the airspeed sensor faster, but increase the likelihood of re-enabling a degraded sensor. The value can be tuned by using the ARSP.TR log message by setting ARSP_WIND_GATE to a value that is higher than the value for ARSP.TR observed with a healthy airspeed sensor. Occasional transients in ARSP.TR above the value set by ARSP_WIND_GATE can be tolerated provided they are less than 5 seconds in duration and less than 10% duty cycle.
2022-06-25 20:21:18 -03:00
// @Description{Copter, Blimp, Rover, Sub}: This parameter and function is not used by this vehicle.
// @Range: 0.0 10.0
// @User: Advanced
AP_GROUPINFO ( " _WIND_GATE " , 26 , AP_Airspeed , _wind_gate , 5.0f ) ,
2022-12-06 16:36:30 -04:00
// @Param: _OFF_PCNT
// @DisplayName: Maximum offset cal speed error
// @Description: The maximum percentage speed change in airspeed reports that is allowed due to offset changes between calibraions before a warning is issued. This potential speed error is in percent of ASPD_FBW_MIN. 0 disables. Helps warn of calibrations without pitot being covered.
// @Range: 0.0 10.0
// @Units: %
// @User: Advanced
AP_GROUPINFO_FRAME ( " _OFF_PCNT " , 27 , AP_Airspeed , max_speed_pcnt , 0 , AP_PARAM_FRAME_PLANE ) ,
2022-06-25 20:21:18 -03:00
2020-02-04 22:18:45 -04:00
# endif
2019-01-29 19:03:35 -04:00
2022-04-07 23:45:58 -03:00
// @Group: _
// @Path: AP_Airspeed_Params.cpp
AP_SUBGROUPINFO ( param [ 0 ] , " _ " , 28 , AP_Airspeed , AP_Airspeed_Params ) ,
2021-06-29 23:35:45 -03:00
# if AIRSPEED_MAX_SENSORS > 1
2022-04-07 23:45:58 -03:00
// @Group: 2_
// @Path: AP_Airspeed_Params.cpp
AP_SUBGROUPINFO ( param [ 1 ] , " 2_ " , 29 , AP_Airspeed , AP_Airspeed_Params ) ,
2021-06-29 23:35:45 -03:00
# endif
2022-01-03 17:16:22 -04:00
2022-11-26 11:53:55 -04:00
// index 30 is used by enable at the top of the table
2012-07-15 22:21:20 -03:00
AP_GROUPEND
} ;
2018-11-16 12:16:23 -04:00
/*
this scaling factor converts from the old system where we used a
0 to 4095 raw ADC value for 0 - 5 V to the new system which gets the
voltage in volts directly from the ADC driver
*/
# define SCALING_OLD_CALIBRATION 819 // 4095/5
2018-03-09 17:12:05 -04:00
2016-08-08 00:29:50 -03:00
AP_Airspeed : : AP_Airspeed ( )
2016-06-08 09:13:53 -03:00
{
AP_Param : : setup_object_defaults ( this , var_info ) ;
2018-03-04 20:13:48 -04:00
2023-01-04 08:20:53 -04:00
// Setup defaults that only apply to first sensor
param [ 0 ] . type . set_default ( ARSPD_DEFAULT_TYPE ) ;
# ifndef HAL_BUILD_AP_PERIPH
2023-03-22 18:19:22 -03:00
param [ 0 ] . bus . set_default ( HAL_AIRSPEED_BUS_DEFAULT ) ;
2023-01-04 08:20:53 -04:00
param [ 0 ] . pin . set_default ( ARSPD_DEFAULT_PIN ) ;
# endif
2018-03-04 20:13:48 -04:00
if ( _singleton ! = nullptr ) {
AP_HAL : : panic ( " AP_Airspeed must be singleton " ) ;
}
_singleton = this ;
2016-08-07 21:48:54 -03:00
}
2016-06-08 09:13:53 -03:00
2022-12-06 16:36:30 -04:00
void AP_Airspeed : : set_fixedwing_parameters ( const AP_FixedWing * _fixed_wing_parameters )
{
fixed_wing_parameters = _fixed_wing_parameters ;
}
2021-09-03 20:56:09 -03:00
// macro for use by HAL_INS_PROBE_LIST
# define GET_I2C_DEVICE(bus, address) hal.i2c_mgr->get_device(bus, address)
bool AP_Airspeed : : add_backend ( AP_Airspeed_Backend * backend )
{
if ( ! backend ) {
return false ;
}
if ( num_sensors > = AIRSPEED_MAX_SENSORS ) {
AP_HAL : : panic ( " Too many airspeed drivers " ) ;
}
const uint8_t i = num_sensors ;
sensor [ num_sensors + + ] = backend ;
if ( ! sensor [ i ] - > init ( ) ) {
2021-11-03 17:19:47 -03:00
GCS_SEND_TEXT ( MAV_SEVERITY_ERROR , " Airspeed %u init failed " , i + 1 ) ;
2021-09-03 20:56:09 -03:00
delete sensor [ i ] ;
sensor [ i ] = nullptr ;
}
return true ;
}
/*
macro to add a backend with check for too many sensors
We don ' t try to start more than the maximum allowed
*/
# define ADD_BACKEND(backend) \
do { add_backend ( backend ) ; \
if ( num_sensors = = AIRSPEED_MAX_SENSORS ) { return ; } \
} while ( 0 )
2022-11-26 11:19:22 -04:00
// convet params to per instance param table
// PARAMETER_CONVERSION - Added: Dec-2022
void AP_Airspeed : : convert_per_instance ( )
{
AP_Param : : ConversionInfo info ;
# ifndef HAL_BUILD_AP_PERIPH
// Vehicle conversion
if ( ! AP_Param : : find_key_by_pointer ( this , info . old_key ) ) {
return ;
}
2023-03-08 19:18:26 -04:00
static const struct convert_table {
2022-11-26 11:19:22 -04:00
uint32_t element [ 2 ] ;
ap_var_type type ;
const char * name ;
} conversion_table [ ] = {
{ { 4042 , 714 } , AP_PARAM_INT8 , " TYPE " } , // ARSPD_TYPE, ARSPD2_TYPE
{ { 74 , 778 } , AP_PARAM_INT8 , " USE " } , // ARSPD_USE, ARSPD2_USE
{ { 138 , 842 } , AP_PARAM_FLOAT , " OFFSET " } , // ARSPD_OFFSET, ARSPD2_OFFSET
{ { 202 , 906 } , AP_PARAM_FLOAT , " RATIO " } , // ARSPD_RATIO, ARSPD2_RATIO
{ { 266 , 970 } , AP_PARAM_INT8 , " PIN " } , // ARSPD_PIN, ARSPD2_PIN
# if AP_AIRSPEED_AUTOCAL_ENABLE
{ { 330 , 1034 } , AP_PARAM_INT8 , " AUTOCAL " } , // ARSPD_AUTOCAL, ARSPD2_AUTOCAL
# endif
{ { 394 , 1098 } , AP_PARAM_INT8 , " TUBE_ORDR " } , // ARSPD_TUBE_ORDER, ARSPD2_TUBE_ORDR
{ { 458 , 1162 } , AP_PARAM_INT8 , " SKIP_CAL " } , // ARSPD_SKIP_CAL, ARSPD2_SKIP_CAL
{ { 522 , 1226 } , AP_PARAM_FLOAT , " PSI_RANGE " } , // ARSPD_PSI_RANGE, ARSPD2_PSI_RANGE
{ { 586 , 1290 } , AP_PARAM_INT8 , " BUS " } , // ARSPD_BUS, ARSPD2_BUS
{ { 1546 , 1610 } , AP_PARAM_INT32 , " DEVID " } , // ARSPD_DEVID, ARSPD2_DEVID
} ;
# else
// Periph conversion
if ( ! AP_Param : : find_top_level_key_by_pointer ( this , info . old_key ) ) {
return ;
}
const struct convert_table {
uint32_t element [ 2 ] ;
ap_var_type type ;
const char * name ;
} conversion_table [ ] = {
{ { 0 , 11 } , AP_PARAM_INT8 , " TYPE " } , // ARSPD_TYPE, ARSPD2_TYPE
# if AP_AIRSPEED_AUTOCAL_ENABLE
{ { 5 , 16 } , AP_PARAM_INT8 , " AUTOCAL " } , // ARSPD_AUTOCAL, ARSPD2_AUTOCAL
# endif
{ { 8 , 19 } , AP_PARAM_FLOAT , " PSI_RANGE " } , // ARSPD_PSI_RANGE, ARSPD2_PSI_RANGE
{ { 24 , 25 } , AP_PARAM_INT32 , " DEVID " } , // ARSPD_DEVID, ARSPD2_DEVID
} ;
# endif
char param_name [ 17 ] { } ;
info . new_name = param_name ;
for ( const auto & elem : conversion_table ) {
info . type = elem . type ;
for ( uint8_t i = 0 ; i < MIN ( AIRSPEED_MAX_SENSORS , 2 ) ; i + + ) {
info . old_group_element = elem . element [ i ] ;
if ( i = = 0 ) {
hal . util - > snprintf ( param_name , sizeof ( param_name ) , " ARSPD_%s " , elem . name ) ;
} else {
hal . util - > snprintf ( param_name , sizeof ( param_name ) , " ARSPD%i_%s " , i + 1 , elem . name ) ;
}
AP_Param : : convert_old_parameter ( & info , 1.0 , 0 ) ;
}
}
}
2013-06-02 22:40:06 -03:00
void AP_Airspeed : : init ( )
2022-04-07 23:45:58 -03:00
{
2022-11-26 11:19:22 -04:00
convert_per_instance ( ) ;
2022-11-26 11:53:55 -04:00
# if ENABLE_PARAMETER
// if either type is set then enable if not manually set
if ( ! _enable . configured ( ) & & ( ( param [ 0 ] . type . get ( ) ! = TYPE_NONE ) | | ( param [ 1 ] . type . get ( ) ! = TYPE_NONE ) ) ) {
_enable . set_and_save ( 1 ) ;
}
// Check if enabled
if ( ! lib_enabled ( ) ) {
return ;
}
# endif
2022-12-30 15:41:16 -04:00
if ( enabled ( 0 ) ) {
allocate ( ) ;
}
}
void AP_Airspeed : : allocate ( )
{
2019-10-03 08:02:07 -03:00
if ( sensor [ 0 ] ! = nullptr ) {
2022-12-30 15:41:16 -04:00
// already initialised, periph may call allocate several times to allow CAN detection
2019-10-03 08:02:07 -03:00
return ;
}
2022-12-15 07:41:30 -04:00
2021-09-03 20:56:09 -03:00
# ifdef HAL_AIRSPEED_PROBE_LIST
// load sensors via a list from hwdef.dat
HAL_AIRSPEED_PROBE_LIST ;
# else
// look for sensors based on type parameters
2017-11-03 03:17:34 -03:00
for ( uint8_t i = 0 ; i < AIRSPEED_MAX_SENSORS ; i + + ) {
2019-10-03 08:02:07 -03:00
# if AP_AIRSPEED_AUTOCAL_ENABLE
2017-11-03 03:17:34 -03:00
state [ i ] . calibration . init ( param [ i ] . ratio ) ;
state [ i ] . last_saved_ratio = param [ i ] . ratio ;
2019-10-03 08:02:07 -03:00
# endif
2017-11-03 03:17:34 -03:00
2019-01-29 19:03:35 -04:00
// Set the enable automatically to false and set the probability that the airspeed is healhy to start with
state [ i ] . failures . health_probability = 1.0f ;
2017-11-03 03:17:34 -03:00
switch ( ( enum airspeed_type ) param [ i ] . type . get ( ) ) {
case TYPE_NONE :
// nothing to do
break ;
case TYPE_I2C_MS4525 :
2022-05-04 05:13:29 -03:00
# if AP_AIRSPEED_MS4525_ENABLED
2017-11-03 03:17:34 -03:00
sensor [ i ] = new AP_Airspeed_MS4525 ( * this , i ) ;
2022-05-24 01:14:25 -03:00
# endif
break ;
case TYPE_SITL :
# if AP_AIRSPEED_SITL_ENABLED
sensor [ i ] = new AP_Airspeed_SITL ( * this , i ) ;
2022-05-04 05:13:29 -03:00
# endif
2017-11-03 03:17:34 -03:00
break ;
case TYPE_ANALOG :
2022-05-04 05:13:29 -03:00
# if AP_AIRSPEED_ANALOG_ENABLED
2017-11-03 03:17:34 -03:00
sensor [ i ] = new AP_Airspeed_Analog ( * this , i ) ;
2022-05-04 05:13:29 -03:00
# endif
2017-11-03 03:17:34 -03:00
break ;
case TYPE_I2C_MS5525 :
2022-05-04 05:13:29 -03:00
# if AP_AIRSPEED_MS5525_ENABLED
2017-11-03 03:17:34 -03:00
sensor [ i ] = new AP_Airspeed_MS5525 ( * this , i , AP_Airspeed_MS5525 : : MS5525_ADDR_AUTO ) ;
2022-05-04 05:13:29 -03:00
# endif
2017-11-03 03:17:34 -03:00
break ;
case TYPE_I2C_MS5525_ADDRESS_1 :
2022-05-04 05:13:29 -03:00
# if AP_AIRSPEED_MS5525_ENABLED
2017-11-03 03:17:34 -03:00
sensor [ i ] = new AP_Airspeed_MS5525 ( * this , i , AP_Airspeed_MS5525 : : MS5525_ADDR_1 ) ;
2022-05-04 05:13:29 -03:00
# endif
2017-11-03 03:17:34 -03:00
break ;
case TYPE_I2C_MS5525_ADDRESS_2 :
2022-05-04 05:13:29 -03:00
# if AP_AIRSPEED_MS5525_ENABLED
2017-11-03 03:17:34 -03:00
sensor [ i ] = new AP_Airspeed_MS5525 ( * this , i , AP_Airspeed_MS5525 : : MS5525_ADDR_2 ) ;
2022-05-04 05:13:29 -03:00
# endif
2017-11-03 03:17:34 -03:00
break ;
case TYPE_I2C_SDP3X :
2022-05-04 05:13:29 -03:00
# if AP_AIRSPEED_SDP3X_ENABLED
2017-11-03 03:17:34 -03:00
sensor [ i ] = new AP_Airspeed_SDP3X ( * this , i ) ;
2022-05-04 05:13:29 -03:00
# endif
2017-11-03 03:17:34 -03:00
break ;
2019-07-18 02:19:09 -03:00
case TYPE_I2C_DLVR_5IN :
2022-05-04 05:13:29 -03:00
# if AP_AIRSPEED_DLVR_ENABLED
2019-07-18 02:19:09 -03:00
sensor [ i ] = new AP_Airspeed_DLVR ( * this , i , 5 ) ;
2022-05-04 05:13:29 -03:00
# endif
2019-07-18 02:19:09 -03:00
break ;
case TYPE_I2C_DLVR_10IN :
2022-05-04 05:13:29 -03:00
# if AP_AIRSPEED_DLVR_ENABLED
2019-07-18 02:19:09 -03:00
sensor [ i ] = new AP_Airspeed_DLVR ( * this , i , 10 ) ;
2022-05-04 05:13:29 -03:00
# endif
2020-08-04 18:53:27 -03:00
break ;
case TYPE_I2C_DLVR_20IN :
2022-05-04 05:13:29 -03:00
# if AP_AIRSPEED_DLVR_ENABLED
2020-08-04 18:53:27 -03:00
sensor [ i ] = new AP_Airspeed_DLVR ( * this , i , 20 ) ;
2022-05-04 05:13:29 -03:00
# endif
2020-08-04 18:53:27 -03:00
break ;
case TYPE_I2C_DLVR_30IN :
2022-05-04 05:13:29 -03:00
# if AP_AIRSPEED_DLVR_ENABLED
2020-08-04 18:53:27 -03:00
sensor [ i ] = new AP_Airspeed_DLVR ( * this , i , 30 ) ;
2022-05-04 05:13:29 -03:00
# endif
2020-08-04 18:53:27 -03:00
break ;
case TYPE_I2C_DLVR_60IN :
2022-05-04 05:13:29 -03:00
# if AP_AIRSPEED_DLVR_ENABLED
2020-08-04 18:53:27 -03:00
sensor [ i ] = new AP_Airspeed_DLVR ( * this , i , 60 ) ;
2022-05-04 05:13:29 -03:00
# endif // AP_AIRSPEED_DLVR_ENABLED
2021-03-23 11:18:41 -03:00
break ;
case TYPE_I2C_ASP5033 :
2022-05-04 05:13:29 -03:00
# if AP_AIRSPEED_ASP5033_ENABLED
2021-03-23 11:18:41 -03:00
sensor [ i ] = new AP_Airspeed_ASP5033 ( * this , i ) ;
2022-05-04 05:13:29 -03:00
# endif
2018-07-11 23:05:12 -03:00
break ;
2018-09-05 03:45:01 -03:00
case TYPE_UAVCAN :
2022-05-04 05:13:29 -03:00
# if AP_AIRSPEED_UAVCAN_ENABLED
2022-10-06 19:28:48 -03:00
sensor [ i ] = AP_Airspeed_UAVCAN : : probe ( * this , i , uint32_t ( param [ i ] . bus_id . get ( ) ) ) ;
2020-07-18 16:49:08 -03:00
# endif
break ;
case TYPE_NMEA_WATER :
2022-05-04 05:13:29 -03:00
# if AP_AIRSPEED_NMEA_ENABLED
2020-07-18 16:49:08 -03:00
# if APM_BUILD_TYPE(APM_BUILD_Rover) || APM_BUILD_TYPE(APM_BUILD_ArduSub)
sensor [ i ] = new AP_Airspeed_NMEA ( * this , i ) ;
2022-05-04 05:13:29 -03:00
# endif
2020-11-16 06:35:15 -04:00
# endif
break ;
case TYPE_MSP :
2022-05-04 05:13:29 -03:00
# if AP_AIRSPEED_MSP_ENABLED
2020-11-16 06:35:15 -04:00
sensor [ i ] = new AP_Airspeed_MSP ( * this , i , 0 ) ;
2018-09-05 03:45:01 -03:00
# endif
break ;
2017-11-03 03:17:34 -03:00
}
if ( sensor [ i ] & & ! sensor [ i ] - > init ( ) ) {
2021-11-03 17:19:47 -03:00
GCS_SEND_TEXT ( MAV_SEVERITY_ERROR , " Airspeed %u init failed " , i + 1 ) ;
2017-11-03 03:17:34 -03:00
delete sensor [ i ] ;
sensor [ i ] = nullptr ;
}
2019-05-03 08:23:58 -03:00
if ( sensor [ i ] ! = nullptr ) {
num_sensors = i + 1 ;
}
2016-11-30 00:49:19 -04:00
}
2022-10-06 19:28:48 -03:00
# if AP_AIRSPEED_UAVCAN_ENABLED
// we need a 2nd pass for DroneCAN sensors so we can match order by DEVID
// the 2nd pass accepts any devid
for ( uint8_t i = 0 ; i < AIRSPEED_MAX_SENSORS ; i + + ) {
if ( sensor [ i ] = = nullptr & & ( enum airspeed_type ) param [ i ] . type . get ( ) = = TYPE_UAVCAN ) {
sensor [ i ] = AP_Airspeed_UAVCAN : : probe ( * this , i , 0 ) ;
if ( sensor [ i ] ! = nullptr ) {
num_sensors = i + 1 ;
}
}
}
# endif // AP_AIRSPEED_UAVCAN_ENABLED
2021-09-03 20:56:09 -03:00
# endif // HAL_AIRSPEED_PROBE_LIST
2022-10-06 19:28:48 -03:00
// set DEVID to zero for any sensors not found. This allows backends to order
// based on previous value of DEVID. This allows for swapping out sensors
for ( uint8_t i = 0 ; i < AIRSPEED_MAX_SENSORS ; i + + ) {
if ( sensor [ i ] = = nullptr ) {
// note we use set() not set_and_save() to allow a sensor to be temporarily
// removed for one boot without losing its slot
param [ i ] . bus_id . set ( 0 ) ;
}
}
2013-06-02 22:40:06 -03:00
}
// read the airspeed sensor
2017-11-03 03:17:34 -03:00
float AP_Airspeed : : get_pressure ( uint8_t i )
2013-06-02 22:40:06 -03:00
{
2017-11-03 03:17:34 -03:00
if ( ! enabled ( i ) ) {
2013-06-02 22:40:06 -03:00
return 0 ;
}
2013-09-28 05:40:29 -03:00
float pressure = 0 ;
2017-11-03 03:17:34 -03:00
if ( sensor [ i ] ) {
state [ i ] . healthy = sensor [ i ] - > get_differential_pressure ( pressure ) ;
2013-06-02 22:40:06 -03:00
}
2013-09-28 05:40:29 -03:00
return pressure ;
2013-06-02 22:40:06 -03:00
}
2014-01-27 19:35:35 -04:00
// get a temperature reading if possible
2017-11-03 03:17:34 -03:00
bool AP_Airspeed : : get_temperature ( uint8_t i , float & temperature )
2014-01-27 19:35:35 -04:00
{
2017-11-03 03:17:34 -03:00
if ( ! enabled ( i ) ) {
2014-01-27 19:35:35 -04:00
return false ;
}
2017-11-03 03:17:34 -03:00
if ( sensor [ i ] ) {
return sensor [ i ] - > get_temperature ( temperature ) ;
2014-01-27 19:35:35 -04:00
}
return false ;
}
2017-11-03 03:17:34 -03:00
// calibrate the zero offset for the airspeed. This must be called at
// least once before the get_airspeed() interface can be used
2014-11-13 06:12:37 -04:00
void AP_Airspeed : : calibrate ( bool in_startup )
2012-07-15 22:21:20 -03:00
{
2022-12-15 07:41:30 -04:00
# ifndef HAL_BUILD_AP_PERIPH
2022-11-26 11:53:55 -04:00
if ( ! lib_enabled ( ) ) {
return ;
}
2019-04-11 06:57:19 -03:00
if ( hal . util - > was_watchdog_reset ( ) ) {
2019-10-03 08:02:07 -03:00
GCS_SEND_TEXT ( MAV_SEVERITY_INFO , " Airspeed: skipping cal " ) ;
2019-04-11 06:57:19 -03:00
return ;
}
2017-11-03 03:17:34 -03:00
for ( uint8_t i = 0 ; i < AIRSPEED_MAX_SENSORS ; i + + ) {
if ( ! enabled ( i ) ) {
continue ;
}
if ( state [ i ] . use_zero_offset ) {
param [ i ] . offset . set ( 0 ) ;
continue ;
}
if ( in_startup & & param [ i ] . skip_cal ) {
continue ;
}
2021-10-05 19:42:22 -03:00
if ( sensor [ i ] = = nullptr ) {
2021-11-03 17:19:47 -03:00
GCS_SEND_TEXT ( MAV_SEVERITY_ERROR , " Airspeed %u not initalized, cannot cal " , i + 1 ) ;
2021-10-05 19:42:22 -03:00
continue ;
}
2017-11-03 03:17:34 -03:00
state [ i ] . cal . start_ms = AP_HAL : : millis ( ) ;
state [ i ] . cal . count = 0 ;
state [ i ] . cal . sum = 0 ;
state [ i ] . cal . read_count = 0 ;
2022-12-08 02:47:13 -04:00
calibration_state [ i ] = CalibrationState : : IN_PROGRESS ;
2020-07-13 13:09:55 -03:00
GCS_SEND_TEXT ( MAV_SEVERITY_INFO , " Airspeed %u calibration started " , i + 1 ) ;
2014-11-13 06:12:37 -04:00
}
2022-12-15 07:41:30 -04:00
# endif // HAL_BUILD_AP_PERIPH
2016-05-24 01:22:40 -03:00
}
2016-06-26 21:52:59 -03:00
/*
2017-11-03 03:17:34 -03:00
update async airspeed zero offset calibration
2016-06-26 21:52:59 -03:00
*/
2017-11-03 03:17:34 -03:00
void AP_Airspeed : : update_calibration ( uint8_t i , float raw_pressure )
2016-05-24 01:22:40 -03:00
{
2022-12-15 07:41:30 -04:00
# ifndef HAL_BUILD_AP_PERIPH
2017-11-03 03:17:34 -03:00
if ( ! enabled ( i ) | | state [ i ] . cal . start_ms = = 0 ) {
return ;
}
2016-12-05 21:19:57 -04:00
// consider calibration complete when we have at least 15 samples
2016-06-26 21:52:59 -03:00
// over at least 1 second
2017-11-03 03:17:34 -03:00
if ( AP_HAL : : millis ( ) - state [ i ] . cal . start_ms > = 1000 & &
state [ i ] . cal . read_count > 15 ) {
if ( state [ i ] . cal . count = = 0 ) {
2021-11-03 17:19:47 -03:00
GCS_SEND_TEXT ( MAV_SEVERITY_WARNING , " Airspeed %u unhealthy " , i + 1 ) ;
2022-12-08 02:47:13 -04:00
calibration_state [ i ] = CalibrationState : : FAILED ;
2016-05-24 01:22:40 -03:00
} else {
2019-10-03 08:02:07 -03:00
GCS_SEND_TEXT ( MAV_SEVERITY_INFO , " Airspeed %u calibrated " , i + 1 ) ;
2022-12-06 16:36:30 -04:00
float calibrated_offset = state [ i ] . cal . sum / state [ i ] . cal . count ;
// check if new offset differs too greatly from last calibration, indicating pitot uncovered in wind
if ( fixed_wing_parameters ! = nullptr ) {
float airspeed_min = fixed_wing_parameters - > airspeed_min . get ( ) ;
// use percentage of ARSPD_FBW_MIN as criteria for max allowed change in offset
float max_change = 0.5 * ( sq ( ( 1 + ( max_speed_pcnt * 0.01 ) ) * airspeed_min ) - sq ( airspeed_min ) ) ;
if ( max_speed_pcnt > 0 & & ( abs ( calibrated_offset - param [ i ] . offset ) > max_change ) & & ( abs ( param [ i ] . offset ) > 0 ) ) {
GCS_SEND_TEXT ( MAV_SEVERITY_WARNING , " Arspd %d offset change large;cover and recal " , i + 1 ) ;
}
}
param [ i ] . offset . set_and_save ( calibrated_offset ) ;
2022-12-08 02:47:13 -04:00
calibration_state [ i ] = CalibrationState : : SUCCESS ;
2013-09-28 05:40:29 -03:00
}
2017-11-03 03:17:34 -03:00
state [ i ] . cal . start_ms = 0 ;
2013-09-28 05:40:29 -03:00
return ;
2012-08-17 03:08:43 -03:00
}
2016-11-30 02:21:48 -04:00
// we discard the first 5 samples
2017-11-03 03:17:34 -03:00
if ( state [ i ] . healthy & & state [ i ] . cal . read_count > 5 ) {
state [ i ] . cal . sum + = raw_pressure ;
state [ i ] . cal . count + + ;
2016-05-24 01:22:40 -03:00
}
2017-11-03 03:17:34 -03:00
state [ i ] . cal . read_count + + ;
2022-12-15 07:41:30 -04:00
# endif // HAL_BUILD_AP_PERIPH
2012-07-15 22:21:20 -03:00
}
2022-12-08 02:47:13 -04:00
// get aggregate calibration state for the Airspeed library:
AP_Airspeed : : CalibrationState AP_Airspeed : : get_calibration_state ( ) const
{
for ( uint8_t i = 0 ; i < AIRSPEED_MAX_SENSORS ; i + + ) {
switch ( calibration_state [ i ] ) {
case CalibrationState : : SUCCESS :
case CalibrationState : : NOT_STARTED :
continue ;
case CalibrationState : : IN_PROGRESS :
return CalibrationState : : IN_PROGRESS ;
case CalibrationState : : FAILED :
return CalibrationState : : FAILED ;
}
}
return CalibrationState : : SUCCESS ;
}
2017-11-03 03:17:34 -03:00
// read one airspeed sensor
void AP_Airspeed : : read ( uint8_t i )
2012-07-15 22:21:20 -03:00
{
2017-11-03 03:17:34 -03:00
if ( ! enabled ( i ) | | ! sensor [ i ] ) {
2012-08-17 03:08:43 -03:00
return ;
}
2020-07-18 16:49:08 -03:00
state [ i ] . last_update_ms = AP_HAL : : millis ( ) ;
// try and get a direct reading of airspeed
if ( sensor [ i ] - > has_airspeed ( ) ) {
state [ i ] . healthy = sensor [ i ] - > get_airspeed ( state [ i ] . airspeed ) ;
2020-12-03 23:12:54 -04:00
state [ i ] . raw_airspeed = state [ i ] . airspeed ; // for logging
2020-07-18 16:49:08 -03:00
return ;
}
2017-11-03 03:17:34 -03:00
float raw_pressure = get_pressure ( i ) ;
2022-12-15 07:41:30 -04:00
float airspeed_pressure = raw_pressure - get_offset ( i ) ;
2014-02-14 07:07:12 -04:00
2014-11-13 02:49:04 -04:00
// remember raw pressure for logging
2017-11-03 03:17:34 -03:00
state [ i ] . corrected_pressure = airspeed_pressure ;
2014-11-13 02:49:04 -04:00
2022-12-15 07:41:30 -04:00
# ifndef HAL_BUILD_AP_PERIPH
bool prev_healthy = state [ i ] . healthy ;
if ( state [ i ] . cal . start_ms ! = 0 ) {
update_calibration ( i , raw_pressure ) ;
}
2017-09-19 16:28:56 -03:00
// filter before clamping positive
2018-06-10 19:45:11 -03:00
if ( ! prev_healthy ) {
// if the previous state was not healthy then we should not
// use an IIR filter, otherwise a bad reading will last for
// some time after the sensor becomees healthy again
state [ i ] . filtered_pressure = airspeed_pressure ;
} else {
state [ i ] . filtered_pressure = 0.7f * state [ i ] . filtered_pressure + 0.3f * airspeed_pressure ;
}
2017-09-19 16:28:56 -03:00
2014-02-13 02:21:06 -04:00
/*
2017-09-19 16:28:56 -03:00
we support different pitot tube setups so user can choose if
2014-02-14 07:07:12 -04:00
they want to be able to detect pressure on the static port
2014-02-13 02:21:06 -04:00
*/
2017-11-03 03:17:34 -03:00
switch ( ( enum pitot_tube_order ) param [ i ] . tube_order . get ( ) ) {
2014-02-14 07:07:12 -04:00
case PITOT_TUBE_ORDER_NEGATIVE :
2017-11-03 03:17:34 -03:00
state [ i ] . last_pressure = - airspeed_pressure ;
state [ i ] . raw_airspeed = sqrtf ( MAX ( - airspeed_pressure , 0 ) * param [ i ] . ratio ) ;
state [ i ] . airspeed = sqrtf ( MAX ( - state [ i ] . filtered_pressure , 0 ) * param [ i ] . ratio ) ;
2017-09-19 16:28:56 -03:00
break ;
2014-02-14 07:07:12 -04:00
case PITOT_TUBE_ORDER_POSITIVE :
2017-11-03 03:17:34 -03:00
state [ i ] . last_pressure = airspeed_pressure ;
state [ i ] . raw_airspeed = sqrtf ( MAX ( airspeed_pressure , 0 ) * param [ i ] . ratio ) ;
state [ i ] . airspeed = sqrtf ( MAX ( state [ i ] . filtered_pressure , 0 ) * param [ i ] . ratio ) ;
2014-02-14 07:07:12 -04:00
break ;
case PITOT_TUBE_ORDER_AUTO :
default :
2017-11-03 03:17:34 -03:00
state [ i ] . last_pressure = fabsf ( airspeed_pressure ) ;
state [ i ] . raw_airspeed = sqrtf ( fabsf ( airspeed_pressure ) * param [ i ] . ratio ) ;
state [ i ] . airspeed = sqrtf ( fabsf ( state [ i ] . filtered_pressure ) * param [ i ] . ratio ) ;
2014-02-14 07:07:12 -04:00
break ;
2014-02-13 02:21:06 -04:00
}
2022-12-15 07:41:30 -04:00
# endif // HAL_BUILD_AP_PERIPH
2017-11-03 03:17:34 -03:00
}
// read all airspeed sensors
2022-01-03 17:16:22 -04:00
void AP_Airspeed : : update ( )
2017-11-03 03:17:34 -03:00
{
2022-11-26 11:53:55 -04:00
if ( ! lib_enabled ( ) ) {
return ;
}
2017-11-03 03:17:34 -03:00
for ( uint8_t i = 0 ; i < AIRSPEED_MAX_SENSORS ; i + + ) {
read ( i ) ;
}
2021-08-18 08:42:16 -03:00
# if HAL_GCS_ENABLED
2017-11-03 03:17:34 -03:00
// debugging until we get MAVLink support for 2nd airspeed sensor
if ( enabled ( 1 ) ) {
2018-05-13 06:29:47 -03:00
gcs ( ) . send_named_float ( " AS2 " , get_airspeed ( 1 ) ) ;
2017-11-03 03:17:34 -03:00
}
# endif
2022-05-13 01:02:24 -03:00
# if HAL_LOGGING_ENABLED
const uint8_t old_primary = primary ;
# endif
2018-03-09 17:12:05 -04:00
// setup primary
if ( healthy ( primary_sensor . get ( ) ) ) {
primary = primary_sensor . get ( ) ;
2019-01-29 22:02:40 -04:00
} else {
for ( uint8_t i = 0 ; i < AIRSPEED_MAX_SENSORS ; i + + ) {
if ( healthy ( i ) ) {
primary = i ;
break ;
}
}
2018-03-09 04:07:34 -04:00
}
2019-01-29 22:02:40 -04:00
check_sensor_failures ( ) ;
2022-01-03 17:16:22 -04:00
# if HAL_LOGGING_ENABLED
2022-05-13 01:02:24 -03:00
if ( primary ! = old_primary ) {
AP : : logger ( ) . Write_Event ( LogEvent : : AIRSPEED_PRIMARY_CHANGED ) ;
}
2022-01-03 17:16:22 -04:00
if ( _log_bit ! = ( uint32_t ) - 1 & & AP : : logger ( ) . should_log ( _log_bit ) ) {
2019-04-05 23:48:44 -03:00
Log_Airspeed ( ) ;
}
2019-10-03 08:02:07 -03:00
# endif
2019-04-05 23:48:44 -03:00
}
2022-05-04 05:13:29 -03:00
# if AP_AIRSPEED_MSP_ENABLED
2020-11-16 06:35:15 -04:00
/*
handle MSP airspeed data
*/
void AP_Airspeed : : handle_msp ( const MSP : : msp_airspeed_data_message_t & pkt )
{
2022-11-26 11:53:55 -04:00
if ( ! lib_enabled ( ) ) {
return ;
}
2020-11-16 06:35:15 -04:00
if ( pkt . instance > 1 ) {
return ; //supporting 2 airspeed sensors at most
}
for ( uint8_t i = 0 ; i < AIRSPEED_MAX_SENSORS ; i + + ) {
if ( sensor [ i ] ) {
sensor [ i ] - > handle_msp ( pkt ) ;
}
}
}
# endif
2022-10-23 20:43:35 -03:00
// @LoggerMessage: HYGR
// @Description: Hygrometer data
// @Field: TimeUS: Time since system startup
// @Field: Id: sensor ID
// @Field: Humidity: percentage humidity
// @Field: Temp: temperature in degrees C
2019-04-05 23:48:44 -03:00
void AP_Airspeed : : Log_Airspeed ( )
{
const uint64_t now = AP_HAL : : micros64 ( ) ;
for ( uint8_t i = 0 ; i < AIRSPEED_MAX_SENSORS ; i + + ) {
2022-10-17 05:35:08 -03:00
if ( ! enabled ( i ) | | sensor [ i ] = = nullptr ) {
2019-04-05 23:48:44 -03:00
continue ;
}
float temperature ;
if ( ! get_temperature ( i , temperature ) ) {
temperature = 0 ;
}
2020-11-18 18:52:48 -04:00
const struct log_ARSP pkt {
LOG_PACKET_HEADER_INIT ( LOG_ARSP_MSG ) ,
2019-04-05 23:48:44 -03:00
time_us : now ,
2020-11-18 18:52:48 -04:00
instance : i ,
2019-04-05 23:48:44 -03:00
airspeed : get_raw_airspeed ( i ) ,
diffpressure : get_differential_pressure ( i ) ,
temperature : ( int16_t ) ( temperature * 100.0f ) ,
rawpressure : get_corrected_pressure ( i ) ,
offset : get_offset ( i ) ,
use : use ( i ) ,
healthy : healthy ( i ) ,
2022-03-01 13:44:43 -04:00
health_prob : get_health_probability ( i ) ,
2022-06-25 20:21:18 -03:00
test_ratio : get_test_ratio ( i ) ,
2019-04-05 23:48:44 -03:00
primary : get_primary ( )
} ;
AP : : logger ( ) . WriteBlock ( & pkt , sizeof ( pkt ) ) ;
2022-10-17 05:35:08 -03:00
# if AP_AIRSPEED_HYGROMETER_ENABLE
struct {
uint32_t sample_ms ;
float temperature ;
float humidity ;
} hygrometer ;
if ( sensor [ i ] - > get_hygrometer ( hygrometer . sample_ms , hygrometer . temperature , hygrometer . humidity ) & &
hygrometer . sample_ms ! = state [ i ] . last_hygrometer_log_ms ) {
AP : : logger ( ) . WriteStreaming ( " HYGR " ,
2022-10-23 20:43:35 -03:00
" TimeUS,Id,Humidity,Temp " ,
" s#%O " ,
" F--- " ,
" QBff " ,
2022-10-17 05:35:08 -03:00
AP_HAL : : micros64 ( ) ,
2022-10-23 20:43:35 -03:00
i ,
2022-10-17 05:35:08 -03:00
hygrometer . humidity ,
hygrometer . temperature ) ;
state [ i ] . last_hygrometer_log_ms = hygrometer . sample_ms ;
}
# endif
2017-11-03 03:17:34 -03:00
}
2012-07-15 22:21:20 -03:00
}
2014-02-17 18:24:14 -04:00
2017-11-03 03:17:34 -03:00
bool AP_Airspeed : : use ( uint8_t i ) const
2017-04-29 03:53:39 -03:00
{
2022-12-15 07:41:30 -04:00
# ifndef HAL_BUILD_AP_PERIPH
2022-11-26 11:53:55 -04:00
if ( ! lib_enabled ( ) ) {
return false ;
}
2021-06-17 03:03:35 -03:00
if ( _force_disable_use ) {
return false ;
}
2017-11-03 03:17:34 -03:00
if ( ! enabled ( i ) | | ! param [ i ] . use ) {
2017-04-29 03:53:39 -03:00
return false ;
}
2021-09-18 14:55:41 -03:00
if ( param [ i ] . use = = 2 & & ! is_zero ( SRV_Channels : : get_output_scaled ( SRV_Channel : : k_throttle ) ) ) {
2017-04-29 03:53:39 -03:00
// special case for gliders with airspeed sensors behind the
// propeller. Allow airspeed to be disabled when throttle is
// running
return false ;
}
return true ;
2022-12-15 07:41:30 -04:00
# else
return false ;
# endif // HAL_BUILD_AP_PERIPH
2017-04-29 03:53:39 -03:00
}
2017-11-03 03:17:34 -03:00
/*
return true if all enabled sensors are healthy
*/
bool AP_Airspeed : : all_healthy ( void ) const
{
for ( uint8_t i = 0 ; i < AIRSPEED_MAX_SENSORS ; i + + ) {
if ( enabled ( i ) & & ! healthy ( i ) ) {
return false ;
}
}
return true ;
}
2018-03-04 20:13:48 -04:00
2022-11-26 11:53:55 -04:00
bool AP_Airspeed : : lib_enabled ( ) const {
# if ENABLE_PARAMETER
return _enable > 0 ;
# endif
return true ;
}
2022-04-20 06:06:58 -03:00
// return true if airspeed is enabled
bool AP_Airspeed : : enabled ( uint8_t i ) const {
2022-11-26 11:53:55 -04:00
if ( ! lib_enabled ( ) ) {
return false ;
}
2022-04-20 06:06:58 -03:00
if ( i < AIRSPEED_MAX_SENSORS ) {
return param [ i ] . type . get ( ) ! = TYPE_NONE ;
}
return false ;
}
// return health status of sensor
bool AP_Airspeed : : healthy ( uint8_t i ) const {
2022-04-20 06:07:57 -03:00
bool ok = state [ i ] . healthy & & enabled ( i ) & & sensor [ i ] ! = nullptr ;
2022-04-20 06:06:58 -03:00
# ifndef HAL_BUILD_AP_PERIPH
// sanity check the offset parameter. Zero is permitted if we are skipping calibration.
ok & = ( fabsf ( param [ i ] . offset ) > 0 | | state [ i ] . use_zero_offset | | param [ i ] . skip_cal ) ;
# endif
return ok ;
}
2022-04-20 06:09:12 -03:00
// return the current airspeed in m/s
float AP_Airspeed : : get_airspeed ( uint8_t i ) const {
if ( ! enabled ( i ) ) {
// we can't have negative airspeed so sending an obviously invalid value
return - 1.0 ;
}
return state [ i ] . airspeed ;
}
// return the unfiltered airspeed in m/s
float AP_Airspeed : : get_raw_airspeed ( uint8_t i ) const {
if ( ! enabled ( i ) ) {
// we can't have negative airspeed so sending an obviously invalid value
return - 1.0 ;
}
return state [ i ] . raw_airspeed ;
}
// return the differential pressure in Pascal for the last airspeed reading
float AP_Airspeed : : get_differential_pressure ( uint8_t i ) const {
if ( ! enabled ( i ) ) {
return 0.0 ;
}
return state [ i ] . last_pressure ;
}
// return the current corrected pressure
float AP_Airspeed : : get_corrected_pressure ( uint8_t i ) const {
if ( ! enabled ( i ) ) {
return 0.0 ;
}
return state [ i ] . corrected_pressure ;
}
2022-10-17 05:35:08 -03:00
# if AP_AIRSPEED_HYGROMETER_ENABLE
bool AP_Airspeed : : get_hygrometer ( uint8_t i , uint32_t & last_sample_ms , float & temperature , float & humidity ) const
{
if ( ! enabled ( i ) | | sensor [ i ] = = nullptr ) {
return false ;
}
return sensor [ i ] - > get_hygrometer ( last_sample_ms , temperature , humidity ) ;
}
# endif // AP_AIRSPEED_HYGROMETER_ENABLE
2022-01-02 03:41:01 -04:00
# else // build type is not appropriate; provide a dummy implementation:
const AP_Param : : GroupInfo AP_Airspeed : : var_info [ ] = { AP_GROUPEND } ;
void AP_Airspeed : : update ( ) { } ;
bool AP_Airspeed : : get_temperature ( uint8_t i , float & temperature ) { return false ; }
void AP_Airspeed : : calibrate ( bool in_startup ) { }
2022-12-08 02:55:47 -04:00
AP_Airspeed : : CalibrationState AP_Airspeed : : get_calibration_state ( ) const { return CalibrationState : : NOT_STARTED ; }
2022-01-02 03:41:01 -04:00
bool AP_Airspeed : : use ( uint8_t i ) const { return false ; }
2022-04-20 06:06:58 -03:00
bool AP_Airspeed : : enabled ( uint8_t i ) const { return false ; }
bool AP_Airspeed : : healthy ( uint8_t i ) const { return false ; }
2022-04-20 06:09:12 -03:00
float AP_Airspeed : : get_airspeed ( uint8_t i ) const { return 0.0 ; }
float AP_Airspeed : : get_differential_pressure ( uint8_t i ) const { return 0.0 ; }
2022-01-02 03:41:01 -04:00
2022-05-04 05:13:29 -03:00
# if AP_AIRSPEED_MSP_ENABLED
2022-01-02 03:41:01 -04:00
void AP_Airspeed : : handle_msp ( const MSP : : msp_airspeed_data_message_t & pkt ) { }
# endif
bool AP_Airspeed : : all_healthy ( void ) const { return false ; }
void AP_Airspeed : : init ( void ) { } ;
AP_Airspeed : : AP_Airspeed ( ) { }
# endif // #if AP_AIRSPEED_DUMMY_METHODS_ENABLED
2018-03-04 20:13:48 -04:00
// singleton instance
AP_Airspeed * AP_Airspeed : : _singleton ;
2019-04-05 22:10:17 -03:00
namespace AP {
AP_Airspeed * airspeed ( )
{
return AP_Airspeed : : get_singleton ( ) ;
}
} ;