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
39
41
42namespace armarx
43{
44
47 {
50
51 defs->component(simulator);
52
53 defs->optional(p.cycleTime, "cycleTime");
54 defs->optional(p.cycleTimeUpdateObjectsInSimulator, "cycleTimeUpdateObjectList");
55 defs->optional(p.objects, "objects", "The objects to (fake) localize.");
56
57 return defs;
58 }
59
61 {
62 addPlugin(articulatedObjectWriterPlugin);
63 addPlugin(articulatedObjectReaderPlugin);
64 }
65
66 void
70
71 void
73 {
75
76 ARMARX_INFO << "Starting `localizeArticulatedObjects` task";
78 this,
80 p.cycleTime,
81 false,
82 "localizeArticulatedObjects");
83 task->start();
84 task->setDelayWarningTolerance(100);
85
86 ARMARX_INFO << "Starting `setupObjects` task";
88 this,
90 p.cycleTimeUpdateObjectsInSimulator,
91 false,
92 "setupObjects");
93 taskUpdateObjects->start();
94 taskUpdateObjects->setDelayWarningTolerance(100);
95 }
96
97 void
102
103 void
107
108 std::string
113
114 std::string
116 {
117 return "ArticulatedObjectLocalizerDynamicSimulation";
118 }
119
120 void
122 {
123 const auto descriptions = articulatedObjectReaderPlugin->get().queryDescriptions(
124 armarx::DateTime::Now(), std::nullopt);
125
126 std::unordered_map<std::string, std::shared_ptr<VirtualRobot::Robot>> newKnownObjects;
127
128 // read access to shared variable "knownObjects"
129 std::unique_lock lck{knownObjectsMtx};
130 std::unordered_map<std::string, std::shared_ptr<VirtualRobot::Robot>> oldKnownObjects =
131 knownObjects;
132 lck.unlock();
133
134 const auto tryRegister = [&](const std::string& name)
135 {
136 ARMARX_DEBUG << "Checking element '" << name << "' ...";
137
138 const auto isKnownObject = [&name, this](const auto description) -> bool
139 {
140 if (description.name == name)
141 {
142 return true;
143 }
144
145 const auto splits = simox::alg::split(description.name, "/");
146 if (not splits.empty())
147 {
148 if (splits.back() == name)
149 {
150 ARMARX_DEBUG << "Relaxed filter successful: Object description '"
151 << description.name << "' matches '" << name;
152 return true;
153 }
154 }
155
156 return false;
157 };
158
159 const auto it = std::find_if(descriptions.begin(), descriptions.end(), isKnownObject);
160 if (it == descriptions.end())
161 {
162 ARMARX_DEBUG << "Element '" << name << "' is either a robot or an unknown object";
163 return;
164 }
165
166 ARMARX_DEBUG << "Found articulated object '" << name << "'";
167
168 // Avoid loading of already existing robots as it takes some time ...
169 if (oldKnownObjects.count(name) > 0)
170 {
171 newKnownObjects[name] = oldKnownObjects.at(name);
172 return;
173 }
174
175 // New robot. Load it.
176 const auto robot = VirtualRobot::RobotIO::loadRobot(
177 ArmarXDataPath::resolvePath(it->xml.serialize().path),
178 VirtualRobot::RobotIO::eStructure);
179
180 if (robot)
181 {
182 ARMARX_CHECK(newKnownObjects.count(name) == 0)
183 << "At the moment, only one object per class supported.";
184 robot->setName(robot->getName() +
185 "/0"); // mark object as instance '0', similar to scene snapshot
186 newKnownObjects[name] = robot;
187 }
188 };
189
190 // robots can be either active or passive
191 const auto robots = simulator->getRobotNames();
192 std::for_each(robots.begin(), robots.end(), tryRegister);
193
194 {
195 std::lock_guard g{knownObjectsMtx};
196 knownObjects = newKnownObjects;
197 }
198 }
199
201 convert(const VirtualRobot::Robot& obj, const armem::Time& timestamp)
202 {
203 ARMARX_DEBUG << "Filename is " << obj.getFilename();
204
206 .description = {.name = obj.getType(),
208 {"PriorKnowledgeData"}, obj.getFilename()),
209 obj.getFilename()),
210 .visualization = {},
211 .info = {}},
212 .instance = "", // TODO(fabian.reister):
213 .config =
214 {
215 .timestamp = timestamp,
216 .globalPose = Eigen::Isometry3f(obj.getRootNode()->getGlobalPose()),
217 .jointMap = obj.getJointValues(),
218 .proprioception = std::nullopt,
219 },
220 .timestamp = timestamp,
221 };
222 }
223
224 void
226 {
227 // remove objects if the no longer exist in the scene
228 // knownObjects.erase(std::remove_if(knownObjects.begin(), knownObjects.end(), [&](const auto&p) -> bool{
229 // const auto& name = p.first;
230 // if(not simulator->hasRobot(name))
231 // {
232 // ARMARX_INFO << "Articulated object '" << name << "' was removed from the scene.";
233 // return true;
234 // }
235
236 // return false;
237 // }), knownObjects.end());
238 std::lock_guard g{knownObjectsMtx};
239
240 for (const auto& [name, robot] : knownObjects)
241 {
242 // it is possible that the object is no longer in the scene
243 const PoseBasePtr poseBase = [this, &name]() -> PoseBasePtr
244 {
245 try
246 {
247 const auto robotPose = simulator->getRobotPose(name);
248 return robotPose;
249 }
250 catch (...)
251 {
252 return nullptr;
253 }
254 }();
255
256
257 if (not poseBase)
258 {
260 << "No articulatd object pose from simulator for object " << name
261 << " ...";
262 continue;
263 }
264
265 robot->setGlobalPose(PosePtr::dynamicCast(poseBase)->toEigen());
266
267 const auto jointMap = simulator->getRobotJointAngles(name);
268 robot->setJointValues(jointMap);
269
272 obj.instance =
273 static_cast<std::string>("0"); // we assume that there is only one object of a type
274
275 ARMARX_DEBUG << "Publishing new state for object `" << name << "`, instance id: `"
276 << obj.instance << "`";
277
278 articulatedObjectWriterPlugin->get().store(obj, true);
279 }
280 }
281
282
284
285} // namespace armarx
std::string timestamp()
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
static std::string getProject(const std::vector< std::string > &projects, const std::string &relativeFilename)
static std::string resolvePath(const std::string &path, bool verbose=true)
Resolves environment variables and home paths and tries to make path absolute.
Brief description of class ArticulatedObjectLocalizerDynamicSimulation.
std::string getDefaultName() const override
Retrieve default name of component.
Default component property definition container.
Definition Component.h:70
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
static DateTime Now()
Definition DateTime.cpp:51
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:99
PluginT * addPlugin(const std::string prefix="", ParamsT &&... params)
The periodic task executes one thread method repeatedly using the time period specified in the constr...
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
armarx::armem::robot_state::Robot ArticulatedObject
Definition types.h:140
armarx::core::time::DateTime Time
This file offers overloads of toIce() and fromIce() functions for STL container types.
armem::articulated_object::ArticulatedObject convert(const VirtualRobot::Robot &obj, const armem::Time &timestamp)
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Eigen::Vector3f toEigen(const pcl::PointXYZ &pt)