diff --git a/ROMFS/px4fmu_common/init.d-posix/rcS b/ROMFS/px4fmu_common/init.d-posix/rcS index e47b685a21..ded652a183 100644 --- a/ROMFS/px4fmu_common/init.d-posix/rcS +++ b/ROMFS/px4fmu_common/init.d-posix/rcS @@ -247,6 +247,8 @@ then battery_simulator start fi +system_power_simulator start + tone_alarm start rc_update start manual_control start diff --git a/src/modules/simulation/Kconfig b/src/modules/simulation/Kconfig index a417092ed9..576834846b 100644 --- a/src/modules/simulation/Kconfig +++ b/src/modules/simulation/Kconfig @@ -9,6 +9,7 @@ menu "Simulation" select MODULES_SIMULATION_SENSOR_BARO_SIM select MODULES_SIMULATION_SENSOR_GPS_SIM select MODULES_SIMULATION_SENSOR_MAG_SIM + select MODULES_SIMULATION_SYSTEM_POWER_SIMULATOR select MODULES_SIMULATION_SIMULATOR_MAVLINK select MODULES_SIMULATION_SIMULATOR_SIH ---help--- diff --git a/src/modules/simulation/system_power_simulator/CMakeLists.txt b/src/modules/simulation/system_power_simulator/CMakeLists.txt new file mode 100644 index 0000000000..1168e557cd --- /dev/null +++ b/src/modules/simulation/system_power_simulator/CMakeLists.txt @@ -0,0 +1,43 @@ +############################################################################ +# +# Copyright (c) 2024 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__system_power_simulator + MAIN system_power_simulator + COMPILE_FLAGS + SRCS + SystemPowerSimulator.cpp + SystemPowerSimulator.hpp + DEPENDS + px4_work_queue + ) diff --git a/src/modules/simulation/system_power_simulator/Kconfig b/src/modules/simulation/system_power_simulator/Kconfig new file mode 100644 index 0000000000..498e8a989c --- /dev/null +++ b/src/modules/simulation/system_power_simulator/Kconfig @@ -0,0 +1,5 @@ +menuconfig MODULES_SIMULATION_SYSTEM_POWER_SIMULATOR + bool "system_power_simulator" + default n + ---help--- + Enable support for system_power_simulator diff --git a/src/modules/simulation/system_power_simulator/SystemPowerSimulator.cpp b/src/modules/simulation/system_power_simulator/SystemPowerSimulator.cpp new file mode 100644 index 0000000000..2c7671c2d2 --- /dev/null +++ b/src/modules/simulation/system_power_simulator/SystemPowerSimulator.cpp @@ -0,0 +1,131 @@ +/**************************************************************************** + * + * Copyright (c) 2024 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 "SystemPowerSimulator.hpp" + +SystemPowerSimulator::SystemPowerSimulator() : + ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::hp_default) +{ +} + +SystemPowerSimulator::~SystemPowerSimulator() +{ + perf_free(_loop_perf); +} + +bool SystemPowerSimulator::init() +{ + ScheduleOnInterval(SYSTEM_POWER_SIMLATOR_SAMPLE_INTERVAL_US); + return true; +} + +void SystemPowerSimulator::Run() +{ + if (should_exit()) { + ScheduleClear(); + exit_and_cleanup(); + return; + } + + perf_begin(_loop_perf); + + const hrt_abstime now_us = hrt_absolute_time(); + + // system power + system_power_s sim_system_power{}; + + sim_system_power.timestamp = now_us; + sim_system_power.voltage5v_v = 5.f; + sim_system_power.usb_connected = false; + sim_system_power.hipower_5v_oc = false; + sim_system_power.periph_5v_oc = false; + sim_system_power.brick_valid = 1; + + _system_power_pub.publish(sim_system_power); + + perf_end(_loop_perf); +} + +int SystemPowerSimulator::task_spawn(int argc, char *argv[]) +{ + SystemPowerSimulator *instance = new SystemPowerSimulator(); + + 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 SystemPowerSimulator::custom_command(int argc, char *argv[]) +{ + return print_usage("unknown command"); +} + + +int SystemPowerSimulator::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("system_power_simulation", "system"); + PRINT_MODULE_USAGE_COMMAND("start"); + PRINT_MODULE_USAGE_DEFAULT_COMMANDS(); + + return 0; +} + +extern "C" __EXPORT int system_power_simulator_main(int argc, char *argv[]) +{ + return SystemPowerSimulator::main(argc, argv); +} diff --git a/src/modules/simulation/system_power_simulator/SystemPowerSimulator.hpp b/src/modules/simulation/system_power_simulator/SystemPowerSimulator.hpp new file mode 100644 index 0000000000..476eb0afcb --- /dev/null +++ b/src/modules/simulation/system_power_simulator/SystemPowerSimulator.hpp @@ -0,0 +1,73 @@ +/**************************************************************************** + * + * Copyright (c) 2024 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 + +using namespace time_literals; + +class SystemPowerSimulator : public ModuleBase, public px4::ScheduledWorkItem +{ +public: + SystemPowerSimulator(); + ~SystemPowerSimulator() 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; + + static constexpr uint32_t SYSTEM_POWER_SIMLATOR_SAMPLE_FREQUENCY_HZ = 100; // Hz + static constexpr uint32_t SYSTEM_POWER_SIMLATOR_SAMPLE_INTERVAL_US = 1_s / SYSTEM_POWER_SIMLATOR_SAMPLE_FREQUENCY_HZ; + + uORB::Publication _system_power_pub{ORB_ID(system_power)}; + + perf_counter_t _loop_perf{perf_alloc(PC_ELAPSED, MODULE_NAME": cycle")}; +};