GetObjectInstance.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::VisualServoGroup
19  * @author Mirko Waechter ( waechter 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 
25 #include "GetObjectInstance.h"
26 
28 
29 #include "VisualServoGroupStatechartContext.generated.h"
30 
31 using namespace armarx;
32 using namespace VisualServoGroup;
33 
34 // DO NOT EDIT NEXT LINE
35 GetObjectInstance::SubClassRegistry GetObjectInstance::Registry(GetObjectInstance::GetName(),
37 
40  GetObjectInstanceGeneratedBase<GetObjectInstance>(stateData)
41 {
42 }
43 
44 void
46 {
47  VisualServoGroupStatechartContext* context = getContext<VisualServoGroupStatechartContext>();
48 
49 
50  ChannelRefPtr objectInstance;
51 
52  if (in.isObjectInstanceChannelSet())
53  {
54  objectInstance = in.getObjectInstanceChannel();
55  }
56  else if (in.isObjectClassChannelSet())
57  {
58  ChannelRefPtr objectMemoryChannel = in.getObjectClassChannel();
59 
60  if (!context->getObjectMemoryObserver())
61  {
62  ARMARX_WARNING << "getObjectMemoryObserver is NULL";
63  setTimeoutEvent(500, createEventObjectNotYetLocalized());
64  return;
65  }
66 
67  memoryx::ChannelRefBaseSequence instances =
68  context->getObjectMemoryObserver()->getObjectInstances(objectMemoryChannel);
69 
70  if (instances.size() == 0)
71  {
72  ARMARX_WARNING << "No instances of the object in the memory - obj channel: "
73  << objectMemoryChannel->output();
74  setTimeoutEvent(500, createEventObjectNotYetLocalized());
75  return;
76  }
77 
78  objectInstance = ChannelRefPtr::dynamicCast(instances.front());
79  }
80  else
81  {
82  throw LocalException("Either ObjectInstanceChannel or ObjectMemoryChannel must be set in " +
83  getStateClassName());
84  }
85 
86 
87  ARMARX_CHECK_EXPRESSION(objectInstance);
88  out.setObjectInstanceChannel(objectInstance);
89  ARMARX_INFO << "Checking existenceCertainty: "
90  << objectInstance->getDataField("existenceCertainty")->getFloat() << " > "
91  << in.getMinimumExistenceCertainty();
92  ARMARX_INFO << "Checking positionUncertainty: "
93  << objectInstance->get<float>("uncertaintyOfPosition") << " < "
94  << in.getMaximalPositionUncertainty();
95 
96  float existenceCertainty = objectInstance->getDataField("existenceCertainty")->getFloat();
97  if (existenceCertainty >= in.getMinimumExistenceCertainty())
98  {
99  float positionUncertainty = objectInstance->get<float>("uncertaintyOfPosition");
100 
101  if (existenceCertainty >= in.getGoodExistenceCertainty() &&
102  positionUncertainty <= in.getMaximalPositionUncertainty())
103  {
104  if (in.getRequestTCPControlUnit())
105  {
106  getTcpControlUnit()->request();
107  }
108  emitObjectInstanceChannelFound();
109  }
110  else
111  {
112  ARMARX_VERBOSE << "Waiting for better localization. Existence certainty: "
113  << existenceCertainty << " (min: " << in.getGoodExistenceCertainty()
114  << "), position uncertainty: " << positionUncertainty
115  << " (max: " << in.getMaximalPositionUncertainty() << ")";
116  if (in.getRequestTCPControlUnit())
117  {
118  getTcpControlUnit()->release();
119  }
120  TimeUtil::MSSleep(100);
121  emitWaitForBetterLocalization();
122  }
123  }
124  else
125  {
126  ARMARX_WARNING << "Object was found earlier but is lost now: Existence certainty is only "
127  << existenceCertainty;
128  emitObjectNotFound();
129  }
130 }
131 
132 void
134 {
135  // put your user code for the execution-phase here
136  // runs in seperate thread, thus can do complex operations
137  // should check constantly whether isRunningTaskStopped() returns true
138 
139  // uncomment this if you need a continous run function. Make sure to use sleep or use blocking wait to reduce cpu load.
140  // while (!isRunningTaskStopped()) // stop run function if returning true
141  // {
142  // // do your calculations
143  // }
144 }
145 
146 void
148 {
149  // put your user code for the breaking point here
150  // execution time should be short (<100ms)
151 }
152 
153 void
155 {
156  // put your user code for the exit point here
157  // execution time should be short (<100ms)
158 }
159 
160 // DO NOT EDIT NEXT FUNCTION
163 {
164  return XMLStateFactoryBasePtr(new GetObjectInstance(stateData));
165 }
armarx::VisualServoGroup::GetObjectInstance::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: GetObjectInstance.cpp:162
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
armarx::VisualServoGroup::GetObjectInstance::Registry
static SubClassRegistry Registry
Definition: GetObjectInstance.h: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:100
armarx::VisualServoGroup::GetObjectInstance::GetObjectInstance
GetObjectInstance(const XMLStateConstructorParams &stateData)
Definition: GetObjectInstance.cpp:38
armarx::XMLStateConstructorParams
Definition: XMLState.h:49
armarx::VisualServoGroup::GetObjectInstance::run
void run() override
Definition: GetObjectInstance.cpp:133
GetObjectInstance.h
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:64
IceInternal::Handle< ChannelRef >
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:146
armarx::VisualServoGroup::GetObjectInstance::onExit
void onExit() override
Definition: GetObjectInstance.cpp:154
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::VisualServoGroup::GetObjectInstance::onEnter
void onEnter() override
Definition: GetObjectInstance.cpp:45
armarx::VisualServoGroup::GetObjectInstance
Definition: GetObjectInstance.h:31
armarx::VisualServoGroup::GetObjectInstance::onBreak
void onBreak() override
Definition: GetObjectInstance.cpp:147
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