diff --git a/boards/px4/sitl/default.px4board b/boards/px4/sitl/default.px4board index cfbaf4ddc9..e94add2456 100644 --- a/boards/px4/sitl/default.px4board +++ b/boards/px4/sitl/default.px4board @@ -40,6 +40,7 @@ CONFIG_MODULES_MC_AUTOTUNE_ATTITUDE_CONTROL=y CONFIG_MODULES_MC_HOVER_THRUST_ESTIMATOR=y CONFIG_MODULES_MC_POS_CONTROL=y CONFIG_MODULES_MC_RATE_CONTROL=y +CONFIG_MODULES_MODE_REGISTER=y CONFIG_MODULES_NAVIGATOR=y CONFIG_MODE_NAVIGATOR_VTOL_TAKEOFF=y CONFIG_MODULES_PAYLOAD_DELIVERER=y diff --git a/src/modules/mode_register/CMakeLists.txt b/src/modules/mode_register/CMakeLists.txt new file mode 100644 index 0000000000..b912b40561 --- /dev/null +++ b/src/modules/mode_register/CMakeLists.txt @@ -0,0 +1,40 @@ +############################################################################ +# +# Copyright (c) 2015 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__mode_register + MAIN mode_register + SRCS + ModeRegister.cpp + ModeRegister.hpp + DEPENDS + ) diff --git a/src/modules/mode_register/Kconfig b/src/modules/mode_register/Kconfig new file mode 100644 index 0000000000..bc96e5f2e5 --- /dev/null +++ b/src/modules/mode_register/Kconfig @@ -0,0 +1,5 @@ +menuconfig MODULES_MODE_REGISTER + bool "mode_register" + default n + ---help--- + Enable support for internal mode reigstration diff --git a/src/modules/mode_register/ModeRegister.cpp b/src/modules/mode_register/ModeRegister.cpp new file mode 100644 index 0000000000..48e02046aa --- /dev/null +++ b/src/modules/mode_register/ModeRegister.cpp @@ -0,0 +1,180 @@ +/**************************************************************************** + * + * 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 "ModeRegister.hpp" + +#include + +ModeRegister::ModeRegister() : + ModuleParams(nullptr), + WorkItem(MODULE_NAME, px4::wq_configurations::nav_and_controllers), + _loop_perf(perf_alloc(PC_ELAPSED, MODULE_NAME": cycle")) +{ + // PX4_INFO("Hello Mode Register"); + doRegister(); +} + +ModeRegister::~ModeRegister() +{ + perf_free(_loop_perf); +} + +bool +ModeRegister::init() +{ + if (!_register_ext_component_reply_sub.registerCallback()) { + PX4_ERR("callback registration failed"); + return false; + } + + return true; +} + +bool ModeRegister::doRegister() +{ +// assert(!_registration->registered()); + +// if (!_skip_message_compatibility_check && +// !messageCompatibilityCheck(node(), {ALL_PX4_ROS2_MESSAGES}, topicNamespacePrefix())) +// { +// return false; +// } + +// onAboutToRegister(); + register_ext_component_request_s request; + strcpy(request.name, "Internal Mode"); + request.register_arming_check = true; + request.register_mode = true; + request.register_mode_executor = true; + request.enable_replace_internal_mode = true; + request.replace_internal_mode = true; + request.activate_mode_immediately = false; + request.px4_ros2_api_version = 1; + request.request_id = 123; + _register_ext_component_request_pub.publish(request); + +// _health_and_arming_checks.overrideRegistration(_registration); +// const RegistrationSettings settings = getRegistrationSettings(); + bool ret = true; +// = _registration->doRegister(settings); + +// if (ret) { +// if (!onRegistered()) { +// ret = false; +// } +// } + + return ret; +} + +void +ModeRegister::Run() +{ + if (should_exit()) { + _register_ext_component_reply_sub.unregisterCallback(); + exit_and_cleanup(); + return; + } + + perf_begin(_loop_perf); + PX4_INFO("Fuuuuuuuuuck"); + + if (!_requested) { + PX4_INFO("Do Register"); + doRegister(); + _requested = true; + } + + register_ext_component_reply_s reply; + + if (_register_ext_component_reply_sub.update(&reply)) { + PX4_INFO("Replied!"); + PX4_INFO(" - success: %f", double(reply.success)); + } + + perf_end(_loop_perf); +} + +int ModeRegister::task_spawn(int argc, char *argv[]) +{ + + ModeRegister *instance = new ModeRegister(); + + 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 ModeRegister::custom_command(int argc, char *argv[]) +{ + return print_usage("unknown command"); +} + +int ModeRegister::print_usage(const char *reason) +{ + if (reason) { + PX4_WARN("%s\n", reason); + } + + PRINT_MODULE_DESCRIPTION( + R"DESCR_STR( +### Description +This implements an internal mode registration. + +)DESCR_STR"); + + PRINT_MODULE_USAGE_NAME("mode_register", "controller"); + PRINT_MODULE_USAGE_COMMAND("start"); + PRINT_MODULE_USAGE_DEFAULT_COMMANDS(); + + return 0; +} + +extern "C" __EXPORT int mode_register_main(int argc, char *argv[]) +{ + return ModeRegister::main(argc, argv); +} diff --git a/src/modules/mode_register/ModeRegister.hpp b/src/modules/mode_register/ModeRegister.hpp new file mode 100644 index 0000000000..541e3a23fd --- /dev/null +++ b/src/modules/mode_register/ModeRegister.hpp @@ -0,0 +1,47 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class ModeRegister : public ModuleBase, public ModuleParams, public px4::WorkItem +{ +public: + ModeRegister(); + ~ModeRegister() 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 doRegister(); + + bool init(); + +private: + void Run() override; + + uORB::SubscriptionCallbackWorkItem _register_ext_component_reply_sub{this, ORB_ID(register_ext_component_reply)}; + + uORB::Publication _register_ext_component_request_pub{ORB_ID(register_ext_component_request)}; + uORB::Publication _unregister_ext_component_pub{ORB_ID(unregister_ext_component)}; + + bool _requested{false}; + perf_counter_t _loop_perf; /**< loop duration performance counter */ + +};