WaitForHandAndObjectChannel.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::PlaceObjectGroup
19  * @author David ( david dot schiebener at kit dot edu )
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
26 
28 #include <RobotAPI/interface/components/ViewSelectionInterface.h>
29 
30 
31 using namespace armarx;
32 using namespace PlaceObjectGroup;
33 
34 // DO NOT EDIT NEXT LINE
35 WaitForHandAndObjectChannel::SubClassRegistry WaitForHandAndObjectChannel::Registry(WaitForHandAndObjectChannel::GetName(), &WaitForHandAndObjectChannel::CreateInstance);
36 
37 
38 
40  XMLStateTemplate<WaitForHandAndObjectChannel>(stateData), WaitForHandAndObjectChannelGeneratedBase<WaitForHandAndObjectChannel>(stateData)
41 {
42 }
43 
45 {
46  PlaceObjectGroupStatechartContextBase* context = getContext<PlaceObjectGroupStatechartContextBase>();
47 
48  if (context->getObjectMemoryObserver()->getObjectInstancesByClass(in.getObjectName()).size() == 0)
49  {
50  ARMARX_VERBOSE << "Requesting object " << in.getObjectName();
51  ChannelRefBasePtr objectMemoryChannel = context->getObjectMemoryObserver()->requestObjectClassRepeated(in.getObjectName(), 200, armarx::DEFAULT_VIEWTARGET_PRIORITY);
52 
53  if (objectMemoryChannel)
54  {
55  local.setObjectMemoryChannel(ChannelRefPtr::dynamicCast(objectMemoryChannel));
56  }
57  }
58 
59  if (context->getObjectMemoryObserver()->getObjectInstancesByClass(in.getHandNameInMemory()).size() == 0)
60  {
61  ARMARX_VERBOSE << "Requesting hand " << in.getHandNameInMemory();
62  ChannelRefBasePtr handMemoryChannel = context->getObjectMemoryObserver()->requestObjectClassRepeated(in.getHandNameInMemory(), 300, armarx::DEFAULT_VIEWTARGET_PRIORITY);
63 
64  if (handMemoryChannel)
65  {
66  //handMemoryChannel->validate();
67  local.setHandMemoryChannel(ChannelRefPtr::dynamicCast(handMemoryChannel));
68  }
69  }
70 }
71 
73 {
74  int waitSec = in.getWaitUntilProcessSec();
75  if (waitSec > 0)
76  {
77  TimeUtil::MSSleep(waitSec * 1000);
78  }
79  else
80  {
81  TimeUtil::MSSleep(1000);
82  }
83 
84  PlaceObjectGroupStatechartContextBase* context = getContext<PlaceObjectGroupStatechartContextBase>();
85  bool bothAvailable = false;
86  memoryx::ChannelRefBaseSequence objectList;
87  armarx::ChannelRefPtr objChannel;
88  memoryx::ChannelRefBaseSequence handList;
89  armarx::ChannelRefPtr handChannel;
90 
91  while (!isRunningTaskStopped() && !bothAvailable) // stop run function if returning true
92  {
93  handList = context->getObjectMemoryObserver()->getObjectInstancesByClass(in.getHandNameInMemory());
94 
95  if (handList.size() > 0)
96  {
97  handChannel = ChannelRefPtr::dynamicCast(handList.at(0));
98  }
99  else
100  {
101  ARMARX_VERBOSE << "Hand " << in.getHandNameInMemory() << " not yet localized";
102  TimeUtil::MSSleep(10);
103  }
104 
105  objectList = context->getObjectMemoryObserver()->getObjectInstancesByClass(in.getObjectName());
106 
107  if (objectList.size() > 0)
108  {
109  objChannel = ChannelRefPtr::dynamicCast(objectList.at(0));
110  }
111  else
112  {
113  ARMARX_VERBOSE << "Object " << in.getObjectName() << " not localized";
114  TimeUtil::MSSleep(10);
115  }
116 
117 
118  if (handChannel)
119  {
120  // sometimes the object cannot be localized, so we are happy with the hand channel...
121  if (objChannel)
122  {
123  out.setObjectChannelRef(objChannel);
124  }
125 
126  out.setHandChannelRef(handChannel);
127  out.setObjectClassName(in.getObjectName());
128  bothAvailable = true;
129  emitHandAndObjectLocalized();
130  }
131  }
132 }
133 
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 {
151 }
152 
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
armarx::PlaceObjectGroup::WaitForHandAndObjectChannel::WaitForHandAndObjectChannel
WaitForHandAndObjectChannel(const XMLStateConstructorParams &stateData)
Definition: WaitForHandAndObjectChannel.cpp:39
WaitForHandAndObjectChannel.h
armarx::PlaceObjectGroup::WaitForHandAndObjectChannel::onEnter
void onEnter() override
Definition: WaitForHandAndObjectChannel.cpp:44
armarx::TimeUtil::MSSleep
static void MSSleep(int durationMS)
lock the calling thread for a given duration (like usleep(...) but using Timeserver time)
Definition: TimeUtil.cpp:94
armarx::XMLStateConstructorParams
Definition: XMLState.h:50
armarx::PlaceObjectGroup::WaitForHandAndObjectChannel::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: WaitForHandAndObjectChannel.cpp:148
IceInternal::Handle< ChannelRef >
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:137
armarx::PlaceObjectGroup::WaitForHandAndObjectChannel
Definition: WaitForHandAndObjectChannel.h:31
armarx::PlaceObjectGroup::WaitForHandAndObjectChannel::onExit
void onExit() override
Definition: WaitForHandAndObjectChannel.cpp:140
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
TimeUtil.h
armarx::PlaceObjectGroup::WaitForHandAndObjectChannel::run
void run() override
Definition: WaitForHandAndObjectChannel.cpp:72
armarx::PlaceObjectGroup::WaitForHandAndObjectChannel::onBreak
void onBreak() override
Definition: WaitForHandAndObjectChannel.cpp:134
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::PlaceObjectGroup::WaitForHandAndObjectChannel::Registry
static SubClassRegistry Registry
Definition: WaitForHandAndObjectChannel.h:45