SMACC2
Loading...
Searching...
No Matches
cp_waypoints_visualizer.cpp
Go to the documentation of this file.
1// Copyright 2025 Robosoft Inc.
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/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20
22
23namespace cl_nav2z
24{
25const std::string frameid = "map";
26
28{
29}
30
32{
33 markersPub_ = getNode()->create_publisher<visualization_msgs::msg::MarkerArray>(
34 "cp_waypoints_visualizer/visualization_markers", rclcpp::QoS(rclcpp::KeepLast(1)));
35
36 this->requiresComponent(waypointsNavigator_, ComponentRequirement::HARD);
37 auto & waypoints = waypointsNavigator_->getWaypoints();
38 auto & waypointsNames = waypointsNavigator_->getWaypointNames();
39
40 int i = 0;
41 for (auto & waypoint : waypoints)
42 {
43 // an unnamed waypoint has an empty name
44 std::string name = waypointsNames[i];
45 if (name.empty())
46 {
47 name = "waypoint_" + std::to_string(i);
48 }
49
50 visualization_msgs::msg::Marker marker;
51 createMarker(waypoint, marker);
52 markers_.markers.push_back(marker);
53
54 visualization_msgs::msg::Marker markerlabel;
55 createMarkerLabel(waypoint, name, markerlabel);
56 markerLabels_.markers.push_back(markerlabel);
57
58 i++;
59 }
60}
61
63 const geometry_msgs::msg::Pose & waypoint, std::string label,
64 visualization_msgs::msg::Marker & marker)
65{
66 marker.header.frame_id = frameid;
67 marker.header.stamp = getNode()->now();
68 marker.ns = "waypoints_labels";
69
70 marker.id = static_cast<int>(markers_.markers.size());
71 marker.type = visualization_msgs::msg::Marker::TEXT_VIEW_FACING;
72 marker.action = visualization_msgs::msg::Marker::ADD;
73 marker.scale.x = 0.3;
74 marker.scale.y = 0.3;
75 marker.scale.z = 0.3;
76 marker.text = label;
77
78 marker.color.a = 1.0;
79 marker.pose = waypoint;
80 marker.pose.position.z += 0.3;
81}
82
84 const geometry_msgs::msg::Pose & waypoint, visualization_msgs::msg::Marker & marker)
85{
86 marker.header.frame_id = frameid;
87 marker.header.stamp = getNode()->now();
88 marker.ns = "waypoints";
89
90 marker.id = static_cast<int>(markers_.markers.size());
91 marker.type = visualization_msgs::msg::Marker::SPHERE;
92 marker.action = visualization_msgs::msg::Marker::ADD;
93 marker.scale.x = 0.1;
94 marker.scale.y = 0.1;
95 marker.scale.z = 0.1;
96
97 marker.color.a = 1.0;
98 marker.pose = waypoint;
99}
100
102{
103 std::lock_guard<std::mutex> guard(m_mutex_);
104
106
107 int i = 0;
108
109 for (auto & marker : markers_.markers)
110 {
111 marker.header.stamp = getNode()->now();
112
113 if (i >= index)
114 {
115 marker.color.r = 1.0;
116 marker.color.g = 0;
117 marker.color.b = 0;
118 }
119 else
120 {
121 marker.color.r = 0.0;
122 marker.color.g = 1.0;
123 marker.color.b = 0;
124 }
125 i++;
126 }
127
128 i = 0;
129 for (auto & marker : markerLabels_.markers)
130 {
131 marker.header.stamp = getNode()->now();
132
133 if (i >= index)
134 {
135 marker.color.r = 1.0;
136 marker.color.g = 0;
137 marker.color.b = 0;
138 }
139 else
140 {
141 marker.color.r = 0.0;
142 marker.color.g = 1.0;
143 marker.color.b = 0;
144 }
145 i++;
146 }
147
148 markersPub_->publish(markers_);
149 markersPub_->publish(markerLabels_);
150}
151
152} // namespace cl_nav2z
const std::vector< geometry_msgs::msg::Pose > & getWaypoints() const
const std::vector< std::string > & getWaypointNames() const
visualization_msgs::msg::MarkerArray markers_
visualization_msgs::msg::MarkerArray markerLabels_
cl_nav2z::CpWaypointNavigator * waypointsNavigator_
rclcpp::Publisher< visualization_msgs::msg::MarkerArray >::SharedPtr markersPub_
void createMarker(const geometry_msgs::msg::Pose &waypoint, visualization_msgs::msg::Marker &m)
CpWaypointsVisualizer(rclcpp::Duration duration)
void createMarkerLabel(const geometry_msgs::msg::Pose &waypoint, std::string label, visualization_msgs::msg::Marker &m)
void requiresComponent(TComponent *&requiredComponentStorage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
rclcpp::Node::SharedPtr getNode()
const std::string frameid