Skip to content

Commit

Permalink
Allow to pass existing TimeSource via NodeOptions
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Roncagliolo <[email protected]>
  • Loading branch information
roncapat committed Nov 14, 2024
1 parent 88ebea9 commit 09cfbf2
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 2 deletions.
3 changes: 3 additions & 0 deletions rclcpp/include/rclcpp/node_interfaces/node_time_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class NodeTimeSource : public NodeTimeSourceInterface
bool use_clock_thread = true
);

RCLCPP_PUBLIC
void attachClock(rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock);

RCLCPP_PUBLIC
virtual
~NodeTimeSource();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "rclcpp/macros.hpp"
#include "rclcpp/node_interfaces/detail/node_interfaces_helpers.hpp"
#include "rclcpp/node_interfaces/node_clock_interface.hpp"
#include "rclcpp/visibility_control.hpp"

namespace rclcpp
Expand All @@ -30,6 +31,10 @@ class NodeTimeSourceInterface
public:
RCLCPP_SMART_PTR_ALIASES_ONLY(NodeTimeSourceInterface)

RCLCPP_PUBLIC
virtual
void attachClock(rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock) = 0;

RCLCPP_PUBLIC
virtual
~NodeTimeSourceInterface() = default;
Expand Down
13 changes: 13 additions & 0 deletions rclcpp/include/rclcpp/node_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "rclcpp/publisher_options.hpp"
#include "rclcpp/qos.hpp"
#include "rclcpp/visibility_control.hpp"
#include "rclcpp/node_interfaces/node_time_source_interface.hpp"

namespace rclcpp
{
Expand Down Expand Up @@ -408,6 +409,16 @@ class NodeOptions
NodeOptions &
allocator(rcl_allocator_t allocator);

/// Return the time source to be used.
RCLCPP_PUBLIC
const rclcpp::node_interfaces::NodeTimeSourceInterface::SharedPtr
time_source() const;

/// Set the time source to be used.
RCLCPP_PUBLIC
NodeOptions &
time_source(rclcpp::node_interfaces::NodeTimeSourceInterface::SharedPtr time_source);

private:
// This is mutable to allow for a const accessor which lazily creates the node options instance.
/// Underlying rcl_node_options structure.
Expand Down Expand Up @@ -456,6 +467,8 @@ class NodeOptions
bool automatically_declare_parameters_from_overrides_ {false};

rcl_allocator_t allocator_ {rcl_get_default_allocator()};

rclcpp::node_interfaces::NodeTimeSourceInterface::SharedPtr time_source_ {nullptr};
};

} // namespace rclcpp
Expand Down
5 changes: 4 additions & 1 deletion rclcpp/src/rclcpp/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ Node::Node(
options.allow_undeclared_parameters(),
options.automatically_declare_parameters_from_overrides()
)),
node_time_source_(new rclcpp::node_interfaces::NodeTimeSource(
node_time_source_(
options.time_source() ? options.time_source() :
std::make_shared<rclcpp::node_interfaces::NodeTimeSource>(
node_base_,
node_topics_,
node_graph_,
Expand All @@ -235,6 +237,7 @@ Node::Node(
sub_namespace_(""),
effective_namespace_(create_effective_namespace(this->get_namespace(), sub_namespace_))
{
node_time_source_->attachClock(node_clock_);
// we have got what we wanted directly from the overrides,
// but declare the parameters anyway so they are visible.
rclcpp::detail::declare_qos_parameters(
Expand Down
6 changes: 5 additions & 1 deletion rclcpp/src/rclcpp/node_interfaces/node_time_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ NodeTimeSource::NodeTimeSource(
node_logging_,
node_clock_,
node_parameters_);
time_source_.attachClock(node_clock_->get_clock());
}

void NodeTimeSource::attachClock(rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock)
{
time_source_.attachClock(clock->get_clock());
}

NodeTimeSource::~NodeTimeSource()
Expand Down
14 changes: 14 additions & 0 deletions rclcpp/src/rclcpp/node_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ NodeOptions::operator=(const NodeOptions & other)
this->automatically_declare_parameters_from_overrides_ =
other.automatically_declare_parameters_from_overrides_;
this->allocator_ = other.allocator_;
this->time_source_ = other.time_source_;
}
return *this;
}
Expand Down Expand Up @@ -397,4 +398,17 @@ NodeOptions::allocator(rcl_allocator_t allocator)
return *this;
}

const rclcpp::node_interfaces::NodeTimeSourceInterface::SharedPtr
NodeOptions::time_source() const
{
return this->time_source_;
}

NodeOptions &
NodeOptions::time_source(rclcpp::node_interfaces::NodeTimeSourceInterface::SharedPtr time_source)
{
this->time_source_ = time_source;
return *this;
}

} // namespace rclcpp

0 comments on commit 09cfbf2

Please sign in to comment.