Carma-platform v4.2.0
CARMA Platform is built on robot operating system (ROS) and utilizes open source software (OSS) that enables Cooperative Driving Automation (CDA) features to allow Automated Driving Systems to interact and cooperate with infrastructure and other vehicles through communication.
sdsm_to_detection_list_component.hpp
Go to the documentation of this file.
1// Copyright 2023 Leidos
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 CARMA_COOPERATIVE_PERCEPTION__SDSM_TO_DETECTION_LIST_COMPONENT_HPP_
16#define CARMA_COOPERATIVE_PERCEPTION__SDSM_TO_DETECTION_LIST_COMPONENT_HPP_
17
18#include <string>
19
20#include <carma_cooperative_perception_interfaces/msg/detection_list.hpp>
21#include <carma_ros2_utils/carma_lifecycle_node.hpp>
22#include <carma_v2x_msgs/msg/sensor_data_sharing_message.hpp>
23#include <rclcpp/rclcpp.hpp>
24#include <rosgraph_msgs/msg/clock.hpp>
25#include <std_msgs/msg/string.hpp>
26
28
30{
31class SdsmToDetectionListNode : public carma_ros2_utils::CarmaLifecycleNode
32{
33 using input_msg_type = carma_v2x_msgs::msg::SensorDataSharingMessage;
34 using input_msg_shared_pointer = typename input_msg_type::SharedPtr;
35 using output_msg_type = carma_cooperative_perception_interfaces::msg::DetectionList;
36
37public:
38 explicit SdsmToDetectionListNode(const rclcpp::NodeOptions & options)
39 : CarmaLifecycleNode{options},
40 publisher_{create_publisher<output_msg_type>("output/detections", 1)},
41 subscription_{create_subscription<input_msg_type>(
42 "input/sdsm", 1, [this](input_msg_shared_pointer msg_ptr) { sdsm_msg_callback(*msg_ptr); })},
43 georeference_subscription_{create_subscription<std_msgs::msg::String>(
44 "input/georeference", 1,
45 [this](std_msgs::msg::String::SharedPtr msg_ptr) { georeference_ = msg_ptr->data; })},
46 cdasim_clock_sub_{create_subscription<rosgraph_msgs::msg::Clock>(
47 "input/cdasim_clock", 1, [this](rosgraph_msgs::msg::Clock::ConstSharedPtr msg_ptr) {
48 cdasim_time_ = msg_ptr->clock;
49 })}
50 {
51 }
52
53 auto sdsm_msg_callback(const input_msg_type & msg) const -> void
54 {
55 try {
56 auto detection_list_msg{to_detection_list_msg(msg, georeference_)};
57
58 if (cdasim_time_) {
59 // When in simulation, ROS time is CARLA time, but SDSMs use CDASim time
60 const auto time_delta{now() - cdasim_time_.value()};
61
62 for (auto & detection : detection_list_msg.detections) {
63 detection.header.stamp = rclcpp::Time(detection.header.stamp) + time_delta;
64 }
65 }
66
67 publisher_->publish(detection_list_msg);
68 } catch (const std::runtime_error & e) {
69 RCLCPP_ERROR_STREAM(get_logger(), "Failed to convert SDSM to detection list: " << e.what());
70 }
71 }
72
73private:
74 rclcpp::Publisher<output_msg_type>::SharedPtr publisher_;
75 rclcpp::Subscription<input_msg_type>::SharedPtr subscription_;
76 rclcpp::Subscription<std_msgs::msg::String>::SharedPtr georeference_subscription_;
77
78 rclcpp::Subscription<rosgraph_msgs::msg::Clock>::SharedPtr cdasim_clock_sub_;
79 std::optional<rclcpp::Time> cdasim_time_{std::nullopt};
80
81 std::string georeference_{""};
82};
83
84} // namespace carma_cooperative_perception
85
86#endif // CARMA_COOPERATIVE_PERCEPTION__SDSM_TO_DETECTION_LIST_COMPONENT_HPP_
rclcpp::Subscription< input_msg_type >::SharedPtr subscription_
carma_cooperative_perception_interfaces::msg::DetectionList output_msg_type
carma_v2x_msgs::msg::SensorDataSharingMessage input_msg_type
rclcpp::Subscription< std_msgs::msg::String >::SharedPtr georeference_subscription_
rclcpp::Publisher< output_msg_type >::SharedPtr publisher_
rclcpp::Subscription< rosgraph_msgs::msg::Clock >::SharedPtr cdasim_clock_sub_
auto sdsm_msg_callback(const input_msg_type &msg) const -> void
auto to_detection_list_msg(const carma_v2x_msgs::msg::SensorDataSharingMessage &sdsm, std::string_view georeference) -> carma_cooperative_perception_interfaces::msg::DetectionList