Skip to content

Commit

Permalink
uavcan: implement OpenDroneID BasicID
Browse files Browse the repository at this point in the history
Signed-off-by: Julian Oes <[email protected]>
  • Loading branch information
julianoes committed Jul 10, 2024
1 parent b0b67d4 commit 6eab0a3
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/drivers/uavcan/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/drivers/uavcan/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 73 additions & 0 deletions src/drivers/uavcan/remoteid.cpp
Original file line number Diff line number Diff line change
@@ -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 <lib/open_drone_id/open_drone_id_translations.hpp>

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<uint8_t>(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);
}
73 changes: 73 additions & 0 deletions src/drivers/uavcan/remoteid.hpp
Original file line number Diff line number Diff line change
@@ -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 <uORB/topics/vehicle_status.h>
#include <uORB/Subscription.hpp>

#include <uavcan/uavcan.hpp>
#include <dronecan/remoteid/BasicID.hpp>

#include <px4_platform_common/module_params.h>

class UavcanRemoteIDController : public ModuleParams
{
public:
UavcanRemoteIDController(uavcan::INode &node);
~UavcanRemoteIDController() = default;

int init();

private:
typedef uavcan::MethodBinder<UavcanRemoteIDController *, void (UavcanRemoteIDController::*)(const uavcan::TimerEvent &)>
TimerCbBinder;

static constexpr unsigned MAX_RATE_HZ = 1;
uavcan::TimerEventForwarder<TimerCbBinder> _timer;

void periodic_update(const uavcan::TimerEvent &);

uavcan::INode &_node;

uORB::SubscriptionData<vehicle_status_s> _vehicle_status_sub{ORB_ID(vehicle_status)};

uavcan::Publisher<dronecan::remoteid::BasicID> _uavcan_pub_remoteid_basicid;

//DEFINE_PARAMETERS(
// (ParamInt<px4::params::UAVCAN_LGT_ANTCL>) _param_mode_anti_col,
// (ParamInt<px4::params::UAVCAN_LGT_STROB>) _param_mode_strobe,
// (ParamInt<px4::params::UAVCAN_LGT_NAV>) _param_mode_nav,
// (ParamInt<px4::params::UAVCAN_LGT_LAND>) _param_mode_land
//)
};
2 changes: 1 addition & 1 deletion src/drivers/uavcan/rgbled.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class UavcanRGBController : public ModuleParams
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 &);
Expand Down
13 changes: 13 additions & 0 deletions src/drivers/uavcan/uavcan_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down
7 changes: 7 additions & 0 deletions src/drivers/uavcan/uavcan_main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -269,6 +273,9 @@ class UavcanNode : public px4::ScheduledWorkItem, public ModuleParams
#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
Expand Down

0 comments on commit 6eab0a3

Please sign in to comment.