17#include <carma_cooperative_perception_interfaces/msg/detection.hpp>
18#include <carma_ros2_utils/carma_lifecycle_node.hpp>
19#include <rclcpp_components/register_node_macro.hpp>
26 const rclcpp_lifecycle::State & ) -> carma_ros2_utils::CallbackReturn
28 RCLCPP_INFO(get_logger(),
"Lifecycle transition: configuring");
30 detection_list_sub_ = create_subscription<
31 carma_cooperative_perception_interfaces::msg::DetectionList>(
32 "input/detection_list", 1,
33 [
this](
const carma_cooperative_perception_interfaces::msg::DetectionList::SharedPtr msg_ptr) {
34 if (
const auto current_state{this->get_current_state().label()}; current_state ==
"active") {
35 attempt_filter_and_republish(*msg_ptr);
39 "Trying to receive message on the topic '%s', but the containing node is not activated. "
40 "Current node state: '%s'",
41 this->detection_list_sub_->get_topic_name(), current_state.c_str());
45 host_vehicle_pose_sub_ = create_subscription<geometry_msgs::msg::PoseStamped>(
46 "input/host_vehicle_pose", 1, [
this](
const geometry_msgs::msg::PoseStamped::SharedPtr msg_ptr) {
47 if (
const auto current_state{this->get_current_state().label()}; current_state ==
"active") {
48 update_host_vehicle_pose(*msg_ptr);
52 "Trying to receive message on the topic '%s', but the containing node is not activated. "
53 "Current node state: '%s'",
54 this->detection_list_sub_->get_topic_name(), current_state.c_str());
59 create_publisher<carma_cooperative_perception_interfaces::msg::DetectionList>(
60 "output/detection_list", 1);
62 RCLCPP_INFO(get_logger(),
"Lifecycle transition: successfully configured");
64 on_set_parameters_callback_ =
65 add_on_set_parameters_callback([
this](
const std::vector<rclcpp::Parameter> & parameters) {
66 rcl_interfaces::msg::SetParametersResult result;
67 result.successful =
true;
68 result.reason =
"success";
70 for (
const auto & parameter : parameters) {
71 if (parameter.get_name() ==
"distance_threshold_meters") {
72 if (this->get_current_state().label() ==
"active") {
73 result.successful = false;
74 result.reason =
"parameter is read-only while node is in 'Active' state";
78 "Cannot change parameter 'distance_threshold_meters': " + result.reason);
83 if (
const auto value{parameter.as_double()}; value < 0) {
84 result.successful =
false;
85 result.reason =
"parameter must be nonnegative";
89 "Cannot change parameter 'distance_threshold_meters': " + result.reason);
93 this->squared_distance_threshold_meters_ = std::pow(value, 2);
96 result.successful =
false;
97 result.reason =
"Unexpected parameter name '" + parameter.get_name() +
'\'';
105 declare_parameter(
"distance_threshold_meters", 0.0);
107 return carma_ros2_utils::CallbackReturn::SUCCESS;
110auto HostVehicleFilterNode::handle_on_activate(
const rclcpp_lifecycle::State & )
111 -> carma_ros2_utils::CallbackReturn
113 RCLCPP_INFO(get_logger(),
"Lifecycle transition: activating");
114 RCLCPP_INFO(get_logger(),
"Lifecycle transition: successfully activated");
116 return carma_ros2_utils::CallbackReturn::SUCCESS;
119auto HostVehicleFilterNode::handle_on_deactivate(
120 const rclcpp_lifecycle::State & ) -> carma_ros2_utils::CallbackReturn
122 RCLCPP_INFO(get_logger(),
"Lifecycle transition: deactivating");
123 RCLCPP_INFO(get_logger(),
"Lifecycle transition: successfully deactivated");
125 return carma_ros2_utils::CallbackReturn::SUCCESS;
128auto HostVehicleFilterNode::handle_on_cleanup(
const rclcpp_lifecycle::State & )
129 -> carma_ros2_utils::CallbackReturn
131 RCLCPP_INFO(get_logger(),
"Lifecycle transition: cleaning up");
134 detection_list_sub_.reset();
135 host_vehicle_pose_sub_.reset();
137 RCLCPP_INFO(get_logger(),
"Lifecycle transition: successfully cleaned up");
139 return carma_ros2_utils::CallbackReturn::SUCCESS;
142auto HostVehicleFilterNode::handle_on_shutdown(
const rclcpp_lifecycle::State & )
143 -> carma_ros2_utils::CallbackReturn
145 RCLCPP_INFO(get_logger(),
"Lifecycle transition: shutting down");
148 detection_list_sub_.reset();
149 host_vehicle_pose_sub_.reset();
151 RCLCPP_INFO(get_logger(),
"Lifecycle transition: successfully shut down");
153 return carma_ros2_utils::CallbackReturn::SUCCESS;
156auto HostVehicleFilterNode::update_host_vehicle_pose(
const geometry_msgs::msg::PoseStamped & msg)
159 host_vehicle_pose_ = msg;
162auto HostVehicleFilterNode::attempt_filter_and_republish(
163 carma_cooperative_perception_interfaces::msg::DetectionList msg) ->
void
165 if (!host_vehicle_pose_.has_value()) {
166 RCLCPP_WARN(get_logger(),
"Could not filter detection list: host vehicle pose unknown");
170 const auto is_within_distance = [
this](
const auto & detection) {
172 detection.semantic_class != detection.SEMANTIC_CLASS_UNKNOWN &&
173 detection.semantic_class != detection.SEMANTIC_CLASS_SMALL_VEHICLE) {
178 this->squared_distance_threshold_meters_;
182 std::remove_if(std::begin(msg.detections), std::end(msg.detections), is_within_distance)};
184 msg.detections.erase(new_end, std::end(msg.detections));
186 this->detection_list_pub_->publish(msg);
190 const geometry_msgs::msg::Pose & a,
const geometry_msgs::msg::Pose & b) ->
double
192 return std::pow(a.position.x - b.position.x, 2) + std::pow(a.position.y - b.position.y, 2) +
193 std::pow(a.position.z - b.position.z, 2);
202RCLCPP_COMPONENTS_REGISTER_NODE(
auto handle_on_configure(const rclcpp_lifecycle::State &) -> carma_ros2_utils::CallbackReturn override
auto euclidean_distance_squared(const geometry_msgs::msg::Pose &a, const geometry_msgs::msg::Pose &b) -> double