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
30
32
33// EarlyVision
34//#include <MemoryX/libraries/helpers/EarlyVisionHelpers/EarlyVisionConverters.h>
35//#include <MemoryX/libraries/helpers/EarlyVisionHelpers/UnscentedTransform.h>
36
37
38using namespace armarx;
39
43{
45 "SimulatorProxyName", "Simulator", "name of dynamics simulator proxy");
47 "RobotName", "Armar3", "Name of robot used for calculating reference frame");
49 "ReferenceFrameName", "EyeLeftCamera", "Name of reference frame to use for pose");
50 //defineRequiredProperty<std::string>("CalibrationFile", "Camera calibration file, will be used for uncertainty calculation");
51 //defineOptionalProperty<float>("2DLocalizationNoise", 4.0, "2D localization noise of object recognition. Used in order to calculate the 3D world localization noise.");
53 "RecognitionCertainty", 0.9f, "Certainty of recognition used in simulation (0...1).");
55 "VisibilityCheck",
56 false,
57 "Whether to simulate camera and checking visibility within camera");
58}
59
60void
62{
63 // using dynamic simulation
64 usingProxy(getProperty<std::string>("SimulatorProxyName").getValue());
65
66 robotName = getProperty<std::string>("RobotName").getValue();
67 frameName = getProperty<std::string>("ReferenceFrameName").getValue();
68
69 recognitionCertainty = getProperty<float>("RecognitionCertainty").getValue();
70 performVisibilityCheck = getProperty<bool>("VisibilityCheck").getValue();
71
72 if (performVisibilityCheck)
73 {
74 ARMARX_WARNING << "Visibility check is nyi...";
75 }
76
77
78 // stereo calibration
79 /*std::string calibrationFileName = getProperty<std::string>("CalibrationFile") .getValue();
80 std::string fullCalibrationFileName;
81
82 if(!ArmarXDataPath::getAbsolutePath(calibrationFileName, fullCalibrationFileName))
83 {
84 ARMARX_ERROR << "Could not find camera calibration file in ArmarXDataPath: " << calibrationFileName;
85 }
86
87 if (!stereoCalibration.LoadCameraParameters(
88 fullCalibrationFileName.c_str(), true))
89 {
90 ARMARX_ERROR << "Error loading camera calibration file: " << fullCalibrationFileName;
91 }
92
93 // setup noise
94 imageNoiseLeft(0) = imageNoiseLeft(1) = getProperty<float>("2DLocalizationNoise").getValue();
95 imageNoiseRight(0) = imageNoiseRight(1) = getProperty<float>("2DLocalizationNoise").getValue();
96 */
97
98 ARMARX_VERBOSE << "Simulating object localization of type " << getName() << " for robot "
99 << robotName;
100}
101
102void
108
111{
112 memoryx::FloatVector mean;
113 mean.push_back(pos(0));
114 mean.push_back(pos(1));
115 mean.push_back(pos(2));
116
117 // assuming 2 mm noise
120 posDist->setMean(mean);
121 posDist->setCovariance(0, 0, 2.0f);
122 posDist->setCovariance(1, 1, 2.0f);
123 posDist->setCovariance(2, 2, 2.0f);
124 return posDist;
125}
126
127memoryx::ObjectLocalizationResultList
129 const memoryx::ObjectClassNameList& objectClassNames,
130 const Ice::Current&)
131{
132 // ARMARX_VERBOSE << "Entering localizeObjectClasses" << flush;
133 memoryx::ObjectLocalizationResultList resultList;
134
135 // assure is fully connected
136 getObjectScheduler()->waitForObjectState(eManagedIceObjectStarted);
137
138 // go through requested object classes
139 for (const auto& className : objectClassNames)
140 {
141 // query simulator for class
142 ARMARX_INFO << deactivateSpam(5) << robotName << " " << frameName << " " << className
143 << flush;
144 ObjectClassInformationSequence objectInstances =
145 simulatorPrx->getObjectClassPoses(robotName, frameName, className);
146
147 // go through instances
148 for (const auto& instanceInfo : objectInstances)
149 {
150 memoryx::ObjectLocalizationResult result;
151
152 result.timeStamp = new TimestampVariant(instanceInfo.timestampMicroSeconds);
153
154 // class name
155 result.objectClassName = instanceInfo.className;
156
157 // pose
158 armarx::FramedPosePtr pose = armarx::FramedPosePtr::dynamicCast(instanceInfo.pose);
159 ARMARX_INFO << deactivateSpam(5) << className << " pose: " << pose->output();
160 FramedPositionPtr position = pose->getPosition();
161 result.position = position;
162 result.orientation = pose->getOrientation();
163
164 // uncertainties
165 result.positionNoise = computePositionNoise(position->toEigen());
166 result.recognitionCertainty = recognitionCertainty;
167
168 resultList.push_back(result);
169
170 /*
171 if(performVisibilityCheck)
172 {
173 if(isVisible(armarx::FramedPositionPtr::dynamicCast(result.position)->toEigen()))
174 resultList.push_back(result);
175 } else
176 resultList.push_back(result);
177 */
178 }
179 }
180
181
182 ARMARX_VERBOSE << deactivateSpam(5) << "Found " << resultList.size() << " instances";
183
185
186 return resultList;
187}
188
189std::string
191{
192 return "ObjectLocalizerDynamicSimulation";
193}
194
195/*
196memoryx::MultivariateNormalDistributionPtr ObjectLocalizerDynamicSimulation::calculateLocalizationUncertainty(Vec2d left_point, Vec2d right_point)
197{
198 // initialize noise
199 UnscentedTransform ut;
200
201 Eigen::MatrixXd combinedNoise(4,4);
202 combinedNoise.setZero();
203 combinedNoise(0,0) = imageNoiseLeft(0);
204 combinedNoise(1,1) = imageNoiseLeft(1);
205 combinedNoise(2,2) = imageNoiseRight(0);
206 combinedNoise(3,3) = imageNoiseRight(1);
207
208 Eigen::VectorXd mean(4);
209 mean << left_point.x, left_point.y, right_point.x, right_point.y;
210
211 Gaussian imageSpaceNoise(4);
212 imageSpaceNoise.setCovariance(combinedNoise);
213 imageSpaceNoise.setMean(mean);
214
215 // calculate sigma points
216 Eigen::MatrixXd sigmapoints = ut.getSigmaPoints(imageSpaceNoise);
217
218 // pass sigma points through system
219 Vec2d l,r;
220 Vec3d w;
221 Eigen::VectorXd base(4);
222 Eigen::VectorXd world(4);
223
224 Eigen::MatrixXd processedpoints(3,sigmapoints.cols());
225
226 for(int n = 0 ; n < sigmapoints.cols() ; n++)
227 {
228
229 Math2d::SetVec(l, sigmapoints(0, n), sigmapoints(1, n));
230 Math2d::SetVec(r, sigmapoints(2, n), sigmapoints(3, n));
231
232 // calc 3d point (2D points are rectified but not distorted)
233 stereoCalibration.Calculate3DPoint(l, r, w, true, true);
234 world << w.x, w.y, w.z, 1.0f;
235
236 processedpoints(0, n) = world(0);
237 processedpoints(1, n) = world(1);
238 processedpoints(2, n) = world(2);
239 }
240
241 // recover covariance
242 Gaussian worldSpaceNoise = ut.extractGaussian(processedpoints);
243
244 return memoryx::EarlyVisionConverters::convertToMemoryX_MULTI(worldSpaceNoise);
245 }
246
247memoryx::MultivariateNormalDistributionPtr ObjectLocalizerDynamicSimulation::calculateLocalizationUncertainty(const Eigen::Vector3f& position)
248{
249 // calculate 2d points for localization uncerainty
250 Vec2d left2d, right2d;
251 getImagePositions(position, left2d, right2d);
252
253 return calculateLocalizationUncertainty(left2d, right2d);
254}
255
256bool ObjectLocalizerDynamicSimulation::isVisible(const Eigen::Vector3f& position)
257{
258 // calculate 2d points for localization uncerainty
259 Vec2d left2d, right2d;
260 getImagePositions(position, left2d, right2d);
261
262 // check if within image
263 const CCalibration::CCameraParameters& leftParams = stereoCalibration.GetLeftCalibration()->GetCameraParameters();
264 const CCalibration::CCameraParameters& rightParams = stereoCalibration.GetRightCalibration()->GetCameraParameters();
265
266 bool visibleInLeft = true;
267 if( (left2d.x < 0) || (left2d.x >= leftParams.width) )
268 visibleInLeft = false;
269 if( (left2d.y < 0) || (left2d.y >= leftParams.height) )
270 visibleInLeft = false;
271
272 bool visibleInRight = true;
273 if( (right2d.x < 0) || (right2d.x >= rightParams.width) )
274 visibleInRight = false;
275 if( (right2d.y < 0) || (right2d.y >= rightParams.height) )
276 visibleInRight = false;
277
278 ARMARX_INFO << "ObjectLocalizer: " << visibleInRight << " " << visibleInLeft << flush;
279
280 return visibleInLeft && visibleInRight;
281}
282
283void ObjectLocalizerDynamicSimulation::getImagePositions(const Eigen::Vector3f& position, Vec2d& left_point, Vec2d& right_point)
284{
285 Vec2d left2d, right2d;
286 Vec3d pos;
287 Math3d::SetVec(pos, position(0), position(1), position(2));
288
289 stereoCalibration.GetLeftCalibration()->WorldToImageCoordinates(pos, left2d);
290 stereoCalibration.GetRightCalibration()->WorldToImageCoordinates(pos, right2d);
291
292 // transform to rectified coordinates
293 Mat3d inverseHLeft, inverseHRight;
294 Math3d::Invert(stereoCalibration.rectificationHomographyLeft, inverseHLeft);
295 Math3d::Invert(stereoCalibration.rectificationHomographyRight, inverseHRight);
296 Math2d::ApplyHomography(inverseHLeft, left2d, left_point);
297 Math2d::ApplyHomography(inverseHRight, right2d, right_point);
298}*/
299
300
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)
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