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
40using namespace armarx;
41using namespace MemoryXUtility;
42
43// DO NOT EDIT NEXT LINE
44AddAndAttachObjectInWorkingMemory::SubClassRegistry
45 AddAndAttachObjectInWorkingMemory::Registry(AddAndAttachObjectInWorkingMemory::GetName(),
47
48void
52
53void
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
183 IceUtil::Time now = TimeUtil::GetTime();
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
221void
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
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition Logging.cpp:75
#define M_PI
Definition MathTools.h:17
constexpr T c
This util class helps with keeping a cycle time during a control cycle.
Definition CycleUtil.h:41
The LinkedPose class.
Definition LinkedPose.h:61
AddAndAttachObjectInWorkingMemory(const XMLStateConstructorParams &stateData)
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
A known object ID of the form "Dataset/ClassName" or "Dataset/ClassName/InstanceName".
Definition ObjectID.h:11
std::string str() const
Return "dataset/className" or "dataset/className/instanceName".
Definition ObjectID.cpp:60
The Pose class.
Definition Pose.h:243
VirtualRobot::RobotPtr createLocalClone()
Clones the structure of this remote robot to a local instance.
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition TimeUtil.cpp:42
SimoxObjectWrapper offers a simplified access to the Simox ManipulationObject (i.e visualization,...
GridFileManager provides utility functions for working with files in Mongo GridFS and links to them s...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
std::string const GlobalFrame
Variable of the global coordinate system.
Definition FramedPose.h:65
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< Pose > PosePtr
Definition Pose.h:306
IceInternal::Handle< ChannelRef > ChannelRefPtr
Definition ChannelRef.h:40
::IceInternal::ProxyHandle<::IceProxy::armarx::SharedRobotInterface > SharedRobotInterfacePrx
Definition FramedPose.h:59
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition XMLState.h:64
void toIce(std::map< IceKeyT, IceValueT > &iceMap, const boost::container::flat_map< CppKeyT, CppValueT > &cppMap)
std::shared_ptr< class RobotNameHelper > RobotNameHelperPtr
IceInternal::Handle< SimoxObjectWrapper > SimoxObjectWrapperPtr
IceInternal::Handle< MotionModelAttachedToOtherObject > MotionModelAttachedToOtherObjectPtr
IceInternal::Handle< ObjectClass > ObjectClassPtr
Definition ObjectClass.h:35
std::shared_ptr< GridFileManager > GridFileManagerPtr