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