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.
external_object_list_to_sdsm_component.cpp
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
16
17#include <rclcpp/rclcpp.hpp>
18#include <rclcpp_components/register_node_macro.hpp>
19
20#include <memory>
22
24{
26: CarmaLifecycleNode{options}
27{
28 lifecycle_publishers_.push_back(sdsm_publisher_);
29 param_callback_handles_.push_back(on_set_parameters_callback_);
30}
31
33 const rclcpp_lifecycle::State & /* previous_state */) -> carma_ros2_utils::CallbackReturn
34{
35 RCLCPP_INFO(get_logger(), "Life cycle state transition: configuring");
36
37 sdsm_publisher_ = create_publisher<sdsm_msg_type>("output/sdsms", 1);
38
39 external_objects_subscription_ = create_subscription<external_objects_msg_type>(
40 "input/external_objects", 1, [this](const external_objects_msg_type::SharedPtr msg_ptr) {
41 const auto current_state{this->get_current_state().label()};
42
43 if (current_state == "active") {
44 publish_as_sdsm(*msg_ptr);
45 } else {
46 RCLCPP_WARN(
47 this->get_logger(),
48 "Trying to receive message on the topic '%s', but the containing node is not activated. "
49 "Current node state: '%s'",
50 this->georeference_subscription_->get_topic_name(), current_state.c_str());
51 }
52 });
53
54 georeference_subscription_ = create_subscription<georeference_msg_type>(
55 "input/georeference", 1, [this](const georeference_msg_type::SharedPtr msg_ptr) {
56 const auto current_state{this->get_current_state().label()};
57
58 if (current_state == "active") {
59 update_georeference(*msg_ptr);
60 } else {
61 RCLCPP_WARN(
62 this->get_logger(),
63 "Trying to receive message on the topic '%s', but the containing node is not "
64 "activated. "
65 "Current node state: '%s'",
66 this->georeference_subscription_->get_topic_name(), current_state.c_str());
67 }
68 });
69
70 current_pose_subscription_ = create_subscription<pose_msg_type>(
71 "input/pose_stamped", 1, [this](const pose_msg_type::SharedPtr msg_ptr) {
72 const auto current_state{this->get_current_state().label()};
73
74 if (current_state == "active") {
75 update_current_pose(*msg_ptr);
76 } else {
77 RCLCPP_WARN(
78 this->get_logger(),
79 "Trying to recieve message on topic '%s', but the containing node is not activated."
80 "current node state: '%s'",
81 this->current_pose_subscription_->get_topic_name(), current_state.c_str());
82 }
83 });
84
85 RCLCPP_INFO(get_logger(), "Lifecycle transition: successfully configured");
86
87 return carma_ros2_utils::CallbackReturn::SUCCESS;
88}
89
91 const rclcpp_lifecycle::State & /* previous_state */) -> carma_ros2_utils::CallbackReturn
92{
93 RCLCPP_INFO(get_logger(), "Life cycle state transition: cleaning up");
94
95 sdsm_publisher_.reset();
96 external_objects_subscription_.reset();
97 georeference_subscription_.reset();
98
99 current_pose_subscription_.reset();
100
101 return carma_ros2_utils::CallbackReturn::SUCCESS;
102}
103
105 const rclcpp_lifecycle::State & /* previous_state */) -> carma_ros2_utils::CallbackReturn
106{
107 RCLCPP_INFO(get_logger(), "Life cycle state transition: shutting down");
108
109 sdsm_publisher_.reset();
110 external_objects_subscription_.reset();
111 georeference_subscription_.reset();
112
113 current_pose_subscription_.reset();
114
115 return carma_ros2_utils::CallbackReturn::SUCCESS;
116}
117
119 -> void
120{
121 if (!map_projector_) {
122 // Set to DEBUG level because this node may start up before any georeference publisher. In that
123 // scenario, temporarily not having a georeference (and therefore, no projector) is expected.
124 RCLCPP_DEBUG(
125 this->get_logger(), "Could not convert external object list to SDSM: unknown georeference");
126
127 return;
128 }
129
130 try {
131 const auto sdsm_output{to_sdsm_msg(msg, current_pose_, map_projector_)};
132
133 sdsm_publisher_->publish(sdsm_output);
134 } catch (const std::invalid_argument & e) {
135 RCLCPP_ERROR(
136 this->get_logger(), "Could not convert external object list to SDSM: %s", e.what());
137 }
138}
139
141{
142 if (map_georeference_ != msg.data)
143 {
144 map_georeference_ = msg.data;
145 map_projector_ = std::make_shared<lanelet::projection::LocalFrameProjector>(map_georeference_.c_str());
146 }
147}
148
150{
151 current_pose_ = msg;
152}
153
154} // namespace carma_cooperative_perception
155
156// This is not our macro, so we should not worry about linting it.
157// clang-tidy added support for ignoring system macros in release 14.0.0 (see the release notes
158// here: https://releases.llvm.org/14.0.0/tools/clang/tools/extra/docs/ReleaseNotes.html), but
159// ament_clang_tidy for ROS 2 Foxy specifically looks for clang-tidy-6.0.
160RCLCPP_COMPONENTS_REGISTER_NODE( // NOLINT
auto publish_as_sdsm(const external_objects_msg_type &msg) const -> void
auto handle_on_shutdown(const rclcpp_lifecycle::State &) -> carma_ros2_utils::CallbackReturn override
auto handle_on_cleanup(const rclcpp_lifecycle::State &) -> carma_ros2_utils::CallbackReturn override
auto handle_on_configure(const rclcpp_lifecycle::State &) -> carma_ros2_utils::CallbackReturn override
auto update_georeference(const georeference_msg_type &proj_string) -> void
rclcpp_lifecycle::LifecyclePublisher< sdsm_msg_type >::SharedPtr sdsm_publisher_
auto to_sdsm_msg(const carma_perception_msgs::msg::ExternalObjectList &external_object_list, const geometry_msgs::msg::PoseStamped &current_pose, const std::shared_ptr< lanelet::projection::LocalFrameProjector > &map_projection) -> carma_v2x_msgs::msg::SensorDataSharingMessage