Skip to content

Commit 19ddcab

Browse files
committed
feat(tier4_autoware_utils): add published time debug class into utils
Signed-off-by: Berkay Karaman <brkay54@gmail.com>
1 parent b86a00d commit 19ddcab

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright 2020 Tier IV, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef TIER4_AUTOWARE_UTILS__ROS__PUBLISHED_TIME_PUBLISHER_HPP_
16+
#define TIER4_AUTOWARE_UTILS__ROS__PUBLISHED_TIME_PUBLISHER_HPP_
17+
18+
#include <rclcpp/rclcpp.hpp>
19+
20+
#include <autoware_optional_msgs/msg/published_time.hpp>
21+
22+
#include <functional>
23+
#include <map>
24+
#include <string>
25+
26+
namespace tier4_autoware_utils
27+
{
28+
using autoware_optional_msgs::msg::PublishedTime;
29+
30+
struct GidHash
31+
{
32+
size_t operator()(const rmw_gid_t & gid) const noexcept
33+
{
34+
// Hashing function that computes a hash value for the GID
35+
std::hash<std::string> hasher;
36+
return hasher(std::string(reinterpret_cast<const char *>(gid.data), RMW_GID_STORAGE_SIZE));
37+
}
38+
};
39+
40+
struct GidEqual
41+
{
42+
bool operator()(const rmw_gid_t & lhs, const rmw_gid_t & rhs) const noexcept
43+
{
44+
return std::memcmp(lhs.data, rhs.data, RMW_GID_STORAGE_SIZE) == 0;
45+
}
46+
};
47+
48+
class PublishedTimePublisher
49+
{
50+
public:
51+
static std::unique_ptr<PublishedTimePublisher> create(
52+
rclcpp::Node * node, const std::string & end_name = "/debug/published_time",
53+
const rclcpp::QoS & qos = rclcpp::QoS(1))
54+
{
55+
const bool use_published_time = node->declare_parameter<bool>("use_published_time", false);
56+
if (use_published_time) {
57+
return std::unique_ptr<PublishedTimePublisher>(
58+
new PublishedTimePublisher(node, end_name, qos));
59+
} else {
60+
return nullptr;
61+
}
62+
}
63+
64+
void publish(
65+
const rclcpp::PublisherBase::ConstSharedPtr & publisher, const rclcpp::Time & header_stamp)
66+
{
67+
const auto & gid = publisher->get_gid();
68+
const auto & topic_name = publisher->get_topic_name();
69+
70+
// if the publisher is not in the map, create a new publisher for published time
71+
if (publishers_.find(gid) == publishers_.end()) {
72+
publishers_[gid] = node_->create_publisher<PublishedTime>(
73+
static_cast<std::string>(topic_name) + end_name_, qos_);
74+
}
75+
76+
const auto & pub_published_time_ = publishers_[gid];
77+
78+
// Check if there are any subscribers, otherwise don't do anything
79+
if (pub_published_time_->get_subscription_count() > 0) {
80+
PublishedTime published_time;
81+
82+
published_time.header_stamp = header_stamp;
83+
published_time.published_stamp = rclcpp::Clock().now();
84+
85+
pub_published_time_->publish(published_time);
86+
}
87+
}
88+
89+
private:
90+
explicit PublishedTimePublisher(
91+
rclcpp::Node * node, const std::string & end_name, const rclcpp::QoS & qos)
92+
: node_(node), end_name_(end_name), qos_(qos)
93+
{
94+
}
95+
96+
rclcpp::Node * node_;
97+
std::string end_name_;
98+
rclcpp::QoS qos_;
99+
100+
// store them for each different publisher of the node
101+
std::unordered_map<rmw_gid_t, rclcpp::Publisher<PublishedTime>::SharedPtr, GidHash, GidEqual>
102+
publishers_;
103+
};
104+
} // namespace tier4_autoware_utils
105+
106+
#endif // TIER4_AUTOWARE_UTILS__ROS__PUBLISHED_TIME_PUBLISHER_HPP_

common/tier4_autoware_utils/package.xml

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
<depend>autoware_auto_perception_msgs</depend>
1616
<depend>autoware_auto_planning_msgs</depend>
1717
<depend>autoware_auto_vehicle_msgs</depend>
18+
<depend>autoware_common_msgs</depend>
19+
<depend>autoware_optional_msgs</depend>
1820
<depend>builtin_interfaces</depend>
1921
<depend>diagnostic_msgs</depend>
2022
<depend>geometry_msgs</depend>

0 commit comments

Comments
 (0)