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(),
36 
37 void
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 =
56  objpose::fromIce(getObjectPoseStorage()->getObjectPoses());
57  for (const objpose::ObjectPose& p : objectPoses)
58  {
59  if (p.objectID == id)
60  {
61  ARMARX_IMPORTANT << "Found object in ObjectPoseStorage: " << id;
62  // out.setObjectChannel(nullptr); // Optional
63  // Miss-use instance ID for our ObjectID.
64  FramedPosePtr pose(new FramedPose(p.objectPoseRobot, "root", ""));
65  ARMARX_INFO << "Object pose from ObjectPoseStorage is " << pose->output();
66 
67  out.setObjectPose(pose);
68  out.setObjectPosition(
69  new FramedPosition(pose->toEigen(), pose->frame, pose->agent));
70  out.setObjectOrientation(
71  new FramedOrientation(pose->toEigen(), pose->frame, pose->agent));
72 
73  // Do not set optional parameter to nullptr (causes NullHandleException)
74  //out.setObjectInstanceChannel(nullptr);
75 
76  emitSuccess();
77  return;
78  }
79  }
80 
81  ARMARX_WARNING << "Object instance ID '" << objectName
82  << "' looks like an armarx::ObjectID, "
83  << "but no such object was found in ObjectPoseStorage (offering "
84  << objectPoses.size() << " objects).";
85  emitNoPoseAvailable();
86  return;
87  }
88  else
89  {
90  memoryx::ObjectMemoryObserverInterfacePrx objectMemoryObserver =
91  getObjectMemoryObserver();
92  std::vector<ChannelRefBasePtr> objectInstanceList =
93  objectMemoryObserver->getObjectInstancesByClass(objectName);
94 
95  ARMARX_INFO << "found " << objectInstanceList.size() << " instances for object "
96  << in.getObjectName();
97  if (objectInstanceList.size())
98  {
99  objectInstanceChannel = objectInstanceList.front();
100  }
101  }
102  }
103 
104  if (!objectInstanceChannel)
105  {
106  emitNoPoseAvailable();
107  return;
108  }
109 
110  ChannelRefPtr channelRef = ChannelRefPtr::dynamicCast(objectInstanceChannel);
111  if (!channelRef || !channelRef->hasDatafield("pose"))
112  {
113  ARMARX_WARNING << "unable to cast channel reference or datafield pose does not exists.";
114  emitNoPoseAvailable();
115  return;
116  }
117 
118  FramedPosePtr pose = channelRef->get<FramedPose>("pose");
119  ARMARX_INFO << "object pose is " << pose->output();
120 
121  out.setObjectPose(pose);
122  out.setObjectPosition(new FramedPosition(pose->toEigen(), pose->frame, pose->agent));
123  out.setObjectOrientation(new FramedOrientation(pose->toEigen(), pose->frame, pose->agent));
124  out.setObjectInstanceChannel(channelRef);
125 
126  emitSuccess();
127 }
128 
129 //void GetObjectPose::run()
130 //{
131 // // put your user code for the execution-phase here
132 // // runs in seperate thread, thus can do complex operations
133 // // should check constantly whether isRunningTaskStopped() returns true
134 //
135 //// uncomment this if you need a continous run function. Make sure to use sleep or use blocking wait to reduce cpu load.
136 // while (!isRunningTaskStopped()) // stop run function if returning true
137 // {
138 // // do your calculations
139 // }
140 //}
141 
142 //void GetObjectPose::onBreak()
143 //{
144 // // put your user code for the breaking point here
145 // // execution time should be short (<100ms)
146 //}
147 
148 void
150 {
151  // put your user code for the exit point here
152  // execution time should be short (<100ms)
153 }
154 
155 // DO NOT EDIT NEXT FUNCTION
158 {
159  return XMLStateFactoryBasePtr(new GetObjectPose(stateData));
160 }
armarx::ObjectID
A known object ID of the form "Dataset/ClassName" or "Dataset/ClassName/InstanceName".
Definition: ObjectID.h:10
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
armarx::objpose::ObjectPoseSeq
std::vector< ObjectPose > ObjectPoseSeq
Definition: forward_declarations.h:20
armarx::FramedPose
The FramedPose class.
Definition: FramedPose.h:280
armarx::VariantType::FramedPose
const VariantTypeId FramedPose
Definition: FramedPose.h:36
armarx::ObjectLocalization::GetObjectPose::onEnter
void onEnter() override
Definition: GetObjectPose.cpp:38
armarx::XMLStateConstructorParams
Definition: XMLState.h:49
armarx::objpose::fromIce
void fromIce(const Box &box, simox::OrientedBox< float > &oobb)
armarx::ObjectLocalization::GetObjectPose::onExit
void onExit() override
Definition: GetObjectPose.cpp:149
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:64
IceInternal::Handle< FramedPose >
FramedPose.h
ObjectPose.h
armarx::ObjectLocalization::GetObjectPose::GetObjectPose
GetObjectPose(const XMLStateConstructorParams &stateData)
Definition: GetObjectPose.h:32
GetObjectPose.h
armarx::ObjectLocalization::GetObjectPose::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: GetObjectPose.cpp:157
ObjectInstance.h
armarx::VariantType::FramedOrientation
const VariantTypeId FramedOrientation
Definition: FramedPose.h:39
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::ObjectLocalization::GetObjectPose::Registry
static SubClassRegistry Registry
Definition: GetObjectPose.h:46
armarx::VariantType::FramedPosition
const VariantTypeId FramedPosition
Definition: FramedPose.h:38
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::objpose::ObjectPose
An object pose as stored by the ObjectPoseStorage.
Definition: ObjectPose.h:33