Skip to content

Commit fd6766a

Browse files
committed
add sync pointcloud publish
Signed-off-by: Berkay Karaman <brkay54@gmail.com>
1 parent 9d55420 commit fd6766a

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

tools/reaction_analyzer/include/reaction_analyzer_node.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ class ReactionAnalyzerNode : public rclcpp::Node
294294
// TEMP
295295
void genericMessagePublisherTimer(const std::string & topic_name);
296296
std::unordered_map<std::string, PublisherVariablesVariant> topic_publisher_map_;
297+
std::vector<PublisherVariables<PointCloud2>> sync_pointcloud_publishers_;
298+
bool publish_pointcloud_sync_{true};
299+
void publishAllPointClouds();
300+
rclcpp::TimerBase::SharedPtr sync_pointcloud_publish_timer_;
297301

298302
// Initialization Variables
299303
geometry_msgs::msg::Pose entity_pose_;

tools/reaction_analyzer/src/reaction_analyzer_node.cpp

+50-5
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,6 @@ ReactionAnalyzerNode::ReactionAnalyzerNode(rclcpp::NodeOptions options)
612612
}
613613
}
614614
}
615-
std::cout << "2" << std::endl;
616615

617616
// set frequencies of the publishers
618617
// After collecting all timestamps for each topic
@@ -953,15 +952,41 @@ ReactionAnalyzerNode::ReactionAnalyzerNode(rclcpp::NodeOptions options)
953952
},
954953
variant);
955954

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_
957956
std::visit(
958957
[&](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+
}
962975
},
963976
variant);
964977
}
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+
}
965990
}
966991

967992
const auto period_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
@@ -970,6 +995,26 @@ ReactionAnalyzerNode::ReactionAnalyzerNode(rclcpp::NodeOptions options)
970995
this, get_clock(), period_ns, std::bind(&ReactionAnalyzerNode::onTimer, this));
971996
}
972997

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+
9731018
void ReactionAnalyzerNode::genericMessagePublisherTimer(const std::string & topic_name)
9741019
{
9751020
PublisherVarAccessor accessor;

0 commit comments

Comments
 (0)