Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4c164e0
Clock subscription callback group spins in its own thread
anaelle-sw Feb 23, 2021
217c904
fix line length
anaelle-sw Feb 23, 2021
4130d78
fix code style divergence
anaelle-sw Feb 23, 2021
459aaae
fix code style divergence
anaelle-sw Feb 23, 2021
75e5888
Initialize only once clock_callbackgroup
anaelle-sw Feb 26, 2021
12837ad
Check if thread joinable before start a new thread
anaelle-sw Feb 26, 2021
faacd8f
Cancel executor and join thread in destroy_clock_sub
anaelle-sw Feb 26, 2021
a713a7e
Check if clock_thread_ is joinable before cancel executor and join
anaelle-sw Feb 26, 2021
60be5e0
Add use_clock_thread as an option
anaelle-sw Mar 11, 2021
7bcf587
Fix code style divergence
anaelle-sw Mar 11, 2021
b8e75fc
Fix code style divergence
anaelle-sw Mar 11, 2021
f4568a1
Fix code style divergence
anaelle-sw Mar 11, 2021
4965bb9
Add TimeSource tests
anaelle-sw Mar 12, 2021
88b9fea
update use_clock_thread value in function attachNode
anaelle-sw Mar 15, 2021
00d762c
join clock thread in function detachNode
anaelle-sw Mar 15, 2021
4441a1f
TimeSource tests: fixes + comments + more tested cases
anaelle-sw Mar 15, 2021
0dfd56c
clean destroy_clock_sub()
anaelle-sw Mar 16, 2021
6eefc01
flag to ensure clock_executor is cancelled
anaelle-sw Mar 16, 2021
cd15fa3
Always re-init clock_callback_group when creating a new clock sub
anaelle-sw Mar 17, 2021
a8f3c2f
spin_until_future_complete() to cleanly cancel executor
anaelle-sw Mar 17, 2021
698e5fb
fix tests warnings
anaelle-sw Mar 18, 2021
9afd726
fix test error: cancel clock executor
anaelle-sw Mar 19, 2021
e16e184
clean comments
anaelle-sw Mar 26, 2021
fdd2390
fix precision loss
anaelle-sw Mar 29, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class NodeTimeSource : public NodeTimeSourceInterface
rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging,
rclcpp::node_interfaces::NodeClockInterface::SharedPtr node_clock,
rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters,
const rclcpp::QoS & qos = rclcpp::RosoutQoS()
const rclcpp::QoS & qos = rclcpp::RosoutQoS(),
bool use_clock_thread = true
);

RCLCPP_PUBLIC
Expand Down
17 changes: 17 additions & 0 deletions rclcpp/include/rclcpp/node_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class NodeOptions
* - start_parameter_services = true
* - start_parameter_event_publisher = true
* - clock_qos = rclcpp::ClockQoS()
* - use_clock_thread = true
* - rosout_qos = rclcpp::RosoutQoS()
* - parameter_event_qos = rclcpp::ParameterEventQoS
* - with history setting and depth from rmw_qos_profile_parameter_events
Expand Down Expand Up @@ -258,6 +259,20 @@ class NodeOptions
NodeOptions &
clock_qos(const rclcpp::QoS & clock_qos);


/// Return the use_clock_thread flag.
RCLCPP_PUBLIC
bool
use_clock_thread() const;

/// Set the use_clock_thread flag, return this for parameter idiom.
/**
* If true, a dedicated thread will be used to subscribe to "/clock" topic.
*/
RCLCPP_PUBLIC
NodeOptions &
use_clock_thread(bool use_clock_thread);

/// Return a reference to the parameter_event_qos QoS.
RCLCPP_PUBLIC
const rclcpp::QoS &
Expand Down Expand Up @@ -384,6 +399,8 @@ class NodeOptions

rclcpp::QoS clock_qos_ = rclcpp::ClockQoS();

bool use_clock_thread_ {true};

rclcpp::QoS parameter_event_qos_ = rclcpp::ParameterEventsQoS(
rclcpp::QoSInitialization::from_rmw(rmw_qos_profile_parameter_events)
);
Expand Down
18 changes: 16 additions & 2 deletions rclcpp/include/rclcpp/time_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "rcl_interfaces/msg/parameter_event.hpp"

#include "rclcpp/node.hpp"
#include "rclcpp/executors.hpp"
#include "rclcpp/node_interfaces/node_parameters_interface.hpp"


Expand Down Expand Up @@ -57,7 +58,10 @@ class TimeSource
* \param qos QoS that will be used when creating a `/clock` subscription.
*/
RCLCPP_PUBLIC
explicit TimeSource(rclcpp::Node::SharedPtr node, const rclcpp::QoS & qos = rclcpp::ClockQoS());
explicit TimeSource(
rclcpp::Node::SharedPtr node,
const rclcpp::QoS & qos = rclcpp::ClockQoS(),
bool use_clock_thread = true);

/// Empty constructor
/**
Expand All @@ -66,7 +70,9 @@ class TimeSource
* \param qos QoS that will be used when creating a `/clock` subscription.
*/
RCLCPP_PUBLIC
explicit TimeSource(const rclcpp::QoS & qos = rclcpp::ClockQoS());
explicit TimeSource(
const rclcpp::QoS & qos = rclcpp::ClockQoS(),
bool use_clock_thread = true);

