diff --git a/src/drivers/uavcan/CMakeLists.txt b/src/drivers/uavcan/CMakeLists.txt index c70d029ea7..f8bc1eea8a 100644 --- a/src/drivers/uavcan/CMakeLists.txt +++ b/src/drivers/uavcan/CMakeLists.txt @@ -147,6 +147,8 @@ px4_add_module( arming_status.hpp beep.cpp beep.hpp + remoteid.cpp + remoteid.hpp rgbled.cpp rgbled.hpp safety_state.cpp diff --git a/src/drivers/uavcan/Kconfig b/src/drivers/uavcan/Kconfig index 0076d36568..37306dabdf 100644 --- a/src/drivers/uavcan/Kconfig +++ b/src/drivers/uavcan/Kconfig @@ -26,6 +26,10 @@ if DRIVERS_UAVCAN bool "Include safety state controller" default y + config UAVCAN_REMOTEID_CONTROLLER + bool "Include remote ID controller" + default y + config UAVCAN_RGB_CONTROLLER bool "Include rgb controller" default y diff --git a/src/drivers/uavcan/remoteid.cpp b/src/drivers/uavcan/remoteid.cpp new file mode 100644 index 0000000000..7c8bc7f822 --- /dev/null +++ b/src/drivers/uavcan/remoteid.cpp @@ -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. + * + ****************************************************************************/ + +#include "remoteid.hpp" +#include + +UavcanRemoteIDController::UavcanRemoteIDController(uavcan::INode &node) : + ModuleParams(nullptr), + _timer(node), + _node(node), + _uavcan_pub_remoteid_basicid(node) +{ +} + +int UavcanRemoteIDController::init() +{ + // Setup timer and call back function for periodic updates + _timer.setCallback(TimerCbBinder(this, &UavcanRemoteIDController::periodic_update)); + _timer.startPeriodic(uavcan::MonotonicDuration::fromMSec(1000 / MAX_RATE_HZ)); + return 0; +} + +void UavcanRemoteIDController::periodic_update(const uavcan::TimerEvent &) +{ + if (!_vehicle_status_sub.update()) { + return; + } + + dronecan::remoteid::BasicID basic_id {}; + // basic_id.id_or_mac // supposedly only used for drone ID data from other UAs + basic_id.id_type = dronecan::remoteid::BasicID::ODID_ID_TYPE_SERIAL_NUMBER; + basic_id.ua_type = static_cast(open_drone_id_translations::odidTypeForMavType( + _vehicle_status_sub.get().system_type)); + + // uas_id: UAS (Unmanned Aircraft System) ID following the format specified by id_type + // TODO: MAV_ODID_ID_TYPE_SERIAL_NUMBER needs to be ANSI/CTA-2063 format + + char uas_id[20] = {}; + board_get_px4_guid_formated((char *)(uas_id), sizeof(uas_id)); + basic_id.uas_id = uas_id; + + _uavcan_pub_remoteid_basicid.broadcast(basic_id); +} diff --git a/src/drivers/uavcan/remoteid.hpp b/src/drivers/uavcan/remoteid.hpp new file mode 100644 index 0000000000..839a2f8cf2 --- /dev/null +++ b/src/drivers/uavcan/remoteid.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 + +class UavcanRemoteIDController : public ModuleParams +{ +public: + UavcanRemoteIDController(uavcan::INode &node); + ~UavcanRemoteIDController() = default; + + int init(); + +private: + typedef uavcan::MethodBinder + TimerCbBinder; + + static constexpr unsigned MAX_RATE_HZ = 1; + uavcan::TimerEventForwarder _timer; + + void periodic_update(const uavcan::TimerEvent &); + + uavcan::INode &_node; + + uORB::SubscriptionData _vehicle_status_sub{ORB_ID(vehicle_status)}; + + uavcan::Publisher _uavcan_pub_remoteid_basicid; + + //DEFINE_PARAMETERS( + // (ParamInt) _param_mode_anti_col, + // (ParamInt) _param_mode_strobe, + // (ParamInt) _param_mode_nav, + // (ParamInt) _param_mode_land + //) +}; diff --git a/src/drivers/uavcan/rgbled.hpp b/src/drivers/uavcan/rgbled.hpp index b1190214b1..be0bc876d3 100644 --- a/src/drivers/uavcan/rgbled.hpp +++ b/src/drivers/uavcan/rgbled.hpp @@ -51,7 +51,7 @@ public: int init(); private: - // Max update rate to avoid exessive bus traffic + // Max update rate to avoid excessive bus traffic static constexpr unsigned MAX_RATE_HZ = 20; void periodic_update(const uavcan::TimerEvent &); diff --git a/src/drivers/uavcan/uavcan_main.cpp b/src/drivers/uavcan/uavcan_main.cpp index fbdc534a9c..87f3110cac 100644 --- a/src/drivers/uavcan/uavcan_main.cpp +++ b/src/drivers/uavcan/uavcan_main.cpp @@ -96,6 +96,9 @@ UavcanNode::UavcanNode(uavcan::ICanDriver &can_driver, uavcan::ISystemClock &sys #if defined(CONFIG_UAVCAN_SAFETY_STATE_CONTROLLER) _safety_state_controller(_node), #endif +#if defined(CONFIG_UAVCAN_REMOTEID_CONTROLLER) + _remoteid_controller(_node), +#endif #if defined(CONFIG_UAVCAN_RGB_CONTROLLER) _rgbled_controller(_node), #endif @@ -558,6 +561,16 @@ UavcanNode::init(uavcan::NodeID node_id, UAVCAN_DRIVER::BusEvent &bus_events) return ret; } +#if defined(CONFIG_UAVCAN_REMOTEID_CONTROLLER) + ret = _remoteid_controller.init(); + + if (ret < 0) { + return ret; + } + +#endif + + #if defined(CONFIG_UAVCAN_RGB_CONTROLLER) ret = _rgbled_controller.init(); diff --git a/src/drivers/uavcan/uavcan_main.hpp b/src/drivers/uavcan/uavcan_main.hpp index 5afa04ef67..d249e5d838 100644 --- a/src/drivers/uavcan/uavcan_main.hpp +++ b/src/drivers/uavcan/uavcan_main.hpp @@ -68,6 +68,10 @@ #include "logmessage.hpp" +#if defined(CONFIG_UAVCAN_REMOTEID_CONTROLLER) +#include "remoteid.hpp" +#endif + #if defined(CONFIG_UAVCAN_RGB_CONTROLLER) #include "rgbled.hpp" #endif @@ -269,6 +273,9 @@ private: #if defined(CONFIG_UAVCAN_SAFETY_STATE_CONTROLLER) UavcanSafetyState _safety_state_controller; #endif +#if defined(CONFIG_UAVCAN_REMOTEID_CONTROLLER) + UavcanRemoteIDController _remoteid_controller; +#endif #if defined(CONFIG_UAVCAN_RGB_CONTROLLER) UavcanRGBController _rgbled_controller; #endif