2 #include <boost/algorithm/clamp.hpp>
4 #include <SimoxUtility/math/convert/mat4f_to_pos.h>
5 #include <SimoxUtility/math/convert/mat4f_to_quat.h>
6 #include <SimoxUtility/math/convert/rpy_to_mat3f.h>
7 #include <SimoxUtility/math/compare/is_equal.h>
8 #include <SimoxUtility/json.h>
10 #include <VirtualRobot/MathTools.h>
27 ik.reset(
new VirtualRobot::DifferentialIK(
28 rns, rns->getRobot()->getRootNode(),
29 VirtualRobot::JacobiProvider::eSVDDamped));
32 numOfJoints = rns->getSize();
33 s.
qpos.resize(numOfJoints);
34 s.
qvel.resize(numOfJoints);
45 nlohmann::json userConfig;
46 nlohmann::json defaultConfig;
50 "armarx_control/controller_config/default/njoint_controller/impedance_controller_config.json",
55 if (defaultConfig.empty())
60 if (configFileName.empty())
63 userConfig = defaultConfig;
67 std::ifstream ifs{configFileName};
70 if (userConfig.empty())
75 if (userConfig.find(
"control") != userConfig.end())
77 auto c = userConfig[
"control"];
78 auto dc = defaultConfig[
"control"];
89 ARMARX_IMPORTANT <<
"default value is not consistent with requested kinematic chain, reinit in preactivation";
90 enablePreactivateInit.store(
true);
105 s.
isRigid = getValue<bool> (
c, dc,
"is_rigid");
112 ARMARX_WARNING <<
"controller parameters not found, they should be placed in 'control' tag in your json file";
120 nlohmann::json userConfig;
121 std::ifstream ifs{configFileName};
123 auto c = userConfig[
"control"];
155 s.
qpos = rns->getJointValuesEigen();
156 s.
qvel.setZero(numOfJoints);
188 if (enablePreactivateInit.load())
227 std::vector<const SensorValue1DoFActuatorTorque*> torqueSensors,
228 std::vector<const SensorValue1DoFActuatorVelocity*> velocitySensors,
229 std::vector<const SensorValue1DoFActuatorPosition*> positionSensors
288 Eigen::MatrixXf jacobi = ik->getJacobianMatrix(
tcp, VirtualRobot::IKSolver::CartesianSelection::All);
289 jacobi.block(0, 0, 3, numOfJoints) = 0.001 * jacobi.block(0, 0, 3, numOfJoints);
294 Eigen::VectorXf qvelRaw(numOfJoints);
295 for (
size_t i = 0; i < velocitySensors.size(); ++i)
297 s.
qpos(i) = positionSensors[i]->position;
298 qvelRaw(i) = velocitySensors[i]->velocity;
303 s.
deltaT = timeSinceLastIteration.toSecondsDouble();
319 Eigen::VectorXf currentKeypointVelocity;
320 currentKeypointVelocity.setZero(
s.
numPoints * 3);
325 currentKeypointVelocity.segment(3*i, 3) = angular_vel.cross(dist) +
s.
currentTwist.head(3);
351 Eigen::Vector3f force = trackingForce.segment(3*i, 3);
352 if (force.norm() > 200)
370 Eigen::Matrix3f deltaPoseMat = VirtualRobot::MathTools::rpy2eigen3f(deltaPose(3), deltaPose(4), deltaPose(5));
392 poseError.tail(3) = VirtualRobot::MathTools::eigen3f2rpy(objDiffMat);
405 Eigen::Matrix3f deltaPoseMat = VirtualRobot::MathTools::rpy2eigen3f(deltaPose(3), deltaPose(4), deltaPose(5));
417 poseErrorImp.tail(3) = VirtualRobot::MathTools::eigen3f2rpy(diffMat);
423 Eigen::VectorXf nullspaceTorque =
428 const Eigen::MatrixXf jtpinv = ik->computePseudoInverseJacobianMatrix(
s.
jacobi.transpose(), lambda);
431 (I -
s.
jacobi.transpose() * jtpinv) * nullspaceTorque;
434 for (
size_t i = 0; i < targets.size(); ++i)
438 if (!targets.at(i)->isValid())
440 targets.at(i)->torque = 0;