1 #ifndef _HAND_EYE_CALIBRATION_VREP_PLUGIN_HPP_ 2 #define _HAND_EYE_CALIBRATION_VREP_PLUGIN_HPP_ 7 #include <boost/log/trivial.hpp> 8 #include <boost/exception/all.hpp> 12 #include "camodocal/calib/HandEyeCalibration.h" 13 #include "camodocal/calib/CamOdoCalibration.h" 19 inline boost::log::formatting_ostream& operator<<(boost::log::formatting_ostream& out, std::vector<T>& v)
22 size_t last = v.size() - 1;
23 for(
size_t i = 0; i < v.size(); ++i) {
50 static const bool debug =
false;
69 return std::make_tuple(
72 "OpticalTrackerBase#0" ,
95 if(allHandlesSet) transformEstimate =
getObjectTransform(opticalTrackerDetectedObjectName,robotTip);
100 BOOST_LOG_TRIVIAL(trace) <<
"Adding hand eye calibration frame #" << ++frameCount << std::endl;
103 auto fiducialInOpticalTrackerBase =
getObjectTransform(opticalTrackerDetectedObjectName,opticalTrackerBase);
106 firstRobotTipInRobotBaseInverse = robotTipInRobotBase.inverse();
107 firstFiducialInOpticalTrackerBaseInverse = fiducialInOpticalTrackerBase.inverse();
108 isFirstFrame =
false;
111 auto robotTipInFirstTipBase = firstRobotTipInRobotBaseInverse * robotTipInRobotBase;
112 auto fiducialInFirstFiducialBase = firstFiducialInOpticalTrackerBaseInverse * fiducialInOpticalTrackerBase;
116 tvecsArm.push_back( robotTipInFirstTipBase.translation() );
119 tvecsFiducial.push_back( fiducialInFirstFiducialBase.translation());
124 std::cout <<
"\nrobotTipInRobotBase:\n" << poseString(robotTipInRobotBase) <<
"\n";
125 std::cout <<
"\nfiducialInOpticalTrackerBase:\n" << poseString(fiducialInOpticalTrackerBase) <<
"\n";
127 std::cout <<
"\nrobotTipInFirstTipBase:\n" << poseString(robotTipInFirstTipBase) <<
"\n";
128 std::cout <<
"\nfiducialInFirstFiducialBase:\n" << poseString(fiducialInFirstFiducialBase) <<
"\n";
131 Eigen::Affine3d RobotTipToFiducial =
getObjectTransform(opticalTrackerDetectedObjectName,robotTip);
132 BOOST_LOG_TRIVIAL(info) <<
"\n" << poseString(RobotTipToFiducial,
"expected RobotTipToFiducial (simulation only): ") << std::endl;
134 BOOST_VERIFY(robotTipInFirstTipBase.translation().norm() - fiducialInFirstFiducialBase.translation().norm() < 0.1);
146 BOOST_LOG_TRIVIAL(trace) <<
"Running Hand Eye Screw Estimate with the following numbers of entries in each category: rvecsFiducial" << rvecsFiducial.size()
147 <<
" tvecsFiducial: " << tvecsFiducial.size() <<
" rvecsArm: " << rvecsArm.size() <<
" tvecsArm: " << tvecsArm.size() << std::endl;
149 BOOST_VERIFY(allHandlesSet);
152 handEyeCalib.estimateHandEyeScrew(
157 transformEstimate.matrix()
162 int ret =
simGetObjectPosition(opticalTrackerDetectedObjectName, opticalTrackerBase, detectedObjectPosition.begin());
163 if(ret==-1) BOOST_THROW_EXCEPTION(std::runtime_error(
"HandEyeCalibrationVrepPlugin: Could not get position"));
164 ret =
simGetObjectQuaternion(opticalTrackerDetectedObjectName, opticalTrackerBase, detectedObjectQuaternion.begin());
165 if(ret==-1) BOOST_THROW_EXCEPTION(std::runtime_error(
"HandEyeCalibrationVrepPlugin: Could not get quaternion"));
169 Eigen::Affine3d RobotTipToFiducial =
getObjectTransform(opticalTrackerDetectedObjectName,robotTip);
170 BOOST_LOG_TRIVIAL(info) <<
"\n" << poseString(RobotTipToFiducial,
"expected RobotTipToFiducial (simulation only): ") << std::endl;
173 BOOST_LOG_TRIVIAL(info) <<
"\n" << poseString(transformEstimate,
"estimated RobotTipToFiducial:") << std::endl;
178 Eigen::Quaterniond eigenQuat(transformEstimate.rotation());
179 BOOST_LOG_TRIVIAL(info) <<
"Hand Eye Screw Estimate quat wxyz\n: " << eigenQuat.w() <<
" " << eigenQuat.x() <<
" " << eigenQuat.y() <<
" " << eigenQuat.z() <<
" " <<
" translation xyz: " << transformEstimate.translation().x() <<
" " << transformEstimate.translation().y() <<
" " << transformEstimate.translation().z() <<
" " << std::endl;
181 BOOST_LOG_TRIVIAL(info) <<
"Optical Tracker Base Measured quat wxyz\n: " << detectedObjectQuaternion[0] <<
" " << detectedObjectQuaternion[1] <<
" " << detectedObjectQuaternion[2] <<
" " << detectedObjectQuaternion[3] <<
" " <<
" translation xyz: " << detectedObjectPosition[0] <<
" " << detectedObjectPosition[1] <<
" " << detectedObjectPosition[2] <<
" " << std::endl;
196 return transformEstimate;
208 simSetObjectPosition (opticalTrackerBase, opticalTrackerDetectedObjectName, detectedObjectPosition.begin());
209 simSetObjectQuaternion(opticalTrackerBase, opticalTrackerDetectedObjectName, detectedObjectQuaternion.begin());
220 robotTip = grl::vrep::getHandleFromParam<RobotTipName> (params_);
221 robotBase = grl::vrep::getHandleFromParam<RobotBaseName> (params_);
222 opticalTrackerBase = grl::vrep::getHandleFromParam<OpticalTrackerBaseName> (params_);
223 opticalTrackerDetectedObjectName = grl::vrep::getHandleFromParam<OpticalTrackerDetectedObjectName>(params_);
225 allHandlesSet =
true;
229 camodocal::HandEyeCalibration handEyeCalib;
230 std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > rvecsArm;
231 std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > tvecsArm;
232 std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > rvecsFiducial;
233 std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > tvecsFiducial;
236 std::size_t frameCount;
237 Eigen::Affine3d firstRobotTipInRobotBaseInverse;
238 Eigen::Affine3d firstFiducialInOpticalTrackerBaseInverse;
241 std::array<float,3> detectedObjectPosition;
242 std::array<float,4> detectedObjectQuaternion;
244 Eigen::Affine3d transformEstimate;
248 int opticalTrackerDetectedObjectName = -1;
249 int opticalTrackerBase = -1;
254 bool allHandlesSet =
false;
261 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
void restoreSensorPosition()
Correct modified transform for measured optical tracker data.
static const Params defaultParams()
Eigen::Affine3d getObjectTransform(int objectHandle, int relativeToObjectHandle=-1)
V-REP handle string for the tip of the first known transform, often a robot tip.
void applyEstimate()
Will apply the stored estimate to the v-rep simulation value.
ptrSimGetObjectPosition simGetObjectPosition
V-REP handle string for the base of the first known transform, often a robot base.
ptrSimSetObjectPosition simSetObjectPosition
Eigen::Vector3d eigenRotToEigenVector3dAngleAxis(Input eigenQuat)
HandEyeCalibrationVrepPlugin(Params params=defaultParams())
void setObjectTransform(int objectHandle, int relativeToObjectHandle, T &transform)
std::tuple< std::string, std::string, std::string, std::string > Params
ptrSimSetObjectQuaternion simSetObjectQuaternion
ptrSimGetObjectQuaternion simGetObjectQuaternion
void estimateHandEyeScrew()
run solver to estimate the unknown transform and set the simulation to the estimated value ...
Eigen::Affine3d getEstimate()
V-REP handle string for the tip of the first known transform, often a robot tip.