closed-loop-dynamics.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020-2022 INRIA
3 //
4 
5 #include <iostream>
6 
17 
18 #include <boost/test/unit_test.hpp>
19 #include <boost/utility/binary.hpp>
20 
21 BOOST_AUTO_TEST_SUITE(BOOST_TEST_MODULE)
22 
23 using namespace pinocchio;
24 using namespace Eigen;
25 
26 BOOST_AUTO_TEST_CASE(closed_loop_constraint_6D_LOCAL)
27 {
30  pinocchio::Data data(model), data_ref(model);
31 
32  model.lowerPositionLimit.head<3>().fill(-1.);
33  model.upperPositionLimit.head<3>().fill(1.);
34 
35  const VectorXd q = randomConfiguration(model);
36  const VectorXd v = VectorXd::Random(model.nv);
37  const VectorXd tau = VectorXd::Random(model.nv);
38 
39  const std::string RF = "rleg6_joint";
40  const std::string LF = "lleg6_joint";
41 
42  // Contact models and data
45 
46  RigidConstraintModel ci_RF_LF(
47  CONTACT_6D, model, model.getJointId(RF), model.getJointId(LF), LOCAL);
48  ci_RF_LF.joint1_placement.setRandom();
49  ci_RF_LF.joint2_placement.setRandom();
50  contact_models.push_back(ci_RF_LF);
51  contact_datas.push_back(RigidConstraintData(ci_RF_LF));
52 
53  Eigen::DenseIndex constraint_dim = 0;
54  for (size_t k = 0; k < contact_models.size(); ++k)
56 
57  const double mu0 = 0.;
58  ProximalSettings prox_settings(1e-12, mu0, 1);
59 
60  Eigen::MatrixXd J_ref(constraint_dim, model.nv);
61  J_ref.setZero();
62 
63  computeAllTerms(model, data_ref, q, v);
64  data_ref.M.triangularView<Eigen::StrictlyLower>() =
65  data_ref.M.transpose().triangularView<Eigen::StrictlyLower>();
66 
67  Data::Matrix6x J_RF_local(6, model.nv), J_LF_local(6, model.nv);
68  J_RF_local.setZero();
69  J_LF_local.setZero();
71  model, data_ref, model.getJointId(RF), ci_RF_LF.joint1_placement, LOCAL, J_RF_local);
73  model, data_ref, model.getJointId(LF), ci_RF_LF.joint2_placement, LOCAL, J_LF_local);
74 
75  const SE3 oMc1_ref = data_ref.oMi[ci_RF_LF.joint1_id] * ci_RF_LF.joint1_placement;
76  const SE3 oMc2_ref = data_ref.oMi[ci_RF_LF.joint2_id] * ci_RF_LF.joint2_placement;
77  const SE3 c1Mc2_ref = oMc1_ref.actInv(oMc2_ref);
78 
79  J_ref = J_RF_local - c1Mc2_ref.toActionMatrix() * J_LF_local;
80 
81  Eigen::VectorXd rhs_ref(constraint_dim);
82 
83  const Motion vc1_ref = ci_RF_LF.joint1_placement.actInv(data_ref.v[ci_RF_LF.joint1_id]);
84  const Motion vc2_ref = ci_RF_LF.joint2_placement.actInv(data_ref.v[ci_RF_LF.joint2_id]);
85  const Motion constraint_velocity_error_ref = vc1_ref - c1Mc2_ref.act(vc2_ref);
86  BOOST_CHECK(constraint_velocity_error_ref.isApprox(Motion(J_ref * v)));
87 
88  const Motion ac1_ref = ci_RF_LF.joint1_placement.actInv(data_ref.a[ci_RF_LF.joint1_id]);
89  const Motion ac2_ref = ci_RF_LF.joint2_placement.actInv(data_ref.a[ci_RF_LF.joint2_id]);
90  const Motion constraint_acceleration_error_ref =
91  ac1_ref - c1Mc2_ref.act(ac2_ref) + constraint_velocity_error_ref.cross(c1Mc2_ref.act(vc2_ref));
92  rhs_ref.segment<6>(0) = constraint_acceleration_error_ref.toVector();
93 
94  Eigen::MatrixXd KKT_matrix_ref =
95  Eigen::MatrixXd::Zero(model.nv + constraint_dim, model.nv + constraint_dim);
96  KKT_matrix_ref.bottomRightCorner(model.nv, model.nv) = data_ref.M;
97  KKT_matrix_ref.topRightCorner(constraint_dim, model.nv) = J_ref;
98  KKT_matrix_ref.bottomLeftCorner(model.nv, constraint_dim) = J_ref.transpose();
99 
102  forwardDynamics(model, data_ref, q, v, tau, J_ref, rhs_ref, mu0);
104 
105  forwardKinematics(model, data_ref, q, v, data_ref.ddq);
106 
107  BOOST_CHECK((J_ref * data_ref.ddq + rhs_ref).isZero());
108 
111 
112  BOOST_CHECK((J_ref * data.ddq + rhs_ref).isZero());
113 
114  BOOST_CHECK(data_ref.ddq.isApprox(data.ddq));
115 
116  const Eigen::MatrixXd KKT_matrix = data.contact_chol.matrix();
117  BOOST_CHECK(KKT_matrix.isApprox(KKT_matrix_ref));
118 
119  // Check with finite differences the error computations
120  Data data_plus(model);
121  const double dt = 1e-8;
122  const Eigen::VectorXd q_plus = integrate(model, q, (v * dt).eval());
123 
124  forwardKinematics(model, data_plus, q_plus, v, Eigen::VectorXd::Zero(model.nv));
125 
126  const SE3 oMc1_plus_ref = data_plus.oMi[ci_RF_LF.joint1_id] * ci_RF_LF.joint1_placement;
127  const SE3 oMc2_plus_ref = data_plus.oMi[ci_RF_LF.joint2_id] * ci_RF_LF.joint2_placement;
128  const SE3 c1Mc2_plus_ref = oMc1_plus_ref.actInv(oMc2_plus_ref);
129 
130  // Position level
131  BOOST_CHECK(contact_datas[0].c1Mc2.isApprox(c1Mc2_ref));
132 
133  // Velocity level
134  BOOST_CHECK(contact_datas[0].contact1_velocity.isApprox(vc1_ref));
135  BOOST_CHECK(contact_datas[0].contact2_velocity.isApprox(vc2_ref));
136 
137  const Motion constraint_velocity_error_fd =
138  -c1Mc2_ref.act(log6(c1Mc2_ref.actInv(c1Mc2_plus_ref))) / dt;
139  BOOST_CHECK(constraint_velocity_error_ref.isApprox(constraint_velocity_error_fd, math::sqrt(dt)));
140  BOOST_CHECK(contact_datas[0].contact_velocity_error.isApprox(constraint_velocity_error_ref));
141 
142  // Acceleration level
143  const Motion vc1_plus_ref = ci_RF_LF.joint1_placement.actInv(data_plus.v[ci_RF_LF.joint1_id]);
144  const Motion vc2_plus_ref = ci_RF_LF.joint2_placement.actInv(data_plus.v[ci_RF_LF.joint2_id]);
145  const Motion constraint_velocity_error_plus_ref = vc1_plus_ref - c1Mc2_plus_ref.act(vc2_plus_ref);
146  const Motion constraint_acceleration_error_fd =
147  (constraint_velocity_error_plus_ref - constraint_velocity_error_ref) / dt;
148  BOOST_CHECK(
149  constraint_acceleration_error_ref.isApprox(constraint_acceleration_error_fd, math::sqrt(dt)));
150 }
151 
152 BOOST_AUTO_TEST_SUITE_END()
pinocchio::RigidConstraintData
RigidConstraintDataTpl< context::Scalar, context::Options > RigidConstraintData
Definition: algorithm/fwd.hpp:27
pinocchio::DataTpl::M
MatrixXs M
The joint space inertia matrix (a square matrix of dim model.nv).
Definition: multibody/data.hpp:199
pinocchio::DataTpl::ddq
TangentVectorType ddq
The joint accelerations computed from ABA.
Definition: multibody/data.hpp:256
frames.hpp
Eigen
pinocchio::DataTpl
Definition: context/generic.hpp:25
pinocchio::forwardKinematics
void forwardKinematics(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< ConfigVectorType > &q)
Update the joint placements according to the current joint configuration.
pinocchio::getFrameJacobian
void getFrameJacobian(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const JointIndex joint_id, const SE3Tpl< Scalar, Options > &placement, const ReferenceFrame reference_frame, const Eigen::MatrixBase< Matrix6xLike > &J)
Returns the jacobian of the frame given by its relative placement w.r.t. a joint frame,...
pinocchio::SE3Tpl< context::Scalar, context::Options >
setup.data
data
Definition: cmake/cython/setup.in.py:48
PINOCCHIO_COMPILER_DIAGNOSTIC_POP
#define PINOCCHIO_COMPILER_DIAGNOSTIC_POP
Definition: include/pinocchio/macros.hpp:130
pinocchio::buildModels::humanoidRandom
void humanoidRandom(ModelTpl< Scalar, Options, JointCollectionTpl > &model, bool usingFF=true)
Create a humanoid kinematic tree with 6-DOF limbs and random joint placements.
explog.hpp
pinocchio::randomConfiguration
void randomConfiguration(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, const Eigen::MatrixBase< ConfigVectorIn1 > &lowerLimits, const Eigen::MatrixBase< ConfigVectorIn2 > &upperLimits, const Eigen::MatrixBase< ReturnType > &qout)
Generate a configuration vector uniformly sampled among provided limits.
Definition: joint-configuration.hpp:315
pinocchio::CONTACT_6D
@ CONTACT_6D
Point contact model.
Definition: algorithm/contact-info.hpp:22
simulation-contact-dynamics.contact_datas
list contact_datas
Definition: simulation-contact-dynamics.py:60
forward-dynamics-derivatives.tau
tau
Definition: forward-dynamics-derivatives.py:25
proximal.hpp
joint-configuration.hpp
pinocchio::ProximalSettingsTpl
Structure containing all the settings parameters for the proximal algorithms.
Definition: algorithm/fwd.hpp:13
pinocchio::integrate
void integrate(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, const Eigen::MatrixBase< ConfigVectorType > &q, const Eigen::MatrixBase< TangentVectorType > &v, const Eigen::MatrixBase< ReturnType > &qout)
Integrate a configuration vector for the specified model for a tangent vector during one unit time.
Definition: joint-configuration.hpp:70
pinocchio::RigidConstraintModelTpl
Definition: algorithm/constraints/fwd.hpp:14
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(closed_loop_constraint_6D_LOCAL)
Definition: closed-loop-dynamics.cpp:26
pinocchio::forwardDynamics
const PINOCCHIO_DEPRECATED DataTpl< Scalar, Options, JointCollectionTpl >::TangentVectorType & forwardDynamics(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< ConfigVectorType > &q, const Eigen::MatrixBase< TangentVectorType1 > &v, const Eigen::MatrixBase< TangentVectorType2 > &tau, const Eigen::MatrixBase< ConstraintMatrixType > &J, const Eigen::MatrixBase< DriftVectorType > &gamma, const Scalar inv_damping=0.)
Compute the forward dynamics with contact constraints. Internally, pinocchio::computeAllTerms is call...
pinocchio::RigidConstraintDataTpl
Definition: algorithm/constraints/fwd.hpp:16
pinocchio::initConstraintDynamics
void initConstraintDynamics(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const std::vector< RigidConstraintModelTpl< Scalar, Options >, Allocator > &contact_models)
Init the forward dynamics data according to the contact information contained in contact_models.
size
FCL_REAL size() const
PINOCCHIO_COMPILER_DIAGNOSTIC_IGNORED_DEPRECECATED_DECLARATIONS
#define PINOCCHIO_COMPILER_DIAGNOSTIC_IGNORED_DEPRECECATED_DECLARATIONS
Definition: include/pinocchio/macros.hpp:131
PINOCCHIO_COMPILER_DIAGNOSTIC_PUSH
#define PINOCCHIO_COMPILER_DIAGNOSTIC_PUSH
macros for pragma push/pop/ignore deprecated warnings
Definition: include/pinocchio/macros.hpp:129
PINOCCHIO_STD_VECTOR_WITH_EIGEN_ALLOCATOR
#define PINOCCHIO_STD_VECTOR_WITH_EIGEN_ALLOCATOR(T)
Definition: container/aligned-vector.hpp:13
pinocchio::q
JointCollectionTpl const Eigen::MatrixBase< ConfigVectorType > & q
Definition: joint-configuration.hpp:1083
pinocchio::v
JointCollectionTpl const Eigen::MatrixBase< ConfigVectorType > const Eigen::MatrixBase< TangentVectorType > & v
Definition: joint-configuration.hpp:1084
fill
fill
pinocchio::log6
MotionTpl< Scalar, Options > log6(const SE3Tpl< Scalar, Options > &M)
Log: SE3 -> se3.
Definition: spatial/explog.hpp:435
pinocchio::DataTpl::Matrix6x
Eigen::Matrix< Scalar, 6, Eigen::Dynamic, Options > Matrix6x
The 6d jacobian type (temporary)
Definition: multibody/data.hpp:92
contact-cholesky.contact_models
list contact_models
Definition: contact-cholesky.py:22
contact-info.hpp
contact-dynamics.hpp
anymal-simulation.constraint_dim
constraint_dim
Definition: anymal-simulation.py:52
sample-models.hpp
pinocchio::MotionTpl
Definition: context/casadi.hpp:27
jacobian.hpp
pinocchio::ModelTpl
Definition: context/generic.hpp:20
constrained-dynamics.hpp
simulation-closed-kinematic-chains.prox_settings
prox_settings
Definition: simulation-closed-kinematic-chains.py:163
classic-acceleration.hpp
pinocchio::computeAllTerms
void computeAllTerms(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< ConfigVectorType > &q, const Eigen::MatrixBase< TangentVectorType > &v)
Computes efficiently all the terms needed for dynamic simulation. It is equivalent to the call at the...
pinocchio::constraintDynamics
const DataTpl< Scalar, Options, JointCollectionTpl >::TangentVectorType & constraintDynamics(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< ConfigVectorType > &q, const Eigen::MatrixBase< TangentVectorType1 > &v, const Eigen::MatrixBase< TangentVectorType2 > &tau, const std::vector< RigidConstraintModelTpl< Scalar, Options >, ConstraintModelAllocator > &contact_models, std::vector< RigidConstraintDataTpl< Scalar, Options >, ConstraintDataAllocator > &contact_datas, ProximalSettingsTpl< Scalar > &settings)
Computes the forward dynamics with contact constraints according to a given list of contact informati...
cartpole.dt
float dt
Definition: cartpole.py:145
pinocchio::model
JointCollectionTpl & model
Definition: joint-configuration.hpp:1082
pinocchio
Main pinocchio namespace.
Definition: timings.cpp:27
pinocchio::LOCAL
@ LOCAL
Definition: multibody/fwd.hpp:50


pinocchio
Author(s):
autogenerated on Fri Nov 1 2024 02:41:39