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 namespace armarx
30 {
33  {
34  }
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
105  {
106  }
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
armarx::SimpleRunningTask
Usage:
Definition: TaskUtil.h:85
armarx::viz::Client::commit
CommitResult commit(StagedCommit const &commit)
Definition: Client.cpp:89
armarx::RobotToArViz::onDisconnectComponent
void onDisconnectComponent() override
Definition: RobotToArViz.cpp:97
armarx::viz::Robot::file
Robot & file(std::string const &project, std::string const &filename)
Definition: Robot.h:16
armarx::RemoteGui::Client::GridLayout::add
GridLayout & add(Widget const &child, Pos pos, Span span=Span{1, 1})
Definition: Widgets.cpp:438
armarx::RobotToArVizPropertyDefinitions
Property definitions of RobotToArViz.
Definition: RobotToArViz.h:40
armarx::RobotToArViz::getDefaultName
std::string getDefaultName() const override
Definition: RobotToArViz.cpp:57
armarx::viz::Layer::add
void add(ElementT const &element)
Definition: Layer.h:31
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:405
armarx::RobotStateComponentPluginUser::synchronizeLocalClone
bool synchronizeLocalClone(const VirtualRobot::RobotPtr &robot) const
Definition: RobotStateComponentPlugin.cpp:470
armarx::viz::Robot::useFullModel
Robot & useFullModel()
Definition: Robot.h:42
visionx::voxelgrid::Label
uint32_t Label
Type of an object label.
Definition: types.h:6
armarx::RemoteGui::Client::GridLayout
Definition: Widgets.h:186
armarx::RobotToArViz::onExitComponent
void onExitComponent() override
Definition: RobotToArViz.cpp:104
armarx::viz::Robot::joints
Robot & joints(std::map< std::string, float > const &values)
Definition: Robot.h:74
armarx::RobotToArViz::createRemoteGuiTab
void createRemoteGuiTab()
Definition: RobotToArViz.cpp:109
armarx::armem::human::Robot
@ Robot
Definition: util.h:17
armarx::RobotStateComponentPluginUser::hasRobot
bool hasRobot(const std::string &id) const
Definition: RobotStateComponentPlugin.cpp:399
armarx::RobotToArViz::onConnectComponent
void onConnectComponent() override
Definition: RobotToArViz.cpp:69
armarx::viz::Robot::useCollisionModel
Robot & useCollisionModel()
Definition: Robot.h:34
armarx::viz::Pose
Definition: Elements.h:178
armarx::viz::Robot
Definition: Robot.h:10
Metronome.h
armarx::LightweightRemoteGuiComponentPluginUser::RemoteGui_startRunningTask
void RemoteGui_startRunningTask()
Definition: LightweightRemoteGuiComponentPlugin.cpp:119
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:69
RobotToArViz.h
armarx::viz::ElementOps::pose
DerivedT & pose(Eigen::Matrix4f const &pose)
Definition: ElementOps.h:176
armarx::ArVizComponentPluginUser::arviz
armarx::viz::Client arviz
Definition: ArVizComponentPlugin.h:42
armarx::PropertyUser::getProperty
Property< PropertyType > getProperty(const std::string &name)
Property creation and retrieval.
Definition: PropertyUser.h:180
armarx::LightweightRemoteGuiComponentPluginUser::RemoteGui_createTab
void RemoteGui_createTab(std::string const &name, RemoteGui::Client::Widget const &rootWidget, RemoteGui::Client::Tab *tab)
Definition: LightweightRemoteGuiComponentPlugin.cpp:103
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:34
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:108
armarx::core::time::Frequency::Hertz
static Frequency Hertz(std::int64_t hertz)
Definition: Frequency.cpp:20
armarx::viz::Client::layer
Layer layer(std::string const &name) const
Definition: Client.cpp:80
armarx::viz::Layer
Definition: Layer.h:12
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::RobotToArViz::RemoteGui_update
void RemoteGui_update() override
Definition: RobotToArViz.cpp:129
armarx::RobotToArVizPropertyDefinitions::RobotToArVizPropertyDefinitions
RobotToArVizPropertyDefinitions(std::string prefix)
Definition: RobotToArViz.cpp:31
armarx::RobotToArViz::onInitComponent
void onInitComponent() override
Definition: RobotToArViz.cpp:63