17 #include <dynamic-graph/factory.h>
19 #include <boost/test/unit_test.hpp>
20 #include <sot/core/debug.hh>
23 #include <tsid/math/utils.hpp>
24 #include <tsid/solvers/solver-HQP-factory.hxx>
25 #include <tsid/solvers/utils.hpp>
26 #include <tsid/utils/statistics.hpp>
27 #include <tsid/utils/stop-watch.hpp>
30 #define ODEBUG(x) std::cout << x << std::endl
34 #define ODEBUG3(x) std::cout << x << std::endl
36 #define DBGFILE "/tmp/debug-sot-torque-control.dat"
38 #define RESETDEBUG5() \
40 std::ofstream DebugFile; \
41 DebugFile.open(DBGFILE, std::ofstream::out); \
44 #define ODEBUG5FULL(x) \
46 std::ofstream DebugFile; \
47 DebugFile.open(DBGFILE, std::ofstream::app); \
48 DebugFile << __FILE__ << ":" << __FUNCTION__ << "(#" << __LINE__ \
49 << "):" << x << std::endl; \
54 std::ofstream DebugFile; \
55 DebugFile.open(DBGFILE, std::ofstream::app); \
56 DebugFile << x << std::endl; \
61 #define ODEBUG4FULL(x)
67 namespace dg = ::dynamicgraph;
69 using namespace dg::command;
72 using namespace tsid::trajectories;
73 using namespace tsid::math;
74 using namespace tsid::contacts;
75 using namespace tsid::tasks;
76 using namespace tsid::solvers;
77 using namespace dg::sot;
79 #define REQUIRE_FINITE(A) assert(is_finite(A))
82 #define PROFILE_TAU_DES_COMPUTATION "SimpleInverseDyn: desired tau"
83 #define PROFILE_HQP_SOLUTION "SimpleInverseDyn: HQP"
84 #define PROFILE_PREPARE_INV_DYN "SimpleInverseDyn: prepare inv-dyn"
85 #define PROFILE_READ_INPUT_SIGNALS "SimpleInverseDyn: read input signals"
87 #define ZERO_FORCE_THRESHOLD 1e-3
89 #define INPUT_SIGNALS \
90 m_posture_ref_posSIN << m_posture_ref_velSIN << m_posture_ref_accSIN \
91 << m_kp_postureSIN << m_kd_postureSIN << m_w_postureSIN \
92 << m_kp_posSIN << m_kd_posSIN << m_com_ref_posSIN \
93 << m_com_ref_velSIN << m_com_ref_accSIN << m_kp_comSIN \
94 << m_kd_comSIN << m_w_comSIN << m_kp_contactSIN \
95 << m_kd_contactSIN << m_w_forcesSIN << m_muSIN \
96 << m_contact_pointsSIN << m_contact_normalSIN \
97 << m_f_minSIN << m_f_maxSIN << m_waist_ref_posSIN \
98 << m_waist_ref_velSIN << m_waist_ref_accSIN \
99 << m_kp_waistSIN << m_kd_waistSIN << m_w_waistSIN \
100 << m_qSIN << m_vSIN << m_active_jointsSIN
102 #define OUTPUT_SIGNALS \
103 m_tau_desSOUT << m_dv_desSOUT << m_v_desSOUT << m_q_desSOUT << m_uSOUT
109 typedef Eigen::Matrix<double, 2, 1>
Vector2;
110 typedef Eigen::Matrix<double, Eigen::Dynamic, 1>
VectorN;
111 typedef Eigen::Matrix<double, Eigen::Dynamic, 1>
VectorN6;
122 CONSTRUCT_SIGNAL_IN(posture_ref_pos,
dynamicgraph::Vector),
123 CONSTRUCT_SIGNAL_IN(posture_ref_vel,
dynamicgraph::Vector),
124 CONSTRUCT_SIGNAL_IN(posture_ref_acc,
dynamicgraph::Vector),
127 CONSTRUCT_SIGNAL_IN(w_posture, double),
135 CONSTRUCT_SIGNAL_IN(w_com, double),
138 CONSTRUCT_SIGNAL_IN(w_forces, double),
139 CONSTRUCT_SIGNAL_IN(mu, double),
140 CONSTRUCT_SIGNAL_IN(contact_points,
dynamicgraph::Matrix),
141 CONSTRUCT_SIGNAL_IN(contact_normal,
dynamicgraph::Vector),
142 CONSTRUCT_SIGNAL_IN(f_min, double),
143 CONSTRUCT_SIGNAL_IN(f_max, double),
144 CONSTRUCT_SIGNAL_IN(waist_ref_pos,
dynamicgraph::Vector),
145 CONSTRUCT_SIGNAL_IN(waist_ref_vel,
dynamicgraph::Vector),
146 CONSTRUCT_SIGNAL_IN(waist_ref_acc,
dynamicgraph::Vector),
149 CONSTRUCT_SIGNAL_IN(w_waist, double),
152 CONSTRUCT_SIGNAL_IN(active_joints,
dynamicgraph::Vector),
153 CONSTRUCT_SIGNAL_INNER(active_joints_checked,
dg::Vector,
156 CONSTRUCT_SIGNAL_OUT(dv_des,
dg::Vector, m_tau_desSOUT),
157 CONSTRUCT_SIGNAL_OUT(v_des,
dg::Vector, m_dv_desSOUT),
158 CONSTRUCT_SIGNAL_OUT(q_des,
dg::Vector, m_v_desSOUT),
159 CONSTRUCT_SIGNAL_OUT(
161 INPUT_SIGNALS << m_tau_desSOUT << m_v_desSOUT << m_q_desSOUT),
163 m_initSucceeded(false),
167 m_robot_util(RefVoidRobotUtil()),
177 docCommandVoid2(
"Initialize the entity.",
178 "Time period in seconds (double)",
179 "Robot reference (string)")));
181 addCommand(
"setControlOutputType",
184 docCommandVoid1(
"Set the type of control output.",
185 "Control type: velocity or torque (string)")));
192 "Update the offset on the CoM based on the CoP measurement.")));
198 SEND_MSG(
"CoM offset updated: " + toString(
m_com_offset), MSG_TYPE_INFO);
208 sotDEBUG(25) <<
"Unrecognized control output type: " << type << endl;
213 return SEND_MSG(
"Init failed: Timestep must be positive", MSG_TYPE_ERROR);
216 std::string localName(robotRef);
217 if (isNameInRobotUtil(localName))
220 SEND_MSG(
"You should have an entity controller manager initialized before",
225 const Eigen::Matrix<double, 3, 4>& contactPoints = m_contact_pointsSIN(0);
226 const Eigen::Vector3d& contactNormal = m_contact_normalSIN(0);
227 const dg::sot::Vector6d& kp_contact = m_kp_contactSIN(0);
228 const dg::sot::Vector6d& kd_contact = m_kd_contactSIN(0);
229 const Eigen::Vector3d& kp_com = m_kp_comSIN(0);
230 const Eigen::Vector3d& kd_com = m_kd_comSIN(0);
231 const VectorN& kp_posture = m_kp_postureSIN(0);
232 const VectorN& kd_posture = m_kd_postureSIN(0);
233 const dg::sot::Vector6d& kp_waist = m_kp_waistSIN(0);
234 const dg::sot::Vector6d& kd_waist = m_kd_waistSIN(0);
236 assert(kp_posture.size() ==
237 static_cast<Eigen::VectorXd::Index
>(
m_robot_util->m_nbJoints));
238 assert(kd_posture.size() ==
239 static_cast<Eigen::VectorXd::Index
>(
m_robot_util->m_nbJoints));
244 const double& w_forces = m_w_forcesSIN(0);
245 const double& mu = m_muSIN(0);
246 const double& fMin = m_f_minSIN(0);
247 const double& fMax = m_f_maxSIN(0);
250 vector<string> package_dirs;
252 new robots::RobotWrapper(
m_robot_util->m_urdf_filename, package_dirs,
253 pinocchio::JointModelFreeFlyer());
271 new Contact6d(
"contact_rfoot", *
m_robot,
273 contactPoints, contactNormal, mu, fMin, fMax);
279 new Contact6d(
"contact_lfoot", *
m_robot,
281 contactPoints, contactNormal, mu, fMin, fMax);
300 Eigen::VectorXd mask_orientation(6);
301 mask_orientation << 0, 0, 0, 1, 1, 1;
318 m_hqpSolver = SolverHQPFactory::createNewSolver(SOLVER_HQP_EIQUADPROG_FAST,
322 }
catch (
const std::exception& e) {
323 std::cout << e.what();
325 "Init failed: Could load URDF :" +
m_robot_util->m_urdf_filename,
338 if (s.size() !=
static_cast<Eigen::VectorXd::Index
>(m_robot_util->m_nbJoints))
339 s.resize(m_robot_util->m_nbJoints);
341 const Eigen::VectorXd& active_joints_sot = m_active_jointsSIN(iter);
342 if (m_enabled ==
false) {
343 if (active_joints_sot.any()) {
346 s = active_joints_sot;
347 Eigen::VectorXd active_joints_urdf(m_robot_util->m_nbJoints);
348 m_robot_util->joints_sot_to_urdf(active_joints_sot, active_joints_urdf);
350 m_taskBlockedJoints =
351 new TaskJointPosture(
"task-posture-blocked", *m_robot);
352 Eigen::VectorXd blocked_joints(m_robot_util->m_nbJoints);
353 for (
unsigned int i = 0; i < m_robot_util->m_nbJoints; i++)
354 if (active_joints_urdf(i) == 0.0)
355 blocked_joints(i) = 1.0;
357 blocked_joints(i) = 0.0;
358 SEND_MSG(
"Blocked joints: " + toString(blocked_joints.transpose()),
360 m_taskBlockedJoints->mask(blocked_joints);
361 TrajectorySample ref_zero(
362 static_cast<unsigned int>(m_robot_util->m_nbJoints));
363 m_taskBlockedJoints->setReference(ref_zero);
364 m_invDyn->addMotionTask(*m_taskBlockedJoints, 1.0, 0);
366 }
else if (!active_joints_sot.any()) {
370 if (m_enabled ==
false)
371 for (
unsigned int i = 0; i < m_robot_util->m_nbJoints; i++) s(i) =
false;
376 if (!m_initSucceeded) {
377 SEND_WARNING_STREAM_MSG(
378 "Cannot compute signal tau_des before initialization!");
381 if (s.size() !=
static_cast<Eigen::VectorXd::Index
>(m_robot_util->m_nbJoints))
382 s.resize(m_robot_util->m_nbJoints);
388 m_active_joints_checkedSINNER(iter);
390 const VectorN6& q_robot = m_qSIN(iter);
391 assert(q_robot.size() ==
392 static_cast<Eigen::VectorXd::Index
>(m_robot_util->m_nbJoints + 6));
393 const VectorN6& v_robot = m_vSIN(iter);
394 assert(v_robot.size() ==
395 static_cast<Eigen::VectorXd::Index
>(m_robot_util->m_nbJoints + 6));
397 const Vector3& x_com_ref = m_com_ref_posSIN(iter);
398 const Vector3& dx_com_ref = m_com_ref_velSIN(iter);
399 const Vector3& ddx_com_ref = m_com_ref_accSIN(iter);
400 const VectorN& x_waist_ref = m_waist_ref_posSIN(iter);
401 const Vector6& dx_waist_ref = m_waist_ref_velSIN(iter);
402 const Vector6& ddx_waist_ref = m_waist_ref_accSIN(iter);
403 const VectorN& q_ref = m_posture_ref_posSIN(iter);
404 assert(q_ref.size() ==
405 static_cast<Eigen::VectorXd::Index
>(m_robot_util->m_nbJoints));
406 const VectorN& dq_ref = m_posture_ref_velSIN(iter);
407 assert(dq_ref.size() ==
408 static_cast<Eigen::VectorXd::Index
>(m_robot_util->m_nbJoints));
409 const VectorN& ddq_ref = m_posture_ref_accSIN(iter);
410 assert(ddq_ref.size() ==
411 static_cast<Eigen::VectorXd::Index
>(m_robot_util->m_nbJoints));
413 const Vector6& kp_contact = m_kp_contactSIN(iter);
414 const Vector6& kd_contact = m_kd_contactSIN(iter);
415 const Vector3& kp_com = m_kp_comSIN(iter);
416 const Vector3& kd_com = m_kd_comSIN(iter);
417 const Vector6& kp_waist = m_kp_waistSIN(iter);
418 const Vector6& kd_waist = m_kd_waistSIN(iter);
420 const VectorN& kp_posture = m_kp_postureSIN(iter);
421 assert(kp_posture.size() ==
422 static_cast<Eigen::VectorXd::Index
>(m_robot_util->m_nbJoints));
423 const VectorN& kd_posture = m_kd_postureSIN(iter);
424 assert(kd_posture.size() ==
425 static_cast<Eigen::VectorXd::Index
>(m_robot_util->m_nbJoints));
428 const double& w_com = m_w_comSIN(iter);
429 const double& w_posture = m_w_postureSIN(iter);
430 const double& w_forces = m_w_forcesSIN(iter);
431 const double& w_waist = m_w_waistSIN(iter);
437 m_sampleCom.pos = x_com_ref - m_com_offset;
438 m_sampleCom.vel = dx_com_ref;
439 m_sampleCom.acc = ddx_com_ref;
440 m_taskCom->setReference(m_sampleCom);
441 m_taskCom->Kp(kp_com);
442 m_taskCom->Kd(kd_com);
443 if (m_w_com != w_com) {
445 m_invDyn->updateTaskWeight(m_taskCom->name(), w_com);
448 m_sampleWaist.pos = x_waist_ref;
449 m_sampleWaist.vel = dx_waist_ref;
450 m_sampleWaist.acc = ddx_waist_ref;
451 m_taskWaist->setReference(m_sampleWaist);
452 m_taskWaist->Kp(kp_waist);
453 m_taskWaist->Kd(kd_waist);
454 if (m_w_waist != w_waist) {
456 m_invDyn->updateTaskWeight(m_taskWaist->name(), w_waist);
459 m_robot_util->joints_sot_to_urdf(q_ref, m_samplePosture.pos);
460 m_robot_util->joints_sot_to_urdf(dq_ref, m_samplePosture.vel);
461 m_robot_util->joints_sot_to_urdf(ddq_ref, m_samplePosture.acc);
462 m_taskPosture->setReference(m_samplePosture);
463 m_taskPosture->Kp(kp_posture);
464 m_taskPosture->Kd(kd_posture);
465 if (m_w_posture != w_posture) {
466 m_w_posture = w_posture;
467 m_invDyn->updateTaskWeight(m_taskPosture->name(), w_posture);
470 const double& fMin = m_f_minSIN(0);
471 const double& fMax = m_f_maxSIN(iter);
472 m_contactLF->setMinNormalForce(fMin);
473 m_contactRF->setMinNormalForce(fMin);
474 m_contactLF->setMaxNormalForce(fMax);
475 m_contactRF->setMaxNormalForce(fMax);
476 m_contactLF->Kp(kp_contact);
477 m_contactLF->Kd(kd_contact);
478 m_contactRF->Kp(kp_contact);
479 m_contactRF->Kd(kd_contact);
480 m_invDyn->updateRigidContactWeights(m_contactLF->name(), w_forces);
481 m_invDyn->updateRigidContactWeights(m_contactRF->name(), w_forces);
485 m_robot_util->config_sot_to_urdf(q_robot, m_q_urdf);
486 m_robot_util->velocity_sot_to_urdf(m_q_urdf, v_robot, m_v_urdf);
488 m_invDyn->computeProblemData(m_t, m_q_urdf, m_v_urdf);
490 pinocchio::SE3 H_lf = m_robot->position(
492 m_robot->model().getJointId(
493 m_robot_util->m_foot_util.m_Left_Foot_Frame_Name));
494 m_contactLF->setReference(H_lf);
495 SEND_MSG(
"Setting left foot reference to " + toString(H_lf),
498 pinocchio::SE3 H_rf = m_robot->position(
500 m_robot->model().getJointId(
501 m_robot_util->m_foot_util.m_Right_Foot_Frame_Name));
502 m_contactRF->setReference(H_rf);
503 SEND_MSG(
"Setting right foot reference to " + toString(H_rf),
505 }
else if (m_timeLast !=
static_cast<unsigned int>(iter - 1)) {
506 SEND_MSG(
"Last time " + toString(m_timeLast) +
507 " is not current time-1: " + toString(iter),
509 if (m_timeLast ==
static_cast<unsigned int>(iter)) {
520 m_timeLast =
static_cast<unsigned int>(iter);
522 const HQPData& hqpData =
523 m_invDyn->computeProblemData(m_t, m_q_urdf, m_v_urdf);
528 SolverHQPBase* solver = m_hqpSolver;
529 getStatistics().store(
"solver dynamic size", 1.0);
531 const HQPOutput& sol = solver->solve(hqpData);
534 if (sol.status != HQP_STATUS_OPTIMAL) {
535 SEND_ERROR_STREAM_MSG(
"HQP solver failed to find a solution: " +
536 toString(sol.status));
537 SEND_DEBUG_STREAM_MSG(tsid::solvers::HQPDataToString(hqpData,
false));
538 SEND_DEBUG_STREAM_MSG(
"q=" + toString(q_robot.transpose(), 1, 5));
539 SEND_DEBUG_STREAM_MSG(
"v=" + toString(v_robot.transpose(), 1, 5));
544 getStatistics().store(
"active inequalities",
545 static_cast<double>(sol.activeSet.size()));
546 getStatistics().store(
"solver iterations", sol.iterations);
548 m_dv_urdf = m_invDyn->getAccelerations(sol);
549 m_robot_util->velocity_urdf_to_sot(m_q_urdf, m_dv_urdf, m_dv_sot);
550 m_robot_util->joints_urdf_to_sot(m_invDyn->getActuatorForces(sol), m_tau_sot);
561 if (!m_initSucceeded) {
562 SEND_WARNING_STREAM_MSG(
563 "Cannot compute signal dv_des before initialization!");
566 if (s.size() != m_robot->nv()) s.resize(m_robot->nv());
573 if (!m_initSucceeded) {
574 SEND_WARNING_STREAM_MSG(
575 "Cannot compute signal v_des before initialization!");
578 if (s.size() != m_robot->nv()) s.resize(m_robot->nv());
580 tsid::math::Vector v_mean;
581 v_mean = m_v_urdf + 0.5 * m_dt * m_dv_urdf;
582 m_v_urdf = m_v_urdf + m_dt * m_dv_urdf;
583 m_q_urdf = pinocchio::integrate(m_robot->model(), m_q_urdf, m_dt * v_mean);
584 m_robot_util->velocity_urdf_to_sot(m_q_urdf, m_v_urdf, m_v_sot);
590 if (!m_initSucceeded) {
591 SEND_WARNING_STREAM_MSG(
592 "Cannot compute signal q_des before initialization!");
595 if (s.size() != m_robot->nv()) s.resize(m_robot->nv());
597 m_robot_util->config_urdf_to_sot(m_q_urdf, m_q_sot);
603 if (!m_initSucceeded) {
604 SEND_WARNING_STREAM_MSG(
"Cannot compute signal u before initialization!");
607 if (s.size() !=
static_cast<Eigen::VectorXd::Index
>(m_robot_util->m_nbJoints))
608 s.resize(m_robot_util->m_nbJoints);
610 const VectorN& kp_pos = m_kp_posSIN(iter);
611 assert(kp_pos.size() ==
612 static_cast<Eigen::VectorXd::Index
>(m_robot_util->m_nbJoints));
613 const VectorN& kd_pos = m_kd_posSIN(iter);
614 assert(kd_pos.size() ==
615 static_cast<Eigen::VectorXd::Index
>(m_robot_util->m_nbJoints));
617 const VectorN6& q_robot = m_qSIN(iter);
618 assert(q_robot.size() ==
619 static_cast<Eigen::VectorXd::Index
>(m_robot_util->m_nbJoints + 6));
620 const VectorN6& v_robot = m_vSIN(iter);
621 assert(v_robot.size() ==
622 static_cast<Eigen::VectorXd::Index
>(m_robot_util->m_nbJoints + 6));
627 kp_pos.cwiseProduct(m_q_sot.tail(m_robot_util->m_nbJoints) -
628 q_robot.tail(m_robot_util->m_nbJoints)) +
629 kd_pos.cwiseProduct(m_v_sot.tail(m_robot_util->m_nbJoints) -
630 v_robot.tail(m_robot_util->m_nbJoints));
642 os <<
"SimpleInverseDyn " << getName();
644 getProfiler().report_all(3, os);
645 getStatistics().report_all(1, os);
647 <<
" nIn " <<
m_invDyn->nIn() <<
"\n";
648 }
catch (ExceptionSignal e) {