5 #include <boost/algorithm/clamp.hpp>
7 #include <SimoxUtility/json.h>
8 #include <SimoxUtility/math/compare/is_equal.h>
9 #include <SimoxUtility/math/convert/mat4f_to_pos.h>
10 #include <SimoxUtility/math/convert/mat4f_to_quat.h>
11 #include <SimoxUtility/math/convert/rpy_to_mat3f.h>
12 #include <VirtualRobot/IK/DifferentialIK.h>
13 #include <VirtualRobot/IK/JacobiProvider.h>
14 #include <VirtualRobot/MathTools.h>
15 #include <VirtualRobot/Robot.h>
16 #include <VirtualRobot/RobotNodeSet.h>
31 const std::string& configFileName)
36 ik.reset(
new VirtualRobot::DifferentialIK(
37 rns, rns->getRobot()->getRootNode(), VirtualRobot::JacobiProvider::eSVDDamped));
40 numOfJoints = rns->getSize();
41 s.
qpos.resize(numOfJoints);
42 s.
qvel.resize(numOfJoints);
53 nlohmann::json userConfig;
54 nlohmann::json defaultConfig;
58 "njoint_controller/impedance_controller_config.json",
63 if (defaultConfig.empty())
68 if (configFileName.empty())
71 <<
"No control parameter specified by user, use default parameter in \n"
73 userConfig = defaultConfig;
77 std::ifstream ifs{configFileName};
80 if (userConfig.empty())
85 if (userConfig.find(
"control") != userConfig.end())
87 auto c = userConfig[
"control"];
88 auto dc = defaultConfig[
"control"];
99 ARMARX_IMPORTANT <<
"default value is not consistent with requested kinematic "
100 "chain, reinit in preactivation";
101 enablePreactivateInit.store(
true);
116 s.
isRigid = getValue<bool>(
c, dc,
"is_rigid");
123 ARMARX_WARNING <<
"controller parameters not found, they should be placed in 'control' "
124 "tag in your json file";
133 nlohmann::json userConfig;
134 std::ifstream ifs{configFileName};
136 auto c = userConfig[
"control"];
175 s.
qpos = rns->getJointValuesEigen();
176 s.
qvel.setZero(numOfJoints);
208 if (enablePreactivateInit.load())
253 std::vector<const SensorValue1DoFActuatorTorque*> torqueSensors,
254 std::vector<const SensorValue1DoFActuatorVelocity*> velocitySensors,
255 std::vector<const SensorValue1DoFActuatorPosition*> positionSensors)
313 Eigen::MatrixXf jacobi =
314 ik->getJacobianMatrix(
tcp, VirtualRobot::IKSolver::CartesianSelection::All);
315 jacobi.block(0, 0, 3, numOfJoints) = 0.001 * jacobi.block(0, 0, 3, numOfJoints);
320 Eigen::VectorXf qvelRaw(numOfJoints);
321 for (
size_t i = 0; i < velocitySensors.size(); ++i)
323 s.
qpos(i) = positionSensors[i]->position;
324 qvelRaw(i) = velocitySensors[i]->velocity;
329 s.
deltaT = timeSinceLastIteration.toSecondsDouble();
349 Eigen::VectorXf currentKeypointVelocity;
350 currentKeypointVelocity.setZero(
s.
numPoints * 3);
355 currentKeypointVelocity.segment(3 * i, 3) =
374 Eigen::VectorXf trackingForce = difference.cwiseProduct(
s.
keypointKp) -
384 Eigen::Vector3f dist =
387 Eigen::Vector3f force = trackingForce.segment(3 * i, 3);
388 if (force.norm() > 200)
406 VirtualRobot::MathTools::rpy2eigen3f(deltaPose(3), deltaPose(4), deltaPose(5));
415 std::vector<ControlTarget1DoFActuatorTorque*> targets)
431 poseError.tail<3>() = VirtualRobot::MathTools::eigen3f2rpy(objDiffMat);
444 VirtualRobot::MathTools::rpy2eigen3f(deltaPose(3), deltaPose(4), deltaPose(5));
456 poseErrorImp.head<3>() =
458 poseErrorImp.tail<3>() = VirtualRobot::MathTools::eigen3f2rpy(diffMat);
463 Eigen::VectorXf nullspaceTorque =
468 const Eigen::MatrixXf jtpinv =
469 ik->computePseudoInverseJacobianMatrix(
s.
jacobi.transpose(), lambda);
471 (I -
s.
jacobi.transpose() * jtpinv) * nullspaceTorque;
474 for (
size_t i = 0; i < targets.size(); ++i)
479 if (!targets.at(i)->isValid())
481 targets.at(i)->torque = 0;