ArticulatedObjectLocalizerDynamicSimulation.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::ArticulatedObjectLocalizerDynamicSimulation
17  * @author Fabian Reister ( fabian dot reister at kit dot edu )
18  * @date 2021
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
24 
25 // Include headers you only need in function definitions in the .cpp.
26 #include <algorithm>
27 #include <mutex>
28 
29 #include <Eigen/Core>
30 
31 #include <SimoxUtility/algorithm/string/string_tools.h>
32 #include <VirtualRobot/Robot.h>
33 #include <VirtualRobot/VirtualRobot.h>
34 #include <VirtualRobot/XML/RobotIO.h>
35 
38 
40 
41 namespace armarx
42 {
43 
46  {
49 
50  defs->component(simulator);
51 
52  defs->optional(p.cycleTime, "cycleTime");
53  defs->optional(p.cycleTimeUpdateObjectsInSimulator, "cycleTimeUpdateObjectList");
54  defs->optional(p.objects, "objects", "The objects to (fake) localize.");
55 
56  return defs;
57  }
58 
60  {
61  addPlugin(articulatedObjectWriterPlugin);
62  addPlugin(articulatedObjectReaderPlugin);
63  }
64 
65  void
67  {
68  }
69 
70  void
72  {
73  setupObjects();
74 
75  ARMARX_INFO << "Starting `localizeArticulatedObjects` task";
77  this,
79  p.cycleTime,
80  false,
81  "localizeArticulatedObjects");
82  task->start();
83  task->setDelayWarningTolerance(100);
84 
85  ARMARX_INFO << "Starting `setupObjects` task";
87  this,
89  p.cycleTimeUpdateObjectsInSimulator,
90  false,
91  "setupObjects");
92  taskUpdateObjects->start();
93  taskUpdateObjects->setDelayWarningTolerance(100);
94  }
95 
96  void
98  {
99  task->stop();
100  }
101 
102  void
104  {
105  }
106 
107  std::string
109  {
110  return "ArticulatedObjectLocalizerDynamicSimulation";
111  }
112 
113  void
115  {
116  const auto descriptions =
117  articulatedObjectReaderPlugin->get().queryDescriptions(armarx::DateTime::Now(), std::nullopt);
118 
119  std::unordered_map<std::string, std::shared_ptr<VirtualRobot::Robot>> newKnownObjects;
120 
121  // read access to shared variable "knownObjects"
122  std::unique_lock lck{knownObjectsMtx};
123  std::unordered_map<std::string, std::shared_ptr<VirtualRobot::Robot>> oldKnownObjects =
124  knownObjects;
125  lck.unlock();
126 
127  const auto tryRegister = [&](const std::string& name)
128  {
129  ARMARX_DEBUG << "Checking element '" << name << "' ...";
130 
131  const auto isKnownObject = [&name, this](const auto description) -> bool
132  {
133  if (description.name == name)
134  {
135  return true;
136  }
137 
138  const auto splits = simox::alg::split(description.name, "/");
139  if (not splits.empty())
140  {
141  if (splits.back() == name)
142  {
143  ARMARX_DEBUG << "Relaxed filter successful: Object description '"
144  << description.name << "' matches '" << name;
145  return true;
146  }
147  }
148 
149  return false;
150  };
151 
152  const auto it = std::find_if(descriptions.begin(), descriptions.end(), isKnownObject);
153  if (it == descriptions.end())
154  {
155  ARMARX_DEBUG << "Element '" << name << "' is either a robot or an unknown object";
156  return;
157  }
158 
159  ARMARX_DEBUG << "Found articulated object '" << name << "'";
160 
161  // Avoid loading of already existing robots as it takes some time ...
162  if (oldKnownObjects.count(name) > 0)
163  {
164  newKnownObjects[name] = oldKnownObjects.at(name);
165  return;
166  }
167 
168  // New robot. Load it.
169  const auto robot = VirtualRobot::RobotIO::loadRobot(
170  ArmarXDataPath::resolvePath(it->xml.serialize().path),
171  VirtualRobot::RobotIO::eStructure);
172 
173  if (robot)
174  {
175  ARMARX_CHECK(newKnownObjects.count(name) == 0)
176  << "At the moment, only one object per class supported.";
177  robot->setName(robot->getName() +
178  "/0"); // mark object as instance '0', similar to scene snapshot
179  newKnownObjects[name] = robot;
180  }
181  };
182 
183  // robots can be either active or passive
184  const auto robots = simulator->getRobotNames();
185  std::for_each(robots.begin(), robots.end(), tryRegister);
186 
187  {
188  std::lock_guard g{knownObjectsMtx};
189  knownObjects = newKnownObjects;
190  }
191  }
192 
194  convert(const VirtualRobot::Robot& obj, const armem::Time& timestamp)
195  {
196  ARMARX_DEBUG << "Filename is " << obj.getFilename();
197 
199  .description = {.name = obj.getType(),
201  {"PriorKnowledgeData"}, obj.getFilename()),
202  obj.getFilename())},
203  .instance = "", // TODO(fabian.reister):
204  .config =
205  {
206  .timestamp = timestamp,
207  .globalPose = Eigen::Affine3f(obj.getRootNode()->getGlobalPose()),
208  .jointMap = obj.getJointValues(),
209  .proprioception = std::nullopt,
210  },
211  .timestamp = timestamp,
212  };
213  }
214 
215  void
217  {
218  // remove objects if the no longer exist in the scene
219  // knownObjects.erase(std::remove_if(knownObjects.begin(), knownObjects.end(), [&](const auto&p) -> bool{
220  // const auto& name = p.first;
221  // if(not simulator->hasRobot(name))
222  // {
223  // ARMARX_INFO << "Articulated object '" << name << "' was removed from the scene.";
224  // return true;
225  // }
226 
227  // return false;
228  // }), knownObjects.end());
229  std::lock_guard g{knownObjectsMtx};
230 
231  for (const auto& [name, robot] : knownObjects)
232  {
233  // it is possible that the object is no longer in the scene
234  const PoseBasePtr poseBase = [this, &name]() -> PoseBasePtr
235  {
236  try
237  {
238  const auto robotPose = simulator->getRobotPose(name);
239  return robotPose;
240  }
241  catch (...)
242  {
243  return nullptr;
244  }
245  }();
246 
247 
248  if (not poseBase)
249  {
251  << "No articulatd object pose from simulator for object " << name
252  << " ...";
253  continue;
254  }
255 
256  robot->setGlobalPose(PosePtr::dynamicCast(poseBase)->toEigen());
257 
258  const auto jointMap = simulator->getRobotJointAngles(name);
259  robot->setJointValues(jointMap);
260 
262  convert(*robot, armarx::DateTime::Now());
263  obj.instance = static_cast<std::string>("0"); // we assume that there is only one object of a type
264 
265  ARMARX_DEBUG << "Publishing new state for object `" << name << "`, instance id: `"
266  << obj.instance << "`";
267 
268  articulatedObjectWriterPlugin->get().store(obj);
269  }
270  }
271 
272 
273 } // namespace armarx
armarx::ArmarXDataPath::resolvePath
static std::string resolvePath(const std::string &path, bool verbose=true)
Resolves environment variables and home paths and tries to make path absolute.
Definition: ArmarXDataPath.cpp:542
armarx::ArticulatedObjectLocalizerDynamicSimulation::setupObjects
void setupObjects()
Definition: ArticulatedObjectLocalizerDynamicSimulation.cpp:114
armarx::ArticulatedObjectLocalizerDynamicSimulation::onInitComponent
void onInitComponent() override
Definition: ArticulatedObjectLocalizerDynamicSimulation.cpp:66
armarx::viz::toEigen
Eigen::Matrix4f toEigen(data::GlobalPose const &pose)
Definition: Interaction.h:46
armarx::ArticulatedObjectLocalizerDynamicSimulation::onDisconnectComponent
void onDisconnectComponent() override
Definition: ArticulatedObjectLocalizerDynamicSimulation.cpp:97
robots
std::vector< VirtualRobot::RobotPtr > robots
Definition: VisualizationRobot.cpp:86
armarx::armem::robot::Robot
Definition: types.h:68
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
Pose.h
armarx::ArticulatedObjectLocalizerDynamicSimulation::ArticulatedObjectLocalizerDynamicSimulation
ArticulatedObjectLocalizerDynamicSimulation()
Definition: ArticulatedObjectLocalizerDynamicSimulation.cpp:59
armarx::ManagedIceObject::addPlugin
PluginT * addPlugin(const std::string prefix="", ParamsT &&...params)
Definition: ManagedIceObject.h:182
armarx::convert
armem::articulated_object::ArticulatedObject convert(const VirtualRobot::Robot &obj, const armem::Time &timestamp)
Definition: ArticulatedObjectLocalizerDynamicSimulation.cpp:194
armarx::ArmarXDataPath::getProject
static std::string getProject(const std::vector< std::string > &projects, const std::string &relativeFilename)
Definition: ArmarXDataPath.cpp:301
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
ArticulatedObjectLocalizerDynamicSimulation.h
armarx::ArticulatedObjectLocalizerDynamicSimulation::onConnectComponent
void onConnectComponent() override
Definition: ArticulatedObjectLocalizerDynamicSimulation.cpp:71
armarx::armem::robot::Robot::description
RobotDescription description
Definition: types.h:70
armarx::ArticulatedObjectLocalizerDynamicSimulation::getDefaultName
std::string getDefaultName() const override
Definition: ArticulatedObjectLocalizerDynamicSimulation.cpp:108
armarx::armem::human::Robot
@ Robot
Definition: util.h:14
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::armem::robot::Robot::instance
std::string instance
Definition: types.h:71
armarx::ArticulatedObjectLocalizerDynamicSimulation::localizeObjects
void localizeObjects()
Definition: ArticulatedObjectLocalizerDynamicSimulation.cpp:216
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
armarx::armem::robot::RobotDescription::name
std::string name
Definition: types.h:21
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
armarx::ArticulatedObjectLocalizerDynamicSimulation::onExitComponent
void onExitComponent() override
Definition: ArticulatedObjectLocalizerDynamicSimulation.cpp:103
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
IceUtil::Handle
Definition: forward_declarations.h:29
armarx::ArticulatedObjectLocalizerDynamicSimulation::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ArticulatedObjectLocalizerDynamicSimulation.cpp:45
armarx::Logging::deactivateSpam
SpamFilterDataPtr deactivateSpam(float deactivationDurationSec=10.0f, const std::string &identifier="", bool deactivate=true) const
disables the logging for the current line for the given amount of seconds.
Definition: Logging.cpp:92
armarx::PeriodicTask
Definition: ArmarXManager.h:70
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
ArmarXDataPath.h
armarx::PackagePath
Definition: PackagePath.h:55
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:36
PackagePath.h