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 
26 
28 
30 
32 
34 
36 
37 #include <SimoxUtility/math/pose/pose.h>
38 
39 //#include <ArmarXCore/core/time/TimeUtil.h>
40 //#include <ArmarXCore/observers/variant/DatafieldRef.h>
41 
42 using namespace armarx;
43 using namespace MemoryXUtility;
44 
45 // DO NOT EDIT NEXT LINE
46 AddAndAttachObjectInWorkingMemory::SubClassRegistry AddAndAttachObjectInWorkingMemory::Registry(AddAndAttachObjectInWorkingMemory::GetName(), &AddAndAttachObjectInWorkingMemory::CreateInstance);
47 
48 
49 
51 {
52 }
53 
55 {
56  memoryx::ObjectInstanceMemorySegmentBasePrx instanceMemory = getWorkingMemory()->getObjectInstancesSegment();
57  std::string objectClassName = in.getObjectClassName();
58 
59  Eigen::Matrix4f transformationOffset = in.getTransformationOffset()->toEigen();
60 
61  SharedRobotInterfacePrx sharedRobot = getRobotStateComponent()->getSynchronizedRobot();
63  VirtualRobot::RobotNodeSetPtr kinematicChain = robot->getRobotNodeSet(in.getAttachToTCPofKinematicChain());
64  VirtualRobot::RobotNodePtr tcp = kinematicChain->getTCP();
65  Eigen::Matrix4f tcpPose = tcp->getGlobalPose();
66 
67  RobotNameHelperPtr nameHelper = getRobotNameHelper();
68  Eigen::Matrix4f objectPose = tcpPose * transformationOffset;
69 
70 
71  std::string objectID = ""; //<-- The output value
72  if (objectClassName.find("/") != std::string::npos)
73  {
74  ARMARX_INFO << "Attach object '" << objectClassName << "' to new ObjectMemory!";
75 
76  armarx::objpose::AttachObjectToRobotNodeInput input;
77  armarx::ObjectID oID(objectClassName);
78  input.objectID = toIce(oID);
79  input.providerName = ""; // Optional
80  input.frameName = "Hand R TCP"; // ToDo: get as argument
81  input.agentName = "Armar6";
82  input.poseInFrame = armarx::PosePtr(new armarx::Pose(transformationOffset * simox::math::pose(Eigen::AngleAxisf(M_PI, Eigen::Vector3f::UnitZ()))));
83  armarx::objpose::AttachObjectToRobotNodeOutput result = getObjectPoseStorage()->attachObjectToRobotNode(input);
84  if (!result.success)
85  {
86  ARMARX_WARNING << "Failed to attach object '" << input.objectID << "' to frame '" << input.frameName << "' of agent '" << input.agentName << "'.";
87  }
88  objectID = oID.str();
89  }
90  else
91  {
92  ARMARX_INFO << "Attach object '" << objectClassName << "' to old WorkingMemory!";
93 
94  std::string memoryHandName = nameHelper->getArm(in.getSide()).getMemoryHandName();
95  auto objClassBase = getPriorKnowledge()->getObjectClassesSegment()->getObjectClassByName(objectClassName);
96  memoryx::ObjectClassPtr objectClass = memoryx::ObjectClassPtr::dynamicCast(objClassBase);
97  if (objectClass)
98  {
99  memoryx::GridFileManagerPtr fileManager(new memoryx::GridFileManager(getPriorKnowledge()->getCommonStorage()));
100  objectClass->addWrapper(new memoryx::EntityWrappers::SimoxObjectWrapper(fileManager));
102  Eigen::Vector3f placeOrientationAngles = simoxWrapper->getPutdownOrientationRPY();
103  Eigen::Matrix3f orientationVisu = (Eigen::AngleAxisf(placeOrientationAngles(0), Eigen::Vector3f::UnitX())
104  * Eigen::AngleAxisf(placeOrientationAngles(1), Eigen::Vector3f::UnitY())
105  * Eigen::AngleAxisf(placeOrientationAngles(2), Eigen::Vector3f::UnitZ())).toRotationMatrix();
106  objectPose.block<3, 3>(0, 0) = objectPose.block<3, 3>(0, 0) * orientationVisu;
107  }
108  else
109  {
110  ARMARX_WARNING << "ObjectClass is not set!";
111  }
112 
113  // Get channel for hand
114  ChannelRefPtr handChannel;
115  if (in.isHandChannelSet())
116  {
117  handChannel = in.getHandChannel();
118  }
119  else
120  {
121  ARMARX_INFO << "Using memory hand name: " << memoryHandName;
122  std::string handChannelClass = memoryHandName;
123 
124  std::vector<ChannelRefBasePtr> objectInstanceList = getObjectMemoryObserver()->getObjectInstancesByClass(handChannelClass);
125  ARMARX_INFO << "found " << objectInstanceList.size() << " instances for object " << handChannelClass;
126  if (objectInstanceList.size())
127  {
128  handChannel = ChannelRefPtr::dynamicCast(objectInstanceList.front());
129  }
130  else
131  {
132  ARMARX_WARNING << "No hand channel defined. Trying to request hand localization.";
133 
134  ARMARX_INFO << "Requesting localization for object class '" << handChannelClass;
135 
136  int cycleTimeInMS = static_cast<int>(1000.0f * 0.2f);
137  int localizationPriority = 50;
138  getObjectMemoryObserver()->requestObjectClassRepeated(handChannelClass, cycleTimeInMS, localizationPriority);
139 
140  // Wait for localization
141  IceUtil::Time startTime = TimeUtil::GetTime();
142  bool timeout = false;
143  CycleUtil c(10);
144  while (!isRunningTaskStopped())
145  {
146  c.waitForCycleDuration();
147 
148  std::vector<ChannelRefBasePtr> objectInstanceList = getObjectMemoryObserver()->getObjectInstancesByClass(handChannelClass);
149  ARMARX_INFO << deactivateSpam(1) << "Found " << objectInstanceList.size() << " instances for object " << handChannelClass;
150 
151  if (!objectInstanceList.empty())
152  {
153  handChannel = ChannelRefPtr::dynamicCast(objectInstanceList.front());
154  if (!handChannel || !handChannel->hasDatafield("pose"))
155  {
156  ARMARX_WARNING << "Unable to cast channel reference or datafield pose does not exists.";
157  emitFailure();
158  return;
159  }
160  else
161  {
162  break;
163  }
164  }
165 
167  double timeElapsed = (now - startTime).toSecondsDouble();
168  timeout = timeElapsed > in.getLocalizationTimeoutInSeconds();
169  if (timeout)
170  {
171  ARMARX_WARNING << "Could not localize hand '" << handChannelClass << "' in "
172  << in.getLocalizationTimeoutInSeconds() << " seconds.";
173  emitFailure();
174  return;
175  }
176  }
177  }
178  }
179 
180  memoryx::MotionModelAttachedToOtherObjectPtr motionModel(new memoryx::MotionModelAttachedToOtherObject(getRobotStateComponent(), handChannel));
181  objectID = instanceMemory->addObjectInstance(objectClassName, objectClassName, new LinkedPose(objectPose, GlobalFrame, sharedRobot), motionModel);
182  }
183 
184  if (objectID.empty())
185  {
186  ARMARX_ERROR << "The output value 'objectID' is empty. It should be either set from the old or the new memory.";
187  }
188  out.setNewObjectID(objectID);
189  emitSuccess();
190 }
191 
192 //void AddAndAttachObjectInWorkingMemory::onBreak()
193 //{
194 // // put your user code for the breaking point here
195 // // execution time should be short (<100ms)
196 //}
197 
199 {
200  // put your user code for the exit point here
201  // execution time should be short (<100ms)
202 }
203 
204 
205 // DO NOT EDIT NEXT FUNCTION
207 {
209 }
210 
ice_conversions.h
armarx::ObjectID
A known object ID of the form "Dataset/ClassName" or "Dataset/ClassName/InstanceName".
Definition: ObjectID.h:11
armarx::MemoryXUtility::AddAndAttachObjectInWorkingMemory::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: AddAndAttachObjectInWorkingMemory.cpp:206
armarx::PosePtr
IceInternal::Handle< Pose > PosePtr
Definition: Pose.h:306
armarx::MemoryXUtility::AddAndAttachObjectInWorkingMemory::onExit
void onExit() override
Definition: AddAndAttachObjectInWorkingMemory.cpp:198
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:62
armarx::XMLStateConstructorParams
Definition: XMLState.h:50
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:43
ObjectClass.h
armarx::MemoryXUtility::AddAndAttachObjectInWorkingMemory::AddAndAttachObjectInWorkingMemory
AddAndAttachObjectInWorkingMemory(const XMLStateConstructorParams &stateData)
Definition: AddAndAttachObjectInWorkingMemory.h:32
IceInternal::Handle< ObjectClass >
armarx::MemoryXUtility::AddAndAttachObjectInWorkingMemory::Registry
static SubClassRegistry Registry
Definition: AddAndAttachObjectInWorkingMemory.h:45
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:72
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:34
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
armarx::MemoryXUtility::AddAndAttachObjectInWorkingMemory::run
void run() override
Definition: AddAndAttachObjectInWorkingMemory.cpp:54
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::MemoryXUtility::AddAndAttachObjectInWorkingMemory::onEnter
void onEnter() override
Definition: AddAndAttachObjectInWorkingMemory.cpp:50
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:34
MotionModelAttachedToOtherObject.h
armarx::TimeUtil::GetTime
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition: TimeUtil.cpp:42
CycleUtil.h
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
SimoxObjectWrapper.h
armarx::Pose
The Pose class.
Definition: Pose.h:242
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:600
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
memoryx::GridFileManagerPtr
std::shared_ptr< GridFileManager > GridFileManagerPtr
Definition: AbstractEntityWrapper.h:32
armarx::RemoteRobot::createLocalClone
VirtualRobot::RobotPtr createLocalClone()
Clones the structure of this remote robot to a local instance.
Definition: RemoteRobot.cpp:328
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:42
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::ObjectID::str
std::string str() const
Return "dataset/className" or "dataset/className/instanceName".
Definition: ObjectID.cpp:55
AddAndAttachObjectInWorkingMemory.h
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18