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.
object_visualizer_node.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 LEIDOS.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
17
18namespace object_visualizer
19{
20 namespace std_ph = std::placeholders;
21
22 Node::Node(const rclcpp::NodeOptions &options)
23 : carma_ros2_utils::CarmaLifecycleNode(options)
24 {
25 // Create initial config
26 config_ = Config();
27
28 // Declare parameters
29 config_.enable_external_objects_viz = declare_parameter<bool>("enable_external_objects_viz", config_.enable_external_objects_viz);
30 config_.enable_roadway_objects_viz = declare_parameter<bool>("enable_roadway_objects_viz", config_.enable_roadway_objects_viz);
31 config_.external_objects_viz_ns = declare_parameter<std::string>("external_objects_viz_ns", config_.external_objects_viz_ns);
32 config_.roadway_obstacles_viz_ns = declare_parameter<std::string>("roadway_obstacles_viz_ns", config_.roadway_obstacles_viz_ns);
33 config_.marker_shape = declare_parameter<uint8_t>("marker_shape", config_.marker_shape);
34
35 }
36
37 rcl_interfaces::msg::SetParametersResult Node::parameter_update_callback(const std::vector<rclcpp::Parameter> &parameters)
38 {
39 // Update parameters
40 auto error = update_params<bool>(
41 {
42 {"enable_external_objects_viz", config_.enable_external_objects_viz},
43 {"enable_roadway_objects_viz", config_.enable_roadway_objects_viz}
44 }, parameters);
45
46 auto error2 = update_params<std::string>(
47 {
48 {"external_objects_viz_ns", config_.external_objects_viz_ns},
49 {"roadway_obstacles_viz_ns", config_.roadway_obstacles_viz_ns}
50 }, parameters);
51
52 auto error3 = update_params<uint8_t>(
53 {
54 {"marker_shape", config_.marker_shape}
55 }, parameters);
56
57 rcl_interfaces::msg::SetParametersResult result;
58
59 result.successful = !error && !error2 && !error3;
60
61 return result;
62 }
63
64 carma_ros2_utils::CallbackReturn Node::handle_on_configure(const rclcpp_lifecycle::State &)
65 {
66 // Reset config
67 config_ = Config();
68
69 // Load parameters
70 get_parameter<bool>("enable_external_objects_viz", config_.enable_external_objects_viz);
71 get_parameter<bool>("enable_roadway_objects_viz", config_.enable_roadway_objects_viz);
72 get_parameter<std::string>("external_objects_viz_ns", config_.external_objects_viz_ns);
73 get_parameter<std::string>("roadway_obstacles_viz_ns", config_.roadway_obstacles_viz_ns);
74 get_parameter<uint8_t>("marker_shape", config_.marker_shape);
75
76 // Register runtime parameter update callback
77 add_on_set_parameters_callback(std::bind(&Node::parameter_update_callback, this, std_ph::_1));
78
79 // Setup subscribers
80 external_objects_sub_ = create_subscription<carma_perception_msgs::msg::ExternalObjectList>("external_objects", 10,
81 std::bind(&Node::external_objects_callback, this, std_ph::_1));
82
83 roadway_obstacles_sub_ = create_subscription<carma_perception_msgs::msg::RoadwayObstacleList>("roadway_obstacles", 10,
84 std::bind(&Node::roadway_obstacles_callback, this, std_ph::_1));
85
86 // Setup publishers
87 external_objects_viz_pub_ = create_publisher<visualization_msgs::msg::MarkerArray>("external_objects_viz", 10);
88 roadway_obstacles_viz_pub_ = create_publisher<visualization_msgs::msg::MarkerArray>("roadway_obstacles_viz", 10);
89
90 // Return success if everthing initialized successfully
91 return CallbackReturn::SUCCESS;
92 }
93
94 void Node::external_objects_callback(carma_perception_msgs::msg::ExternalObjectList::UniquePtr msg)
95 {
96 RCLCPP_DEBUG_STREAM( get_logger(), "external_objects_callback called");
97
99 RCLCPP_DEBUG_STREAM( get_logger(), "external_objects_callback called, but visualization is not enabled.");
100 return;
101 }
102
103 visualization_msgs::msg::MarkerArray viz_msg;
104 //delete all markers before adding new ones
105 visualization_msgs::msg::Marker marker;
106 viz_msg.markers.reserve(msg->objects.size() + 1); //+1 to account for delete all marker
107 marker.id = 0;
108 marker.action = visualization_msgs::msg::Marker::DELETEALL;
109 viz_msg.markers.push_back(marker);
110
111
112 size_t id = 0; // We always count the id from zero so we can delete markers later in a consistent manner
113
114 for (auto obj : msg->objects) {
115 visualization_msgs::msg::Marker marker;
116
117 marker.header = msg->header;
118
120
121 // Pedestrian's will be represented as red box
122 if (obj.object_type == carma_perception_msgs::msg::ExternalObject::PEDESTRIAN) {
123
124 marker.color.r = 1.0;
125 marker.color.g = 0.0;
126 marker.color.b = 0.0;
127 marker.color.a = 1.0;
128
129 } // All other detected objects will be blue
130 else {
131
132 marker.color.r = 0.0;
133 marker.color.g = 0.0;
134 marker.color.b = 1.0;
135 marker.color.a = 1.0;
136 }
137
138 marker.pose = obj.pose.pose;
139 marker.pose.position.z = std::max(1.0, obj.size.z); // lifts the marker above ground so that it doesn't clip
140
141 marker.id = id;
142 if (static_cast<int>(config_.marker_shape) < 1 || static_cast<int>(config_.marker_shape) > 3)
143 {
144 throw std::invalid_argument("Marker shape is not valid: " + std::to_string(static_cast<int>(config_.marker_shape)) + ". Please choose from 1: CUBE, 2: SPHERE, 3: CYLINDER");
145 }
146 marker.type = config_.marker_shape;
147 marker.action = visualization_msgs::msg::Marker::ADD;
148
149 // overwrite size in case any previous stack doesn't provide size
150 // such as carma_cooperative_perception at the moment
151 marker.scale.x = std::max(1.0, obj.size.x) * 2; // Size in carma is half the length/width/height
152 marker.scale.y = std::max(1.0, obj.size.y) * 2;
153 marker.scale.z = std::max(1.0, obj.size.z) * 2;
154
155 viz_msg.markers.push_back(marker);
156
157 id++;
158 }
159
161
162 external_objects_viz_pub_->publish(viz_msg);
163
164 }
165
166 void Node::clear_and_update_old_objects(visualization_msgs::msg::MarkerArray &viz_msg, size_t &old_size)
167 {
168 size_t size_swap = viz_msg.markers.size();
169
170 // Delete any markers from the previous message that are no longer in the current message
171 if (viz_msg.markers.size() < old_size) {
172
173 for (size_t i = viz_msg.markers.size(); i < old_size; ++i) {
174 visualization_msgs::msg::Marker marker;
175 marker.id = i;
176 marker.action = visualization_msgs::msg::Marker::DELETE;
177 viz_msg.markers.push_back(marker);
178 }
179
180 }
181
182 old_size = size_swap; // Assign the new size of added objects to the old size
183 }
184
185 void Node::roadway_obstacles_callback(carma_perception_msgs::msg::RoadwayObstacleList::UniquePtr msg)
186 {
187 RCLCPP_DEBUG_STREAM( get_logger(), "roadway_obstacles_callback called");
188
190 RCLCPP_DEBUG_STREAM( get_logger(), "roadway_obstacles_callback called, but visualization is not enabled.");
191 return;
192 }
193
194 visualization_msgs::msg::MarkerArray viz_msg;
195 //delete all markers before adding new ones
196 visualization_msgs::msg::Marker marker;
197 viz_msg.markers.reserve(msg->roadway_obstacles.size() + 1); //+1 to account for delete all marker
198 marker.id = 0;
199 marker.action = visualization_msgs::msg::Marker::DELETEALL;
200 viz_msg.markers.push_back(marker);
201
202 size_t id = 0; // We always count the id from zero so we can delete markers later in a consistent manner
203 for (auto obj : msg->roadway_obstacles) {
204 visualization_msgs::msg::Marker marker;
205
206 marker.header = obj.object.header;
207
209
210 // Pedestrian's will be represented as a red box
211 if (obj.object.object_type == carma_perception_msgs::msg::ExternalObject::PEDESTRIAN) {
212
213 marker.color.r = 1.0;
214 marker.color.g = 0.0;
215 marker.color.b = 0.0;
216 marker.color.a = 1.0;
217
218 } // Connected vehicles will be green
219 else if (obj.connected_vehicle_type.type != carma_perception_msgs::msg::ConnectedVehicleType::NOT_CONNECTED){
220 marker.color.r = 0.0;
221 marker.color.g = 1.0;
222 marker.color.b = 0.0;
223 marker.color.a = 1.0;
224 } // All other detected objects will be blue
225 else {
226
227 marker.color.r = 0.0;
228 marker.color.g = 0.0;
229 marker.color.b = 1.0;
230 marker.color.a = 1.0;
231 }
232
233 marker.id = id;
234 marker.type = visualization_msgs::msg::Marker::CUBE;
235 marker.action = visualization_msgs::msg::Marker::ADD;
236 marker.pose = obj.object.pose.pose;
237 marker.scale.x = obj.object.size.x * 2.0; // Size in carma is half the length/width/height
238 marker.scale.y = obj.object.size.y * 2.0;
239 marker.scale.z = obj.object.size.z * 2.0;
240
241 viz_msg.markers.push_back(marker);
242
243 id++;
244 }
245
247
248 roadway_obstacles_viz_pub_->publish(viz_msg);
249
250 }
251
252} // object_visualizer
253
254#include "rclcpp_components/register_node_macro.hpp"
255
256// Register the component with class_loader
257RCLCPP_COMPONENTS_REGISTER_NODE(object_visualizer::Node)
This node provides RViz visualization messages for recieved ExternalObject and RoadwayObstacle messag...
void roadway_obstacles_callback(carma_perception_msgs::msg::RoadwayObstacleList::UniquePtr msg)
Roadway obstacles callback. Converts the message to a visualization message to republish.
rcl_interfaces::msg::SetParametersResult parameter_update_callback(const std::vector< rclcpp::Parameter > &parameters)
Example callback for dynamic parameter updates.
carma_ros2_utils::CallbackReturn handle_on_configure(const rclcpp_lifecycle::State &)
carma_ros2_utils::PubPtr< visualization_msgs::msg::MarkerArray > roadway_obstacles_viz_pub_
void clear_and_update_old_objects(visualization_msgs::msg::MarkerArray &viz_msg, size_t &old_size)
Adds a marker deletion for a number of markers which is the delta between the new marker count and th...
carma_ros2_utils::SubPtr< carma_perception_msgs::msg::ExternalObjectList > external_objects_sub_
Node(const rclcpp::NodeOptions &)
Node constructor.
carma_ros2_utils::SubPtr< carma_perception_msgs::msg::RoadwayObstacleList > roadway_obstacles_sub_
carma_ros2_utils::PubPtr< visualization_msgs::msg::MarkerArray > external_objects_viz_pub_
void external_objects_callback(carma_perception_msgs::msg::ExternalObjectList::UniquePtr msg)
External objects callback. Converts the message to a visualization message to republish.
auto to_string(const UtmZone &zone) -> std::string
Definition: utm_zone.cpp:21
Stuct containing the algorithm configuration values for object_visualizer.
std::string external_objects_viz_ns
External Objects marker rviz namespace.
bool enable_roadway_objects_viz
If true then RViz markers will be forwarded for received carma_perception_msgs/RoadwayObstacleList me...
bool enable_external_objects_viz
If true then RViz markers will be forwarded for received carma_perception_msgs/ExternalObjectList mes...
std::string roadway_obstacles_viz_ns
Roadway Obstacles marker rviz namespace.