Carma-platform v4.11.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 <chrono>
19#include <string>
20
21#include <carma_cooperative_perception_interfaces/msg/detection_list.hpp>
22#include <carma_ros2_utils/carma_lifecycle_node.hpp>
23#include <carma_v2x_msgs/msg/sensor_data_sharing_message.hpp>
24#include <rclcpp/rclcpp.hpp>
25#include <rosgraph_msgs/msg/clock.hpp>
26#include <std_msgs/msg/string.hpp>
27
29
31{
32
33class SdsmToDetectionListNode : public carma_ros2_utils::CarmaLifecycleNode
34{
35 using input_msg_type = carma_v2x_msgs::msg::SensorDataSharingMessage;
36 using input_msg_shared_pointer = typename input_msg_type::SharedPtr;
37 using output_msg_type = carma_cooperative_perception_interfaces::msg::DetectionList;
38
39public:
40 auto handle_on_configure(const rclcpp_lifecycle::State & /* previous_state */)
41 -> carma_ros2_utils::CallbackReturn override
42 {
43 // Declare parameters
44 declare_parameter("overwrite_covariance", config_.overwrite_covariance);
45 declare_parameter("pose_covariance_x", config_.pose_covariance_x);
46 declare_parameter("pose_covariance_y", config_.pose_covariance_y);
47 declare_parameter("pose_covariance_z", config_.pose_covariance_z);
48 declare_parameter("pose_covariance_yaw", config_.pose_covariance_yaw);
49 declare_parameter("twist_covariance_x", config_.twist_covariance_x);
50 declare_parameter("twist_covariance_z", config_.twist_covariance_z);
51 declare_parameter("twist_covariance_yaw", config_.twist_covariance_yaw);
52 declare_parameter("adjust_pose", config_.adjust_pose);
53 declare_parameter("x_offset", config_.x_offset);
54 declare_parameter("y_offset", config_.y_offset);
55 declare_parameter("yaw_offset", config_.yaw_offset);
56
57 // Get parameters
58 config_.overwrite_covariance = get_parameter("overwrite_covariance").as_bool();
59 config_.pose_covariance_x = get_parameter("pose_covariance_x").as_double();
60 config_.pose_covariance_y = get_parameter("pose_covariance_y").as_double();
61 config_.pose_covariance_z = get_parameter("pose_covariance_z").as_double();
62 config_.pose_covariance_yaw = get_parameter("pose_covariance_yaw").as_double();
63 config_.twist_covariance_x = get_parameter("twist_covariance_x").as_double();
64 config_.twist_covariance_z = get_parameter("twist_covariance_z").as_double();
65 config_.adjust_pose = get_parameter("adjust_pose").as_bool();
66 config_.x_offset = get_parameter("x_offset").as_double();
67 config_.y_offset = get_parameter("y_offset").as_double();
68 config_.yaw_offset = get_parameter("yaw_offset").as_double();
69
70 // Set up parameter validation callback
71 on_set_parameters_callback_ = add_on_set_parameters_callback(
72 [this](const std::vector<rclcpp::Parameter> & parameters) {
73 return update_parameters(parameters);
74 });
75
76 // Use the stream operator for debug output
77 RCLCPP_INFO_STREAM(get_logger(), "SdsmToDetectionListNode Configuration: " << config_);
78
79 return carma_ros2_utils::CallbackReturn::SUCCESS;
80 }
81
82 explicit SdsmToDetectionListNode(const rclcpp::NodeOptions & options)
83 : CarmaLifecycleNode{options},
84 publisher_{create_publisher<output_msg_type>("output/detections", 1)},
85 subscription_{create_subscription<input_msg_type>(
86 "input/sdsm", 100, [this](input_msg_shared_pointer msg_ptr) { sdsm_msg_callback(*msg_ptr); })},
87 georeference_subscription_{create_subscription<std_msgs::msg::String>(
88 "input/georeference", 1,
89 [this](std_msgs::msg::String::SharedPtr msg_ptr) { georeference_ = msg_ptr->data; })},
90 cdasim_clock_sub_{create_subscription<rosgraph_msgs::msg::Clock>(
91 "input/cdasim_clock", 1,
92 [this](rosgraph_msgs::msg::Clock::ConstSharedPtr msg_ptr) { cdasim_time_ = msg_ptr->clock; })}
93 {
94 // Initialize the valid parameter names to check when setting the at runtime
95 valid_parameter_names_ = {
96 "overwrite_covariance",
97 "pose_covariance_x",
98 "pose_covariance_y",
99 "pose_covariance_z",
100 "pose_covariance_yaw",
101 "twist_covariance_x",
102 "twist_covariance_z",
103 "twist_covariance_yaw",
104 "adjust_pose",
105 "x_offset",
106 "y_offset",
107 "yaw_offset"
108 };
109 }
110
111 auto sdsm_msg_callback(const input_msg_type & msg) const -> void
112 {
113 if (georeference_.empty()){
114 RCLCPP_WARN_STREAM(get_logger(), "Georeference not defined yet, ignoring SDSM.");
115 return;
116 }
117
118 try {
119 std::optional<SdsmToDetectionListConfig> conversion_adjustment = std::nullopt;
120 if (config_.overwrite_covariance || config_.adjust_pose) {
121 conversion_adjustment = config_;
122 }
123 auto detection_list_msg{
124 to_detection_list_msg(msg, georeference_, cdasim_time_ != std::nullopt,
125 conversion_adjustment)
126 };
127
128 if (cdasim_time_) {
129 // When in simulation, ROS time is CARLA time, but SDSMs use CDASim time
130 const auto time_delta{now() - cdasim_time_.value()};
131
132 for (auto & detection : detection_list_msg.detections) {
133 detection.header.stamp = rclcpp::Time(detection.header.stamp) + time_delta;
134 }
135 } else if (get_parameter("use_sim_time").as_bool()) {
136 // The ROS clock is simulated (use_sim_time) but no CDASim clock was received. SDSM
137 // timestamps are wall-clock (UTC), so re-express each detection time in ROS (sim) time:
138 // keep its age relative to the wall clock and subtract that age from the current ROS time.
139 const auto wall_now_ns{std::chrono::duration_cast<std::chrono::nanoseconds>(
140 std::chrono::system_clock::now().time_since_epoch())
141 .count()};
142 const auto ros_now_ns{now().nanoseconds()};
143
144 for (auto & detection : detection_list_msg.detections) {
145 const auto detection_wall_ns{rclcpp::Time(detection.header.stamp).nanoseconds()};
146 const auto age_ns{wall_now_ns - detection_wall_ns};
147 detection.header.stamp = rclcpp::Time(ros_now_ns - age_ns, RCL_ROS_TIME);
148 }
149 }
150 RCLCPP_DEBUG_STREAM(
151 get_logger(), "SDSM to detection list conversion: "
152 << detection_list_msg.detections.size() << " detections");
153 publisher_->publish(detection_list_msg);
154 } catch (const std::runtime_error & e) {
155 RCLCPP_ERROR_STREAM(get_logger(), "Failed to convert SDSM to detection list: " << e.what());
156 }
157 }
158
159private:
160 rclcpp::Publisher<output_msg_type>::SharedPtr publisher_;
161 rclcpp::Subscription<input_msg_type>::SharedPtr subscription_;
162 rclcpp::Subscription<std_msgs::msg::String>::SharedPtr georeference_subscription_;
163
164 rclcpp::Subscription<rosgraph_msgs::msg::Clock>::SharedPtr cdasim_clock_sub_;
165 std::optional<rclcpp::Time> cdasim_time_{std::nullopt};
166
167 std::string georeference_{""};
168
169 // Configuration object
171
172 OnSetParametersCallbackHandle::SharedPtr on_set_parameters_callback_{nullptr};
173
174 // Parameter validation function
175 rcl_interfaces::msg::SetParametersResult update_parameters(
176 const std::vector<rclcpp::Parameter> & parameters)
177 {
178 rcl_interfaces::msg::SetParametersResult result;
179 result.successful = true;
180 result.reason = "success";
181
182 for (const auto & parameter : parameters) {
183 // Check if parameter name is valid
184 if (valid_parameter_names_.find(parameter.get_name()) == valid_parameter_names_.end()) {
185 result.successful = false;
186 result.reason = "Unexpected parameter name '" + parameter.get_name() + "'";
187 RCLCPP_ERROR_STREAM(get_logger(), "Cannot change parameter: " << result.reason);
188 break;
189 }
190
191 // Validate covariance values (must be non-negative)
192 if (parameter.get_name() != "adjust_pose" &&
193 parameter.get_name() != "overwrite_covariance") {
194 if (const auto value{parameter.as_double()}; value < 0.0) {
195 result.successful = false;
196 result.reason = "Covariance parameter must be non-negative";
197 RCLCPP_ERROR_STREAM(
198 get_logger(),
199 "Cannot change parameter '" << parameter.get_name() << "': " << result.reason);
200 break;
201 } else {
202 // Update the appropriate parameter in the config
203 update_config_double_parameter(parameter);
204 }
205 } else if (parameter.get_name() == "overwrite_covariance"){
206 config_.overwrite_covariance = parameter.as_bool();
207 } else if (parameter.get_name() == "adjust_pose"){
208 config_.adjust_pose = parameter.as_bool();
209 }
210 }
211
212 // If parameters were successfully updated, log the new configuration
213 if (result.successful) {
214 RCLCPP_DEBUG_STREAM(get_logger(), "Updated configuration: " << config_);
215 }
216
217 return result;
218 }
219
220 // Helper function to update a specific parameter in the config
221 void update_config_double_parameter(const rclcpp::Parameter & parameter) {
222 const std::string & name = parameter.get_name();
223 const double value = parameter.as_double();
224
225 if (name == "pose_covariance_x") {
226 config_.pose_covariance_x = value;
227 } else if (name == "pose_covariance_y") {
228 config_.pose_covariance_y = value;
229 } else if (name == "pose_covariance_z") {
230 config_.pose_covariance_z = value;
231 } else if (name == "pose_covariance_yaw") {
232 config_.pose_covariance_yaw = value;
233 } else if (name == "twist_covariance_x") {
234 config_.twist_covariance_x = value;
235 } else if (name == "twist_covariance_z") {
236 config_.twist_covariance_z = value;
237 } else if (name == "twist_covariance_yaw") {
238 config_.twist_covariance_yaw = value;
239 } else if (name == "x_offset") {
240 config_.x_offset = value;
241 } else if (name == "y_offset") {
242 config_.y_offset = value;
243 } else if (name == "yaw_offset") {
244 config_.yaw_offset = value;
245 }
246 }
247
248 // Set of valid parameter names for validation
249 std::unordered_set<std::string> valid_parameter_names_;
250};
251
252} // namespace carma_cooperative_perception
253
254#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
rcl_interfaces::msg::SetParametersResult update_parameters(const std::vector< rclcpp::Parameter > &parameters)
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 handle_on_configure(const rclcpp_lifecycle::State &) -> carma_ros2_utils::CallbackReturn override
auto sdsm_msg_callback(const input_msg_type &msg) const -> void
rclcpp::Logger get_logger()
Return the module-level logger used by all basic_autonomy functions.
Definition: log.cpp:32
auto to_detection_list_msg(const carma_v2x_msgs::msg::SensorDataSharingMessage &sdsm, std::string_view georeference, bool is_simulation, const std::optional< SdsmToDetectionListConfig > &conversion_adjustment) -> carma_cooperative_perception_interfaces::msg::DetectionList
Converts a carma_v2x_msgs::msg::SensorDataSharingMessage (SDSM) to carma_cooperative_perception_inter...