RobotToArViz.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package RobotAPI::ArmarXObjects::RobotToArViz
17 * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
18 * @date 2020
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23#include "RobotToArViz.h"
24
25#include <filesystem>
26
28
29namespace armarx
30{
35
38 {
41
42 // defs->optional(updateFrequency, "updateFrequency", "Target number of updates per second.");
43 defs->defineOptionalProperty(
44 "updateFrequency", updateFrequencyHz, "Target number of updates per second.");
45 defs->optional(gui.useCollisionModel,
46 "UseCollisionModel",
47 "Use the collision model for visualization");
48
49 defs->optional(gui.showRobotNodeFrames,
50 "ShowRobotNodeFrames",
51 "If true, show frames of robot nodes (can be changed in RemoteGui).");
52
53 return defs;
54 }
55
56 std::string
58 {
59 return "RobotToArViz";
60 }
61
62 void
64 {
65 getProperty(updateFrequencyHz, "updateFrequency");
66 }
67
68 void
70 {
71 // Load robot.
72 if (!RobotState::hasRobot(robotName))
73 {
74 this->robot = RobotState::addRobot(robotName,
75 VirtualRobot::RobotIO::RobotDescription::eStructure);
76 }
77
78 // Initialize robot visu element.
79 this->robotViz = viz::Robot(robot->getName());
80
81 // Set file location. Try to use ArmarX data directory.
82
83 if (!trySetFilePathFromDataDir(this->robotViz, robot->getFilename()))
84 {
85 // Use absolute path.
86 this->robotViz.file("", robot->getFilename());
87 }
88
91
92 task = new SimpleRunningTask<>([this]() { this->updateRobotRun(); });
93 task->start();
94 }
95
96 void
98 {
99 task->stop();
100 task = nullptr;
101 }
102
103 void
107
108 void
110 {
111 using namespace RemoteGui::Client;
112
113 GridLayout root;
114 int row = 0;
115 {
116 tab.showRobotNodeFrames.setValue(gui.showRobotNodeFrames);
117 root.add(Label("Show Robot Node Frames"), {row, 0})
118 .add(tab.showRobotNodeFrames, {row, 1});
119 row += 1;
120
121 tab.useCollisionModel.setValue(gui.useCollisionModel);
122 root.add(Label("Use Collision Model"), {row, 0}).add(tab.useCollisionModel, {row, 1});
123 row += 1;
124 }
125 RemoteGui_createTab(getName(), root, &tab);
126 }
127
128 void
130 {
131 if (tab.showRobotNodeFrames.hasValueChanged())
132 {
133 std::scoped_lock lock(guiMutex);
134 gui.showRobotNodeFrames = tab.showRobotNodeFrames.getValue();
135 }
136 if (tab.useCollisionModel.hasValueChanged())
137 {
138 std::scoped_lock lock(guiMutex);
139 gui.useCollisionModel = tab.useCollisionModel.getValue();
140 }
141 }
142
143 void
144 RobotToArViz::updateRobotRun()
145 {
146 Metronome metronome(Frequency::Hertz(updateFrequencyHz));
147 while (task and not task->isStopped())
148 {
149 updateRobotOnce();
150
151 metronome.waitForNextTick();
152 }
153 }
154
155 void
156 RobotToArViz::updateRobotOnce()
157 {
159
160 Gui gui;
161 {
162 std::scoped_lock lock(guiMutex);
163 gui = this->gui;
164 }
165
166 std::vector<viz::Layer> layers;
167
168 robotViz.joints(robot->getConfig()->getRobotNodeJointValueMap())
169 .pose(robot->getGlobalPose());
170
171 if (gui.useCollisionModel)
172 {
173 robotViz.useCollisionModel();
174 }
175 else
176 {
177 robotViz.useFullModel();
178 }
179
180 viz::Layer& layerRobot = layers.emplace_back(arviz.layer(robot->getName()));
181 layerRobot.add(robotViz);
182
183
184 // Still commit layer if disabled to clear it.
185 viz::Layer& layerFrames = layers.emplace_back(arviz.layer(robot->getName() + " Frames"));
186 if (gui.showRobotNodeFrames)
187 {
188 for (const auto& node : robot->getRobotNodes())
189 {
190 layerFrames.add(viz::Pose(node->getName()).pose(node->getGlobalPose()));
191 }
192 }
193
194 arviz.commit(layers);
195 }
196
197 bool
198 RobotToArViz::trySetFilePathFromDataDir(viz::Robot& robotViz, const std::string& absolutePath)
199 {
200 // Set file location. Try to use ArmarX data directory.
201 std::filesystem::path filepath = absolutePath;
202
203 for (auto it = filepath.begin(); it != filepath.end(); ++it)
204 {
205 if (*it == "data")
206 {
207 auto jt = it;
208 ++jt;
209 std::filesystem::path localPath = *jt;
210 ++jt;
211 for (; jt != filepath.end(); ++jt)
212 {
213 localPath /= *jt;
214 }
215
216 --it;
217 std::string package = *it;
218
219 robotViz.file(package, localPath.string());
220
221 return true;
222 }
223 }
224 return false;
225 }
226} // namespace armarx
int Label(int n[], int size, int *curLabel, MiscLib::Vector< std::pair< int, size_t > > *labels)
Definition Bitmap.cpp:801
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
Definition Component.cpp:46
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
static Frequency Hertz(std::int64_t hertz)
Definition Frequency.cpp:20
std::string getName() const
Retrieve name of object.
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
bool synchronizeLocalClone(const VirtualRobot::RobotPtr &robot) const
VirtualRobot::RobotPtr addRobot(const std::string &id, const VirtualRobot::RobotPtr &robot, const VirtualRobot::RobotNodeSetPtr &rns={}, const VirtualRobot::RobotNodePtr &node={})
Property definitions of RobotToArViz.
RobotToArVizPropertyDefinitions(std::string prefix)
void onInitComponent() override
void onDisconnectComponent() override
void RemoteGui_update() override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void onConnectComponent() override
void onExitComponent() override
std::string getDefaultName() const override
bool isStopped()
Retrieve whether stop() has been called.
Simple rate limiter for use in loops to maintain a certain frequency given a clock.
Definition Metronome.h:57
Robot & file(std::string const &project, std::string const &filename)
Definition Robot.h:16
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
SimpleRunningTask(Ts...) -> SimpleRunningTask< std::function< void(void)> >
void RemoteGui_createTab(std::string const &name, RemoteGui::Client::Widget const &rootWidget, RemoteGui::Client::Tab *tab)
GridLayout & add(Widget const &child, Pos pos, Span span=Span{1, 1})
Definition Widgets.cpp:438
void add(ElementT const &element)
Definition Layer.h:31