SceneModifier.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, 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
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 
26 #include "SceneModifier.h"
32 
33 #include <RobotAPI/interface/components/ViewSelectionInterface.h>
34 
35 #include <MemoryX/interface/memorytypes/MemoryEntities.h>
40 
41 #include <Eigen/Core>
42 
43 #include <VirtualRobot/MathTools.h>
44 
45 #include <signal.h>
46 
47 using namespace armarx;
48 using namespace memoryx;
49 
51 {
52  usingProxy(getProperty<std::string>("WorkingMemoryName").getValue());
53  usingProxy(getProperty<std::string>("PriorKnowledgeName").getValue());
54  usingProxy(getProperty<std::string>("SimulatorName").getValue());
55 
56  if (getProperty<bool>("LocalizeObject").getValue())
57  {
58  usingProxy("ObjectMemoryObserver");
59  usingProxy("ConditionHandler");
60  }
61 }
63 {
64  ARMARX_INFO << "Exiting SceneModifier";
65  // how do we terminate the session correctly?
66  //this->getArmarXManager()->shutdown();
67  //raise(SIGTERM);
68 }
69 
71 {
72  if (classChannel1 && memoryObserver)
73  {
74  ARMARX_INFO << "Releasing obj class ";
75  memoryObserver->releaseObjectClass(classChannel1);
76  classChannel1 = NULL;
77  }
78 }
79 
80 bool SceneModifier::checkForConfigs(const std::string& suffix)
81 {
82  std::stringstream strStr;
83 
84  strStr << "SceneModificationType";
85 
86  if (!suffix.empty())
87  {
88  strStr << suffix;
89  }
90 
91  ARMARX_DEBUG << "Checking for property " << strStr.str() << "...";
92 
93  ///////////////// add/move/remove objects
94  std::string mode;
95 
96  try
97  {
98  mode = getProperty<std::string>(strStr.str()).getValue();
99  }
100  catch (...)
101  {
102  ARMARX_DEBUG << "Failed to get property " << strStr.str() << "...";
103  return false;
104  }
105 
106  if (mode.empty())
107  {
108  return false;
109  }
110 
111  if (mode == "addObject")
112  {
113  addObject(suffix);
114  }
115  else if (mode == "removeObject")
116  {
117  removeObject(suffix);
118  }
119  else if (mode == "moveObject")
120  {
121  moveObject(suffix);
122  }
123  else if (mode != "none")
124  {
125  ARMARX_ERROR << "Unknown mode: " << mode << std::endl;
126  }
127 
128  return true;
129 }
130 
132 {
133  ARMARX_INFO << "Starting SceneModifier";
134 
135  workingMemoryProxy = getProxy<memoryx::WorkingMemoryInterfacePrx>(getProperty<std::string>("WorkingMemoryName").getValue());
136  priorKnowledgeProxy = getProxy<memoryx::PriorKnowledgeInterfacePrx>(getProperty<std::string>("PriorKnowledgeName").getValue());
137  simulatorPrx = getProxy<SimulatorInterfacePrx>(getProperty<std::string>("SimulatorName").getValue());
138 
139  checkForConfigs("");
140 
141  for (int i = 0; i < 10; i++)
142  {
143  std::stringstream strStr;
144  strStr << "_" << i;
145  checkForConfigs(strStr.str());
146  }
147 
148 
149  ////////////// start object localizer
150  if (getProperty<bool>("LocalizeObject").getValue())
151  {
152  std::string objClassName = getProperty<std::string>("ObjectClassName").getValue();
153  ARMARX_VERBOSE << "localizing object " << objClassName;
154 
155  // retrieve proxies
156  memoryObserver = getProxy<memoryx::ObjectMemoryObserverInterfacePrx>("ObjectMemoryObserver");
157  conditionHandler = getProxy<ConditionHandlerInterfacePrx>("ConditionHandler");
158  int timeS = getProperty<int>("LocalizeObject_seconds").getValue();
159  localizeSingleObject(objClassName, timeS);
160  }
161 
162 
163  /////////////// modify robot
164  std::string attachObjectName = getProperty<std::string>("AttachObjectToRobot.ObjectName").getValue();
165  std::string attachRobotName = getProperty<std::string>("AttachObjectToRobot.RobotName").getValue();
166  std::string attachRobotNodeName = getProperty<std::string>("AttachObjectToRobot.RobotNodeName").getValue();
167 
168  if (!attachObjectName.empty() && !attachRobotName.empty() && !attachRobotNodeName.empty())
169  {
170  ARMARX_VERBOSE << "Attaching " << attachObjectName << "to robot " << attachRobotName << " at robot node " << attachRobotNodeName;
171  attachObject(attachObjectName, attachRobotName, attachRobotNodeName);
172  }
173 
174 
175  // that's it, we can shut down
176  // todo: how to shutdown correctly?!
177  //this->terminate();
178  //raise(SIGTERM);
179 }
180 
182 {
183  std::stringstream ss1, ss2, ss3, ss4, ss5, ss6;
184  ss1 << "PositionX";
185  ss2 << "PositionY";
186  ss3 << "PositionZ";
187  ss4 << "OrientationRoll";
188  ss5 << "OrientationPitch";
189  ss6 << "OrientationYaw";
190 
191  if (!suffix.empty())
192  {
193  ss1 << suffix;
194  ss2 << suffix;
195  ss3 << suffix;
196  ss4 << suffix;
197  ss5 << suffix;
198  ss6 << suffix;
199  }
200 
201  float x = getProperty<float>(ss1.str()).getValue();
202  float y = getProperty<float>(ss2.str()).getValue();
203  float z = getProperty<float>(ss3.str()).getValue();
204  float ro = getProperty<float>(ss4.str()).getValue();
205  float pi = getProperty<float>(ss5.str()).getValue();
206  float ya = getProperty<float>(ss6.str()).getValue();
207 
208  Eigen::Vector3f pos = {x, y, z};
209  Eigen::Matrix4f globalPose;
210  VirtualRobot::MathTools::rpy2eigen4f(ro, pi, ya, globalPose);
211  globalPose.block<3, 1>(0, 3) = pos;
212  return globalPose;
213 }
214 
215 bool SceneModifier::getStatic(const std::string& suffix)
216 {
217  std::stringstream ss1;
218  ss1 << "StaticObject";
219 
220 
221  if (!suffix.empty())
222  {
223  ss1 << suffix;
224  }
225 
226  bool x = getProperty<bool>(ss1.str()).getValue();
227 
228  return x;
229 }
230 
231 void SceneModifier::attachObject(const std::string& objName, const std::string& robotName, const std::string& robotNodeName)
232 {
233  if (!simulatorPrx->hasObject(objName))
234  {
235  ARMARX_ERROR << "Object " << objName << " is not present in simulator...";
236  return;
237  }
238 
239  if (!simulatorPrx->hasRobot(robotName))
240  {
241  ARMARX_ERROR << "Robot " << robotName << " is not present in simulator...";
242  return;
243  }
244 
245  if (!simulatorPrx->hasRobotNode(robotName, robotNodeName))
246  {
247  ARMARX_ERROR << "Robot node " << robotNodeName << " is not present in simulator...";
248  return;
249  }
250 
251  simulatorPrx->objectGrasped(robotName, robotNodeName, objName);
252 }
253 
254 void SceneModifier::addObject(const std::string& suffix)
255 {
256  std::stringstream ss1, ss2;
257  ss1 << "ObjectClassName";
258  ss2 << "ObjectInstanceName";
259 
260  if (!suffix.empty())
261  {
262  ss1 << suffix;
263  ss2 << suffix;
264  }
265 
266  ARMARX_DEBUG << "checking for object class property: " << ss1.str() << ", and instance name:" << ss2.str();
267 
268  std::string objClassName = getProperty<std::string>(ss1.str()).getValue();
269  std::string objInstanceName = getProperty<std::string>(ss2.str()).getValue();
270  ARMARX_DEBUG << "adding object class " << objClassName << ", instance name:" << objInstanceName;
271 
272 
273  memoryx::PersistentObjectClassSegmentBasePrx classesSegmentPrx = priorKnowledgeProxy->getObjectClassesSegment();
274 
275  //memoryx::EntityIdList list = classesSegmentPrx->getAllEntityIds();
276  //for (size_t i=0;i<list.size();i++)
277  //{
278  // memoryx::EntityPtr e = memoryx::EntityPtr::dynamicCast(classesSegmentPrx->getEntityById(list.at(i)));
279  // ARMARX_IMPORTANT_S << "i:" << i << ", id:" << e->getId() << ", name:" << e->getName();
280  //}
281  memoryx::EntityBasePtr classesEntity = classesSegmentPrx->getEntityByName(objClassName);
282 
283  if (!classesEntity)
284  {
285  ARMARX_ERROR << "No memory entity found with class name " << objClassName;
286  return;
287  }
288 
289  memoryx::ObjectClassPtr objectClass = memoryx::ObjectClassPtr::dynamicCast(classesEntity);
290 
291  if (!objectClass)
292  {
293  ARMARX_ERROR << "Could not cast entitiy to object class, name: " << objClassName;
294  return;
295  }
296 
297  Eigen::Matrix4f globalPose = getGlobalPose(suffix);
298  PosePtr pose = new Pose(globalPose);
299 
300  bool isStatic = getStatic(suffix);
301 
302  if (simulatorPrx->hasObject(objInstanceName))
303  {
304  ARMARX_ERROR << "Object " << objInstanceName << " is already present...";
305  return;
306  }
307 
308  ARMARX_IMPORTANT << "Adding Object " << objInstanceName << " at :\n" << pose;
309  simulatorPrx->addObject(objectClass, objInstanceName, pose, isStatic);
310 }
311 
312 void SceneModifier::removeObject(const std::string& suffix)
313 {
314  /*std::stringstream ss1,ss2;
315  ss1 << "ObjectClassName";
316  ss2 << "ObjectInstanceName";
317  if (!suffix.empty())
318  {
319  ss1 << _ << suffix;
320  ss2 << _ << suffix;
321  }
322  std::string objClassName = getProperty<std::string>(ss1).getValue();
323  std::string objInstanceName = getProperty<std::string>(ss2).getValue();*/
324 
325  ARMARX_WARNING << " NYI....";
326 }
327 
328 void SceneModifier::moveObject(const std::string& suffix)
329 {
330  //std::string objClassName = getProperty<std::string>("ObjectClassName").getValue();
331  std::stringstream ss1, ss2;
332  ss1 << "ObjectClassName";
333  ss2 << "ObjectInstanceName";
334 
335  if (!suffix.empty())
336  {
337  ss1 << suffix;
338  ss2 << suffix;
339  }
340 
341  std::string objInstanceName = getProperty<std::string>(ss2.str()).getValue();
342  ARMARX_DEBUG << "moving object instance :" << objInstanceName;
343 
344  Eigen::Matrix4f globalPose = getGlobalPose(suffix);
345  PosePtr pose = new Pose(globalPose);
346 
347  if (!simulatorPrx->hasObject(objInstanceName))
348  {
349  ARMARX_ERROR << "Object " << objInstanceName << " is not present in simulator...";
350  return;
351  }
352 
353  ARMARX_VERBOSE << "Moving object " << objInstanceName << " to :\n" << pose;
354  simulatorPrx->setObjectPose(objInstanceName, pose);
355 }
356 
357 
358 void SceneModifier::localizeSingleObject(const std::string& objClass, int sleepS)
359 {
360  /////////////////////////////////////////////////////////////////////////////////////////////////
361  // USECASE I: query a specific object and wait until localization finished
362  /////////////////////////////////////////////////////////////////////////////////////////////////
363 
364  ARMARX_VERBOSE << "Initiating localization of " << objClass << " for " << sleepS << " seconds";
365 
366  // 1. request the object class (corresponding to the classes in prior knowledge see PriorMemoryEditorGui)
367  classChannel1 = memoryObserver->requestObjectClassRepeated(objClass, 50, armarx::DEFAULT_VIEWTARGET_PRIORITY);
368 
369  if (!classChannel1)
370  {
371  ARMARX_ERROR << "Could not find class " << objClass << " in prior knowledge";
372  return;
373  }
374 
375 
376  // 2. install condition on the localizationFinished datafield
377  Literal objectLocalizationFinished1(ChannelRefPtr::dynamicCast(classChannel1)->getDataFieldIdentifier("localizationFinished"), "equals", Literal::createParameterList(true));
378  EventBasePtr e1 = new EventBase();
379  ConditionIdentifier id1 = conditionHandler->installCondition(EventListenerInterfacePrx::uncheckedCast(getProxy()), objectLocalizationFinished1.getImpl(), e1, true, false, {});
380 
381  ARMARX_VERBOSE << "Sleeping " << sleepS << " seconds";
382  TimeUtil::MSSleep(sleepS * 1000);
383 
384  // 3. fetch object instances for the requested object class
385  ChannelRefBaseSequence channelRefs = memoryObserver->getObjectInstances(classChannel1);
386  ChannelRefBaseSequence::iterator iter = channelRefs.begin();
387  ARMARX_INFO_S << "Found the following instance:";
388 
389  while (iter != channelRefs.end())
390  {
391  ChannelRefPtr channelRef = ChannelRefPtr::dynamicCast(*iter);
392  ARMARX_INFO << channelRef->getDataField("instanceName");
393  iter++;
394  }
395 
396  // 4. release object class (do not forget)
397  memoryObserver->releaseObjectClass(classChannel1);
398  classChannel1 = NULL;
399 
400  conditionHandler->removeCondition(id1);
401 }
402 
armarx::SceneModifier::moveObject
void moveObject(const std::string &suffix)
Definition: SceneModifier.cpp:328
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::SceneModifier::removeObject
void removeObject(const std::string &suffix)
Definition: SceneModifier.cpp:312
armarx::navigation::core::Pose
Eigen::Isometry3f Pose
Definition: basic_types.h:31
ArmarXManager.h
armarx::TimeUtil::MSSleep
static void MSSleep(int durationMS)
lock the calling thread for a given duration (like usleep(...) but using Timeserver time)
Definition: TimeUtil.cpp:94
armarx::Literal::createParameterList
static VarList createParameterList()
Static helper method to create an empty parameterlist.
Definition: Term.cpp:129
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
ObjectClass.h
Term.h
pi
#define pi
Definition: Transition.cpp:37
IceInternal::Handle< ObjectClass >
armarx::SceneModifier::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: SceneModifier.cpp:70
ObserverObjectFactories.h
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::SceneModifier::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: SceneModifier.cpp:62
armarx::SceneModifier::attachObject
void attachObject(const std::string &objName, const std::string &robotName, const std::string &robotNodeName)
Definition: SceneModifier.cpp:231
MemoryXCoreObjectFactories.h
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::SceneModifier::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: SceneModifier.cpp:50
armarx::SceneModifier::getStatic
bool getStatic(const std::string &suffix)
Definition: SceneModifier.cpp:215
armarx::SceneModifier::getGlobalPose
Eigen::Matrix4f getGlobalPose(const std::string &suffix)
Definition: SceneModifier.cpp:181
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
ObjectInstance.h
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
SceneModifier.h
armarx::SceneModifier::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: SceneModifier.cpp:131
MemoryXTypesObjectFactories.h
armarx::SceneModifier::checkForConfigs
bool checkForConfigs(const std::string &suffix)
Definition: SceneModifier.cpp:80
armarx::Literal
Definition: Term.h:208
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:195
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::SceneModifier::localizeSingleObject
void localizeSingleObject(const std::string &objClass, int sleepS)
Definition: SceneModifier.cpp:358
armarx::SceneModifier::addObject
void addObject(const std::string &suffix)
Definition: SceneModifier.cpp:254
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
ChannelRef.h
armarx::Term::getImpl
TermImplPtr getImpl() const
Retrieve term implementation object as used in the ArmarX Framework in order to build distributed exp...
Definition: Term.cpp:103