2 #ifndef _INVERSE_KINEMATICS_VREP_PLUGIN_ 3 #define _INVERSE_KINEMATICS_VREP_PLUGIN_ 12 #include <boost/format.hpp> 14 #include <Eigen/Geometry> 18 #include <SpaceVecAlg/SpaceVecAlg> 22 #include <RBDyn/Body.h> 23 #include <RBDyn/Joint.h> 24 #include <RBDyn/MultiBody.h> 25 #include <RBDyn/MultiBodyConfig.h> 26 #include <RBDyn/MultiBodyGraph.h> 27 #include <RBDyn/EulerIntegration.h> 35 #include <Tasks/Tasks.h> 36 #include <Tasks/Bounds.h> 37 #include <Tasks/QPConstr.h> 38 #include <Tasks/QPContactConstr.h> 39 #include <Tasks/QPMotionConstr.h> 40 #include <Tasks/QPSolver.h> 41 #include <Tasks/QPTasks.h> 50 #include <boost/lexical_cast.hpp> 51 #include <boost/range/algorithm/copy.hpp> 65 const std::vector<std::string>& vrepJointNames,
66 const std::vector<int>& vrepJointHandles,
67 const rbd::MultiBody& simArmMultiBody,
68 const rbd::MultiBodyConfig& simArmConfig,
69 std::string debug =
"")
72 for (std::size_t i = 0; i < vrepJointHandles.size(); ++ i)
75 std::string jointName = vrepJointNames[i];
76 std::size_t jointIdx = simArmMultiBody.jointIndexByName(jointName);
77 float futureAngle = simArmConfig.q[jointIdx][0];
86 str+=boost::lexical_cast<std::string>(futureAngle);
87 if (i<vrepJointHandles.size()-1) str+=
", ";
93 BOOST_LOG_TRIVIAL(trace) << debug <<
" jointAngles: " << str;
107 const std::vector<std::string>& vrepJointNames,
108 const std::vector<int>& vrepJointHandles,
109 const rbd::MultiBody& simArmMultiBody,
110 rbd::MultiBodyConfig& simArmConfig,
111 std::string debug =
"")
115 for (std::size_t i = 0; i < vrepJointHandles.size(); ++ i)
118 std::string jointName = vrepJointNames[i];
119 std::size_t jointIdx = simArmMultiBody.jointIndexByName(jointName);
121 if(simArmConfig.q[jointIdx].size()>0) simArmConfig.q[jointIdx][0] = futureAngle;
130 str+=boost::lexical_cast<std::string>(futureAngle);
131 if (i<vrepJointHandles.size()-1) str+=
", ";
137 BOOST_LOG_TRIVIAL(trace) << debug <<
" jointAngles: " << str;
175 return std::make_tuple(
"RobotMillTipTarget",
"IK_Group1_iiwa");
204 ikGroupBaseName_ = (std::get<VrepRobotArmDriver::RobotTargetBaseName>(armDriverSimulatedParams));
206 ikGroupTipName_ = (std::get<VrepRobotArmDriver::RobotTipName>(armDriverSimulatedParams));
208 ikGroupTargetName_ = (std::get<VrepRobotArmDriver::RobotTargetName>(armDriverSimulatedParams));
209 robotFlangeTipName_ = (std::get<VrepRobotArmDriver::RobotFlangeTipName>(armDriverSimulatedParams));
219 bool isForwardJoint =
true;
234 std::vector<float> initialJointAngles;
239 initialJointAngles.push_back(angle);
268 rbd::Joint::Type jointType = rbd::Joint::Fixed;
272 jointType = rbd::Joint::Rev;
274 rbd::Joint j_i(jointType, Eigen::Vector3d::UnitZ(), isForwardJoint, thisJointName);
290 auto I = Eigen::Matrix3d::Identity();
291 auto h = Eigen::Vector3d::Zero();
293 sva::RBInertiad rbi_i(mass, h, I);
300 rbd::Body b_i(rbi_i,bodyName);
305 std::string dummyName0((
"Dummy"+ boost::lexical_cast<std::string>(0+10)));
308 BOOST_LOG_TRIVIAL(trace) << dummyName0 <<
" \n" << eto0.matrix();
311 std::vector<int> frameHandles;
321 sva::PTransformd from(sva::PTransformd::Identity());
330 rbd_mbg_.linkBodies(prevBody, to, nextBody, from, curJoint);
341 const std::size_t simulatedRobotIndex = 0;
342 auto& simArmMultiBody =
rbd_mbs_[simulatedRobotIndex];
343 auto& simArmConfig =
rbd_mbcs_[simulatedRobotIndex];
346 for (std::size_t i = 0; i < jointHandles_.size(); ++i) {
351 rbd::forwardKinematics(simArmMultiBody, simArmConfig);
352 rbd::forwardVelocity(simArmMultiBody, simArmConfig);
381 const std::size_t simulatedRobotIndex = 0;
382 auto& simArmMultiBody =
rbd_mbs_[simulatedRobotIndex];
383 auto& simArmConfig =
rbd_mbcs_[simulatedRobotIndex];
389 std::string dummyName2((
"Dummy"+ boost::lexical_cast<std::string>(i+11)));
393 if(print) BOOST_LOG_TRIVIAL(trace) << dummyName2 <<
" V-REP World\n" << eto.matrix();
398 if(print) BOOST_LOG_TRIVIAL(trace) << dummyName2 <<
" V-REP JointInPrevFrame\n" << NextJointinPrevFrame.matrix();
401 bool dummy_world_frame =
true;
402 if( dummy_world_frame )
405 sva::PTransform<double> plinkWorld = simArmConfig.bodyPosW[simArmMultiBody.bodyIndexByName(
linkNames_[i])];
407 std::string dummyName((
"Dummy0"+ boost::lexical_cast<std::string>(i+1)));
409 if(print) BOOST_LOG_TRIVIAL(trace) << dummyName <<
" RBDyn World\n" << linkWorld.matrix();
411 prevDummy=currentDummy;
412 sva::PTransform<double> plinkToSon = simArmConfig.parentToSon[simArmMultiBody.bodyIndexByName(
linkNames_[i])];
414 if(print) BOOST_LOG_TRIVIAL(trace) << dummyName <<
" RBDyn ParentLinkToSon\n" << linkToSon.matrix();
420 sva::PTransform<double> plinkWorld = simArmConfig.parentToSon[simArmMultiBody.bodyIndexByName(
linkNames_[i])];
422 std::string dummyName((
"Dummy0"+ boost::lexical_cast<std::string>(i+1)));
424 if(print) BOOST_LOG_TRIVIAL(trace) << dummyName <<
" RBDyn\n" << linkWorld.matrix();
426 prevDummy=currentDummy;
444 const std::size_t simulatedRobotIndex = 0;
445 auto& simArmMultiBody =
rbd_mbs_[simulatedRobotIndex];
446 auto& simArmConfig =
rbd_mbcs_[simulatedRobotIndex];
448 std::vector<float> vrepJointAngles;
454 vrepJointAngles.push_back(angle);
462 rbd::forwardKinematics(simArmMultiBody, simArmConfig);
463 rbd::forwardVelocity(simArmMultiBody, simArmConfig);
482 const bool runOnce =
false,
500 auto & llim = std::get<vrep::VrepRobotArmDriver::JointLowerPositionLimit>(
currentArmState_);
501 std::vector<double> llimits(llim.begin(),llim.end());
504 auto & ulim = std::get<vrep::VrepRobotArmDriver::JointUpperPositionLimit>(
currentArmState_);
505 std::vector<double> ulimits(ulim.begin(),ulim.end());
508 auto & currentJointPos = std::get<vrep::VrepRobotArmDriver::JointPosition>(
currentArmState_);
509 std::vector<double> currentJointPosVec(currentJointPos.begin(),currentJointPos.end());
513 Eigen::Affine3d currentEndEffectorPose =
515 std::get<vrep::VrepRobotArmDriver::RobotTipName>(handleParams)
516 ,std::get<vrep::VrepRobotArmDriver::RobotTargetBaseName>(handleParams)
518 auto currentEigenT = currentEndEffectorPose.translation();
521 Eigen::Affine3d desiredEndEffectorPose =
523 std::get<vrep::VrepRobotArmDriver::RobotTargetName>(handleParams)
524 ,std::get<vrep::VrepRobotArmDriver::RobotTargetBaseName>(handleParams)
526 auto desiredEigenT = desiredEndEffectorPose.translation();
535 const std::size_t simulatedRobotIndex = 0;
536 auto& simArmMultiBody =
rbd_mbs_[simulatedRobotIndex];
537 auto& simArmConfig =
rbd_mbcs_[simulatedRobotIndex];
546 rbd::forwardKinematics(simArmMultiBody, simArmConfig);
547 rbd::forwardVelocity(simArmMultiBody, simArmConfig);
556 tasks::qp::QPSolver solver;
560 sva::PTransformd targetWorldTransform;
566 BOOST_LOG_TRIVIAL(trace) <<
"target translation (vrep format):\n"<< targetWorldTransform.translation();
571 targetWorldTransform = simArmConfig.bodyPosW[simArmMultiBody.bodyIndexByName(
ikGroupTipName_)];
572 targetWorldTransform.translation().z() -= 0.0001;
573 BOOST_LOG_TRIVIAL(trace) <<
"target translation (rbdyn format):\n"<< targetWorldTransform.translation();
575 tasks::qp::PositionTask posTask(
rbd_mbs_, simulatedRobotIndex,
ikGroupTipName_,targetWorldTransform.translation());
576 tasks::qp::SetPointTask posTaskSp(
rbd_mbs_, simulatedRobotIndex, &posTask, 50., 1.);
577 tasks::qp::OrientationTask oriTask(
rbd_mbs_,simulatedRobotIndex,
ikGroupTipName_,targetWorldTransform.rotation());
578 tasks::qp::SetPointTask oriTaskSp(
rbd_mbs_, simulatedRobotIndex, &oriTask, 50., 1.);
581 double inf = std::numeric_limits<double>::infinity();
589 std::vector<std::vector<double> > lBound = simArmConfig.q;
590 std::vector<std::vector<double> > uBound = simArmConfig.q;
591 std::vector<std::vector<double> > lVelBound = simArmConfig.alpha;
592 std::vector<std::vector<double> > uVelBound = simArmConfig.alpha;
600 std::size_t jointIdx = simArmMultiBody.jointIndexByName(jointName);
604 if(boost::iequals(jointName,
"cutter_joint"))
606 lBound[jointIdx][0] = -inf;
607 uBound[jointIdx][0] = inf;
609 else if(i<llimits.size() && lBound[jointIdx].size()==1)
611 lBound[jointIdx][0] = llimits[i];
612 uBound[jointIdx][0] = ulimits[i];
616 if(lVelBound[jointIdx].size()==1)
620 lVelBound[jointIdx][0] = -0.99;
621 uVelBound[jointIdx][0] = 0.99;
625 tasks::qp::DamperJointLimitsConstr dampJointConstr(
rbd_mbs_, simulatedRobotIndex, {lBound, uBound},{lVelBound, uVelBound}, 0.125, 0.025, 1., simulationTimeStep);
628 dampJointConstr.addToSolver(solver);
629 BOOST_VERIFY(solver.nrBoundConstraints() == 1);
630 BOOST_VERIFY(solver.nrConstraints() == 1);
633 solver.updateConstrSize();
635 solver.addTask(&posTaskSp);
636 BOOST_VERIFY(solver.nrTasks() == 1);
637 solver.addTask(&oriTaskSp);
638 solver.addTask(&postureTask);
646 rbd::InverseKinematics ik(simArmMultiBody,simArmMultiBody.bodyIndexByName(
ikGroupTipName_));
647 ik.sInverseKinematics(simArmMultiBody,simArmConfig,targetWorldTransform);
656 int numSolverIterations = 10;
657 double timeStepDividedIntoIterations = simulationTimeStep/numSolverIterations;
660 for(
int i = 0; i < numSolverIterations; ++i)
667 rbd::sEulerIntegration(simArmMultiBody, simArmConfig, simulationTimeStep);
669 rbd::sForwardKinematics(simArmMultiBody, simArmConfig);
670 rbd::sForwardVelocity(simArmMultiBody, simArmConfig);
678 rbd::sEulerIntegration(simArmMultiBody, simArmConfig, simulationTimeStep);
679 rbd::sForwardKinematics(simArmMultiBody, simArmConfig);
680 rbd::sForwardVelocity(simArmMultiBody, simArmConfig);
692 const bool ik =
true;
std::vector< int > linkHandles_
Eigen::Affine3d PTranformToEigenAffine(sva::PTransform< double > &ptransform)
std::vector< int > rbd_jointHandles_
std::tuple< std::string,std::string > Params
AlgToUseE
Configures updateKinematics the algorithm the kinematics should use for solving.
ptrSimGetJointPosition simGetJointPosition
GoalPosE
Configures updateKinematics with the goal the kinematics should aim for.
int getHandle(const std::string ¶m)
std::vector< std::string > rbd_bodyNames_
ptrSimGetObjectType simGetObjectType
std::vector< int > linkRespondableHandles_
std::vector< rbd::MultiBody > rbd_mbs_
ptrSimGetObjectHandle simGetObjectHandle
std::string robotFlangeTipName_
Eigen::Affine3d getObjectTransform(int objectHandle, int relativeToObjectHandle=-1)
ptrSimGetIkGroupHandle simGetIkGroupHandle
vrep::VrepRobotArmDriver::State currentArmState_
InverseKinematicsVrepPlugin()
OutputIterator copy(std::string model, OutputIterator it, grl::revolute_joint_velocity_open_chain_state_constraint_tag)
copy vector of joint velocity limits in radians/s
std::vector< rbd::Joint > rbd_joints_
void SetRBDynArmFromVrep(const std::vector< std::string > &vrepJointNames, const std::vector< int > &vrepJointHandles, const rbd::MultiBody &simArmMultiBody, rbd::MultiBodyConfig &simArmConfig, std::string debug="")
ptrSimSetJointPosition simSetJointPosition
std::string ikGroupBaseName_
std::vector< sva::RBInertia< double > > rbd_inertias_
OutputIterator getHandles(const SinglePassRange inputRange, OutputIterator out)
std::vector< std::string > linkRespondableNames_
std::vector< int > bodyHandles_
std::shared_ptr< vrep::VrepRobotArmDriver > VrepRobotArmDriverSimulatedP_
void updateKinematics(const bool runOnce=false, const GoalPosE solveForPosition=GoalPosE::realGoalPosition, const AlgToUseE alg=AlgToUseE::multiIterQP, const PostureTaskStrategyE postureStrategy=PostureTaskStrategyE::constant)
void construct(Params params=defaultParams())
void setObjectTransform(int objectHandle, int relativeToObjectHandle, T &transform)
VrepVFControllerParamsIndex
std::vector< rbd::Body > rbd_bodies_
void debugFrames(bool print=false)
std::vector< std::string > linkNames_
std::string ikGroupTipName_
std::vector< std::string > jointNames_
rbd::MultiBodyGraph rbd_mbg_
static Params defaultParams()
std::vector< int > jointHandles_
std::shared_ptr< vrep::VrepRobotArmDriver > VrepRobotArmDriverMeasuredP_
std::string ikGroupTargetName_
void SetVRepArmFromRBDyn(const std::vector< std::string > &vrepJointNames, const std::vector< int > &vrepJointHandles, const rbd::MultiBody &simArmMultiBody, const rbd::MultiBodyConfig &simArmConfig, std::string debug="")
std::vector< std::string > rbd_jointNames_
sva::PTransform< double > getObjectPTransform(int objectHandle, int relativeToObjectHandle=-1)
std::vector< rbd::MultiBodyConfig > rbd_mbcs_
std::vector< rbd::MultiBodyConfig > rbd_prev_mbcs_
rbd_prev_mbcs_ is for debugging
std::tuple< JointScalar, JointScalar, JointScalar, JointScalar, JointScalar, TransformationMatrices, JointStateTag, JointScalar > State
static const Params measuredArmParams()
ptrSimSetExplicitHandling simSetExplicitHandling
std::vector< rbd::MultiBodyConfig > rbd_preferred_mbcs_
preferred posture to resolve ambiguities
ptrSimGetSimulationTimeStep simGetSimulationTimeStep