GraspObject.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2014-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package RobotSkillTemplates::GraspObjectGroup
19  * @author Mirko Waechter ( waechter at kit dot edu )
20  * @date 2014
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "GraspObject.h"
26 
27 #include "GraspObjectGroupStatechartContext.generated.h"
28 
29 
31 #include <RobotAPI/interface/components/ViewSelectionInterface.h>
32 
33 
34 using namespace armarx;
35 using namespace GraspObjectGroup;
36 
37 
38 // DO NOT EDIT NEXT LINE
40 
41 
42 
44  XMLStateTemplate < GraspObject > (stateData),
45  GraspObjectGeneratedBase<GraspObject> (stateData)
46 {
47 }
48 
49 
50 
52 {
53  GraspObjectGroupStatechartContext* context = getContext<GraspObjectGroupStatechartContext>();
54  context->getViewSelection()->activateAutomaticViewSelection();
55 
56  // get memory channel for the hand
57  std::string handName = in.getHandNameInMemory();
58  ChannelRefBasePtr handMemoryChannel = context->getObjectMemoryObserver()->requestObjectClassRepeated(handName, 150, armarx::DEFAULT_VIEWTARGET_PRIORITY);
59 
60  if (handMemoryChannel)
61  {
62  handMemoryChannel->validate();
63  local.setHandMemoryChannel(ChannelRefPtr::dynamicCast(handMemoryChannel));
64  }
65  else
66  {
67  emitInitializingGraspingFailed();
68  ARMARX_WARNING << "Unknown Object Class: " << handName;
69  }
70 
71  // get memory channel for the object
72  std::string objectName = "";
73 
74  if (in.isObjectInstanceChannelSet())
75  {
76  // make sure that the object is tracked
77  objectName = in.getObjectInstanceChannel()->getDataField("className")->getString();
78  memoryx::ObjectClassPtr obj = memoryx::ObjectClassPtr::dynamicCast(context->getWorkingMemory()->getObjectClassesSegment()->getEntityByName(objectName));
79  ChannelRefBasePtr objectMemoryChannel;
80  if (in.isPregivenObjectClassMemoryChannelSet())
81  {
82  objectMemoryChannel = in.getPregivenObjectClassMemoryChannel();
83  }
84  else
85  {
86  objectMemoryChannel = context->getObjectMemoryObserver()->requestObjectClassRepeated(objectName, 1000 / in.getObjectLocalizationFrequency(), armarx::DEFAULT_VIEWTARGET_PRIORITY);
87  }
88 
89  if (objectMemoryChannel)
90  {
91  local.setObjectMemoryChannel(ChannelRefPtr::dynamicCast(objectMemoryChannel));
92  }
93  else
94  {
95  ARMARX_WARNING << "Something went wrong when requesting " << objectName;
96  }
97  }
98  else
99  {
100  objectName = in.getObjectName();
101  ChannelRefBasePtr objectMemoryChannel;
102  if (in.isPregivenObjectClassMemoryChannelSet())
103  {
104  objectMemoryChannel = in.getPregivenObjectClassMemoryChannel();
105  }
106  else
107  {
108  objectMemoryChannel = context->getObjectMemoryObserver()->requestObjectClassRepeated(objectName, 1000 / in.getObjectLocalizationFrequency(), armarx::DEFAULT_VIEWTARGET_PRIORITY);
109  }
110  if (objectMemoryChannel)
111  {
112  local.setObjectMemoryChannel(ChannelRefPtr::dynamicCast(objectMemoryChannel));
113  }
114  else
115  {
116  emitInitializingGraspingFailed();
117  ARMARX_WARNING << "Unknown Object Class: " << objectName;
118  }
119  }
120 
121  std::string speechOutput = "I will now grasp the " + objectName + " with my ";
122 
123  if (handName.find("left") != std::string::npos || handName.find("Left") != std::string::npos)
124  {
125  speechOutput += "left hand";
126  }
127  else
128  {
129  speechOutput += "right hand";
130  }
131 
132  // ARMARX_INFO << "Speech output: '" << speechOutput << "'";
133  // context->getTextToSpeech()->reportText(speechOutput);
134 
135  std::string kinematicChainName = in.getKinematicChainName();
136  std::string tcpName = context->getRobot()->getRobotNodeSet(kinematicChainName)->getTCP()->getName();
137  local.setTcpNameInRobotModel(tcpName);
138 }
139 
140 
141 
142 
143 
144 
146 {
147  GraspObjectGroupStatechartContext* context = getContext<GraspObjectGroupStatechartContext>();
148 
149  // put your user code for the exit point here
150  // execution time should be short (<100ms)
151  if (!local.isHandMemoryChannelSet())
152  {
153  context->getObjectMemoryObserver()->releaseObjectClass(local.getHandMemoryChannel());
154  }
155 
156  if (!in.isPregivenObjectClassMemoryChannelSet() && local.isObjectMemoryChannelSet())
157  {
158  context->getObjectMemoryObserver()->releaseObjectClass(local.getObjectMemoryChannel());
159  }
160 }
161 
162 
163 
164 // DO NOT EDIT NEXT FUNCTION
165 std::string GraspObject::GetName()
166 {
167  return "GraspObject";
168 }
169 
170 
171 
172 // DO NOT EDIT NEXT FUNCTION
174 {
175  return XMLStateFactoryBasePtr(new GraspObject(stateData));
176 }
177 
armarx::GraspObjectGroup::GraspObject::GetName
static std::string GetName()
Definition: GraspObject.cpp:165
armarx::XMLStateConstructorParams
Definition: XMLState.h:50
armarx::GraspObjectGroup::GraspObject::Registry
static SubClassRegistry Registry
Definition: GraspObject.h:46
IceInternal::Handle< ObjectClass >
ObjectMemoryObserver.h
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:137
GraspObject.h
armarx::GraspObjectGroup::GraspObject::GraspObject
GraspObject(XMLStateConstructorParams stateData)
Definition: GraspObject.cpp:43
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
armarx::GraspObjectGroup::GraspObject::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: GraspObject.cpp:173
armarx::GraspObjectGroup::GraspObject::onEnter
void onEnter() override
Definition: GraspObject.cpp:51
armarx::GraspObjectGroup::GraspObject
Definition: GraspObject.h:33
armarx::GraspObjectGroup::GraspObject::onExit
void onExit() override
Definition: GraspObject.cpp:145
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