AddAndAttachObjectInWorkingMemory.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 RobotSkillTemplates::MemoryXUtility
17  * @author Fabian Paus ( fabian dot paus at kit dot edu )
18  * @date 2019
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
24 
25 #include <SimoxUtility/math/pose/pose.h>
26 #include <VirtualRobot/RobotNodeSet.h>
27 
29 
31 
36 
37 //#include <ArmarXCore/core/time/TimeUtil.h>
38 //#include <ArmarXCore/observers/variant/DatafieldRef.h>
39 
40 using namespace armarx;
41 using namespace MemoryXUtility;
42 
43 // DO NOT EDIT NEXT LINE
44 AddAndAttachObjectInWorkingMemory::SubClassRegistry
45  AddAndAttachObjectInWorkingMemory::Registry(AddAndAttachObjectInWorkingMemory::GetName(),
47 
48 void
50 {
51 }
52 
53 void
55 {
56  memoryx::ObjectInstanceMemorySegmentBasePrx instanceMemory =
57  getWorkingMemory()->getObjectInstancesSegment();
58  std::string objectClassName = in.getObjectClassName();
59 
60  Eigen::Matrix4f transformationOffset = in.getTransformationOffset()->toEigen();
61 
62  SharedRobotInterfacePrx sharedRobot = getRobotStateComponent()->getSynchronizedRobot();
64  VirtualRobot::RobotNodeSetPtr kinematicChain =
65  robot->getRobotNodeSet(in.getAttachToTCPofKinematicChain());
66  VirtualRobot::RobotNodePtr tcp = kinematicChain->getTCP();
67  Eigen::Matrix4f tcpPose = tcp->getGlobalPose();
68 
69  RobotNameHelperPtr nameHelper = getRobotNameHelper();
70  Eigen::Matrix4f objectPose = tcpPose * transformationOffset;
71 
72 
73  std::string objectID = ""; //<-- The output value
74  if (objectClassName.find("/") != std::string::npos)
75  {
76  ARMARX_INFO << "Attach object '" << objectClassName << "' to new ObjectMemory!";
77 
78  armarx::objpose::AttachObjectToRobotNodeInput input;
79  armarx::ObjectID oID(objectClassName);
80  input.objectID = toIce(oID);
81  input.providerName = ""; // Optional
82  input.frameName = "Hand R TCP"; // ToDo: get as argument
83  input.agentName = "Armar6";
84  input.poseInFrame = armarx::PosePtr(
85  new armarx::Pose(transformationOffset *
86  simox::math::pose(Eigen::AngleAxisf(M_PI, Eigen::Vector3f::UnitZ()))));
87  armarx::objpose::AttachObjectToRobotNodeOutput result =
88  getObjectPoseStorage()->attachObjectToRobotNode(input);
89  if (!result.success)
90  {
91  ARMARX_WARNING << "Failed to attach object '" << input.objectID << "' to frame '"
92  << input.frameName << "' of agent '" << input.agentName << "'.";
93  }
94  objectID = oID.str();
95  }
96  else
97  {
98  ARMARX_INFO << "Attach object '" << objectClassName << "' to old WorkingMemory!";
99 
100  std::string memoryHandName = nameHelper->getArm(in.getSide()).getMemoryHandName();
101  auto objClassBase =
102  getPriorKnowledge()->getObjectClassesSegment()->getObjectClassByName(objectClassName);
103  memoryx::ObjectClassPtr objectClass = memoryx::ObjectClassPtr::dynamicCast(objClassBase);
104  if (objectClass)
105  {
106  memoryx::GridFileManagerPtr fileManager(
107  new memoryx::GridFileManager(getPriorKnowledge()->getCommonStorage()));
108  objectClass->addWrapper(new memoryx::EntityWrappers::SimoxObjectWrapper(fileManager));
110  objectClass->getWrapper<memoryx::EntityWrappers::SimoxObjectWrapper>();
111  Eigen::Vector3f placeOrientationAngles = simoxWrapper->getPutdownOrientationRPY();
112  Eigen::Matrix3f orientationVisu =
113  (Eigen::AngleAxisf(placeOrientationAngles(0), Eigen::Vector3f::UnitX()) *
114  Eigen::AngleAxisf(placeOrientationAngles(1), Eigen::Vector3f::UnitY()) *
115  Eigen::AngleAxisf(placeOrientationAngles(2), Eigen::Vector3f::UnitZ()))
116  .toRotationMatrix();
117  objectPose.block<3, 3>(0, 0) = objectPose.block<3, 3>(0, 0) * orientationVisu;
118  }
119  else
120  {
121  ARMARX_WARNING << "ObjectClass is not set!";
122  }
123 
124  // Get channel for hand
125  ChannelRefPtr handChannel;
126  if (in.isHandChannelSet())
127  {
128  handChannel = in.getHandChannel();
129  }
130  else
131  {
132  ARMARX_INFO << "Using memory hand name: " << memoryHandName;
133  std::string handChannelClass = memoryHandName;
134 
135  std::vector<ChannelRefBasePtr> objectInstanceList =
136  getObjectMemoryObserver()->getObjectInstancesByClass(handChannelClass);
137  ARMARX_INFO << "found " << objectInstanceList.size() << " instances for object "
138  << handChannelClass;
139  if (objectInstanceList.size())
140  {
141  handChannel = ChannelRefPtr::dynamicCast(objectInstanceList.front());
142  }
143  else
144  {
145  ARMARX_WARNING << "No hand channel defined. Trying to request hand localization.";
146 
147  ARMARX_INFO << "Requesting localization for object class '" << handChannelClass;
148 
149  int cycleTimeInMS = static_cast<int>(1000.0f * 0.2f);
150  int localizationPriority = 50;
151  getObjectMemoryObserver()->requestObjectClassRepeated(
152  handChannelClass, cycleTimeInMS, localizationPriority);
153 
154  // Wait for localization
155  IceUtil::Time startTime = TimeUtil::GetTime();
156  bool timeout = false;
157  CycleUtil c(10);
158  while (!isRunningTaskStopped())
159  {
160  c.waitForCycleDuration();
161 
162  std::vector<ChannelRefBasePtr> objectInstanceList =
163  getObjectMemoryObserver()->getObjectInstancesByClass(handChannelClass);
164  ARMARX_INFO << deactivateSpam(1) << "Found " << objectInstanceList.size()
165  << " instances for object " << handChannelClass;
166 
167  if (!objectInstanceList.empty())
168  {
169  handChannel = ChannelRefPtr::dynamicCast(objectInstanceList.front());
170  if (!handChannel || !handChannel->hasDatafield("pose"))
171  {
172  ARMARX_WARNING << "Unable to cast channel reference or datafield pose "
173  "does not exists.";
174  emitFailure();
175  return;
176  }
177  else
178  {
179  break;
180  }
181  }
182 
184  double timeElapsed = (now - startTime).toSecondsDouble();
185  timeout = timeElapsed > in.getLocalizationTimeoutInSeconds();
186  if (timeout)
187  {
188  ARMARX_WARNING << "Could not localize hand '" << handChannelClass << "' in "
189  << in.getLocalizationTimeoutInSeconds() << " seconds.";
190  emitFailure();
191  return;
192  }
193  }
194  }
195  }
196 
198  new memoryx::MotionModelAttachedToOtherObject(getRobotStateComponent(), handChannel));
199  objectID =
200  instanceMemory->addObjectInstance(objectClassName,
201  objectClassName,
202  new LinkedPose(objectPose, GlobalFrame, sharedRobot),
203  motionModel);
204  }
205 
206  if (objectID.empty())
207  {
208  ARMARX_ERROR << "The output value 'objectID' is empty. It should be either set from the "
209  "old or the new memory.";
210  }
211  out.setNewObjectID(objectID);
212  emitSuccess();
213 }
214 
215 //void AddAndAttachObjectInWorkingMemory::onBreak()
216 //{
217 // // put your user code for the breaking point here
218 // // execution time should be short (<100ms)
219 //}
220 
221 void
223 {
224  // put your user code for the exit point here
225  // execution time should be short (<100ms)
226 }
227 
228 // DO NOT EDIT NEXT FUNCTION
231 {
233 }
ice_conversions.h
armarx::ObjectID
A known object ID of the form "Dataset/ClassName" or "Dataset/ClassName/InstanceName".
Definition: ObjectID.h:10
armarx::MemoryXUtility::AddAndAttachObjectInWorkingMemory::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: AddAndAttachObjectInWorkingMemory.cpp:230
armarx::PosePtr
IceInternal::Handle< Pose > PosePtr
Definition: Pose.h:306
armarx::MemoryXUtility::AddAndAttachObjectInWorkingMemory::onExit
void onExit() override
Definition: AddAndAttachObjectInWorkingMemory.cpp:222
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:65
armarx::XMLStateConstructorParams
Definition: XMLState.h:49
armarx::CycleUtil
This util class helps with keeping a cycle time during a control cycle.
Definition: CycleUtil.h:40
GridFileManager.h
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
ObjectClass.h
armarx::MemoryXUtility::AddAndAttachObjectInWorkingMemory::AddAndAttachObjectInWorkingMemory
AddAndAttachObjectInWorkingMemory(const XMLStateConstructorParams &stateData)
Definition: AddAndAttachObjectInWorkingMemory.h:32
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:64
IceInternal::Handle< ObjectClass >
armarx::MemoryXUtility::AddAndAttachObjectInWorkingMemory::Registry
static SubClassRegistry Registry
Definition: AddAndAttachObjectInWorkingMemory.h:47
armarx::toIce
void toIce(std::map< IceKeyT, IceValueT > &iceMap, const boost::container::flat_map< CppKeyT, CppValueT > &cppMap)
Definition: ice_conversions_boost_templates.h:15
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:75
armarx::VariantType::LinkedPose
const VariantTypeId LinkedPose
Definition: LinkedPose.h:43
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::RobotNameHelperPtr
std::shared_ptr< class RobotNameHelper > RobotNameHelperPtr
Definition: RobotNameHelper.h:32
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:12
armarx::MemoryXUtility::AddAndAttachObjectInWorkingMemory::run
void run() override
Definition: AddAndAttachObjectInWorkingMemory.cpp:54
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
armarx::MemoryXUtility::AddAndAttachObjectInWorkingMemory::onEnter
void onEnter() override
Definition: AddAndAttachObjectInWorkingMemory.cpp:49
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
memoryx::EntityWrappers::SimoxObjectWrapper
SimoxObjectWrapper offers a simplified access to the Simox ManipulationObject (i.e visualization,...
Definition: SimoxObjectWrapper.h:46
memoryx::MotionModelAttachedToOtherObject
Definition: MotionModelAttachedToOtherObject.h:32
MotionModelAttachedToOtherObject.h
armarx::TimeUtil::GetTime
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition: TimeUtil.cpp:42
CycleUtil.h
SimoxObjectWrapper.h
armarx::Pose
The Pose class.
Definition: Pose.h:242
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
memoryx::GridFileManagerPtr
std::shared_ptr< GridFileManager > GridFileManagerPtr
Definition: AbstractEntityWrapper.h:33
armarx::RemoteRobot::createLocalClone
VirtualRobot::RobotPtr createLocalClone()
Clones the structure of this remote robot to a local instance.
Definition: RemoteRobot.cpp:381
IceInternal::ProxyHandle<::IceProxy::armarx::SharedRobotInterface >
memoryx::GridFileManager
GridFileManager provides utility functions for working with files in Mongo GridFS and links to them s...
Definition: GridFileManager.h:41
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:649
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::ObjectID::str
std::string str() const
Return "dataset/className" or "dataset/className/instanceName".
Definition: ObjectID.cpp:60
AddAndAttachObjectInWorkingMemory.h
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19