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 #include <VirtualRobot/RobotNodeSet.h>
27 
28 #include <RobotAPI/interface/components/ViewSelectionInterface.h>
29 
30 #include "GraspObjectGroupStatechartContext.generated.h"
32 
33 
34 using namespace armarx;
35 using namespace GraspObjectGroup;
36 
37 
38 // DO NOT EDIT NEXT LINE
39 GraspObject::SubClassRegistry GraspObject::Registry(GraspObject::GetName(),
41 
43  XMLStateTemplate<GraspObject>(stateData), GraspObjectGeneratedBase<GraspObject>(stateData)
44 {
45 }
46 
47 void
49 {
50  GraspObjectGroupStatechartContext* context = getContext<GraspObjectGroupStatechartContext>();
51  context->getViewSelection()->activateAutomaticViewSelection();
52 
53  // get memory channel for the hand
54  std::string handName = in.getHandNameInMemory();
55  ChannelRefBasePtr handMemoryChannel =
56  context->getObjectMemoryObserver()->requestObjectClassRepeated(
57  handName, 150, armarx::DEFAULT_VIEWTARGET_PRIORITY);
58 
59  if (handMemoryChannel)
60  {
61  handMemoryChannel->validate();
62  local.setHandMemoryChannel(ChannelRefPtr::dynamicCast(handMemoryChannel));
63  }
64  else
65  {
66  emitInitializingGraspingFailed();
67  ARMARX_WARNING << "Unknown Object Class: " << handName;
68  }
69 
70  // get memory channel for the object
71  std::string objectName = "";
72 
73  if (in.isObjectInstanceChannelSet())
74  {
75  // make sure that the object is tracked
76  objectName = in.getObjectInstanceChannel()->getDataField("className")->getString();
77  memoryx::ObjectClassPtr obj = memoryx::ObjectClassPtr::dynamicCast(
78  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(
87  objectName,
88  1000 / in.getObjectLocalizationFrequency(),
89  armarx::DEFAULT_VIEWTARGET_PRIORITY);
90  }
91 
92  if (objectMemoryChannel)
93  {
94  local.setObjectMemoryChannel(ChannelRefPtr::dynamicCast(objectMemoryChannel));
95  }
96  else
97  {
98  ARMARX_WARNING << "Something went wrong when requesting " << objectName;
99  }
100  }
101  else
102  {
103  objectName = in.getObjectName();
104  ChannelRefBasePtr objectMemoryChannel;
105  if (in.isPregivenObjectClassMemoryChannelSet())
106  {
107  objectMemoryChannel = in.getPregivenObjectClassMemoryChannel();
108  }
109  else
110  {
111  objectMemoryChannel = context->getObjectMemoryObserver()->requestObjectClassRepeated(
112  objectName,
113  1000 / in.getObjectLocalizationFrequency(),
114  armarx::DEFAULT_VIEWTARGET_PRIORITY);
115  }
116  if (objectMemoryChannel)
117  {
118  local.setObjectMemoryChannel(ChannelRefPtr::dynamicCast(objectMemoryChannel));
119  }
120  else
121  {
122  emitInitializingGraspingFailed();
123  ARMARX_WARNING << "Unknown Object Class: " << objectName;
124  }
125  }
126 
127  std::string speechOutput = "I will now grasp the " + objectName + " with my ";
128 
129  if (handName.find("left") != std::string::npos || handName.find("Left") != std::string::npos)
130  {
131  speechOutput += "left hand";
132  }
133  else
134  {
135  speechOutput += "right hand";
136  }
137 
138  // ARMARX_INFO << "Speech output: '" << speechOutput << "'";
139  // context->getTextToSpeech()->reportText(speechOutput);
140 
141  std::string kinematicChainName = in.getKinematicChainName();
142  std::string tcpName =
143  context->getRobot()->getRobotNodeSet(kinematicChainName)->getTCP()->getName();
144  local.setTcpNameInRobotModel(tcpName);
145 }
146 
147 void
149 {
150  GraspObjectGroupStatechartContext* context = getContext<GraspObjectGroupStatechartContext>();
151 
152  // put your user code for the exit point here
153  // execution time should be short (<100ms)
154  if (!local.isHandMemoryChannelSet())
155  {
156  context->getObjectMemoryObserver()->releaseObjectClass(local.getHandMemoryChannel());
157  }
158 
159  if (!in.isPregivenObjectClassMemoryChannelSet() && local.isObjectMemoryChannelSet())
160  {
161  context->getObjectMemoryObserver()->releaseObjectClass(local.getObjectMemoryChannel());
162  }
163 }
164 
165 // DO NOT EDIT NEXT FUNCTION
166 std::string
168 {
169  return "GraspObject";
170 }
171 
172 // DO NOT EDIT NEXT FUNCTION
175 {
176  return XMLStateFactoryBasePtr(new GraspObject(stateData));
177 }
armarx::GraspObjectGroup::GraspObject::GetName
static std::string GetName()
Definition: GraspObject.cpp:167
armarx::XMLStateConstructorParams
Definition: XMLState.h:49
armarx::GraspObjectGroup::GraspObject::Registry
static SubClassRegistry Registry
Definition: GraspObject.h:46
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:64
IceInternal::Handle< ObjectClass >
ObjectMemoryObserver.h
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:146
GraspObject.h
armarx::GraspObjectGroup::GraspObject::GraspObject
GraspObject(XMLStateConstructorParams stateData)
Definition: GraspObject.cpp:42
armarx::GraspObjectGroup::GraspObject::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: GraspObject.cpp:174
armarx::GraspObjectGroup::GraspObject::onEnter
void onEnter() override
Definition: GraspObject.cpp:48
armarx::GraspObjectGroup::GraspObject
Definition: GraspObject.h:34
armarx::GraspObjectGroup::GraspObject::onExit
void onExit() override
Definition: GraspObject.cpp:148
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