RobotHandLocalizationDynamicSimulation.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2013-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 VisionX::Component
19  * @author David Schiebener (schiebener at kit dot edu)
20  * @date 2013
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
26 
27 // Core
30 
33 
34 // MemoryX
36 
37 using namespace armarx;
38 using namespace memoryx;
39 using namespace memoryx::EntityWrappers;
40 
41 
43 
44 void
46 {
47  usingProxy(getProperty<std::string>("RobotStateComponentName").getValue());
48  tcpNameLeft = getProperty<std::string>("TCPNameLeft").getValue();
49  tcpNameRight = getProperty<std::string>("TCPNameRight").getValue();
50  referenceFrame = getProperty<std::string>("ReferenceFrameName").getValue();
51 
52  std::string simPrxName = getProperty<std::string>("SimulatorName").getValue();
53 
54  if (!simPrxName.empty())
55  {
56  usingProxy(simPrxName);
57  }
58 }
59 
60 void
62 {
63  ARMARX_VERBOSE << "onConnect";
64 
65  try
66  {
67  RobotStateComponentInterfacePrx robotStateComponent =
68  getProxy<RobotStateComponentInterfacePrx>(
69  getProperty<std::string>("RobotStateComponentName").getValue());
70  remoteRobot.reset(new RemoteRobot(robotStateComponent->getSynchronizedRobot()));
71 
72  std::string simPrxName = getProperty<std::string>("SimulatorName").getValue();
73 
74  if (!simPrxName.empty())
75  {
76  simulatorProxy = getProxy<SimulatorInterfacePrx>(simPrxName);
77  }
78 
79 
80  // check nodes
81  VirtualRobot::RobotNodePtr rn = remoteRobot->getRobotNode(referenceFrame);
82  ARMARX_CHECK_EXPRESSION(rn) << "Reference frame not present in remote robot!";
83  rn = remoteRobot->getRobotNode(tcpNameLeft);
84  ARMARX_CHECK_EXPRESSION(rn) << "tcpNameLeft frame not present in remote robot!";
85  rn = remoteRobot->getRobotNode(tcpNameRight);
86  ARMARX_CHECK_EXPRESSION(rn) << "tcpNameRight frame not present in remote robot!";
87  }
88  catch (...)
89  {
91  }
92 }
93 
94 void
96 {
97  ARMARX_VERBOSE << "onDisConnect";
98 
99  try
100  {
101  remoteRobot.reset();
102  simulatorProxy = nullptr;
103  }
104  catch (...)
105  {
107  }
108 }
109 
110 bool
112  const memoryx::EntityPtr& objectClassEntity,
113  const memoryx::GridFileManagerPtr& fileManager)
114 {
115  ARMARX_VERBOSE << "Adding object class " << objectClassEntity->getName() << armarx::flush;
116 
117  return true;
118 }
119 
120 memoryx::ObjectLocalizationResultList
122  const ::memoryx::ObjectClassNameList& objectClassNames,
123  const ::Ice::Current&)
124 {
125  ARMARX_DEBUG << "localizeObjectClasses() started";
127  bool firstEntryIsLeft = objectClassNames.at(0).find("left") != std::string::npos ||
128  objectClassNames.at(0).find("Left") != std::string::npos;
129 
130  auto agentName = remoteRobot->getName();
131  PosePtr rnPoseLeft =
132  simulatorProxy
133  ? PosePtr::dynamicCast(simulatorProxy->getRobotNodePose(agentName, tcpNameLeft))
134  : new Pose(remoteRobot->getRobotNode(tcpNameLeft)->getGlobalPose());
135  PosePtr rnPoseRight =
136  simulatorProxy
137  ? PosePtr::dynamicCast(simulatorProxy->getRobotNodePose(agentName, tcpNameRight))
138  : new Pose(remoteRobot->getRobotNode(tcpNameRight)->getGlobalPose());
139  Eigen::Matrix4f tcpLeftGlobalPose = rnPoseLeft->toEigen();
140  Eigen::Matrix4f tcpRightGlobalPose = rnPoseRight->toEigen();
141  //Eigen::Matrix4f cameraPose = remoteRobot->getRobotNode(referenceFrame)->getPoseInRootFrame();
142  // convert all poses to reference frame/camera coordinates
143  Eigen::Matrix4f cameraPose =
144  simulatorProxy
145  ? PosePtr::dynamicCast(simulatorProxy->getRobotNodePose(agentName, referenceFrame))
146  ->toEigen()
147  : remoteRobot->getRobotNode(referenceFrame)->getGlobalPose();
148  tcpLeftGlobalPose = cameraPose.inverse() * tcpLeftGlobalPose;
149  tcpRightGlobalPose = cameraPose.inverse() * tcpRightGlobalPose;
150 
151  armarx::FramedPositionPtr positionLeftHand =
152  new armarx::FramedPosition(tcpLeftGlobalPose, referenceFrame, agentName);
153  armarx::FramedPositionPtr positionRightHand =
154  new armarx::FramedPosition(tcpRightGlobalPose, referenceFrame, agentName);
155  armarx::FramedOrientationPtr orientationLeftHand =
156  new armarx::FramedOrientation(tcpLeftGlobalPose, referenceFrame, agentName);
157  armarx::FramedOrientationPtr orientationRightHand =
158  new armarx::FramedOrientation(tcpRightGlobalPose, referenceFrame, agentName);
159 
160  // get poses of hands and markers
161  /*Eigen::Matrix4f leftHandPose = remoteRobot->getRobotNode(tcpNameLeft)->getPoseInRootFrame();
162  Eigen::Matrix4f rightHandPose = remoteRobot->getRobotNode(tcpNameRight)->getPoseInRootFrame();
163  Eigen::Matrix4f leftMarkerPose = remoteRobot->getRobotNode(markerNameLeft)->getPoseInRootFrame();
164  Eigen::Matrix4f rightMarkerPose = remoteRobot->getRobotNode(markerNameRight)->getPoseInRootFrame();*/
165 
166  // return poses from simulator
167  memoryx::ObjectLocalizationResult result;
168  result.timeStamp = timestamp;
169 
170  memoryx::ObjectLocalizationResultList resultList;
171  result.recognitionCertainty = 1;
172 
173  if (objectClassNames.size() == 1)
174  {
175  ARMARX_DEBUG << "Returning hand pose from simulator for one hand: "
176  << objectClassNames.at(0);
177 
178  result.objectClassName = objectClassNames.at(0);
179 
180  if (firstEntryIsLeft)
181  {
182  result.position = positionLeftHand;
183  result.orientation = orientationLeftHand;
184 
185  result.positionNoise = computePositionNoise(positionLeftHand->toEigen());
186  }
187  else
188  {
189  result.position = positionRightHand;
190  result.orientation = orientationRightHand;
191  result.positionNoise = computePositionNoise(positionRightHand->toEigen());
192  }
193  resultList.push_back(result);
194  }
195  else if (objectClassNames.size() == 2)
196  {
197  ARMARX_INFO << "Returning hand pose from simulator for two hands: "
198  << objectClassNames.at(0) << ", " << objectClassNames.at(1);
199 
200  result.objectClassName = firstEntryIsLeft ? objectClassNames.at(0) : objectClassNames.at(1);
201  result.position = positionLeftHand;
202  result.orientation = orientationLeftHand;
203  result.positionNoise = computePositionNoise(positionLeftHand->toEigen());
204  resultList.push_back(result);
205 
206  result.objectClassName = firstEntryIsLeft ? objectClassNames.at(1) : objectClassNames.at(0);
207  result.position = positionRightHand;
208  result.orientation = orientationRightHand;
209  result.positionNoise = computePositionNoise(positionRightHand->toEigen());
210  resultList.push_back(result);
211  }
212  else
213  {
214  ARMARX_WARNING << "More than 2 hands requested - this case is not handeled here";
215  }
216 
217 
218  ARMARX_DEBUG << "Finished localization, returning.";
219 
220  return resultList;
221 }
222 
224 RobotHandLocalizationDynamicSimulation::computePositionNoise(const Eigen::Vector3f& pos)
225 {
227  mean.push_back(pos(0));
228  mean.push_back(pos(1));
229  mean.push_back(pos(2));
230 
231  // assuming 2 mm noise
233  posDist->setMean(mean);
234  posDist->setCovariance(0, 0, 2.0f);
235  posDist->setCovariance(1, 1, 2.0f);
236  posDist->setCovariance(2, 2, 2.0f);
237  return posDist;
238 }
armarx::RobotHandLocalizationDynamicSimulation::RobotHandLocalizationDynamicSimulation
RobotHandLocalizationDynamicSimulation()
RemoteRobot.h
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
armarx::RobotHandLocalizationDynamicSimulation::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: RobotHandLocalizationDynamicSimulation.cpp:95
armarx::navigation::core::Pose
Eigen::Isometry3f Pose
Definition: basic_types.h:31
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
armarx::RobotHandLocalizationDynamicSimulation::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: RobotHandLocalizationDynamicSimulation.cpp:45
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
armarx::TimestampVariant::nowPtr
static TimestampVariantPtr nowPtr()
Definition: TimestampVariant.h:126
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::FloatVector
::std::vector<::Ice::Float > FloatVector
Definition: KinematicUnitGuiPlugin.h:325
armarx::RobotHandLocalizationDynamicSimulation::addObjectClass
virtual bool addObjectClass(const memoryx::EntityPtr &objectClassEntity, const memoryx::GridFileManagerPtr &fileManager)
Add a memory entity representing the hand marker in order to set its properties.
Definition: RobotHandLocalizationDynamicSimulation.cpp:111
armarx::RobotHandLocalizationDynamicSimulation::localizeObjectClasses
memoryx::ObjectLocalizationResultList localizeObjectClasses(const ::memoryx::ObjectClassNameList &, const ::Ice::Current &=Ice::emptyCurrent) override
localize one or both hand markers
Definition: RobotHandLocalizationDynamicSimulation.cpp:121
armarx::mean
std::optional< float > mean(const boost::circular_buffer< NameValueMap > &buffer, const std::string &key)
Definition: KinematicUnitGuiPlugin.cpp:1620
FramedPose.h
RobotHandLocalizationDynamicSimulation.h
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
TimestampVariant.h
computePositionNoise
::memoryx::MultivariateNormalDistributionPtr computePositionNoise(const Eigen::Vector3f &pos)
Definition: FakeWorkingMemoryObjectLocalizer.cpp:68
armarx::RobotHandLocalizationDynamicSimulation::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: RobotHandLocalizationDynamicSimulation.cpp:61
armarx::RemoteRobot
Mimics the behaviour of the VirtualRobot::Robot class while redirecting everything to an Ice proxy.
Definition: RemoteRobot.h:144
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
memoryx::EntityWrappers
Definition: AbstractEntityWrapper.cpp:30
armarx::VariantType::FramedOrientation
const VariantTypeId FramedOrientation
Definition: FramedPose.h:39
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
memoryx::GridFileManagerPtr
std::shared_ptr< GridFileManager > GridFileManagerPtr
Definition: AbstractEntityWrapper.h:33
Entity.h
IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface >
armarx::VariantType::FramedPosition
const VariantTypeId FramedPosition
Definition: FramedPose.h:38
armarx::handleExceptions
void handleExceptions()
Definition: Exception.cpp:157
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
ArmarXDataPath.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::VariantType::MultivariateNormalDistribution
const armarx::VariantTypeId MultivariateNormalDistribution
Definition: ProbabilityMeasures.h:40