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
32 
33 // MemoryX
35 
36 using namespace armarx;
37 using namespace memoryx;
38 using namespace memoryx::EntityWrappers;
39 
40 
42  = default;
43 
44 
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 
62 {
63  ARMARX_VERBOSE << "onConnect";
64 
65  try
66  {
67  RobotStateComponentInterfacePrx robotStateComponent = getProxy<RobotStateComponentInterfacePrx>(getProperty<std::string>("RobotStateComponentName").getValue());
68  remoteRobot.reset(new RemoteRobot(robotStateComponent->getSynchronizedRobot()));
69 
70  std::string simPrxName = getProperty<std::string>("SimulatorName").getValue();
71 
72  if (!simPrxName.empty())
73  {
74  simulatorProxy = getProxy<SimulatorInterfacePrx>(simPrxName);
75  }
76 
77 
78  // check nodes
79  VirtualRobot::RobotNodePtr rn = remoteRobot->getRobotNode(referenceFrame);
80  ARMARX_CHECK_EXPRESSION(rn) << "Reference frame not present in remote robot!";
81  rn = remoteRobot->getRobotNode(tcpNameLeft);
82  ARMARX_CHECK_EXPRESSION(rn) << "tcpNameLeft frame not present in remote robot!";
83  rn = remoteRobot->getRobotNode(tcpNameRight);
84  ARMARX_CHECK_EXPRESSION(rn) << "tcpNameRight frame not present in remote robot!";
85  }
86  catch (...)
87  {
89  }
90 }
91 
93 {
94  ARMARX_VERBOSE << "onDisConnect";
95 
96  try
97  {
98  remoteRobot.reset();
99  simulatorProxy = nullptr;
100  }
101  catch (...)
102  {
104  }
105 }
106 
107 
108 
110 {
111  ARMARX_VERBOSE << "Adding object class " << objectClassEntity->getName() << armarx::flush;
112 
113  return true;
114 }
115 
116 
117 memoryx::ObjectLocalizationResultList RobotHandLocalizationDynamicSimulation::localizeObjectClasses(const ::memoryx::ObjectClassNameList& objectClassNames, const ::Ice::Current&)
118 {
119  ARMARX_DEBUG << "localizeObjectClasses() started";
121  bool firstEntryIsLeft = objectClassNames.at(0).find("left") != std::string::npos || objectClassNames.at(0).find("Left") != std::string::npos;
122 
123  auto agentName = remoteRobot->getName();
124  PosePtr rnPoseLeft = simulatorProxy ? PosePtr::dynamicCast(simulatorProxy->getRobotNodePose(agentName, tcpNameLeft)) : new Pose(remoteRobot->getRobotNode(tcpNameLeft)->getGlobalPose());
125  PosePtr rnPoseRight = simulatorProxy ? PosePtr::dynamicCast(simulatorProxy->getRobotNodePose(agentName, tcpNameRight)) : new Pose(remoteRobot->getRobotNode(tcpNameRight)->getGlobalPose());
126  Eigen::Matrix4f tcpLeftGlobalPose = rnPoseLeft->toEigen();
127  Eigen::Matrix4f tcpRightGlobalPose = rnPoseRight->toEigen();
128  //Eigen::Matrix4f cameraPose = remoteRobot->getRobotNode(referenceFrame)->getPoseInRootFrame();
129  // convert all poses to reference frame/camera coordinates
130  Eigen::Matrix4f cameraPose = simulatorProxy ? PosePtr::dynamicCast(simulatorProxy->getRobotNodePose(agentName, referenceFrame))->toEigen() : remoteRobot->getRobotNode(referenceFrame)->getGlobalPose();
131  tcpLeftGlobalPose = cameraPose.inverse() * tcpLeftGlobalPose;
132  tcpRightGlobalPose = cameraPose.inverse() * tcpRightGlobalPose;
133 
134  armarx::FramedPositionPtr positionLeftHand = new armarx::FramedPosition(tcpLeftGlobalPose, referenceFrame, agentName);
135  armarx::FramedPositionPtr positionRightHand = new armarx::FramedPosition(tcpRightGlobalPose, referenceFrame, agentName);
136  armarx::FramedOrientationPtr orientationLeftHand = new armarx::FramedOrientation(tcpLeftGlobalPose, referenceFrame, agentName);
137  armarx::FramedOrientationPtr orientationRightHand = new armarx::FramedOrientation(tcpRightGlobalPose, referenceFrame, agentName);
138 
139  // get poses of hands and markers
140  /*Eigen::Matrix4f leftHandPose = remoteRobot->getRobotNode(tcpNameLeft)->getPoseInRootFrame();
141  Eigen::Matrix4f rightHandPose = remoteRobot->getRobotNode(tcpNameRight)->getPoseInRootFrame();
142  Eigen::Matrix4f leftMarkerPose = remoteRobot->getRobotNode(markerNameLeft)->getPoseInRootFrame();
143  Eigen::Matrix4f rightMarkerPose = remoteRobot->getRobotNode(markerNameRight)->getPoseInRootFrame();*/
144 
145  // return poses from simulator
146  memoryx::ObjectLocalizationResult result;
147  result.timeStamp = timestamp;
148 
149  memoryx::ObjectLocalizationResultList resultList;
150  result.recognitionCertainty = 1;
151 
152  if (objectClassNames.size() == 1)
153  {
154  ARMARX_DEBUG << "Returning hand pose from simulator for one hand: " << objectClassNames.at(0);
155 
156  result.objectClassName = objectClassNames.at(0);
157 
158  if (firstEntryIsLeft)
159  {
160  result.position = positionLeftHand;
161  result.orientation = orientationLeftHand;
162 
163  result.positionNoise = computePositionNoise(positionLeftHand->toEigen());
164  }
165  else
166  {
167  result.position = positionRightHand;
168  result.orientation = orientationRightHand;
169  result.positionNoise = computePositionNoise(positionRightHand->toEigen());
170  }
171  resultList.push_back(result);
172  }
173  else if (objectClassNames.size() == 2)
174  {
175  ARMARX_INFO << "Returning hand pose from simulator for two hands: " << objectClassNames.at(0) << ", " << objectClassNames.at(1);
176 
177  result.objectClassName = firstEntryIsLeft ? objectClassNames.at(0) : objectClassNames.at(1);
178  result.position = positionLeftHand;
179  result.orientation = orientationLeftHand;
180  result.positionNoise = computePositionNoise(positionLeftHand->toEigen());
181  resultList.push_back(result);
182 
183  result.objectClassName = firstEntryIsLeft ? objectClassNames.at(1) : objectClassNames.at(0);
184  result.position = positionRightHand;
185  result.orientation = orientationRightHand;
186  result.positionNoise = computePositionNoise(positionRightHand->toEigen());
187  resultList.push_back(result);
188  }
189  else
190  {
191  ARMARX_WARNING << "More than 2 hands requested - this case is not handeled here";
192  }
193 
194 
195  ARMARX_DEBUG << "Finished localization, returning.";
196 
197  return resultList;
198 }
199 
200 
201 ::memoryx::MultivariateNormalDistributionPtr RobotHandLocalizationDynamicSimulation::computePositionNoise(const Eigen::Vector3f& pos)
202 {
204  mean.push_back(pos(0));
205  mean.push_back(pos(1));
206  mean.push_back(pos(2));
207 
208  // assuming 2 mm noise
210  posDist->setMean(mean);
211  posDist->setCovariance(0, 0, 2.0f);
212  posDist->setCovariance(1, 1, 2.0f);
213  posDist->setCovariance(2, 2, 2.0f);
214  return posDist;
215 }
216 
armarx::RobotHandLocalizationDynamicSimulation::RobotHandLocalizationDynamicSimulation
RobotHandLocalizationDynamicSimulation()
RemoteRobot.h
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
armarx::RobotHandLocalizationDynamicSimulation::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: RobotHandLocalizationDynamicSimulation.cpp:92
armarx::navigation::core::Pose
Eigen::Isometry3f Pose
Definition: basic_types.h:31
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:111
IceInternal::Handle
Definition: forward_declarations.h:8
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:109
armarx::RobotHandLocalizationDynamicSimulation::localizeObjectClasses
memoryx::ObjectLocalizationResultList localizeObjectClasses(const ::memoryx::ObjectClassNameList &, const ::Ice::Current &=Ice::emptyCurrent) override
localize one or both hand markers
Definition: RobotHandLocalizationDynamicSimulation.cpp:117
armarx::mean
std::optional< float > mean(const boost::circular_buffer< NameValueMap > &buffer, const std::string &key)
Definition: KinematicUnitGuiPlugin.cpp:1615
FramedPose.h
RobotHandLocalizationDynamicSimulation.h
armarx::FloatVector
::std::vector< ::Ice::Float > FloatVector
Definition: KinematicUnitGuiPlugin.h:327
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
TimestampVariant.h
computePositionNoise
::memoryx::MultivariateNormalDistributionPtr computePositionNoise(const Eigen::Vector3f &pos)
Definition: FakeWorkingMemoryObjectLocalizer.cpp:66
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:139
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
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:28
armarx::VariantType::FramedOrientation
const VariantTypeId FramedOrientation
Definition: FramedPose.h:40
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
memoryx::GridFileManagerPtr
std::shared_ptr< GridFileManager > GridFileManagerPtr
Definition: AbstractEntityWrapper.h:32
Entity.h
IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface >
armarx::VariantType::FramedPosition
const VariantTypeId FramedPosition
Definition: FramedPose.h:39
armarx::handleExceptions
void handleExceptions()
Definition: Exception.cpp:141
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
ArmarXDataPath.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::VariantType::MultivariateNormalDistribution
const armarx::VariantTypeId MultivariateNormalDistribution
Definition: ProbabilityMeasures.h:36