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 
29 
30 namespace armarx
31 {
34  {
35  }
36 
38  {
40 
41  // defs->optional(updateFrequency, "updateFrequency", "Target number of updates per second.");
42  defs->defineOptionalProperty("updateFrequency", updateFrequencyHz, "Target number of updates per second.");
43  defs->optional(gui.useCollisionModel, "UseCollisionModel", "Use the collision model for visualization");
44 
45  defs->optional(gui.showRobotNodeFrames, "ShowRobotNodeFrames",
46  "If true, show frames of robot nodes (can be changed in RemoteGui).");
47 
48  return defs;
49  }
50 
51 
52  std::string RobotToArViz::getDefaultName() const
53  {
54  return "RobotToArViz";
55  }
56 
57 
59  {
60  getProperty(updateFrequencyHz, "updateFrequency");
61  }
62 
63 
65  {
66  // Load robot.
67  if (!RobotState::hasRobot(robotName))
68  {
69  this->robot = RobotState::addRobot(robotName, VirtualRobot::RobotIO::RobotDescription::eStructure);
70  }
71 
72  // Initialize robot visu element.
73  this->robotViz = viz::Robot(robot->getName());
74 
75  // Set file location. Try to use ArmarX data directory.
76 
77  if (!trySetFilePathFromDataDir(this->robotViz, robot->getFilename()))
78  {
79  // Use absolute path.
80  this->robotViz.file("", robot->getFilename());
81  }
82 
85 
86  task = new SimpleRunningTask<>([this]()
87  {
88  this->updateRobotRun();
89  });
90  task->start();
91  }
92 
93 
95  {
96  task->stop();
97  task = nullptr;
98  }
99 
100 
102  {
103  }
104 
106  {
107  using namespace RemoteGui::Client;
108 
109  GridLayout root;
110  int row = 0;
111  {
112  tab.showRobotNodeFrames.setValue(gui.showRobotNodeFrames);
113  root.add(Label("Show Robot Node Frames"), {row, 0}).add(tab.showRobotNodeFrames, {row, 1});
114  row += 1;
115 
116  tab.useCollisionModel.setValue(gui.useCollisionModel);
117  root.add(Label("Use Collision Model"), {row, 0}).add(tab.useCollisionModel, {row, 1});
118  row += 1;
119  }
120  RemoteGui_createTab(getName(), root, &tab);
121  }
122 
124  {
125  if (tab.showRobotNodeFrames.hasValueChanged())
126  {
127  std::scoped_lock lock(guiMutex);
128  gui.showRobotNodeFrames = tab.showRobotNodeFrames.getValue();
129  }
130  if (tab.useCollisionModel.hasValueChanged())
131  {
132  std::scoped_lock lock(guiMutex);
133  gui.useCollisionModel = tab.useCollisionModel.getValue();
134  }
135  }
136 
137 
138  void RobotToArViz::updateRobotRun()
139  {
140  Metronome metronome(Frequency::Hertz(updateFrequencyHz));
141  while (task and not task->isStopped())
142  {
143  updateRobotOnce();
144 
145  metronome.waitForNextTick();
146  }
147  }
148 
149 
150  void RobotToArViz::updateRobotOnce()
151  {
153 
154  Gui gui;
155  {
156  std::scoped_lock lock(guiMutex);
157  gui = this->gui;
158  }
159 
160  std::vector<viz::Layer> layers;
161 
162  robotViz.joints(robot->getConfig()->getRobotNodeJointValueMap())
163  .pose(robot->getGlobalPose());
164 
165  if (gui.useCollisionModel)
166  {
167  robotViz.useCollisionModel();
168  }
169  else
170  {
171  robotViz.useFullModel();
172  }
173 
174  viz::Layer& layerRobot = layers.emplace_back(arviz.layer(robot->getName()));
175  layerRobot.add(robotViz);
176 
177 
178  // Still commit layer if disabled to clear it.
179  viz::Layer& layerFrames = layers.emplace_back(arviz.layer(robot->getName() + " Frames"));
180  if (gui.showRobotNodeFrames)
181  {
182  for (const auto& node : robot->getRobotNodes())
183  {
184  layerFrames.add(viz::Pose(node->getName()).pose(node->getGlobalPose()));
185  }
186  }
187 
188  arviz.commit(layers);
189  }
190 
191 
192  bool RobotToArViz::trySetFilePathFromDataDir(viz::Robot& robotViz, const std::string& absolutePath)
193  {
194  // Set file location. Try to use ArmarX data directory.
195  std::filesystem::path filepath = absolutePath;
196 
197  for (auto it = filepath.begin(); it != filepath.end(); ++it)
198  {
199  if (*it == "data")
200  {
201  auto jt = it;
202  ++jt;
203  std::filesystem::path localPath = *jt;
204  ++jt;
205  for (; jt != filepath.end(); ++jt)
206  {
207  localPath /= *jt;
208  }
209 
210  --it;
211  std::string package = *it;
212 
213  robotViz.file(package, localPath.string());
214 
215  return true;
216  }
217  }
218  return false;
219  }
220 }
armarx::SimpleRunningTask
Usage:
Definition: TaskUtil.h:70
armarx::viz::Client::commit
CommitResult commit(StagedCommit const &commit)
Definition: Client.cpp:80
armarx::RobotToArViz::onDisconnectComponent
void onDisconnectComponent() override
Definition: RobotToArViz.cpp:94
armarx::viz::Robot::file
Robot & file(std::string const &project, std::string const &filename)
Definition: Robot.h:14
armarx::RemoteGui::Client::GridLayout::add
GridLayout & add(Widget const &child, Pos pos, Span span=Span{1, 1})
Definition: Widgets.cpp:412
armarx::RobotToArVizPropertyDefinitions
Property definitions of RobotToArViz.
Definition: RobotToArViz.h:40
armarx::RobotToArViz::getDefaultName
std::string getDefaultName() const override
Definition: RobotToArViz.cpp:52
armarx::viz::Layer::add
void add(ElementT const &element)
Definition: Layer.h:29
armarx::RobotStateComponentPluginUser::addRobot
VirtualRobot::RobotPtr addRobot(const std::string &id, const VirtualRobot::RobotPtr &robot, const VirtualRobot::RobotNodeSetPtr &rns={}, const VirtualRobot::RobotNodePtr &node={})
Definition: RobotStateComponentPlugin.cpp:331
armarx::RobotStateComponentPluginUser::synchronizeLocalClone
bool synchronizeLocalClone(const VirtualRobot::RobotPtr &robot) const
Definition: RobotStateComponentPlugin.cpp:371
armarx::viz::Robot::useFullModel
Robot & useFullModel()
Definition: Robot.h:29
visionx::voxelgrid::Label
uint32_t Label
Type of an object label.
Definition: types.h:7
armarx::RemoteGui::Client::GridLayout
Definition: Widgets.h:186
armarx::RobotToArViz::onExitComponent
void onExitComponent() override
Definition: RobotToArViz.cpp:101
armarx::viz::Robot::joints
Robot & joints(std::map< std::string, float > const &values)
Definition: Robot.h:57
armarx::RobotToArViz::createRemoteGuiTab
void createRemoteGuiTab()
Definition: RobotToArViz.cpp:105
armarx::armem::human::Robot
@ Robot
Definition: util.h:14
armarx::RobotStateComponentPluginUser::hasRobot
bool hasRobot(const std::string &id) const
Definition: RobotStateComponentPlugin.cpp:326
armarx::RobotToArViz::onConnectComponent
void onConnectComponent() override
Definition: RobotToArViz.cpp:64
armarx::viz::Robot::useCollisionModel
Robot & useCollisionModel()
Definition: Robot.h:22
armarx::viz::Pose
Definition: Elements.h:179
armarx::viz::Robot
Definition: Robot.h:9
Metronome.h
armarx::LightweightRemoteGuiComponentPluginUser::RemoteGui_startRunningTask
void RemoteGui_startRunningTask()
Definition: LightweightRemoteGuiComponentPlugin.cpp:110
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
RobotToArViz.h
armarx::viz::ElementOps::pose
DerivedT & pose(Eigen::Matrix4f const &pose)
Definition: ElementOps.h:159
armarx::ArVizComponentPluginUser::arviz
armarx::viz::Client arviz
Definition: ArVizComponentPlugin.h:43
armarx::PropertyUser::getProperty
Property< PropertyType > getProperty(const std::string &name)
Property creation and retrieval.
Definition: PropertyUser.h:179
armarx::LightweightRemoteGuiComponentPluginUser::RemoteGui_createTab
void RemoteGui_createTab(std::string const &name, RemoteGui::Client::Widget const &rootWidget, RemoteGui::Client::Tab *tab)
Definition: LightweightRemoteGuiComponentPlugin.cpp:95
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::core::time::Metronome
Simple rate limiter for use in loops to maintain a certain frequency given a clock.
Definition: Metronome.h:35
armarx::RobotToArViz::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: RobotToArViz.cpp:37
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:107
armarx::core::time::Frequency::Hertz
static Frequency Hertz(std::int64_t hertz)
Definition: Frequency.cpp:23
armarx::viz::Client::layer
Layer layer(std::string const &name) const
Definition: Client.cpp:73
armarx::viz::Layer
Definition: Layer.h:12
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::RobotToArViz::RemoteGui_update
void RemoteGui_update() override
Definition: RobotToArViz.cpp:123
armarx::RobotToArVizPropertyDefinitions::RobotToArVizPropertyDefinitions
RobotToArVizPropertyDefinitions(std::string prefix)
Definition: RobotToArViz.cpp:32
armarx::RobotToArViz::onInitComponent
void onInitComponent() override
Definition: RobotToArViz.cpp:58