/// Attack node to the time source.
/**
Expand Down Expand Up @@ -118,6 +124,11 @@ class TimeSource
RCLCPP_PUBLIC
~TimeSource();

protected:
// Dedicated thread for clock subscription.
bool use_clock_thread_;
std::thread clock_executor_thread_;
Comment thread
wjwwood marked this conversation as resolved.
Resolved

private:
// Preserve the node reference
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_;
Expand All @@ -140,6 +151,9 @@ class TimeSource
using SubscriptionT = rclcpp::Subscription<MessageT, Alloc>;
std::shared_ptr<SubscriptionT> clock_subscription_{nullptr};
std::mutex clock_sub_lock_;
rclcpp::CallbackGroup::SharedPtr clock_callback_group_;
rclcpp::executors::SingleThreadedExecutor clock_executor_;
Comment thread
jacobperron marked this conversation as resolved.
Resolved
std::promise<void> cancel_clock_executor_promise_;

// The clock callback itself
void clock_cb(const rosgraph_msgs::msg::Clock::SharedPtr msg);
Expand Down
3 changes: 2 additions & 1 deletion rclcpp/src/rclcpp/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ Node::Node(
node_logging_,
node_clock_,
node_parameters_,
options.clock_qos()
options.clock_qos(),
options.use_clock_thread()
)),
node_waitables_(new rclcpp::node_interfaces::NodeWaitables(node_base_.get())),
node_options_(options),
Expand Down
5 changes: 3 additions & 2 deletions rclcpp/src/rclcpp/node_interfaces/node_time_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ NodeTimeSource::NodeTimeSource(
rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging,
rclcpp::node_interfaces::NodeClockInterface::SharedPtr node_clock,
rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters,
const rclcpp::QoS & qos)
const rclcpp::QoS & qos,
bool use_clock_thread)
: node_base_(node_base),
node_topics_(node_topics),
node_graph_(node_graph),
node_services_(node_services),
node_logging_(node_logging),
node_clock_(node_clock),
node_parameters_(node_parameters),
time_source_(qos)
time_source_(qos, use_clock_thread)
{
time_source_.attachNode(
node_base_,
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 @@ -78,6 +78,7 @@ NodeOptions::operator=(const NodeOptions & other)
this->start_parameter_services_ = other.start_parameter_services_;
this->start_parameter_event_publisher_ = other.start_parameter_event_publisher_;
this->clock_qos_ = other.clock_qos_;
this->use_clock_thread_ = other.use_clock_thread_;
this->parameter_event_qos_ = other.parameter_event_qos_;
this->rosout_qos_ = other.rosout_qos_;
this->parameter_event_publisher_options_ = other.parameter_event_publisher_options_;
Expand Down Expand Up @@ -272,6 +273,19 @@ NodeOptions::clock_qos(const rclcpp::QoS & clock_qos)
return *this;
}

bool
NodeOptions::use_clock_thread() const
{
return this->use_clock_thread_;
}

NodeOptions &
NodeOptions::use_clock_thread(bool use_clock_thread)
{
this->use_clock_thread_ = use_clock_thread;
return *this;
}

const rclcpp::QoS &
NodeOptions::parameter_event_qos() const
{
Expand Down
42 changes: 37 additions & 5 deletions rclcpp/src/rclcpp/time_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,29 @@
namespace rclcpp
{

TimeSource::TimeSource(std::shared_ptr<rclcpp::Node> node, const rclcpp::QoS & qos)
: logger_(rclcpp::get_logger("rclcpp")),
TimeSource::TimeSource(
std::shared_ptr<rclcpp::Node> node,
const rclcpp::QoS & qos,
bool use_clock_thread)
: use_clock_thread_(use_clock_thread),
logger_(rclcpp::get_logger("rclcpp")),
qos_(qos)
{
this->attachNode(node);
}

TimeSource::TimeSource(const rclcpp::QoS & qos)
: logger_(rclcpp::get_logger("rclcpp")),
TimeSource::TimeSource(
const rclcpp::QoS & qos,
bool use_clock_thread)
: use_clock_thread_(use_clock_thread),
logger_(rclcpp::get_logger("rclcpp")),
qos_(qos)
{
}

void TimeSource::attachNode(rclcpp::Node::SharedPtr node)
{
use_clock_thread_ = node->get_node_options().use_clock_thread();
attachNode(
node->get_node_base_interface(),
node->get_node_topics_interface(),
Expand Down Expand Up @@ -127,7 +135,7 @@ void TimeSource::attachNode(
void TimeSource::detachNode()
{
this->ros_time_active_ = false;
clock_subscription_.reset();
destroy_clock_sub();
parameter_subscription_.reset();
node_base_.reset();
node_topics_.reset();
Expand Down Expand Up @@ -242,6 +250,24 @@ void TimeSource::create_clock_sub()
rclcpp::QosPolicyKind::Reliability,
});

if (use_clock_thread_) {
clock_callback_group_ = node_base_->create_callback_group(
rclcpp::CallbackGroupType::MutuallyExclusive,
false
);
options.callback_group = clock_callback_group_;
if (!clock_executor_thread_.joinable()) {
clock_executor_thread_ = std::thread(
[this]() {
cancel_clock_executor_promise_ = std::promise<void>{};
auto future = cancel_clock_executor_promise_.get_future();
clock_executor_.add_callback_group(clock_callback_group_, node_base_);
clock_executor_.spin_until_future_complete(future);
Comment thread
wjwwood marked this conversation as resolved.
Resolved
}
);
}
}

clock_subscription_ = rclcpp::create_subscription<rosgraph_msgs::msg::Clock>(
node_parameters_,
node_topics_,
Expand All @@ -255,6 +281,12 @@ void TimeSource::create_clock_sub()
void TimeSource::destroy_clock_sub()
{
std::lock_guard<std::mutex> guard(clock_sub_lock_);
if (clock_executor_thread_.joinable()) {
cancel_clock_executor_promise_.set_value();
clock_executor_.cancel();
clock_executor_thread_.join();
clock_executor_.remove_callback_group(clock_callback_group_);
}
clock_subscription_.reset();
}

Expand Down
Loading