ObjectLocalizerDynamicSimulation.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 ArmarXSimulation::ArmarXObjects::ObjectLocalizerDynamicSimulation
19 * @author welke ( welke 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
31
33
34// EarlyVision
35//#include <MemoryX/libraries/helpers/EarlyVisionHelpers/EarlyVisionConverters.h>
36//#include <MemoryX/libraries/helpers/EarlyVisionHelpers/UnscentedTransform.h>
37
38
39using namespace armarx;
40
44{
46 "SimulatorProxyName", "Simulator", "name of dynamics simulator proxy");
48 "RobotName", "Armar3", "Name of robot used for calculating reference frame");
50 "ReferenceFrameName", "EyeLeftCamera", "Name of reference frame to use for pose");
51 //defineRequiredProperty<std::string>("CalibrationFile", "Camera calibration file, will be used for uncertainty calculation");
52 //defineOptionalProperty<float>("2DLocalizationNoise", 4.0, "2D localization noise of object recognition. Used in order to calculate the 3D world localization noise.");
54 "RecognitionCertainty", 0.9f, "Certainty of recognition used in simulation (0...1).");
56 "VisibilityCheck",
57 false,
58 "Whether to simulate camera and checking visibility within camera");
59}
60
61void
63{
64 // using dynamic simulation
65 usingProxy(getProperty<std::string>("SimulatorProxyName").getValue());
66
67 robotName = getProperty<std::string>("RobotName").getValue();
68 frameName = getProperty<std::string>("ReferenceFrameName").getValue();
69
70 recognitionCertainty = getProperty<float>("RecognitionCertainty").getValue();
71 performVisibilityCheck = getProperty<bool>("VisibilityCheck").getValue();
72
73 if (performVisibilityCheck)
74 {
75 ARMARX_WARNING << "Visibility check is nyi...";
76 }
77
78
79 // stereo calibration
80 /*std::string calibrationFileName = getProperty<std::string>("CalibrationFile") .getValue();
81 std::string fullCalibrationFileName;
82
83 if(!ArmarXDataPath::getAbsolutePath(calibrationFileName, fullCalibrationFileName))
84 {
85 ARMARX_ERROR << "Could not find camera calibration file in ArmarXDataPath: " << calibrationFileName;
86 }
87
88 if (!stereoCalibration.LoadCameraParameters(
89 fullCalibrationFileName.c_str(), true))
90 {
91 ARMARX_ERROR << "Error loading camera calibration file: " << fullCalibrationFileName;
92 }
93
94 // setup noise
95 imageNoiseLeft(0) = imageNoiseLeft(1) = getProperty<float>("2DLocalizationNoise").getValue();
96 imageNoiseRight(0) = imageNoiseRight(1) = getProperty<float>("2DLocalizationNoise").getValue();
97 */
98
99 ARMARX_VERBOSE << "Simulating object localization of type " << getName() << " for robot "
100 << robotName;
101}
102
103void
109
112{
113 memoryx::FloatVector mean;
114 mean.push_back(pos(0));
115 mean.push_back(pos(1));
116 mean.push_back(pos(2));
117
118 // assuming 2 mm noise
121 posDist->setMean(mean);
122 posDist->setCovariance(0, 0, 2.0f);
123 posDist->setCovariance(1, 1, 2.0f);
124 posDist->setCovariance(2, 2, 2.0f);
125 return posDist;
126}
127
128memoryx::ObjectLocalizationResultList
130 const memoryx::ObjectClassNameList& objectClassNames,
131 const Ice::Current&)
132{
133 // ARMARX_VERBOSE << "Entering localizeObjectClasses" << flush;
134 memoryx::ObjectLocalizationResultList resultList;
135
136 // assure is fully connected
137 getObjectScheduler()->waitForObjectState(eManagedIceObjectStarted);
138
139 // go through requested object classes
140 for (const auto& className : objectClassNames)
141 {
142 // query simulator for class
143 ARMARX_INFO << deactivateSpam(5) << robotName << " " << frameName << " " << className
144 << flush;
145 ObjectClassInformationSequence objectInstances =
146 simulatorPrx->getObjectClassPoses(robotName, frameName, className);
147
148 // go through instances
149 for (const auto& instanceInfo : objectInstances)
150 {
151 memoryx::ObjectLocalizationResult result;
152
153 result.timeStamp = new TimestampVariant(instanceInfo.timestampMicroSeconds);
154
155 // class name
156 result.objectClassName = instanceInfo.className;
157
158 // pose
159 armarx::FramedPosePtr pose = armarx::FramedPosePtr::dynamicCast(instanceInfo.pose);
160 ARMARX_INFO << deactivateSpam(5) << className << " pose: " << pose->output();
161 FramedPositionPtr position = pose->getPosition();
162 result.position = position;
163 result.orientation = pose->getOrientation();
164
165 // uncertainties
166 result.positionNoise = computePositionNoise(position->toEigen());
167 result.recognitionCertainty = recognitionCertainty;
168
169 resultList.push_back(result);
170
171 /*
172 if(performVisibilityCheck)
173 {
174 if(isVisible(armarx::FramedPositionPtr::dynamicCast(result.position)->toEigen()))
175 resultList.push_back(result);
176 } else
177 resultList.push_back(result);
178 */
179 }
180 }
181
182
183 ARMARX_VERBOSE << deactivateSpam(5) << "Found " << resultList.size() << " instances";
184
186
187 return resultList;
188}
189
190std::string
195
196std::string
198{
199 return "ObjectLocalizerDynamicSimulation";
200}
201
202/*
203memoryx::MultivariateNormalDistributionPtr ObjectLocalizerDynamicSimulation::calculateLocalizationUncertainty(Vec2d left_point, Vec2d right_point)
204{
205 // initialize noise
206 UnscentedTransform ut;
207
208 Eigen::MatrixXd combinedNoise(4,4);
209 combinedNoise.setZero();
210 combinedNoise(0,0) = imageNoiseLeft(0);
211 combinedNoise(1,1) = imageNoiseLeft(1);
212 combinedNoise(2,2) = imageNoiseRight(0);
213 combinedNoise(3,3) = imageNoiseRight(1);
214
215 Eigen::VectorXd mean(4);
216 mean << left_point.x, left_point.y, right_point.x, right_point.y;
217
218 Gaussian imageSpaceNoise(4);
219 imageSpaceNoise.setCovariance(combinedNoise);
220 imageSpaceNoise.setMean(mean);
221
222 // calculate sigma points
223 Eigen::MatrixXd sigmapoints = ut.getSigmaPoints(imageSpaceNoise);
224
225 // pass sigma points through system
226 Vec2d l,r;
227 Vec3d w;
228 Eigen::VectorXd base(4);
229 Eigen::VectorXd world(4);
230
231 Eigen::MatrixXd processedpoints(3,sigmapoints.cols());
232
233 for(int n = 0 ; n < sigmapoints.cols() ; n++)
234 {
235
236 Math2d::SetVec(l, sigmapoints(0, n), sigmapoints(1, n));
237 Math2d::SetVec(r, sigmapoints(2, n), sigmapoints(3, n));
238
239 // calc 3d point (2D points are rectified but not distorted)
240 stereoCalibration.Calculate3DPoint(l, r, w, true, true);
241 world << w.x, w.y, w.z, 1.0f;
242
243 processedpoints(0, n) = world(0);
244 processedpoints(1, n) = world(1);
245 processedpoints(2, n) = world(2);
246 }
247
248 // recover covariance
249 Gaussian worldSpaceNoise = ut.extractGaussian(processedpoints);
250
251 return memoryx::EarlyVisionConverters::convertToMemoryX_MULTI(worldSpaceNoise);
252 }
253
254memoryx::MultivariateNormalDistributionPtr ObjectLocalizerDynamicSimulation::calculateLocalizationUncertainty(const Eigen::Vector3f& position)
255{
256 // calculate 2d points for localization uncerainty
257 Vec2d left2d, right2d;
258 getImagePositions(position, left2d, right2d);
259
260 return calculateLocalizationUncertainty(left2d, right2d);
261}
262
263bool ObjectLocalizerDynamicSimulation::isVisible(const Eigen::Vector3f& position)
264{
265 // calculate 2d points for localization uncerainty
266 Vec2d left2d, right2d;
267 getImagePositions(position, left2d, right2d);
268
269 // check if within image
270 const CCalibration::CCameraParameters& leftParams = stereoCalibration.GetLeftCalibration()->GetCameraParameters();
271 const CCalibration::CCameraParameters& rightParams = stereoCalibration.GetRightCalibration()->GetCameraParameters();
272
273 bool visibleInLeft = true;
274 if( (left2d.x < 0) || (left2d.x >= leftParams.width) )
275 visibleInLeft = false;
276 if( (left2d.y < 0) || (left2d.y >= leftParams.height) )
277 visibleInLeft = false;
278
279 bool visibleInRight = true;
280 if( (right2d.x < 0) || (right2d.x >= rightParams.width) )
281 visibleInRight = false;
282 if( (right2d.y < 0) || (right2d.y >= rightParams.height) )
283 visibleInRight = false;
284
285 ARMARX_INFO << "ObjectLocalizer: " << visibleInRight << " " << visibleInLeft << flush;
286
287 return visibleInLeft && visibleInRight;
288}
289
290void ObjectLocalizerDynamicSimulation::getImagePositions(const Eigen::Vector3f& position, Vec2d& left_point, Vec2d& right_point)
291{
292 Vec2d left2d, right2d;
293 Vec3d pos;
294 Math3d::SetVec(pos, position(0), position(1), position(2));
295
296 stereoCalibration.GetLeftCalibration()->WorldToImageCoordinates(pos, left2d);
297 stereoCalibration.GetRightCalibration()->WorldToImageCoordinates(pos, right2d);
298
299 // transform to rectified coordinates
300 Mat3d inverseHLeft, inverseHRight;
301 Math3d::Invert(stereoCalibration.rectificationHomographyLeft, inverseHLeft);
302 Math3d::Invert(stereoCalibration.rectificationHomographyRight, inverseHRight);
303 Math2d::ApplyHomography(inverseHLeft, left2d, left_point);
304 Math2d::ApplyHomography(inverseHRight, right2d, right_point);
305}*/
306
307
314
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
Definition Component.cpp:46
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
SpamFilterDataPtr deactivateSpam(float deactivationDurationSec=10.0f, const std::string &identifier="", bool deactivate=true) const
disables the logging for the current line for the given amount of seconds.
Definition Logging.cpp:99
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
ArmarXObjectSchedulerPtr getObjectScheduler() const
std::string getName() const
Retrieve name of object.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
This component connects to the ArmarX Simulator and reports object poses from the physics world.
void onInitComponent() override
Pure virtual hook for the subclass.
memoryx::ObjectLocalizationResultList localizeObjectClasses(const memoryx::ObjectClassNameList &objectClassNames, const Ice::Current &c=Ice::emptyCurrent) override
localizes simulated object instances
void onConnectComponent() override
Pure virtual hook for the subclass.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
::memoryx::MultivariateNormalDistributionPtr computePositionNoise(const Eigen::Vector3f &pos)
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
PropertyDefinition< PropertyType > & defineOptionalProperty(const std::string &name, PropertyType defaultValue, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
static void SleepMS(float milliseconds)
Definition TimeUtil.h:203
Implements a Variant type for timestamps.
The MultivariateNormalDistribution class.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#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)
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
IceInternal::Handle< FramedPosition > FramedPositionPtr
Definition FramedPose.h:149
const LogSender::manipulator flush
Definition LogSender.h:251
IceInternal::Handle< FramedPose > FramedPosePtr
Definition FramedPose.h:272
IceInternal::Handle< MultivariateNormalDistribution > MultivariateNormalDistributionPtr