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.
trajectory_visualizer.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020-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 */
16
17#include <rclcpp/rclcpp.hpp>
18#include <limits>
19#include <math.h>
21
22namespace trajectory_visualizer {
23
24 namespace std_ph = std::placeholders;
25
26 TrajectoryVisualizer::TrajectoryVisualizer(const rclcpp::NodeOptions &options)
27 : carma_ros2_utils::CarmaLifecycleNode(options)
28 {
29 // Create initial config
30 config_ = Config();
31
32 // Declare parameters
33 config_.max_speed = declare_parameter<double>("max_speed", config_.max_speed);
34 }
35
36 carma_ros2_utils::CallbackReturn TrajectoryVisualizer::handle_on_configure(const rclcpp_lifecycle::State &)
37 {
38 // Setup subscribers
39 traj_sub_ = create_subscription<carma_planning_msgs::msg::TrajectoryPlan>("plan_trajectory", 50,std::bind(&TrajectoryVisualizer::callbackPlanTrajectory,this,std_ph::_1));
40 // Setup publisher
41 traj_marker_pub_ = create_publisher<visualization_msgs::msg::MarkerArray>("trajectory_visualizer", 1);
42
43 // Return success if everything initialized successfully
44 return CallbackReturn::SUCCESS;
45 }
46
47 bool validateTime(const rclcpp::Time& time) {
48
49 int64_t sec64 = static_cast<int64_t>(floor(time.seconds()));
50 if (sec64 < std::numeric_limits<int32_t>::min() || sec64 > std::numeric_limits<int32_t>::max())
51 return false;
52
53 return true;
54 }
55
56 void TrajectoryVisualizer::callbackPlanTrajectory(carma_planning_msgs::msg::TrajectoryPlan::UniquePtr msg)
57 {
58 if (msg->trajectory_points.size() == 0)
59 {
60 RCLCPP_WARN_STREAM(this->get_logger(),"No trajectory point in plan_trajectory! Returning");
61 }
62 visualization_msgs::msg::MarkerArray tmp_marker_array;
63 // display by markers the velocity between each trajectory point/target time.
64 visualization_msgs::msg::Marker marker;
65 marker.header.frame_id = "map";
66 marker.header.stamp = this->now();
67 marker.type = visualization_msgs::msg::Marker::ARROW;
68 marker.action = visualization_msgs::msg::Marker::ADD;
69 marker.ns = "trajectory_visualizer";
70
71
72 marker.scale.x = 2;
73 marker.scale.y = 2;
74 marker.scale.z = 1;
75 marker.frame_locked = true;
76
77 size_t count = std::max(prev_marker_list_size_, msg->trajectory_points.size());
78
79 for (size_t i = 1; i < count; i++)
80 {
81 marker.id = i;
82
83 if (i >= msg->trajectory_points.size()) { // If we need to delete previous points
84 marker.action = visualization_msgs::msg::Marker::DELETE;
85 tmp_marker_array.markers.push_back(marker);
86 continue;
87 }
88
89 double max_speed = config_.max_speed * MPH_TO_MS;
90 double speed = max_speed;
91
92 rclcpp::Time t2 = msg->trajectory_points[i].target_time;
93 rclcpp::Time t1 = msg->trajectory_points[i - 1].target_time;
94
95 if (validateTime(t2) && validateTime(t1) && t2 > t1 ) {
96 rclcpp::Duration dt = t2 - t1;
97
98 double dx = msg->trajectory_points[i].x - msg->trajectory_points[i-1].x;
99 double dy = msg->trajectory_points[i].y - msg->trajectory_points[i-1].y;
100 double dist = sqrt(dx * dx + dy * dy);
101
102 speed = dist/ dt.seconds();
103 }
104
105
106
107 // map color to the scale of the speed
108 // red being the highest, green being the lowest (0ms)
109
110 RCLCPP_DEBUG_STREAM(this->get_logger(),"Speed:" << speed << "ms, max_speed:" << max_speed << "ms");
111 if (speed > max_speed)
112 {
113 RCLCPP_DEBUG_STREAM(this->get_logger(),"Speed was big, so capped at " << max_speed << "ms");
114 speed = max_speed;
115 }
116
117 marker.color.r = 0.0f;
118 marker.color.g = 0.0f;
119 marker.color.b = 0.0f;
120 marker.color.a = 1.0f;
121
122
123 double ratio = speed / max_speed;
124 if (ratio >= 0.75f)
125 {
126 marker.color.r = 1.0f;
127 }
128 else if (ratio >= 0.5f)
129 {
130 marker.color.b = 1.0f;
131 }
132 else if (ratio >= 0.25)
133 {
134 marker.color.b = 1.0f;
135 marker.color.g = 1.0f;
136
137 }
138 else if (ratio >= 0.0)
139 {
140 marker.color.g = 1.0f;
141 }
142
143 if (speed < 1.0) // Less than 2.5 mph is usually stopping
144 {
145 // Gray
146 marker.color.r = 0.5f;
147 marker.color.g = 0.5f;
148 marker.color.b = 0.5f;
149 }
150
151 marker.points = {};
152 geometry_msgs::msg::Point start;
153 start.x = msg->trajectory_points[i-1].x;
154 start.y = msg->trajectory_points[i-1].y;
155
156 geometry_msgs::msg::Point end;
157 end.x = msg->trajectory_points[i].x;
158 end.y = msg->trajectory_points[i].y;
159 marker.points.push_back(start);
160 marker.points.push_back(end);
161
162 tmp_marker_array.markers.push_back(marker);
163 }
164
165 prev_marker_list_size_ = msg->trajectory_points.size();
166 traj_marker_pub_->publish(tmp_marker_array);
167 }
168
169}
170
171#include "rclcpp_components/register_node_macro.hpp"
172
173// Register the component with class_loader
174RCLCPP_COMPONENTS_REGISTER_NODE(trajectory_visualizer::TrajectoryVisualizer)
175
TrajectoryVisualizer(const rclcpp::NodeOptions &)
TrajectoryVisualizer constructor.
carma_ros2_utils::CallbackReturn handle_on_configure(const rclcpp_lifecycle::State &)
void callbackPlanTrajectory(carma_planning_msgs::msg::TrajectoryPlan::UniquePtr msg)
carma_ros2_utils::PubPtr< visualization_msgs::msg::MarkerArray > traj_marker_pub_
carma_ros2_utils::SubPtr< carma_planning_msgs::msg::TrajectoryPlan > traj_sub_
bool validateTime(const rclcpp::Time &time)
max_speed values for trajectory_visualizer