Visualization.cpp
Go to the documentation of this file.
1#include "Visualization.h"
2
3#include <chrono>
4#include <cstddef>
5#include <mutex>
6#include <thread>
7
11
16
19
21{
22
24 params(params), arviz(arviz)
25 {
26 robotVis = viz::Robot("robot").file(params.robotModelFileName.serialize().package,
27 params.robotModelFileName.serialize().path);
28 robotVis.useCollisionModel();
29 robotVis.hide();
30 robotVis.overrideColor(viz::Color::white(0));
31
32
33 arviz.commitLayerContaining("target", robotVis);
34 }
35
36 void
38 {
39 // TODO(fabian.reister): more finegrained locking
40 std::lock_guard g{paramsMutex};
41
42 if (!params.enableVisualization)
43 {
44 return;
45 }
46
47 if (trajectory.poses().size() < 2)
48 {
49 ARMARX_WARNING << "Unable to visualize path with less than two segments";
50 return;
51 }
52
53 // viz::Layer pathLayer = arviz.layer("path");
54 // for (size_t i = 1; i < path.size(); i++)
55 // {
56 // Eigen::Vector3f prevPose = Eigen::Vector3f::Zero();
57 // prevPose.head<2>() = path.at(i - 1).head<2>();
58 // Eigen::Vector3f currentPose = Eigen::Vector3f::Zero();
59 // currentPose.head<2>() = path.at(i).head<2>();
60 // viz::Arrow a = viz::Arrow("path_segment_" + std::to_string(i));
61 // a.color(viz::Color::orange());
62 // a.width(25);
63 // a.fromTo(prevPose, currentPose);
64 // pathLayer.add(a);
65 // }
66
67 // arviz.commit({pathLayer});
68
69 visualizeTrajectory(trajectory);
70 }
71
72 void
73 Visualization::visualizeTrajectory(const core::GlobalTrajectory& trajectory)
74 {
76 {
77 return;
78 }
79
80 viz::Layer robotLayer = arviz.layer("trajectory");
81
82 robotVis.overrideColor(viz::Color::white(params.alphaValue));
83 robotVis.show();
84 robotLayer.add(robotVis);
85
86 const auto resampledTrajectory = trajectory.resample(10);
87 // const auto duration = trajectory.duration(core::VelocityInterpolation::LastWaypoint);
88
89 const float speedup = 1.0;
90
91 ARMARX_INFO << "Visualizing trajectory";
92
93 const auto points = resampledTrajectory.points();
94 for (size_t i = 0; i < (points.size() - 1); i++)
95 {
96 const auto& wp = points.at(i);
97 robotVis.pose(wp.waypoint.pose.matrix());
98
99 std::lock_guard<std::mutex> lock(mutex);
100 arviz.commit({robotLayer});
101
102 const float distance =
103 (wp.waypoint.pose.translation() - points.at(i + 1).waypoint.pose.translation())
104 .norm();
105
106 ARMARX_CHECK(wp.velocity > 0.F);
107 const float velocity = 200;
108 const int dt = static_cast<int>(speedup * distance / velocity); // [ms]
109
110 std::this_thread::sleep_for(std::chrono::milliseconds(dt));
111 }
112
113 ARMARX_DEBUG << "Done visualizing trajectory";
114
115 robotVis.hide();
116
117 std::lock_guard<std::mutex> lock(mutex);
118 arviz.commit({robotLayer});
119 }
120
121 void
122 Visualization::asyncHideDelayed()
123 {
124 std::thread{
125 [this]
126 {
127 std::this_thread::sleep_for(std::chrono::seconds(params.hideDelaySeconds));
128
129 std::lock_guard<std::mutex> lock(mutex);
130
131 if (lastUpdate <= (TimeUtil::GetTime().toSeconds() - params.hideDelaySeconds))
132 {
133 robotVis.hide();
134 // arviz.commitLayerContaining("target", robotVis);
135 arviz.commit(arviz.layer("target"));
136 arviz.commit(arviz.layer("path"));
137 }
138 }}
139 .detach();
140 }
141
142 void
144 {
145 // TODO(fabian.reister): more finegrained locking
146 std::lock_guard g{paramsMutex};
147
148 if (!params.enableVisualization)
149 {
150 return;
151 }
152
153 robotVis.overrideColor(viz::Color::green(255, params.alphaValue));
154 {
155 std::lock_guard<std::mutex> lock(mutex);
156 arviz.commitLayerContaining("target", robotVis);
157 }
158 asyncHideDelayed();
159 }
160
161 void
163 {
164 // TODO(fabian.reister): more finegrained locking
165 std::lock_guard g{paramsMutex};
166
167 if (!params.enableVisualization)
168 {
169 return;
170 }
171 robotVis.overrideColor(viz::Color::red(255, params.alphaValue));
172 {
173 std::lock_guard<std::mutex> lock(mutex);
174 arviz.commitLayerContaining("target", robotVis);
175 }
176
177 asyncHideDelayed();
178 }
179
180 void
182 {
183 // TODO(fabian.reister): more finegrained locking
184 std::lock_guard g{paramsMutex};
185
186 if (!params.enableVisualization)
187 {
188 return;
189 }
190
191 robotVis.show();
192 robotVis.overrideColor(viz::Color::azure(128, params.alphaValue));
193 robotVis.pose(m.matrix());
194
195 std::lock_guard<std::mutex> lock(mutex);
196 arviz.commitLayerContaining("target", robotVis);
197
198 lastUpdate = TimeUtil::GetTime().toSeconds();
199 }
200
201
202} // namespace armarx::navigation::util
constexpr T dt
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition TimeUtil.cpp:42
void visualize(const core::GlobalTrajectory &trajectory)
Visualization(armarx::viz::Client &arviz, const Params &params)
CommitResult commit(StagedCommit const &commit)
Definition Client.cpp:89
DerivedT & pose(Eigen::Matrix4f const &pose)
Definition ElementOps.h:176
Robot & useCollisionModel()
Definition Robot.h:34
Robot & file(std::string const &project, std::string const &filename)
Definition Robot.h:16
Robot & overrideColor(Color c)
Definition Robot.h:50
Layer layer(std::string const &name) const override
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
Eigen::Isometry3f Pose
Definition basic_types.h:31
This file is part of ArmarX.
Definition geometry.cpp:13
double distance(const Point &a, const Point &b)
Definition point.hpp:95
void add(ElementT const &element)
Definition Layer.h:31