From d5ddb442412b4d8c45a74bd4afe143ac7a3d8e8e Mon Sep 17 00:00:00 2001 From: Jaeyoung Lim Date: Fri, 10 Feb 2023 10:55:19 +0100 Subject: [PATCH] Add sensor airspeed sim This commit adds the airspeed sensor sim to simulate airspeed sensors --- .../init.d-posix/airframes/4003_gz_rc_cessna | 1 - .../init.d-posix/px4-rc.simulator | 3 + src/modules/simulation/Kconfig | 1 + .../sensor_airspeed_sim/CMakeLists.txt | 43 ++++ .../simulation/sensor_airspeed_sim/Kconfig | 5 + .../sensor_airspeed_sim/SensorAirspeedSim.cpp | 206 ++++++++++++++++++ .../sensor_airspeed_sim/SensorAirspeedSim.hpp | 95 ++++++++ .../sensor_airspeed_sim/parameters.c | 41 ++++ 8 files changed, 394 insertions(+), 1 deletion(-) create mode 100644 src/modules/simulation/sensor_airspeed_sim/CMakeLists.txt create mode 100644 src/modules/simulation/sensor_airspeed_sim/Kconfig create mode 100644 src/modules/simulation/sensor_airspeed_sim/SensorAirspeedSim.cpp create mode 100644 src/modules/simulation/sensor_airspeed_sim/SensorAirspeedSim.hpp create mode 100644 src/modules/simulation/sensor_airspeed_sim/parameters.c diff --git a/ROMFS/px4fmu_common/init.d-posix/airframes/4003_gz_rc_cessna b/ROMFS/px4fmu_common/init.d-posix/airframes/4003_gz_rc_cessna index cfda755a73..5fa839247e 100644 --- a/ROMFS/px4fmu_common/init.d-posix/airframes/4003_gz_rc_cessna +++ b/ROMFS/px4fmu_common/init.d-posix/airframes/4003_gz_rc_cessna @@ -55,7 +55,6 @@ param set-default RWTO_TKOFF 1 param set-default CA_AIRFRAME 1 param set-default COM_PREARM_MODE 2 -param set-default CBRK_AIRSPD_CHK 162128 param set-default CA_ROTOR_COUNT 1 param set-default CA_ROTOR0_PX 0.3 diff --git a/ROMFS/px4fmu_common/init.d-posix/px4-rc.simulator b/ROMFS/px4fmu_common/init.d-posix/px4-rc.simulator index 2ea8171e89..acc02a06d2 100644 --- a/ROMFS/px4fmu_common/init.d-posix/px4-rc.simulator +++ b/ROMFS/px4fmu_common/init.d-posix/px4-rc.simulator @@ -80,6 +80,7 @@ elif [ "$PX4_SIMULATOR" = "gz" ] || [ "$(param show -q SIM_GZ_EN)" -eq "1" ]; th sensor_baro_sim start sensor_gps_sim start sensor_mag_sim start + sensor_airspeed_sim start else echo "ERROR [init] gz_bridge failed to start" @@ -93,6 +94,7 @@ elif [ "$PX4_SIMULATOR" = "gz" ] || [ "$(param show -q SIM_GZ_EN)" -eq "1" ]; th sensor_baro_sim start sensor_gps_sim start sensor_mag_sim start + sensor_airspeed_sim start else echo "ERROR [init] gz_bridge failed to start" @@ -107,6 +109,7 @@ elif [ "$PX4_SIMULATOR" = "gz" ] || [ "$(param show -q SIM_GZ_EN)" -eq "1" ]; th sensor_baro_sim start sensor_gps_sim start sensor_mag_sim start + sensor_airspeed_sim start else echo "ERROR [init] gz_bridge failed to start" diff --git a/src/modules/simulation/Kconfig b/src/modules/simulation/Kconfig index 7544eaa6fd..569ab38c6a 100644 --- a/src/modules/simulation/Kconfig +++ b/src/modules/simulation/Kconfig @@ -4,6 +4,7 @@ menu "Simulation" default n select MODULES_SIMULATION_BATTERY_SIMULATOR select MODULES_SIMULATION_PWM_OUT_SIM + select MODULES_SIMULATION_SENSOR_AIRSPEED_SIM select MODULES_SIMULATION_SENSOR_BARO_SIM select MODULES_SIMULATION_SENSOR_GPS_SIM select MODULES_SIMULATION_SENSOR_MAG_SIM diff --git a/src/modules/simulation/sensor_airspeed_sim/CMakeLists.txt b/src/modules/simulation/sensor_airspeed_sim/CMakeLists.txt new file mode 100644 index 0000000000..d993fe49af --- /dev/null +++ b/src/modules/simulation/sensor_airspeed_sim/CMakeLists.txt @@ -0,0 +1,43 @@ +############################################################################ +# +# Copyright (c) 2021-2022 PX4 Development Team. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name PX4 nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +px4_add_module( + MODULE modules__simulation__sensor_airspeed_sim + MAIN sensor_airspeed_sim + COMPILE_FLAGS + SRCS + SensorAirspeedSim.cpp + SensorAirspeedSim.hpp + DEPENDS + px4_work_queue + ) diff --git a/src/modules/simulation/sensor_airspeed_sim/Kconfig b/src/modules/simulation/sensor_airspeed_sim/Kconfig new file mode 100644 index 0000000000..c485cb4676 --- /dev/null +++ b/src/modules/simulation/sensor_airspeed_sim/Kconfig @@ -0,0 +1,5 @@ +menuconfig MODULES_SIMULATION_SENSOR_AIRSPEED_SIM + bool "sensor_airspeed_sim" + default n + ---help--- + Enable support for sensor_airspeed_sim diff --git a/src/modules/simulation/sensor_airspeed_sim/SensorAirspeedSim.cpp b/src/modules/simulation/sensor_airspeed_sim/SensorAirspeedSim.cpp new file mode 100644 index 0000000000..a407ea8100 --- /dev/null +++ b/src/modules/simulation/sensor_airspeed_sim/SensorAirspeedSim.cpp @@ -0,0 +1,206 @@ +/**************************************************************************** + * + * Copyright (c) 2021 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#include "SensorAirspeedSim.hpp" + +#include +#include +#include + +using namespace matrix; + +SensorAirspeedSim::SensorAirspeedSim() : + ModuleParams(nullptr), + ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::hp_default) +{ +} + +SensorAirspeedSim::~SensorAirspeedSim() +{ + perf_free(_loop_perf); +} + +bool SensorAirspeedSim::init() +{ + ScheduleOnInterval(125_ms); // 8 Hz + return true; +} + +float SensorAirspeedSim::generate_wgn() +{ + // generate white Gaussian noise sample with std=1 + + // algorithm 1: + // float temp=((float)(rand()+1))/(((float)RAND_MAX+1.0f)); + // return sqrtf(-2.0f*logf(temp))*cosf(2.0f*M_PI_F*rand()/RAND_MAX); + // algorithm 2: from BlockRandGauss.hpp + static float V1, V2, S; + static bool phase = true; + float X; + + if (phase) { + do { + float U1 = (float)rand() / (float)RAND_MAX; + float U2 = (float)rand() / (float)RAND_MAX; + V1 = 2.0f * U1 - 1.0f; + V2 = 2.0f * U2 - 1.0f; + S = V1 * V1 + V2 * V2; + } while (S >= 1.0f || fabsf(S) < 1e-8f); + + X = V1 * float(sqrtf(-2.0f * float(logf(S)) / S)); + + } else { + X = V2 * float(sqrtf(-2.0f * float(logf(S)) / S)); + } + + phase = !phase; + return X; +} + +void SensorAirspeedSim::Run() +{ + if (should_exit()) { + ScheduleClear(); + exit_and_cleanup(); + return; + } + + perf_begin(_loop_perf); + + // Check if parameters have changed + if (_parameter_update_sub.updated()) { + // clear update + parameter_update_s param_update; + _parameter_update_sub.copy(¶m_update); + + updateParams(); + } + + if (_vehicle_local_position_sub.updated() && _vehicle_global_position_sub.updated() + && _vehicle_attitude_sub.updated()) { + + vehicle_local_position_s lpos{}; + _vehicle_local_position_sub.copy(&lpos); + + vehicle_global_position_s gpos{}; + _vehicle_global_position_sub.copy(&gpos); + + vehicle_attitude_s attitude{}; + _vehicle_attitude_sub.copy(&attitude); + + Vector3f local_velocity = Vector3f{lpos.vx, lpos.vy, lpos.vz}; + Vector3f body_velocity = Dcmf{Quatf{attitude.q}} .transpose() * local_velocity; + + // device id + device::Device::DeviceId device_id; + device_id.devid_s.bus_type = device::Device::DeviceBusType::DeviceBusType_SIMULATION; + device_id.devid_s.bus = 0; + device_id.devid_s.address = 0; + device_id.devid_s.devtype = DRV_DIFF_PRESS_DEVTYPE_SIM; + + const float alt_amsl = gpos.alt; + const float temperature_local = TEMPERATURE_MSL - LAPSE_RATE * alt_amsl; + const float density_ratio = powf(TEMPERATURE_MSL / temperature_local, 4.256f); + const float air_density = AIR_DENSITY_MSL / density_ratio; + + // calculate differential pressure + noise in hPa + const float diff_pressure_noise = (float)generate_wgn() * 0.01f; + float diff_pressure = sign(body_velocity(0)) * 0.005f * air_density * body_velocity(0) * body_velocity( + 0) + diff_pressure_noise; + + + differential_pressure_s differential_pressure{}; + // report.timestamp_sample = time; + differential_pressure.device_id = 1377548; // 1377548: DRV_DIFF_PRESS_DEVTYPE_SIM, BUS: 1, ADDR: 5, TYPE: SIMULATION + differential_pressure.differential_pressure_pa = (double)diff_pressure * 100.0; // hPa to Pa; + differential_pressure.temperature = temperature_local; + differential_pressure.timestamp = hrt_absolute_time(); + _differential_pressure_pub.publish(differential_pressure); + + } + + perf_end(_loop_perf); +} + +int SensorAirspeedSim::task_spawn(int argc, char *argv[]) +{ + SensorAirspeedSim *instance = new SensorAirspeedSim(); + + if (instance) { + _object.store(instance); + _task_id = task_id_is_work_queue; + + if (instance->init()) { + return PX4_OK; + } + + } else { + PX4_ERR("alloc failed"); + } + + delete instance; + _object.store(nullptr); + _task_id = -1; + + return PX4_ERROR; +} + +int SensorAirspeedSim::custom_command(int argc, char *argv[]) +{ + return print_usage("unknown command"); +} + +int SensorAirspeedSim::print_usage(const char *reason) +{ + if (reason) { + PX4_WARN("%s\n", reason); + } + + PRINT_MODULE_DESCRIPTION( + R"DESCR_STR( +### Description + + +)DESCR_STR"); + + PRINT_MODULE_USAGE_NAME("sensor_arispeed_sim", "system"); + PRINT_MODULE_USAGE_COMMAND("start"); + PRINT_MODULE_USAGE_DEFAULT_COMMANDS(); + + return 0; +} + +extern "C" __EXPORT int sensor_airspeed_sim_main(int argc, char *argv[]) +{ + return SensorAirspeedSim::main(argc, argv); +} diff --git a/src/modules/simulation/sensor_airspeed_sim/SensorAirspeedSim.hpp b/src/modules/simulation/sensor_airspeed_sim/SensorAirspeedSim.hpp new file mode 100644 index 0000000000..24abd6c77b --- /dev/null +++ b/src/modules/simulation/sensor_airspeed_sim/SensorAirspeedSim.hpp @@ -0,0 +1,95 @@ +/**************************************************************************** + * + * Copyright (c) 2021 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace time_literals; + +static constexpr float TEMPERATURE_MSL = 288.15; // temperature at MSL [K] (15 [C]) +static constexpr float PRESSURE_MSL = 101325.0; // pressure at MSL [Pa] +static constexpr float LAPSE_RATE = 0.0065; // reduction in temperature with altitude for troposphere [K/m] +static constexpr float AIR_DENSITY_MSL = 1.225; // air density at MSL [kg/m^3] + +class SensorAirspeedSim : public ModuleBase, public ModuleParams, public px4::ScheduledWorkItem +{ +public: + SensorAirspeedSim(); + ~SensorAirspeedSim() override; + + /** @see ModuleBase */ + static int task_spawn(int argc, char *argv[]); + + /** @see ModuleBase */ + static int custom_command(int argc, char *argv[]); + + /** @see ModuleBase */ + static int print_usage(const char *reason = nullptr); + + bool init(); + +private: + void Run() override; + + // generate white Gaussian noise sample with std=1 + static float generate_wgn(); + + // generate white Gaussian noise sample as a 3D vector with specified std + matrix::Vector3f noiseGauss3f(float stdx, float stdy, float stdz) { return matrix::Vector3f(generate_wgn() * stdx, generate_wgn() * stdy, generate_wgn() * stdz); } + + uORB::SubscriptionInterval _parameter_update_sub{ORB_ID(parameter_update), 1_s}; + uORB::Subscription _vehicle_attitude_sub{ORB_ID(vehicle_attitude)}; + uORB::Subscription _vehicle_global_position_sub{ORB_ID(vehicle_global_position_groundtruth)}; + uORB::Subscription _vehicle_local_position_sub{ORB_ID(vehicle_local_position_groundtruth)}; + + uORB::PublicationMulti _differential_pressure_pub{ORB_ID(differential_pressure)}; + + perf_counter_t _loop_perf{perf_alloc(PC_ELAPSED, MODULE_NAME": cycle")}; + + // DEFINE_PARAMETERS( + // (ParamInt) _sim_gps_used + // ) +}; diff --git a/src/modules/simulation/sensor_airspeed_sim/parameters.c b/src/modules/simulation/sensor_airspeed_sim/parameters.c new file mode 100644 index 0000000000..0b4d4d11cb --- /dev/null +++ b/src/modules/simulation/sensor_airspeed_sim/parameters.c @@ -0,0 +1,41 @@ +/**************************************************************************** + * + * Copyright (c) 2021 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/** + * simulated GPS number of satellites used + * + * @min 0 + * @max 50 + * @group Simulator + */ +// PARAM_DEFINE_INT32(SIM_GPS_USED, 10);