SMACC2
Loading...
Searching...
No Matches
cp_trajectory_visualizer.hpp
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
21#pragma once
22
23#include <smacc2/component.hpp>
25
26#include <geometry_msgs/msg/pose_stamped.hpp>
27#include <visualization_msgs/msg/marker.hpp>
28#include <visualization_msgs/msg/marker_array.hpp>
29
30#include <mutex>
31#include <string>
32#include <vector>
33
34namespace cl_moveit2z
35{
45{
46public:
52 CpTrajectoryVisualizer(double publishRate = 10.0) : publishRate_(publishRate), enabled_(false) {}
53
54 virtual ~CpTrajectoryVisualizer() = default;
55
59 inline void onInitialize() override
60 {
62 getNode()->create_publisher<visualization_msgs::msg::MarkerArray>("trajectory_markers", 1);
63 RCLCPP_INFO(getLogger(), "[CpTrajectoryVisualizer] Initialized");
64
65 // Initialize timing for publish rate control
66 lastPublishTime_ = getNode()->now();
67 }
68
72 inline void update() override
73 {
74 std::lock_guard<std::mutex> guard(markersMutex_);
75
76 if (!enabled_ || !markersReady_)
77 {
78 return;
79 }
80
81 // Rate limiting
82 auto now = getNode()->now();
83 auto elapsed = (now - lastPublishTime_).seconds();
84 if (elapsed < (1.0 / publishRate_))
85 {
86 return;
87 }
88
89 markersPub_->publish(markers_);
90 lastPublishTime_ = now;
91 }
92
99 inline void setTrajectory(
100 const std::vector<geometry_msgs::msg::PoseStamped> & poses,
101 const std::string & ns = "trajectory")
102 {
103 std::lock_guard<std::mutex> guard(markersMutex_);
104
105 RCLCPP_INFO(
106 getLogger(), "[CpTrajectoryVisualizer] Setting trajectory with %lu poses", poses.size());
107
108 markers_.markers.clear();
109
110 if (poses.empty())
111 {
112 RCLCPP_WARN(getLogger(), "[CpTrajectoryVisualizer] Empty trajectory provided");
113 markersReady_ = false;
114 return;
115 }
116
117 // Create spheres for each pose
118 for (size_t i = 0; i < poses.size(); ++i)
119 {
120 visualization_msgs::msg::Marker marker;
121 marker.header = poses[i].header;
122 marker.ns = ns;
123 marker.id = static_cast<int>(i);
124 marker.type = visualization_msgs::msg::Marker::SPHERE;
125 marker.action = visualization_msgs::msg::Marker::ADD;
126
127 marker.pose = poses[i].pose;
128
129 marker.scale.x = markerScale_;
130 marker.scale.y = markerScale_;
131 marker.scale.z = markerScale_;
132
133 marker.color.r = markerColor_[0];
134 marker.color.g = markerColor_[1];
135 marker.color.b = markerColor_[2];
136 marker.color.a = markerColor_[3];
137
138 markers_.markers.push_back(marker);
139 }
140
141 // Create line strip connecting all poses
142 if (poses.size() > 1)
143 {
144 visualization_msgs::msg::Marker lineMarker;
145 lineMarker.header = poses[0].header;
146 lineMarker.ns = ns + "_path";
147 lineMarker.id = static_cast<int>(poses.size());
148 lineMarker.type = visualization_msgs::msg::Marker::LINE_STRIP;
149 lineMarker.action = visualization_msgs::msg::Marker::ADD;
150
151 lineMarker.scale.x = markerScale_ * 0.3; // Line width
152
153 lineMarker.color.r = markerColor_[0];
154 lineMarker.color.g = markerColor_[1];
155 lineMarker.color.b = markerColor_[2];
156 lineMarker.color.a = markerColor_[3] * 0.7; // Slightly transparent
157
158 for (const auto & pose : poses)
159 {
160 lineMarker.points.push_back(pose.pose.position);
161 }
162
163 markers_.markers.push_back(lineMarker);
164 }
165
166 markersReady_ = true;
167 enabled_ = true;
168
169 RCLCPP_INFO(
170 getLogger(), "[CpTrajectoryVisualizer] Created %lu markers", markers_.markers.size());
171 }
172
176 inline void clearMarkers()
177 {
178 std::lock_guard<std::mutex> guard(markersMutex_);
179
180 RCLCPP_INFO(getLogger(), "[CpTrajectoryVisualizer] Clearing markers");
181
182 // Send delete action for all markers
183 for (auto & marker : markers_.markers)
184 {
185 marker.action = visualization_msgs::msg::Marker::DELETE;
186 }
187
188 if (!markers_.markers.empty())
189 {
190 markersPub_->publish(markers_);
191 }
192
193 markers_.markers.clear();
194 markersReady_ = false;
195 enabled_ = false;
196 }
197
201 inline void setColor(float r, float g, float b, float a = 1.0)
202 {
203 markerColor_[0] = r;
204 markerColor_[1] = g;
205 markerColor_[2] = b;
206 markerColor_[3] = a;
207 }
208
212 inline void setScale(double scale) { markerScale_ = scale; }
213
217 inline void setEnabled(bool enabled)
218 {
219 std::lock_guard<std::mutex> guard(markersMutex_);
220 enabled_ = enabled;
221 }
222
226 inline bool isEnabled() const { return enabled_; }
227
228private:
229 rclcpp::Publisher<visualization_msgs::msg::MarkerArray>::SharedPtr markersPub_;
230 visualization_msgs::msg::MarkerArray markers_;
231 std::mutex markersMutex_;
232
233 bool markersReady_ = false;
234 bool enabled_ = false;
235
236 // Marker appearance
237 double markerScale_ = 0.02; // 2cm spheres
238 float markerColor_[4] = {0.0f, 1.0f, 0.0f, 0.8f}; // Green, slightly transparent
239
240 // Publishing rate control
242 rclcpp::Time lastPublishTime_;
243};
244
245} // namespace cl_moveit2z
Component for visualizing trajectories as RViz markers.
CpTrajectoryVisualizer(double publishRate=10.0)
Constructor.
void setEnabled(bool enabled)
Enable/disable marker publishing.
void setColor(float r, float g, float b, float a=1.0)
Set marker color (RGBA values 0.0-1.0)
visualization_msgs::msg::MarkerArray markers_
rclcpp::Publisher< visualization_msgs::msg::MarkerArray >::SharedPtr markersPub_
void onInitialize() override
Initialize the marker publisher.
void setTrajectory(const std::vector< geometry_msgs::msg::PoseStamped > &poses, const std::string &ns="trajectory")
Set trajectory to visualize.
virtual ~CpTrajectoryVisualizer()=default
bool isEnabled() const
Check if visualizer is enabled.
void update() override
Periodic update - publishes markers if enabled.
void setScale(double scale)
Set marker scale (sphere diameter in meters)
rclcpp::Logger getLogger() const
rclcpp::Node::SharedPtr getNode()