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 
55 using namespace armarx;
56 
57 void
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 
69 bool
70 MMMSimulation::isMotionLoaded(const Ice::Current&)
71 {
72  return motionData != nullptr;
73 }
74 
75 bool
76 MMMSimulation::loadMMMFile(const std::string& filePath,
77  const std::string& projects,
78  bool createTrajectoryPlayer,
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  {
121  this->createTrajectoryPlayer();
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 
134 void
135 MMMSimulation::playMotion(const Ice::Current&)
136 {
138  {
139  trajectoryPlayer->resetTrajectoryPlayer(true);
140  trajectoryPlayer->startTrajectoryPlayer();
141  }
142 }
143 
144 void
145 MMMSimulation::pauseMotion(const Ice::Current&)
146 {
147  if (trajectoryPlayer)
148  {
149  trajectoryPlayer->pauseTrajectoryPlayer();
150  }
151 }
152 
153 void
154 MMMSimulation::stopMotion(const Ice::Current&)
155 {
156  if (trajectoryPlayer)
157  {
158  trajectoryPlayer->stopTrajectoryPlayer();
159  }
160 }
161 
162 void
163 MMMSimulation::setLoopBack(bool state, const Ice::Current&)
164 {
165  if (trajectoryPlayer)
166  {
167  trajectoryPlayer->setLoopPlayback(state);
168  }
169 }
170 
171 void
173 {
175  {
176  if (trajectoryLoaded)
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 
188 void
190 {
191  simulatorPrx =
192  getProxy<SimulatorInterfacePrx>(getProperty<std::string>("SimulatorName").getValue());
193 
194  initTask = new RunningTask<MMMSimulation>(this, &MMMSimulation::initialize);
195  initTask->start();
196 }
197 
198 void
199 MMMSimulation::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",
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 =
256  Component::create<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("");
286  Component::create<SelfLocalizationDynamicSimulation>(
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 =
306  Component::create<KinematicUnitDynamicSimulation>(
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 =
324  Component::create<RobotPoseUnitDynamicSimulation>(
325  properties, robotposeUnitConfigName, getConfigDomain());
326  robotPoseUnitName = robotposecUnitComponent->getName();
327  getArmarXManager()->addObject(robotposecUnitComponent);
328  }
329 
330  if (motionData)
331  {
332  if (!trajectoryPlayer)
333  {
334  this->createTrajectoryPlayer();
335  }
336  if (trajectoryPlayer)
337  {
338  loadTrajectory();
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 
352 void
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 
385  trajectoryPlayer = getProxy<TrajectoryPlayerInterfacePrx>(trajectoryPlayerName);
386 }
387 
388 void
390 {
391  if (simulatorPrx->hasRobot(agentName))
392  {
393  simulatorPrx->removeRobot(agentName);
394  }
395 }
396 
397 void
399 {
400 }
401 
404 {
406 }
armarx::ManagedIceObject::waitForProxy
void waitForProxy(std::string const &name, bool addToDependencies)
Definition: ManagedIceObject.cpp:178
armarx::CMakePackageFinder::packageFound
bool packageFound() const
Returns whether or not this package was found with cmake.
Definition: CMakePackageFinder.cpp:511
armarx::navigation::core::Pose
Eigen::Isometry3f Pose
Definition: basic_types.h:31
armarx::MMMSimulation::createPropertyDefinitions
PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: MMMSimulation.cpp:403
armarx::PosePtr
IceInternal::Handle< Pose > PosePtr
Definition: Pose.h:306
armarx::MMMSimulation::trajectoryPlayerName
std::string trajectoryPlayerName
Definition: MMMSimulation.h:156
armarx::MMMSimulation::trajectoryPlayer
TrajectoryPlayerInterfacePrx trajectoryPlayer
Definition: MMMSimulation.h:151
armarx::MMMSimulation::agentName
std::string agentName
Definition: MMMSimulation.h:164
armarx::Split
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelperTemplates.h:36
armarx::ManagedIceObject::getArmarXManager
ArmarXManagerPtr getArmarXManager() const
Returns the ArmarX manager used to add and remove components.
Definition: ManagedIceObject.cpp:360
armarx::CMakePackageFinder
The CMakePackageFinder class provides an interface to the CMake Package finder capabilities.
Definition: CMakePackageFinder.h:52
armarx::MMMSimulation::setLoopBack
void setLoopBack(bool state, const Ice::Current &=Ice::emptyCurrent) override
Definition: MMMSimulation.cpp:163
ButterworthFilter.h
SelfLocalizationDynamicSimulation.h
armarx::MMMSimulation::loadMMMFile
bool loadMMMFile(const std::string &filePath, const std::string &projects=std::string(), bool createTrajectoryPlayer=true, const Ice::Current &=Ice::emptyCurrent) override
Definition: MMMSimulation.cpp:76
armarx::PropertyUser::hasProperty
bool hasProperty(const std::string &name)
Definition: PropertyUser.cpp:234
armarx::MMMSimulation::motionWrapper
std::shared_ptr< MotionFileWrapper > motionWrapper
Definition: MMMSimulation.h:159
TrajectoryPlayer.h
armarx::MMMSimulation::createTrajectoryPlayer
void createTrajectoryPlayer()
Definition: MMMSimulation.cpp:353
armarx::RunningTask
Definition: ArmarXMultipleObjectsScheduler.h:36
KinematicUnitDynamicSimulation.h
StringHelpers.h
armarx::MMMSimulation::onInitComponent
void onInitComponent() override
Definition: MMMSimulation.cpp:58
armarx::MMMSimulation::trajectoryLoaded
bool trajectoryLoaded
Definition: MMMSimulation.h:162
IceInternal::Handle< Trajectory >
armarx::MMMSimulation::isMotionLoaded
bool isMotionLoaded(const Ice::Current &=Ice::emptyCurrent) override
Definition: MMMSimulation.cpp:70
armarx::MMMSimulation::simulatorPrx
SimulatorInterfacePrx simulatorPrx
Definition: MMMSimulation.h:152
armarx::MMMSimulation::kinematicUnitName
std::string kinematicUnitName
Definition: MMMSimulation.h:154
Ice::createProperties
Ice::PropertiesPtr createProperties()
MMMSimulation.h
armarx::MMMSimulationPropertyDefinitions
Definition: MMMSimulation.h:49
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
armarx::CMakePackageFinder::getDataDir
std::string getDataDir() const
Definition: CMakePackageFinder.h:194
armarx::MMMSimulation::loadTrajectory
void loadTrajectory()
Definition: MMMSimulation.cpp:172
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
armarx::Component::getConfigDomain
std::string getConfigDomain()
Retrieve config domain for this component as set in constructor.
Definition: Component.cpp:65
armarx::MMMSimulation::mmmMutex
std::recursive_mutex mmmMutex
Definition: MMMSimulation.h:167
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
armarx::MMMSimulation::robotPoseUnitName
std::string robotPoseUnitName
Definition: MMMSimulation.h:155
ExpressionException.h
armarx::MMMSimulation::onDisconnectComponent
void onDisconnectComponent() override
Definition: MMMSimulation.cpp:389
armarx::control::common::getValue
T getValue(nlohmann::json &userConfig, nlohmann::json &defaultConfig, const std::string &entryName)
Definition: utils.h:71
armarx::MMMSimulation::startPose
Eigen::Matrix4f startPose
Definition: MMMSimulation.h:157
Trajectory.h
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
CMakePackageFinder.h
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::ArmarXDataPath::addDataPaths
static void addDataPaths(const std::string &dataPathList)
Definition: ArmarXDataPath.cpp:554
armarx::MMMSimulation::modelScaling
float modelScaling
Definition: MMMSimulation.h:163
RobotPoseUnitDynamicSimulation.h
MMMPlayer.h
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::MMMSimulation::pauseMotion
void pauseMotion(const Ice::Current &=Ice::emptyCurrent) override
Definition: MMMSimulation.cpp:145
MotionFileWrapper.h
armarx::MMMSimulation::stopMotion
void stopMotion(const Ice::Current &=Ice::emptyCurrent) override
Definition: MMMSimulation.cpp:154
armarx::MMMSimulation::motionData
std::shared_ptr< MotionData > motionData
Definition: MMMSimulation.h:160
armarx::MMMSimulation::onConnectComponent
void onConnectComponent() override
Definition: MMMSimulation.cpp:189
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:108
armarx::ArmarXDataPath::getAbsolutePath
static bool getAbsolutePath(const std::string &relativeFilename, std::string &storeAbsoluteFilename, const std::vector< std::string > &additionalSearchPaths={}, bool verbose=true)
Definition: ArmarXDataPath.cpp:109
armarx::PropertyUser::getIceProperties
Ice::PropertiesPtr getIceProperties() const
Returns the set of Ice properties.
Definition: PropertyUser.cpp:221
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::MMMSimulation::onExitComponent
void onExitComponent() override
Definition: MMMSimulation.cpp:398
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:35
ArmarXDataPath.h
Variant.h
armarx::MMMSimulation::modelFileName
std::string modelFileName
Definition: MMMSimulation.h:165
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:154
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::MMMSimulation::playMotion
void playMotion(const Ice::Current &=Ice::emptyCurrent) override
Definition: MMMSimulation.cpp:135
KinematicUnitObserver.h
armarx::MMMSimulation::motionPath
std::string motionPath
Definition: MMMSimulation.h:161
armarx::MotionFileWrapper::create
static MotionFileWrapperPtr create(const std::string &motionFilePath, double butterworthFilterCutOffFreq=0.0, const std::string relativeModelRoot="mmm")
Definition: MotionFileWrapper.cpp:50
RobotStateComponent.h