GetObjectPose.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::ObjectLocalization
17  * @author Markus Grotz ( markus dot grotz at kit dot edu )
18  * @date 2016
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "GetObjectPose.h"
24 
25 using namespace armarx;
26 using namespace ObjectLocalization;
27 
30 
32 
33 // DO NOT EDIT NEXT LINE
34 GetObjectPose::SubClassRegistry GetObjectPose::Registry(GetObjectPose::GetName(), &GetObjectPose::CreateInstance);
35 
36 
37 
39 {
40  // TODO: THIS MUST BE ADAPTED TO HANDLE armarx::ObjectID in the instance channel or object name!
41  ChannelRefBasePtr objectInstanceChannel;
42 
43  if (in.isObjectInstanceChannelSet() && in.getObjectInstanceChannel())
44  {
45  ARMARX_INFO << "using object instance channel";
46  objectInstanceChannel = in.getObjectInstanceChannel();
47  }
48  else if (in.isObjectNameSet())
49  {
50  std::string objectName = in.getObjectName();
51  if (objectName.find("/") != std::string::npos)
52  {
53  // It's an ObjectID.
54  ObjectID id(objectName);
55  objpose::ObjectPoseSeq objectPoses = objpose::fromIce(getObjectPoseStorage()->getObjectPoses());
56  for (const objpose::ObjectPose& p : objectPoses)
57  {
58  if (p.objectID == id)
59  {
60  ARMARX_IMPORTANT << "Found object in ObjectPoseStorage: " << id;
61  // out.setObjectChannel(nullptr); // Optional
62  // Miss-use instance ID for our ObjectID.
63  FramedPosePtr pose(new FramedPose(p.objectPoseRobot, "root", ""));
64  ARMARX_INFO << "Object pose from ObjectPoseStorage is " << pose->output();
65 
66  out.setObjectPose(pose);
67  out.setObjectPosition(new FramedPosition(pose->toEigen(), pose->frame, pose->agent));
68  out.setObjectOrientation(new FramedOrientation(pose->toEigen(), pose->frame, pose->agent));
69 
70  // Do not set optional parameter to nullptr (causes NullHandleException)
71  //out.setObjectInstanceChannel(nullptr);
72 
73  emitSuccess();
74  return;
75  }
76  }
77 
78  ARMARX_WARNING << "Object instance ID '" << objectName << "' looks like an armarx::ObjectID, "
79  << "but no such object was found in ObjectPoseStorage (offering " << objectPoses.size() << " objects).";
80  emitNoPoseAvailable();
81  return;
82  }
83  else
84  {
85  memoryx::ObjectMemoryObserverInterfacePrx objectMemoryObserver = getObjectMemoryObserver();
86  std::vector<ChannelRefBasePtr> objectInstanceList = objectMemoryObserver->getObjectInstancesByClass(objectName);
87 
88  ARMARX_INFO << "found " << objectInstanceList.size() << " instances for object " << in.getObjectName();
89  if (objectInstanceList.size())
90  {
91  objectInstanceChannel = objectInstanceList.front();
92  }
93  }
94  }
95 
96  if (!objectInstanceChannel)
97  {
98  emitNoPoseAvailable();
99  return;
100  }
101 
102  ChannelRefPtr channelRef = ChannelRefPtr::dynamicCast(objectInstanceChannel);
103  if (!channelRef || !channelRef->hasDatafield("pose"))
104  {
105  ARMARX_WARNING << "unable to cast channel reference or datafield pose does not exists.";
106  emitNoPoseAvailable();
107  return;
108  }
109 
110  FramedPosePtr pose = channelRef->get<FramedPose>("pose");
111  ARMARX_INFO << "object pose is " << pose->output();
112 
113  out.setObjectPose(pose);
114  out.setObjectPosition(new FramedPosition(pose->toEigen(), pose->frame, pose->agent));
115  out.setObjectOrientation(new FramedOrientation(pose->toEigen(), pose->frame, pose->agent));
116  out.setObjectInstanceChannel(channelRef);
117 
118  emitSuccess();
119 }
120 
121 //void GetObjectPose::run()
122 //{
123 // // put your user code for the execution-phase here
124 // // runs in seperate thread, thus can do complex operations
125 // // should check constantly whether isRunningTaskStopped() returns true
126 //
127 //// uncomment this if you need a continous run function. Make sure to use sleep or use blocking wait to reduce cpu load.
128 // while (!isRunningTaskStopped()) // stop run function if returning true
129 // {
130 // // do your calculations
131 // }
132 //}
133 
134 //void GetObjectPose::onBreak()
135 //{
136 // // put your user code for the breaking point here
137 // // execution time should be short (<100ms)
138 //}
139 
141 {
142  // put your user code for the exit point here
143  // execution time should be short (<100ms)
144 }
145 
146 
147 // DO NOT EDIT NEXT FUNCTION
149 {
150  return XMLStateFactoryBasePtr(new GetObjectPose(stateData));
151 }
152 
armarx::ObjectID
A known object ID of the form "Dataset/ClassName" or "Dataset/ClassName/InstanceName".
Definition: ObjectID.h:11
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::objpose::ObjectPoseSeq
std::vector< ObjectPose > ObjectPoseSeq
Definition: forward_declarations.h:20
armarx::FramedPose
The FramedPose class.
Definition: FramedPose.h:258
armarx::VariantType::FramedPose
const VariantTypeId FramedPose
Definition: FramedPose.h:37
armarx::ObjectLocalization::GetObjectPose::onEnter
void onEnter() override
Definition: GetObjectPose.cpp:38
armarx::XMLStateConstructorParams
Definition: XMLState.h:50
armarx::objpose::fromIce
void fromIce(const Box &box, simox::OrientedBox< float > &oobb)
armarx::ObjectLocalization::GetObjectPose::onExit
void onExit() override
Definition: GetObjectPose.cpp:140
IceInternal::Handle< FramedPose >
FramedPose.h
ObjectPose.h
armarx::ObjectLocalization::GetObjectPose::GetObjectPose
GetObjectPose(const XMLStateConstructorParams &stateData)
Definition: GetObjectPose.h:33
GetObjectPose.h
armarx::ObjectLocalization::GetObjectPose::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: GetObjectPose.cpp:148
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
ObjectInstance.h
armarx::VariantType::FramedOrientation
const VariantTypeId FramedOrientation
Definition: FramedPose.h:40
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::ObjectLocalization::GetObjectPose::Registry
static SubClassRegistry Registry
Definition: GetObjectPose.h:46
armarx::VariantType::FramedPosition
const VariantTypeId FramedPosition
Definition: FramedPose.h:39
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::objpose::ObjectPose
An object pose as stored by the ObjectPoseStorage.
Definition: ObjectPose.h:36