SMACC2
Loading...
Searching...
No Matches
waypoints_visualizer.cpp
Go to the documentation of this file.
1// Copyright 2021 RobosoftAI 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
27CpWaypointsVisualizer::CpWaypointsVisualizer(rclcpp::Duration duration) : ISmaccUpdatable(duration)
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
37 auto & waypoints = waypointsNavigator_->getWaypoints();
38 auto & waypointsNames = waypointsNavigator_->getWaypointNames();
39
40 int i = 0;
41 for (auto & waypoint : waypoints)
42 {
43 std::string name;
44 if ((long)waypointsNames.size() > i)
45 {
46 name = waypointsNames[i];
47 }
48 else
49 {
50 name = "waypoint_" + std::to_string(i);
51 }
52
53 visualization_msgs::msg::Marker marker;
54 createMarker(waypoint, marker);
55 markers_.markers.push_back(marker);
56
57 visualization_msgs::msg::Marker markerlabel;
58 createMarkerLabel(waypoint, name, markerlabel);
59 markerLabels_.markers.push_back(markerlabel);
60
61 i++;
62 }
63}
64
66 const geometry_msgs::msg::Pose & waypoint, std::string label,
67 visualization_msgs::msg::Marker & marker)
68{
69 marker.header.frame_id = frameid;
70 marker.header.stamp = getNode()->now();
71 marker.ns = "waypoints_labels";
72
73 marker.id = markers_.markers.size();
74 marker.type = visualization_msgs::msg::Marker::TEXT_VIEW_FACING;
75 marker.action = visualization_msgs::msg::Marker::ADD;
76 marker.scale.x = 0.3;
77 marker.scale.y = 0.3;
78 marker.scale.z = 0.3;
79 marker.text = label;
80
81 marker.color.a = 1.0;
82 marker.pose = waypoint;
83 marker.pose.position.z += 0.3;
84}
85
87 const geometry_msgs::msg::Pose & waypoint, visualization_msgs::msg::Marker & marker)
88{
89 marker.header.frame_id = frameid;
90 marker.header.stamp = getNode()->now();
91 marker.ns = "waypoints";
92
93 marker.id = markers_.markers.size();
94 marker.type = visualization_msgs::msg::Marker::SPHERE;
95 marker.action = visualization_msgs::msg::Marker::ADD;
96 marker.scale.x = 0.1;
97 marker.scale.y = 0.1;
98 marker.scale.z = 0.1;
99
100 marker.color.a = 1.0;
101 marker.pose = waypoint;
102}
103
105{
106 std::lock_guard<std::mutex> guard(m_mutex_);
107
109
110 int i = 0;
111
112 for (auto & marker : markers_.markers)
113 {
114 marker.header.stamp = getNode()->now();
115
116 if (i >= index)
117 {
118 marker.color.r = 1.0;
119 marker.color.g = 0;
120 marker.color.b = 0;
121 }
122 else
123 {
124 marker.color.r = 0.0;
125 marker.color.g = 1.0;
126 marker.color.b = 0;
127 }
128 i++;
129 }
130
131 i = 0;
132 for (auto & marker : markerLabels_.markers)
133 {
134 marker.header.stamp = getNode()->now();
135
136 if (i >= index)
137 {
138 marker.color.r = 1.0;
139 marker.color.g = 0;
140 marker.color.b = 0;
141 }
142 else
143 {
144 marker.color.r = 0.0;
145 marker.color.g = 1.0;
146 marker.color.b = 0;
147 }
148 i++;
149 }
150
151 //markers_.header.stamp = getNode()->now();
152 markersPub_->publish(markers_);
153 markersPub_->publish(markerLabels_);
154}
155
156} // namespace cl_nav2z
visualization_msgs::msg::MarkerArray markers_
visualization_msgs::msg::MarkerArray markerLabels_
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)
cl_nav2z::WaypointNavigator * waypointsNavigator_
const std::vector< geometry_msgs::msg::Pose > & getWaypoints() const
const std::vector< std::string > & getWaypointNames() const
void requiresComponent(TComponent *&requiredComponentStorage, bool throwExceptionIfNotExist=false)
rclcpp::Node::SharedPtr getNode()
const std::string frameid