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
28
29// Core
32
35
36// MemoryX
38
39using namespace armarx;
40using namespace memoryx;
41using namespace memoryx::EntityWrappers;
42
43
45
46void
48{
49 usingProxy(getProperty<std::string>("RobotStateComponentName").getValue());
50 tcpNameLeft = getProperty<std::string>("TCPNameLeft").getValue();
51 tcpNameRight = getProperty<std::string>("TCPNameRight").getValue();
52 referenceFrame = getProperty<std::string>("ReferenceFrameName").getValue();
53
54 std::string simPrxName = getProperty<std::string>("SimulatorName").getValue();
55
56 if (!simPrxName.empty())
57 {
58 usingProxy(simPrxName);
59 }
60}
61
62void
64{
65 ARMARX_VERBOSE << "onConnect";
66
67 try
68 {
69 RobotStateComponentInterfacePrx robotStateComponent =
71 getProperty<std::string>("RobotStateComponentName").getValue());
72 remoteRobot.reset(new RemoteRobot(robotStateComponent->getSynchronizedRobot()));
73
74 std::string simPrxName = getProperty<std::string>("SimulatorName").getValue();
75
76 if (!simPrxName.empty())
77 {
78 simulatorProxy = getProxy<SimulatorInterfacePrx>(simPrxName);
79 }
80
81
82 // check nodes
83 VirtualRobot::RobotNodePtr rn = remoteRobot->getRobotNode(referenceFrame);
84 ARMARX_CHECK_EXPRESSION(rn) << "Reference frame not present in remote robot!";
85 rn = remoteRobot->getRobotNode(tcpNameLeft);
86 ARMARX_CHECK_EXPRESSION(rn) << "tcpNameLeft frame not present in remote robot!";
87 rn = remoteRobot->getRobotNode(tcpNameRight);
88 ARMARX_CHECK_EXPRESSION(rn) << "tcpNameRight frame not present in remote robot!";
89 }
90 catch (...)
91 {
93 }
94}
95
96void
98{
99 ARMARX_VERBOSE << "onDisConnect";
100
101 try
102 {
103 remoteRobot.reset();
104 simulatorProxy = nullptr;
105 }
106 catch (...)
107 {
109 }
110}
111
112bool
114 const memoryx::EntityPtr& objectClassEntity,
115 const memoryx::GridFileManagerPtr& fileManager)
116{
117 ARMARX_VERBOSE << "Adding object class " << objectClassEntity->getName() << armarx::flush;
118
119 return true;
120}
121
122memoryx::ObjectLocalizationResultList
124 const ::memoryx::ObjectClassNameList& objectClassNames,
125 const ::Ice::Current&)
126{
127 ARMARX_DEBUG << "localizeObjectClasses() started";
129 bool firstEntryIsLeft = objectClassNames.at(0).find("left") != std::string::npos ||
130 objectClassNames.at(0).find("Left") != std::string::npos;
131
132 auto agentName = remoteRobot->getName();
133 PosePtr rnPoseLeft =
134 simulatorProxy
135 ? PosePtr::dynamicCast(simulatorProxy->getRobotNodePose(agentName, tcpNameLeft))
136 : new Pose(remoteRobot->getRobotNode(tcpNameLeft)->getGlobalPose());
137 PosePtr rnPoseRight =
138 simulatorProxy
139 ? PosePtr::dynamicCast(simulatorProxy->getRobotNodePose(agentName, tcpNameRight))
140 : new Pose(remoteRobot->getRobotNode(tcpNameRight)->getGlobalPose());
141 Eigen::Matrix4f tcpLeftGlobalPose = rnPoseLeft->toEigen();
142 Eigen::Matrix4f tcpRightGlobalPose = rnPoseRight->toEigen();
143 //Eigen::Matrix4f cameraPose = remoteRobot->getRobotNode(referenceFrame)->getPoseInRootFrame();
144 // convert all poses to reference frame/camera coordinates
145 Eigen::Matrix4f cameraPose =
146 simulatorProxy
147 ? PosePtr::dynamicCast(simulatorProxy->getRobotNodePose(agentName, referenceFrame))
148 ->toEigen()
149 : remoteRobot->getRobotNode(referenceFrame)->getGlobalPose();
150 tcpLeftGlobalPose = cameraPose.inverse() * tcpLeftGlobalPose;
151 tcpRightGlobalPose = cameraPose.inverse() * tcpRightGlobalPose;
152
153 armarx::FramedPositionPtr positionLeftHand =
154 new armarx::FramedPosition(tcpLeftGlobalPose, referenceFrame, agentName);
155 armarx::FramedPositionPtr positionRightHand =
156 new armarx::FramedPosition(tcpRightGlobalPose, referenceFrame, agentName);
157 armarx::FramedOrientationPtr orientationLeftHand =
158 new armarx::FramedOrientation(tcpLeftGlobalPose, referenceFrame, agentName);
159 armarx::FramedOrientationPtr orientationRightHand =
160 new armarx::FramedOrientation(tcpRightGlobalPose, referenceFrame, agentName);
161
162 // get poses of hands and markers
163 /*Eigen::Matrix4f leftHandPose = remoteRobot->getRobotNode(tcpNameLeft)->getPoseInRootFrame();
164 Eigen::Matrix4f rightHandPose = remoteRobot->getRobotNode(tcpNameRight)->getPoseInRootFrame();
165 Eigen::Matrix4f leftMarkerPose = remoteRobot->getRobotNode(markerNameLeft)->getPoseInRootFrame();
166 Eigen::Matrix4f rightMarkerPose = remoteRobot->getRobotNode(markerNameRight)->getPoseInRootFrame();*/
167
168 // return poses from simulator
169 memoryx::ObjectLocalizationResult result;
170 result.timeStamp = timestamp;
171
172 memoryx::ObjectLocalizationResultList resultList;
173 result.recognitionCertainty = 1;
174
175 if (objectClassNames.size() == 1)
176 {
177 ARMARX_DEBUG << "Returning hand pose from simulator for one hand: "
178 << objectClassNames.at(0);
179
180 result.objectClassName = objectClassNames.at(0);
181
182 if (firstEntryIsLeft)
183 {
184 result.position = positionLeftHand;
185 result.orientation = orientationLeftHand;
186
187 result.positionNoise = computePositionNoise(positionLeftHand->toEigen());
188 }
189 else
190 {
191 result.position = positionRightHand;
192 result.orientation = orientationRightHand;
193 result.positionNoise = computePositionNoise(positionRightHand->toEigen());
194 }
195 resultList.push_back(result);
196 }
197 else if (objectClassNames.size() == 2)
198 {
199 ARMARX_INFO << "Returning hand pose from simulator for two hands: "
200 << objectClassNames.at(0) << ", " << objectClassNames.at(1);
201
202 result.objectClassName = firstEntryIsLeft ? objectClassNames.at(0) : objectClassNames.at(1);
203 result.position = positionLeftHand;
204 result.orientation = orientationLeftHand;
205 result.positionNoise = computePositionNoise(positionLeftHand->toEigen());
206 resultList.push_back(result);
207
208 result.objectClassName = firstEntryIsLeft ? objectClassNames.at(1) : objectClassNames.at(0);
209 result.position = positionRightHand;
210 result.orientation = orientationRightHand;
211 result.positionNoise = computePositionNoise(positionRightHand->toEigen());
212 resultList.push_back(result);
213 }
214 else
215 {
216 ARMARX_WARNING << "More than 2 hands requested - this case is not handeled here";
217 }
218
219
220 ARMARX_DEBUG << "Finished localization, returning.";
221
222 return resultList;
223}
224
226RobotHandLocalizationDynamicSimulation::computePositionNoise(const Eigen::Vector3f& pos)
227{
229 mean.push_back(pos(0));
230 mean.push_back(pos(1));
231 mean.push_back(pos(2));
232
233 // assuming 2 mm noise
235 posDist->setMean(mean);
236 posDist->setCovariance(0, 0, 2.0f);
237 posDist->setCovariance(1, 1, 2.0f);
238 posDist->setCovariance(2, 2, 2.0f);
239 return posDist;
240}
241
std::string timestamp()
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
Property< PropertyType > getProperty(const std::string &name)
The FramedOrientation class.
Definition FramedPose.h:216
The FramedPosition class.
Definition FramedPose.h:158
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
The Pose class.
Definition Pose.h:243
Mimics the behaviour of the VirtualRobot::Robot class while redirecting everything to an Ice proxy.
RobotHandLocalizationDynamicSimulation queries the armarx simulator for the current poses of the hand...
void onInitComponent() override
Pure virtual hook for the subclass.
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.
memoryx::ObjectLocalizationResultList localizeObjectClasses(const ::memoryx::ObjectClassNameList &, const ::Ice::Current &=Ice::emptyCurrent) override
localize one or both hand markers
void onConnectComponent() override
Pure virtual hook for the subclass.
static TimestampVariantPtr nowPtr()
The MultivariateNormalDistribution class.
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::optional< float > mean(const boost::circular_buffer< NameValueMap > &buffer, const std::string &key)
::std::vector<::Ice::Float > FloatVector
void handleExceptions()
IceInternal::Handle< Pose > PosePtr
Definition Pose.h:306
IceInternal::Handle< FramedPosition > FramedPositionPtr
Definition FramedPose.h:149
IceInternal::Handle< FramedOrientation > FramedOrientationPtr
Definition FramedPose.h:207
const LogSender::manipulator flush
Definition LogSender.h:251
::IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface > RobotStateComponentInterfacePrx
IceInternal::Handle< TimestampVariant > TimestampVariantPtr
VirtualRobot headers.
IceInternal::Handle< MultivariateNormalDistribution > MultivariateNormalDistributionPtr
IceInternal::Handle< Entity > EntityPtr
Typedef of EntityPtr as IceInternal::Handle<Entity> for convenience.
Definition Entity.h:45
std::shared_ptr< GridFileManager > GridFileManagerPtr