Component.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 ArmarXSimulation::ArmarXObjects::object_memory_to_simulation
17  * @author Fabian Reister ( fabian dot reister at kit dot edu )
18  * @date 2022
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 
24 #include "Component.h"
25 
28 
30 
31 // Include headers you only need in function definitions in the .cpp.
32 
33 // #include <Eigen/Core>
34 
35 // #include <SimoxUtility/color/Color.h>
36 
37 
39 {
40 
41  const std::string Component::defaultName = "ObjectMemoryToSimulation";
42 
43 
46  {
49 
50  // Publish to a topic (passing the TopicListenerPrx).
51  // def->topic(myTopicListener);
52 
53  // Subscribe to a topic (passing the topic name).
54  // def->topic<PlatformUnitListener>("MyTopic");
55 
56  // Use (and depend on) another component (passing the ComponentInterfacePrx).
57  def->component(simulator);
58 
59 
60  // Add a required property. (The component won't start without a value being set.)
61  def->optional(properties.objectPoseProviderName, "p.objectPoseProviderName");
62 
63  // Add an optionalproperty.
64  // def->optional(properties.boxLayerName, "p.box.LayerName", "Name of the box layer in ArViz.");
65  // def->optional(properties.numBoxes, "p.box.Number", "Number of boxes to draw in ArViz.");
66 
67  return def;
68  }
69 
70 
71  void
73  {
74  // Topics and properties defined above are automagically registered.
75 
76  // Keep debug observer data until calling `sendDebugObserverBatch()`.
77  // (Requies the armarx::DebugObserverComponentPluginUser.)
78  // setDebugObserverBatchModeEnabled(true);
79  }
80 
81 
82  void
84  {
85  // Do things after connecting to topics and components.
86 
87  /* (Requies the armarx::DebugObserverComponentPluginUser.)
88  // Use the debug observer to log data over time.
89  // The data can be viewed in the ObserverView and the LivePlotter.
90  // (Before starting any threads, we don't need to lock mutexes.)
91  {
92  setDebugObserverDatafield("numBoxes", properties.numBoxes);
93  setDebugObserverDatafield("boxLayerName", properties.boxLayerName);
94  sendDebugObserverBatch();
95  }
96  */
97 
98  /* (Requires the armarx::ArVizComponentPluginUser.)
99  // Draw boxes in ArViz.
100  // (Before starting any threads, we don't need to lock mutexes.)
101  drawBoxes(properties, arviz);
102  */
103 
104  /* (Requires the armarx::LightweightRemoteGuiComponentPluginUser.)
105  // Setup the remote GUI.
106  {
107  createRemoteGuiTab();
108  RemoteGui_startRunningTask();
109  }
110  */
111 
112  synchronizeSimulator();
113  }
114 
115 
116  void
118  {
119  }
120 
121 
122  void
124  {
125  }
126 
127 
128  std::string
130  {
131  return Component::defaultName;
132  }
133 
134 
135  std::string
137  {
138  return Component::defaultName;
139  }
140 
142  {
143  return {packageFileLocation.package, packageFileLocation.relativePath};
144  }
145 
146  void Component::synchronizeSimulator(const ::Ice::Current&)
147  {
148  ARMARX_INFO << "Synchronizing memory and simulator";
149 
150  // TODO enable list for provider
151  const std::vector<armarx::objpose::ObjectPose> objectPoses = (properties.objectPoseProviderName.empty()) ? getObjectPoses() : getObjectPosesByProvider(properties.objectPoseProviderName);
152 
153  ARMARX_INFO << "Found " << objectPoses.size() << " objects";
154 
155  for (const auto& objectPose : objectPoses)
156  {
157  ARMARX_INFO << objectPose.objectID.str();
158 
159  ObjectFinder finder;
160  const auto objectInfo = finder.findObject(objectPose);
161  ARMARX_CHECK(objectInfo.has_value());
162 
163  const auto globalPoseIce = armarx::toIce(objectPose.objectPoseGlobal);
164  const auto instanceName = objectPose.objectID.str();
165 
166  const auto articulatedModelFile = objectInfo->getArticulatedModel();
167  if (articulatedModelFile.has_value())
168  {
169  ARMARX_INFO << "Found robot: " << articulatedModelFile->absolutePath;
170  // only works on the same machine
171  const auto robotName = simulator->addRobotFromFile(toPackagePath(articulatedModelFile.value()).serialize());
172 
173  simulator->setRobotPose(robotName, globalPoseIce);
174  // simulator->setRobotJointAngles(objectPose.objectJointValues);
175  }
176 
177  else
178  {
179  ARMARX_INFO << "Found object: " << toPackagePath(objectInfo->simoxXML()).toSystemPath();
180  const armarx::PackagePath packagePath(toPackagePath(objectInfo->simoxXML()));
181  simulator->addObjectFromFile(
182  packagePath.serialize(), instanceName, globalPoseIce, objectPose.isStatic);
183  }
184  }
185  }
186 
187 
188  /* (Requires the armarx::LightweightRemoteGuiComponentPluginUser.)
189  void
190  Component::createRemoteGuiTab()
191  {
192  using namespace armarx::RemoteGui::Client;
193 
194  // Setup the widgets.
195 
196  tab.boxLayerName.setValue(properties.boxLayerName);
197 
198  tab.numBoxes.setValue(properties.numBoxes);
199  tab.numBoxes.setRange(0, 100);
200 
201  tab.drawBoxes.setLabel("Draw Boxes");
202 
203  // Setup the layout.
204 
205  GridLayout grid;
206  int row = 0;
207  {
208  grid.add(Label("Box Layer"), {row, 0}).add(tab.boxLayerName, {row, 1});
209  ++row;
210 
211  grid.add(Label("Num Boxes"), {row, 0}).add(tab.numBoxes, {row, 1});
212  ++row;
213 
214  grid.add(tab.drawBoxes, {row, 0}, {2, 1});
215  ++row;
216  }
217 
218  VBoxLayout root = {grid, VSpacer()};
219  RemoteGui_createTab(getName(), root, &tab);
220  }
221 
222 
223  void
224  Component::RemoteGui_update()
225  {
226  if (tab.boxLayerName.hasValueChanged() || tab.numBoxes.hasValueChanged())
227  {
228  std::scoped_lock lock(propertiesMutex);
229  properties.boxLayerName = tab.boxLayerName.getValue();
230  properties.numBoxes = tab.numBoxes.getValue();
231 
232  {
233  setDebugObserverDatafield("numBoxes", properties.numBoxes);
234  setDebugObserverDatafield("boxLayerName", properties.boxLayerName);
235  sendDebugObserverBatch();
236  }
237  }
238  if (tab.drawBoxes.wasClicked())
239  {
240  // Lock shared variables in methods running in seperate threads
241  // and pass them to functions. This way, the called functions do
242  // not need to think about locking.
243  std::scoped_lock lock(propertiesMutex, arvizMutex);
244  drawBoxes(properties, arviz);
245  }
246  }
247  */
248 
249 
250  /* (Requires the armarx::ArVizComponentPluginUser.)
251  void
252  Component::drawBoxes(const Component::Properties& p, viz::Client& arviz)
253  {
254  // Draw something in ArViz (requires the armarx::ArVizComponentPluginUser.
255  // See the ArVizExample in RobotAPI for more examples.
256 
257  viz::Layer layer = arviz.layer(p.boxLayerName);
258  for (int i = 0; i < p.numBoxes; ++i)
259  {
260  layer.add(viz::Box("box_" + std::to_string(i))
261  .position(Eigen::Vector3f(i * 100, 0, 0))
262  .size(20).color(simox::Color::blue()));
263  }
264  arviz.commit(layer);
265  }
266  */
267 
268 
269  // FIXME enable after migration ARMARX_REGISTER_COMPONENT_EXECUTABLE(Component, Component::GetDefaultName());
270 
271 } // namespace armarx::simulation::components::object_memory_to_simulation
Pose.h
armarx::simulation::components::object_memory_to_simulation::Component::getDefaultName
std::string getDefaultName() const override
Definition: Component.cpp:129
armarx::simulation::components::object_memory_to_simulation::Component::onExitComponent
void onExitComponent() override
Definition: Component.cpp:123
armarx::PackageFileLocation
Definition: ObjectInfo.h:22
armarx::PackageFileLocation::relativePath
std::string relativePath
Relative to the package's data directory.
Definition: ObjectInfo.h:28
armarx::simulation::components::object_memory_to_simulation::Component::onConnectComponent
void onConnectComponent() override
Definition: Component.cpp:83
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::PackagePath::serialize
data::PackagePath serialize() const
Definition: PackagePath.cpp:76
armarx::simulation::components::object_memory_to_simulation
Definition: Component.cpp:38
armarx::toIce
void toIce(std::map< IceKeyT, IceValueT > &iceMap, const boost::container::flat_map< CppKeyT, CppValueT > &cppMap)
Definition: ice_conversions_boost_templates.h:15
armarx::ObjectFinder
Used to find objects in the ArmarX objects repository [1] (formerly [2]).
Definition: ObjectFinder.h:23
armarx::simulation::components::object_memory_to_simulation::Component::onDisconnectComponent
void onDisconnectComponent() override
Definition: Component.cpp:117
armarx::PackagePath::toSystemPath
static std::filesystem::path toSystemPath(const data::PackagePath &pp)
Definition: PackagePath.cpp:54
armarx::simulation::components::object_memory_to_simulation::Component::onInitComponent
void onInitComponent() override
Definition: Component.cpp:72
armarx::PackageFileLocation::package
std::string package
Name of the ArmarX package.
Definition: ObjectInfo.h:25
Component.h
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
Decoupled.h
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::ObjectFinder::findObject
std::optional< ObjectInfo > findObject(const std::string &dataset, const std::string &name) const
Definition: ObjectFinder.cpp:65
armarx::simulation::components::object_memory_to_simulation::toPackagePath
armarx::PackagePath toPackagePath(const armarx::PackageFileLocation &packageFileLocation)
Definition: Component.cpp:141
armarx::simulation::components::object_memory_to_simulation::Component::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: Component.cpp:45
armarx::PackagePath
Definition: PackagePath.h:55
armarx::ObjectPoseClientPluginUser::getObjectPoses
objpose::ObjectPoseSeq getObjectPoses()
Definition: ObjectPoseClientPlugin.cpp:70
armarx::ObjectPoseClientPluginUser::getObjectPosesByProvider
objpose::ObjectPoseSeq getObjectPosesByProvider(const std::string &providerName)
Definition: ObjectPoseClientPlugin.cpp:75
PackagePath.h
armarx::simulation::components::object_memory_to_simulation::Component::GetDefaultName
static std::string GetDefaultName()
Get the component's default name.
Definition: Component.cpp:136