@@ -612,7 +612,6 @@ ReactionAnalyzerNode::ReactionAnalyzerNode(rclcpp::NodeOptions options)
612
612
}
613
613
}
614
614
}
615
- std::cout << " 2" << std::endl;
616
615
617
616
// set frequencies of the publishers
618
617
// After collecting all timestamps for each topic
@@ -953,15 +952,41 @@ ReactionAnalyzerNode::ReactionAnalyzerNode(rclcpp::NodeOptions options)
953
952
},
954
953
variant);
955
954
956
- // Ensure conjugate_pointclouds or the relevant struct is accessible and appropriately scoped
955
+ // Conditionally create the timer based on the message type and publish_pointcloud_sync_
957
956
std::visit (
958
957
[&](auto & var) {
959
- var.timer = rclcpp::create_timer (this , get_clock (), period_ns, [this , topic_name]() {
960
- this ->genericMessagePublisherTimer (topic_name);
961
- });
958
+ using MessageType = typename decltype (var.empty_area_message )::element_type;
959
+
960
+ if constexpr (std::is_same_v<MessageType, sensor_msgs::msg::PointCloud2>) {
961
+ if (!publish_pointcloud_sync_) {
962
+ // do not create timer if the pointcloud messages will be published synchronously
963
+ var.timer = rclcpp::create_timer (this , get_clock (), period_ns, [this , topic_name]() {
964
+ this ->genericMessagePublisherTimer (topic_name);
965
+ });
966
+ } else {
967
+ // It is Pointcloud2 message and it will be published synchronously
968
+ sync_pointcloud_publishers_.push_back (var);
969
+ }
970
+ } else {
971
+ var.timer = rclcpp::create_timer (this , get_clock (), period_ns, [this , topic_name]() {
972
+ this ->genericMessagePublisherTimer (topic_name);
973
+ });
974
+ }
962
975
},
963
976
variant);
964
977
}
978
+
979
+ if (publish_pointcloud_sync_) {
980
+ if (sync_pointcloud_publishers_.empty ()) {
981
+ RCLCPP_ERROR (get_logger (), " No pointcloud publishers found for synchronous publishing" );
982
+ return ;
983
+ }
984
+ const auto period_ns =
985
+ std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::duration<double >(0.1 ));
986
+ sync_pointcloud_publish_timer_ = rclcpp::create_timer (
987
+ this , get_clock (), period_ns,
988
+ std::bind (&ReactionAnalyzerNode::publishAllPointClouds, this ));
989
+ }
965
990
}
966
991
967
992
const auto period_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
@@ -970,6 +995,26 @@ ReactionAnalyzerNode::ReactionAnalyzerNode(rclcpp::NodeOptions options)
970
995
this , get_clock (), period_ns, std::bind (&ReactionAnalyzerNode::onTimer, this ));
971
996
}
972
997
998
+ void ReactionAnalyzerNode::publishAllPointClouds ()
999
+ {
1000
+ mutex_.lock ();
1001
+ const auto is_spawned = spawn_cmd_time_;
1002
+ mutex_.unlock ();
1003
+ const auto current_time = this ->now ();
1004
+
1005
+ for (auto & publisher_var : sync_pointcloud_publishers_) {
1006
+ std::unique_ptr<sensor_msgs::msg::PointCloud2> msg_cloud_pub =
1007
+ std::make_unique<sensor_msgs::msg::PointCloud2>();
1008
+ if (!is_spawned) {
1009
+ *msg_cloud_pub = *publisher_var.empty_area_message ;
1010
+ } else {
1011
+ *msg_cloud_pub = *publisher_var.object_spawned_message ;
1012
+ }
1013
+ msg_cloud_pub->header .stamp = current_time;
1014
+ publisher_var.publisher ->publish (std::move (msg_cloud_pub));
1015
+ }
1016
+ }
1017
+
973
1018
void ReactionAnalyzerNode::genericMessagePublisherTimer (const std::string & topic_name)
974
1019
{
975
1020
PublisherVarAccessor accessor;
0 commit comments