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 
28 
30 
32 #include <ArmarXCore/interface/observers/VariantBase.h>
35 #include <VirtualRobot/MathTools.h>
43 #include <MemoryX/interface/components/WorkingMemoryInterface.h>
46 #include <SimoxUtility/math/convert.h>
49 
50 #include <Ice/ObjectAdapter.h>
51 
52 using namespace armarx;
53 
54 
56 {
57  usingProxy(getProperty<std::string>("SimulatorName").getValue());
58  if (getProperty<bool>("LoadToMemory"))
59  {
60  usingProxy(getProperty<std::string>("WorkingMemoryName").getValue());
61  }
62 
63  trajectoryLoaded = false;
64 }
65 
66 bool MMMSimulation::isMotionLoaded(const Ice::Current&)
67 {
68  return motionData != nullptr;
69 }
70 
71 bool MMMSimulation::loadMMMFile(const std::string& filePath, const std::string& projects, bool createTrajectoryPlayer, const Ice::Current&)
72 {
73  if (filePath.empty())
74  {
75  return false;
76  }
77 
78  std::unique_lock lock(mmmMutex);
79 
80  if (!projects.empty())
81  {
82  std::vector<std::string> proj = armarx::Split(projects, ",;", true, true);
83 
84  for (std::string& p : proj)
85  {
86  ARMARX_INFO << "Adding to datapaths of " << p;
88 
89  if (!finder.packageFound())
90  {
91  ARMARX_WARNING << "ArmarX Package " << p << " has not been found!";
92  }
93  else
94  {
95  ARMARX_INFO << "Adding to datapaths: " << finder.getDataDir();
97  }
98  }
99  }
100 
102 
104  if (motionWrapper)
105  {
106  motionData = motionWrapper->getMotionDataByModel(modelFileName);
107  if (motionData)
108  {
109  ARMARX_INFO << "Loaded motion with model " << modelFileName << " from file " << filePath;
111  {
112  this->createTrajectoryPlayer();
113  }
114  return true;
115  }
116  else
117  {
118  ARMARX_ERROR << "Failed to load motion with model " << modelFileName << ". Valid model names are " << motionWrapper->getModelNames();
119  }
120  }
121  return false;
122 }
123 
124 void MMMSimulation::playMotion(const Ice::Current&)
125 {
127  {
128  trajectoryPlayer->resetTrajectoryPlayer(true);
129  trajectoryPlayer->startTrajectoryPlayer();
130  }
131 }
132 
133 void MMMSimulation::pauseMotion(const Ice::Current&)
134 {
135  if (trajectoryPlayer)
136  {
137  trajectoryPlayer->pauseTrajectoryPlayer();
138  }
139 }
140 
141 void MMMSimulation::stopMotion(const Ice::Current&)
142 {
143  if (trajectoryPlayer)
144  {
145  trajectoryPlayer->stopTrajectoryPlayer();
146  }
147 }
148 
149 void MMMSimulation::setLoopBack(bool state, const Ice::Current&)
150 {
151  if (trajectoryPlayer)
152  {
153  trajectoryPlayer->setLoopPlayback(state);
154  }
155 }
156 
158 {
160  {
161  if (trajectoryLoaded)
162  {
163  trajectoryPlayer->resetTrajectoryPlayer(true);
164  }
165  TrajectoryBasePtr t = motionData->getJointTrajectory();
167  trajectoryPlayer->loadJointTraj(t);
168  trajectoryPlayer->loadBasePoseTraj(motionData->getPoseTrajectory());
169  trajectoryLoaded = true;
170  }
171 }
172 
174 {
175  simulatorPrx = getProxy<SimulatorInterfacePrx>(getProperty<std::string>("SimulatorName").getValue());
176 
177  initTask = new RunningTask<MMMSimulation>(this, &MMMSimulation::initialize);
178  initTask->start();
179 }
180 
181 void MMMSimulation::initialize()
182 {
183  const std::string name = this->getName();
184  agentName = getProperty<std::string>("AgentName").getValue();
185  const std::string mmmModelFileProject = getProperty<std::string>("RobotFileNameProject").getValue();
186  const std::string mmmModelFilePath = getProperty<std::string>("RobotFileName").getValue();
187  const std::string mmmRobotNodeSetName = getProperty<std::string>("RobotNodeSetName").getValue();
188  const std::string simulatorName = getProperty<std::string>("SimulatorName").getValue();
189  //std::string robotName = getProperty<std::string>("RobotName").getValue();
190  modelFileName = std::filesystem::path(mmmModelFilePath).stem();
191 
192  loadMMMFile(getProperty<std::string>("MMMFile").getValue(), "", false);
193  if (hasProperty("Scaling") && motionData)
194  {
195  motionData->setScaling(getProperty<float>("Scaling").getValue());
196  }
197  modelScaling = motionData ? motionData->scaling : getProperty<float>("Scaling").getValue();
198 
199 
200  Eigen::Vector3f position(getProperty<float>("StartPose.x").getValue(), getProperty<float>("StartPose.y").getValue(), getProperty<float>("StartPose.z").getValue());
201  Eigen::Vector3f orientation(getProperty<float>("StartPose.roll").getValue(), getProperty<float>("StartPose.pitch").getValue(), getProperty<float>("StartPose.yaw").getValue());
202  startPose = simox::math::pos_rpy_to_mat4f(position, orientation);
203  std::string robotName = simulatorPrx->addScaledRobotName(agentName, ArmarXDataPath::getAbsolutePath(mmmModelFilePath), modelScaling);
204  agentName = robotName;
205  simulatorPrx->setRobotPose(robotName, PosePtr(new Pose(startPose)));
206 
207  ARMARX_INFO << "Added robot " << robotName << " to simulator";
208 
209  const std::string robotStateConfigName = "RobotStateComponent";
210  {
211  Ice::StringSeq pros;
212  std::string packageName("RobotAPI");
213  armarx::CMakePackageFinder finder(packageName);
214  std::string appPath = finder.getBinaryDir() + "/RobotStateComponentRun";
215  pros.push_back(appPath);
216  Ice::PropertiesPtr properties = Ice::createProperties(pros);
217  //Ice::PropertiesPtr properties = getIceProperties()->clone();
218 
219  const std::string robotSateConfPre = getConfigDomain() + "." + robotStateConfigName + ".";
220  properties->setProperty(robotSateConfPre + "AgentName", agentName);
221  properties->setProperty(robotSateConfPre + "RobotFileName", mmmModelFilePath);
222  properties->setProperty(robotSateConfPre + "RobotFileNameProject", mmmModelFileProject);
223  properties->setProperty(robotSateConfPre + "RobotNodeSetName", mmmRobotNodeSetName);
224  properties->setProperty(robotSateConfPre + "RobotModelScaling", std::to_string(modelScaling));
225  properties->setProperty(robotSateConfPre + "ObjectName", robotStateConfigName + name);
226  properties->setProperty(getConfigDomain() + ".RobotStateObserver." + "ObjectName", "RobotStateObserver" + name);
227  ARMARX_DEBUG << "creating unit " << robotStateConfigName << " using these properties: " << properties->getPropertiesForPrefix("");
228  IceInternal::Handle<RobotStateComponent> robotStateComponent = Component::create<RobotStateComponent>(properties, robotStateConfigName, getConfigDomain());
229  //robotStateComponent->getSynchronizedRobot(Ice::emptyCurrent);
230  getArmarXManager()->addObject(robotStateComponent);
231  }
232 
233  if (getProperty<bool>("LoadToMemory"))
234  {
235  Ice::StringSeq pros;
236  std::string packageName("ArmarXSimulation");
237  armarx::CMakePackageFinder finder(packageName);
238  std::string appPath = finder.getBinaryDir() + "/SelfLocalizationDynamicSimulationAppRun";
239  pros.push_back(appPath);
240  Ice::PropertiesPtr properties = Ice::createProperties(pros);
241  //Ice::PropertiesPtr properties = getIceProperties()->clone();
242 
243  const std::string localizationConfigName = "SelfLocalizationDynamicSimulation";
244  const std::string localizationConfPre = getConfigDomain() + "." + localizationConfigName + ".";
245  properties->setProperty(localizationConfPre + "AgentName", agentName);
246  properties->setProperty(localizationConfPre + "RobotStateComponentName", robotStateConfigName + name);
247  properties->setProperty(localizationConfPre + "RobotName", robotName);
248  properties->setProperty(localizationConfPre + "SimulatorName", simulatorName);
249  properties->setProperty(localizationConfPre + "ObjectName", localizationConfigName + name);
250  properties->setProperty(localizationConfPre + "WorkingMemoryName", getProperty<std::string>("WorkingMemoryName").getValue());
251  ARMARX_DEBUG << "creating unit " << localizationConfigName << " using these properties: " << properties->getPropertiesForPrefix("");
252  IceInternal::Handle<SelfLocalizationDynamicSimulation> localizationComponent = Component::create<SelfLocalizationDynamicSimulation>(properties, localizationConfigName, getConfigDomain());
253  getArmarXManager()->addObject(localizationComponent);
254  }
255 
256  std::string kinematicUnitConfigName = "KinematicUnit";
257  {
258  const std::string kinematicUnitConfPre = getConfigDomain() + "." + kinematicUnitConfigName + ".";
259  Ice::PropertiesPtr properties = getIceProperties()->clone();
260  properties->setProperty(kinematicUnitConfPre + "RobotName", robotName);
261  properties->setProperty(kinematicUnitConfPre + "RobotFileName", mmmModelFilePath);
262  properties->setProperty(kinematicUnitConfPre + "RobotFileNameProject", mmmModelFileProject);
263  properties->setProperty(kinematicUnitConfPre + "RobotNodeSetName", mmmRobotNodeSetName);
264  properties->setProperty(kinematicUnitConfPre + "ObjectName", kinematicUnitConfigName + name);
265  properties->setProperty(kinematicUnitConfPre + "SimulatorName", simulatorName);
266  ARMARX_DEBUG << "creating unit " << kinematicUnitConfigName << " using these properties: " << properties->getPropertiesForPrefix("");
267  IceInternal::Handle<KinematicUnit> kinematicUnitComponent = Component::create<KinematicUnitDynamicSimulation>(properties, kinematicUnitConfigName, getConfigDomain());
268  kinematicUnitName = kinematicUnitComponent->getName();
269  getArmarXManager()->addObject(kinematicUnitComponent);
270  }
271 
272  const std::string robotposeUnitConfigName = "RobotPoseUnit";
273  {
274  const std::string robotposeUnitConfPre = getConfigDomain() + "." + robotposeUnitConfigName + ".";
275  Ice::PropertiesPtr properties = getIceProperties()->clone();
276  properties->setProperty(robotposeUnitConfPre + "RobotName", robotName);
277  properties->setProperty(robotposeUnitConfPre + "ObjectName", robotposeUnitConfigName + name);
278  properties->setProperty(robotposeUnitConfPre + "SimulatorProxyName", simulatorName);
279  ARMARX_DEBUG << "creating unit " << robotposeUnitConfigName << " using these properties: " << properties->getPropertiesForPrefix("");
280  IceInternal::Handle<RobotPoseUnit> robotposecUnitComponent = Component::create<RobotPoseUnitDynamicSimulation>(properties, robotposeUnitConfigName, getConfigDomain());
281  robotPoseUnitName = robotposecUnitComponent->getName();
282  getArmarXManager()->addObject(robotposecUnitComponent);
283  }
284 
285  if (motionData)
286  {
287  if (!trajectoryPlayer)
288  {
289  this->createTrajectoryPlayer();
290  }
291  if (trajectoryPlayer)
292  {
293  loadTrajectory();
294  if (getProperty<bool>("AutoPlay").getValue())
295  {
296  playMotion();
297  }
298  }
299  else
300  {
301  ARMARX_ERROR << "Could not load motion as trajectory player could not be not instanciated";
302  }
303  }
304 }
305 
307 {
308  Eigen::Vector3f position = simox::math::mat4f_to_pos(startPose);
309  Eigen::Vector3f orientation = simox::math::mat4f_to_rpy(startPose);
310  const std::string trajectoryPlayerConfigName = "TrajectoryPlayer";
311  const std::string trajectoryPlayerConfPre = getConfigDomain() + "." + trajectoryPlayerConfigName + ".";
312  Ice::PropertiesPtr properties = getIceProperties()->clone();
313  properties->setProperty(trajectoryPlayerConfPre + "KinematicTopicName", kinematicUnitName);
314  properties->setProperty(trajectoryPlayerConfPre + "KinematicUnitName", kinematicUnitName);
315  properties->setProperty(trajectoryPlayerConfPre + "ObjectName", trajectoryPlayerConfigName + getName());
316  properties->setProperty(trajectoryPlayerConfPre + "LoopPlayback", std::to_string(getProperty<bool>("LoopPlayback").getValue()));
317  properties->setProperty(trajectoryPlayerConfPre + "Offset.x", std::to_string(position(0)));
318  properties->setProperty(trajectoryPlayerConfPre + "Offset.y", std::to_string(position(1)));
319  properties->setProperty(trajectoryPlayerConfPre + "Offset.z", std::to_string(position(2)));
320  properties->setProperty(trajectoryPlayerConfPre + "Offset.roll", std::to_string(orientation(0)));
321  properties->setProperty(trajectoryPlayerConfPre + "Offset.pitch", std::to_string(orientation(1)));
322  properties->setProperty(trajectoryPlayerConfPre + "Offset.yaw", std::to_string(orientation(2)));
323  properties->setProperty(trajectoryPlayerConfPre + "EnableRobotPoseUnit", "true");
324  properties->setProperty(trajectoryPlayerConfPre + "RobotPoseUnitName", robotPoseUnitName);
325  ARMARX_DEBUG << "creating unit " << trajectoryPlayerConfigName << " using these properties: " << properties->getPropertiesForPrefix("");
326  auto trajectoryPlayerComponent = Component::create<TrajectoryPlayer>(properties, trajectoryPlayerConfigName, getConfigDomain());
327  getArmarXManager()->addObject(trajectoryPlayerComponent);
328  trajectoryPlayerName = trajectoryPlayerComponent->getName();
329 
331  trajectoryPlayer = getProxy<TrajectoryPlayerInterfacePrx>(trajectoryPlayerName);
332 }
333 
335 {
336  if (simulatorPrx->hasRobot(agentName))
337  {
338  simulatorPrx->removeRobot(agentName);
339  }
340 }
341 
343 {
344 
345 }
346 
348 {
350 }
armarx::ManagedIceObject::waitForProxy
void waitForProxy(std::string const &name, bool addToDependencies)
Definition: ManagedIceObject.cpp:174
armarx::CMakePackageFinder::packageFound
bool packageFound() const
Returns whether or not this package was found with cmake.
Definition: CMakePackageFinder.cpp:485
armarx::navigation::core::Pose
Eigen::Isometry3f Pose
Definition: basic_types.h:31
armarx::MMMSimulation::createPropertyDefinitions
PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: MMMSimulation.cpp:347
armarx::PosePtr
IceInternal::Handle< Pose > PosePtr
Definition: Pose.h:306
armarx::MMMSimulation::trajectoryPlayerName
std::string trajectoryPlayerName
Definition: MMMSimulation.h:151
armarx::MMMSimulation::trajectoryPlayer
TrajectoryPlayerInterfacePrx trajectoryPlayer
Definition: MMMSimulation.h:146
armarx::MMMSimulation::agentName
std::string agentName
Definition: MMMSimulation.h:159
armarx::Split
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelperTemplates.h:35
armarx::ManagedIceObject::getArmarXManager
ArmarXManagerPtr getArmarXManager() const
Returns the ArmarX manager used to add and remove components.
Definition: ManagedIceObject.cpp:348
armarx::CMakePackageFinder
The CMakePackageFinder class provides an interface to the CMake Package finder capabilities.
Definition: CMakePackageFinder.h:53
armarx::MMMSimulation::setLoopBack
void setLoopBack(bool state, const Ice::Current &=Ice::emptyCurrent) override
Definition: MMMSimulation.cpp:149
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:71
armarx::PropertyUser::hasProperty
bool hasProperty(const std::string &name)
Definition: PropertyUser.cpp:243
armarx::MMMSimulation::motionWrapper
std::shared_ptr< MotionFileWrapper > motionWrapper
Definition: MMMSimulation.h:154
TrajectoryPlayer.h
armarx::MMMSimulation::createTrajectoryPlayer
void createTrajectoryPlayer()
Definition: MMMSimulation.cpp:306
armarx::RunningTask
Definition: ArmarXMultipleObjectsScheduler.h:35
KinematicUnitDynamicSimulation.h
StringHelpers.h
armarx::MMMSimulation::onInitComponent
void onInitComponent() override
Definition: MMMSimulation.cpp:55
armarx::MMMSimulation::trajectoryLoaded
bool trajectoryLoaded
Definition: MMMSimulation.h:157
IceInternal::Handle< Trajectory >
armarx::MMMSimulation::isMotionLoaded
bool isMotionLoaded(const Ice::Current &=Ice::emptyCurrent) override
Definition: MMMSimulation.cpp:66
armarx::MMMSimulation::simulatorPrx
SimulatorInterfacePrx simulatorPrx
Definition: MMMSimulation.h:147
armarx::MMMSimulation::kinematicUnitName
std::string kinematicUnitName
Definition: MMMSimulation.h:149
Ice::createProperties
Ice::PropertiesPtr createProperties()
MMMSimulation.h
armarx::MMMSimulationPropertyDefinitions
Definition: MMMSimulation.h:48
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::CMakePackageFinder::getDataDir
std::string getDataDir() const
Definition: CMakePackageFinder.h:176
armarx::MMMSimulation::loadTrajectory
void loadTrajectory()
Definition: MMMSimulation.cpp:157
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::Component::getConfigDomain
std::string getConfigDomain()
Retrieve config domain for this component as set in constructor.
Definition: Component.cpp:60
armarx::MMMSimulation::mmmMutex
std::recursive_mutex mmmMutex
Definition: MMMSimulation.h:162
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::MMMSimulation::robotPoseUnitName
std::string robotPoseUnitName
Definition: MMMSimulation.h:150
ExpressionException.h
armarx::MMMSimulation::onDisconnectComponent
void onDisconnectComponent() override
Definition: MMMSimulation.cpp:334
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:152
Trajectory.h
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
CMakePackageFinder.h
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::ArmarXDataPath::addDataPaths
static void addDataPaths(const std::string &dataPathList)
Definition: ArmarXDataPath.cpp:559
armarx::MMMSimulation::modelScaling
float modelScaling
Definition: MMMSimulation.h:158
RobotPoseUnitDynamicSimulation.h
MMMPlayer.h
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::MMMSimulation::pauseMotion
void pauseMotion(const Ice::Current &=Ice::emptyCurrent) override
Definition: MMMSimulation.cpp:133
MotionFileWrapper.h
armarx::MMMSimulation::stopMotion
void stopMotion(const Ice::Current &=Ice::emptyCurrent) override
Definition: MMMSimulation.cpp:141
armarx::MMMSimulation::motionData
std::shared_ptr< MotionData > motionData
Definition: MMMSimulation.h:155
armarx::MMMSimulation::onConnectComponent
void onConnectComponent() override
Definition: MMMSimulation.cpp:173
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:107
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:111
armarx::PropertyUser::getIceProperties
Ice::PropertiesPtr getIceProperties() const
Returns the set of Ice properties.
Definition: PropertyUser.cpp:229
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::MMMSimulation::onExitComponent
void onExitComponent() override
Definition: MMMSimulation.cpp:342
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
ArmarXDataPath.h
Variant.h
armarx::MMMSimulation::modelFileName
std::string modelFileName
Definition: MMMSimulation.h:160
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:151
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::MMMSimulation::playMotion
void playMotion(const Ice::Current &=Ice::emptyCurrent) override
Definition: MMMSimulation.cpp:124
KinematicUnitObserver.h
armarx::MMMSimulation::motionPath
std::string motionPath
Definition: MMMSimulation.h:156
armarx::MotionFileWrapper::create
static MotionFileWrapperPtr create(const std::string &motionFilePath, double butterworthFilterCutOffFreq=0.0, const std::string relativeModelRoot="mmm")
Definition: MotionFileWrapper.cpp:49
RobotStateComponent.h