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