MMMSimulation.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2020, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarXSimulation::components::MMMSimulation
19 * @author Andre Meixner ( andre dot meixner at kit dot edu )
20 * @date 2020
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24
25#include "MMMSimulation.h"
26
27#include <Ice/ObjectAdapter.h>
28
29#include <SimoxUtility/math/convert.h>
30#include <VirtualRobot/MathTools.h>
31
37#include <ArmarXCore/interface/observers/VariantBase.h>
40
44
48
52
53#include <MemoryX/interface/components/WorkingMemoryInterface.h>
54
55using namespace armarx;
56
57void
59{
60 usingProxy(getProperty<std::string>("SimulatorName").getValue());
61 if (getProperty<bool>("LoadToMemory"))
62 {
63 usingProxy(getProperty<std::string>("WorkingMemoryName").getValue());
64 }
65
66 trajectoryLoaded = false;
67}
68
69bool
71{
72 return motionData != nullptr;
73}
74
75bool
76MMMSimulation::loadMMMFile(const std::string& filePath,
77 const std::string& projects,
79 const Ice::Current&)
80{
81 if (filePath.empty())
82 {
83 return false;
84 }
85
86 std::unique_lock lock(mmmMutex);
87
88 if (!projects.empty())
89 {
90 std::vector<std::string> proj = armarx::Split(projects, ",;", true, true);
91
92 for (std::string& p : proj)
93 {
94 ARMARX_INFO << "Adding to datapaths of " << p;
96
97 if (!finder.packageFound())
98 {
99 ARMARX_WARNING << "ArmarX Package " << p << " has not been found!";
100 }
101 else
102 {
103 ARMARX_INFO << "Adding to datapaths: " << finder.getDataDir();
105 }
106 }
107 }
108
110
112 if (motionWrapper)
113 {
114 motionData = motionWrapper->getMotionDataByModel(modelFileName);
115 if (motionData)
116 {
117 ARMARX_INFO << "Loaded motion with model " << modelFileName << " from file "
118 << filePath;
120 {
122 }
123 return true;
124 }
125 else
126 {
127 ARMARX_ERROR << "Failed to load motion with model " << modelFileName
128 << ". Valid model names are " << motionWrapper->getModelNames();
129 }
130 }
131 return false;
132}
133
134void
135MMMSimulation::playMotion(const Ice::Current&)
136{
138 {
139 trajectoryPlayer->resetTrajectoryPlayer(true);
140 trajectoryPlayer->startTrajectoryPlayer();
141 }
142}
143
144void
145MMMSimulation::pauseMotion(const Ice::Current&)
146{
148 {
149 trajectoryPlayer->pauseTrajectoryPlayer();
150 }
151}
152
153void
154MMMSimulation::stopMotion(const Ice::Current&)
155{
157 {
158 trajectoryPlayer->stopTrajectoryPlayer();
159 }
160}
161
162void
163MMMSimulation::setLoopBack(bool state, const Ice::Current&)
164{
166 {
167 trajectoryPlayer->setLoopPlayback(state);
168 }
169}
170
171void
173{
175 {
177 {
178 trajectoryPlayer->resetTrajectoryPlayer(true);
179 }
180 TrajectoryBasePtr t = motionData->getJointTrajectory();
182 trajectoryPlayer->loadJointTraj(t);
183 trajectoryPlayer->loadBasePoseTraj(motionData->getPoseTrajectory());
184 trajectoryLoaded = true;
185 }
186}
187
188void
190{
193
194 initTask = new RunningTask<MMMSimulation>(this, &MMMSimulation::initialize);
195 initTask->start();
196}
197
198void
199MMMSimulation::initialize()
200{
201 const std::string name = this->getName();
202 agentName = getProperty<std::string>("AgentName").getValue();
203 const std::string mmmModelFileProject =
204 getProperty<std::string>("RobotFileNameProject").getValue();
205 const std::string mmmModelFilePath = getProperty<std::string>("RobotFileName").getValue();
206 const std::string mmmRobotNodeSetName = getProperty<std::string>("RobotNodeSetName").getValue();
207 const std::string simulatorName = getProperty<std::string>("SimulatorName").getValue();
208 //std::string robotName = getProperty<std::string>("RobotName").getValue();
209 modelFileName = std::filesystem::path(mmmModelFilePath).stem();
210
211 loadMMMFile(getProperty<std::string>("MMMFile").getValue(), "", false);
212 if (hasProperty("Scaling") && motionData)
213 {
214 motionData->setScaling(getProperty<float>("Scaling").getValue());
215 }
216 modelScaling = motionData ? motionData->scaling : getProperty<float>("Scaling").getValue();
217
218
219 Eigen::Vector3f position(getProperty<float>("StartPose.x").getValue(),
220 getProperty<float>("StartPose.y").getValue(),
221 getProperty<float>("StartPose.z").getValue());
222 Eigen::Vector3f orientation(getProperty<float>("StartPose.roll").getValue(),
223 getProperty<float>("StartPose.pitch").getValue(),
224 getProperty<float>("StartPose.yaw").getValue());
225 startPose = simox::math::pos_rpy_to_mat4f(position, orientation);
226 std::string robotName = simulatorPrx->addScaledRobotName(
228 agentName = robotName;
229 simulatorPrx->setRobotPose(robotName, PosePtr(new Pose(startPose)));
230
231 ARMARX_INFO << "Added robot " << robotName << " to simulator";
232
233 const std::string robotStateConfigName = "RobotStateComponent";
234 {
235 Ice::StringSeq pros;
236 std::string packageName("RobotAPI");
237 armarx::CMakePackageFinder finder(packageName);
238 std::string appPath = finder.getBinaryDir() + "/RobotStateComponentRun";
239 pros.push_back(appPath);
240 Ice::PropertiesPtr properties = Ice::createProperties(pros);
241 //Ice::PropertiesPtr properties = getIceProperties()->clone();
242
243 const std::string robotSateConfPre = getConfigDomain() + "." + robotStateConfigName + ".";
244 properties->setProperty(robotSateConfPre + "AgentName", agentName);
245 properties->setProperty(robotSateConfPre + "RobotFileName", mmmModelFilePath);
246 properties->setProperty(robotSateConfPre + "RobotFileNameProject", mmmModelFileProject);
247 properties->setProperty(robotSateConfPre + "RobotNodeSetName", mmmRobotNodeSetName);
248 properties->setProperty(robotSateConfPre + "RobotModelScaling",
249 std::to_string(modelScaling));
250 properties->setProperty(robotSateConfPre + "ObjectName", robotStateConfigName + name);
251 properties->setProperty(getConfigDomain() + ".RobotStateObserver." + "ObjectName",
252 "RobotStateObserver" + name);
253 ARMARX_DEBUG << "creating unit " << robotStateConfigName
254 << " using these properties: " << properties->getPropertiesForPrefix("");
255 IceInternal::Handle<RobotStateComponent> robotStateComponent =
257 properties, robotStateConfigName, getConfigDomain());
258 //robotStateComponent->getSynchronizedRobot(Ice::emptyCurrent);
259 getArmarXManager()->addObject(robotStateComponent);
260 }
261
262 if (getProperty<bool>("LoadToMemory"))
263 {
264 Ice::StringSeq pros;
265 std::string packageName("ArmarXSimulation");
266 armarx::CMakePackageFinder finder(packageName);
267 std::string appPath = finder.getBinaryDir() + "/SelfLocalizationDynamicSimulationAppRun";
268 pros.push_back(appPath);
269 Ice::PropertiesPtr properties = Ice::createProperties(pros);
270 //Ice::PropertiesPtr properties = getIceProperties()->clone();
271
272 const std::string localizationConfigName = "SelfLocalizationDynamicSimulation";
273 const std::string localizationConfPre =
274 getConfigDomain() + "." + localizationConfigName + ".";
275 properties->setProperty(localizationConfPre + "AgentName", agentName);
276 properties->setProperty(localizationConfPre + "RobotStateComponentName",
277 robotStateConfigName + name);
278 properties->setProperty(localizationConfPre + "RobotName", robotName);
279 properties->setProperty(localizationConfPre + "SimulatorName", simulatorName);
280 properties->setProperty(localizationConfPre + "ObjectName", localizationConfigName + name);
281 properties->setProperty(localizationConfPre + "WorkingMemoryName",
282 getProperty<std::string>("WorkingMemoryName").getValue());
283 ARMARX_DEBUG << "creating unit " << localizationConfigName
284 << " using these properties: " << properties->getPropertiesForPrefix("");
285 IceInternal::Handle<SelfLocalizationDynamicSimulation> localizationComponent =
287 properties, localizationConfigName, getConfigDomain());
288 getArmarXManager()->addObject(localizationComponent);
289 }
290
291 std::string kinematicUnitConfigName = "KinematicUnit";
292 {
293 const std::string kinematicUnitConfPre =
294 getConfigDomain() + "." + kinematicUnitConfigName + ".";
295 Ice::PropertiesPtr properties = getIceProperties()->clone();
296 properties->setProperty(kinematicUnitConfPre + "RobotName", robotName);
297 properties->setProperty(kinematicUnitConfPre + "RobotFileName", mmmModelFilePath);
298 properties->setProperty(kinematicUnitConfPre + "RobotFileNameProject", mmmModelFileProject);
299 properties->setProperty(kinematicUnitConfPre + "RobotNodeSetName", mmmRobotNodeSetName);
300 properties->setProperty(kinematicUnitConfPre + "ObjectName",
301 kinematicUnitConfigName + name);
302 properties->setProperty(kinematicUnitConfPre + "SimulatorName", simulatorName);
303 ARMARX_DEBUG << "creating unit " << kinematicUnitConfigName
304 << " using these properties: " << properties->getPropertiesForPrefix("");
305 IceInternal::Handle<KinematicUnit> kinematicUnitComponent =
307 properties, kinematicUnitConfigName, getConfigDomain());
308 kinematicUnitName = kinematicUnitComponent->getName();
309 getArmarXManager()->addObject(kinematicUnitComponent);
310 }
311
312 const std::string robotposeUnitConfigName = "RobotPoseUnit";
313 {
314 const std::string robotposeUnitConfPre =
315 getConfigDomain() + "." + robotposeUnitConfigName + ".";
316 Ice::PropertiesPtr properties = getIceProperties()->clone();
317 properties->setProperty(robotposeUnitConfPre + "RobotName", robotName);
318 properties->setProperty(robotposeUnitConfPre + "ObjectName",
319 robotposeUnitConfigName + name);
320 properties->setProperty(robotposeUnitConfPre + "SimulatorProxyName", simulatorName);
321 ARMARX_DEBUG << "creating unit " << robotposeUnitConfigName
322 << " using these properties: " << properties->getPropertiesForPrefix("");
323 IceInternal::Handle<RobotPoseUnit> robotposecUnitComponent =
325 properties, robotposeUnitConfigName, getConfigDomain());
326 robotPoseUnitName = robotposecUnitComponent->getName();
327 getArmarXManager()->addObject(robotposecUnitComponent);
328 }
329
330 if (motionData)
331 {
332 if (!trajectoryPlayer)
333 {
335 }
337 {
339 if (getProperty<bool>("AutoPlay").getValue())
340 {
341 playMotion();
342 }
343 }
344 else
345 {
347 << "Could not load motion as trajectory player could not be not instanciated";
348 }
349 }
350}
351
352void
354{
355 Eigen::Vector3f position = simox::math::mat4f_to_pos(startPose);
356 Eigen::Vector3f orientation = simox::math::mat4f_to_rpy(startPose);
357 const std::string trajectoryPlayerConfigName = "TrajectoryPlayer";
358 const std::string trajectoryPlayerConfPre =
359 getConfigDomain() + "." + trajectoryPlayerConfigName + ".";
360 Ice::PropertiesPtr properties = getIceProperties()->clone();
361 properties->setProperty(trajectoryPlayerConfPre + "KinematicTopicName", kinematicUnitName);
362 properties->setProperty(trajectoryPlayerConfPre + "KinematicUnitName", kinematicUnitName);
363 properties->setProperty(trajectoryPlayerConfPre + "ObjectName",
364 trajectoryPlayerConfigName + getName());
365 properties->setProperty(trajectoryPlayerConfPre + "LoopPlayback",
366 std::to_string(getProperty<bool>("LoopPlayback").getValue()));
367 properties->setProperty(trajectoryPlayerConfPre + "Offset.x", std::to_string(position(0)));
368 properties->setProperty(trajectoryPlayerConfPre + "Offset.y", std::to_string(position(1)));
369 properties->setProperty(trajectoryPlayerConfPre + "Offset.z", std::to_string(position(2)));
370 properties->setProperty(trajectoryPlayerConfPre + "Offset.roll",
371 std::to_string(orientation(0)));
372 properties->setProperty(trajectoryPlayerConfPre + "Offset.pitch",
373 std::to_string(orientation(1)));
374 properties->setProperty(trajectoryPlayerConfPre + "Offset.yaw", std::to_string(orientation(2)));
375 properties->setProperty(trajectoryPlayerConfPre + "EnableRobotPoseUnit", "true");
376 properties->setProperty(trajectoryPlayerConfPre + "RobotPoseUnitName", robotPoseUnitName);
377 ARMARX_DEBUG << "creating unit " << trajectoryPlayerConfigName
378 << " using these properties: " << properties->getPropertiesForPrefix("");
379 auto trajectoryPlayerComponent = Component::create<TrajectoryPlayer>(
380 properties, trajectoryPlayerConfigName, getConfigDomain());
381 getArmarXManager()->addObject(trajectoryPlayerComponent);
382 trajectoryPlayerName = trajectoryPlayerComponent->getName();
383
386}
387
388void
390{
391 if (simulatorPrx->hasRobot(agentName))
392 {
393 simulatorPrx->removeRobot(agentName);
394 }
395}
396
397void
401
static bool getAbsolutePath(const std::string &relativeFilename, std::string &storeAbsoluteFilename, const std::vector< std::string > &additionalSearchPaths={}, bool verbose=true)
static void addDataPaths(const std::string &dataPathList)
The CMakePackageFinder class provides an interface to the CMake Package finder capabilities.
bool packageFound() const
Returns whether or not this package was found with cmake.
static TPtr create(Ice::PropertiesPtr properties=Ice::createProperties(), const std::string &configName="", const std::string &configDomain="ArmarX")
Factory method for a component.
Definition Component.h:116
std::string getConfigDomain()
Retrieve config domain for this component as set in constructor.
Definition Component.cpp:76
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
Eigen::Matrix4f startPose
void onInitComponent() override
void stopMotion(const Ice::Current &=Ice::emptyCurrent) override
void pauseMotion(const Ice::Current &=Ice::emptyCurrent) override
std::string trajectoryPlayerName
bool isMotionLoaded(const Ice::Current &=Ice::emptyCurrent) override
std::string kinematicUnitName
void onDisconnectComponent() override
TrajectoryPlayerInterfacePrx trajectoryPlayer
std::string robotPoseUnitName
std::shared_ptr< MotionData > motionData
std::shared_ptr< MotionFileWrapper > motionWrapper
void playMotion(const Ice::Current &=Ice::emptyCurrent) override
SimulatorInterfacePrx simulatorPrx
void onConnectComponent() override
PropertyDefinitionsPtr createPropertyDefinitions() override
bool loadMMMFile(const std::string &filePath, const std::string &projects=std::string(), bool createTrajectoryPlayer=true, const Ice::Current &=Ice::emptyCurrent) override
void setLoopBack(bool state, const Ice::Current &=Ice::emptyCurrent) override
void onExitComponent() override
std::recursive_mutex mmmMutex
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
void waitForProxy(std::string const &name, bool addToDependencies)
std::string getName() const
Retrieve name of object.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
ArmarXManagerPtr getArmarXManager() const
Returns the ArmarX manager used to add and remove components.
static MotionFileWrapperPtr create(const std::string &motionFilePath, double butterworthFilterCutOffFreq=0.0, const std::string relativeModelRoot="mmm")
The Pose class.
Definition Pose.h:243
Ice::PropertiesPtr getIceProperties() const
Returns the set of Ice properties.
bool hasProperty(const std::string &name)
Property< PropertyType > getProperty(const std::string &name)
Property creation and retrieval.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#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
Ice::PropertiesPtr createProperties()
::IceInternal::Handle<::Ice::Properties > PropertiesPtr
T getValue(nlohmann::json &userConfig, nlohmann::json &defaultConfig, const std::string &entryName)
Definition utils.h:80
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
IceInternal::Handle< Trajectory > TrajectoryPtr
Definition Trajectory.h:52
IceInternal::Handle< Pose > PosePtr
Definition Pose.h:306
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.