12 #include <casadi/casadi.hpp>
15 #include <boost/test/unit_test.hpp>
16 #include <boost/utility/binary.hpp>
18 BOOST_AUTO_TEST_SUITE(BOOST_TEST_MODULE)
23 typedef casadi::SX ADScalar;
26 using casadi::SXVector;
30 model.lowerPositionLimit.head<3>().
fill(-1.);
31 model.upperPositionLimit.head<3>().
fill(1.);
33 typedef Model::ConfigVectorType ConfigVector;
34 typedef Eigen::Map<ConfigVector> ConfigVectorMap;
40 ADModel ad_model =
model.cast<ADScalar>();
41 typedef ADModel::ConfigVectorType cConfig_t;
42 typedef ADModel::TangentVectorType cTangent_t;
48 casadi::SX v_all = vertcat(cv0, cv1);
52 q0_ad = Eigen::Map<cConfig_t>(
static_cast<SXVector
>(cs_q0).
data(),
model.nq, 1);
53 q1_ad = Eigen::Map<cConfig_t>(
static_cast<SXVector
>(cs_q1).
data(),
model.nq, 1);
54 v0_ad = Eigen::Map<cTangent_t>(
static_cast<SXVector
>(cv0).
data(),
model.nv, 1);
55 v1_ad = Eigen::Map<cTangent_t>(
static_cast<SXVector
>(cv1).
data(),
model.nv, 1);
61 casadi::SX d_vec(
model.nv);
63 ADScalar dist_expr =
d.dot(
d);
67 casadi::SX zero_vec = casadi::SX::zeros(2 *
model.nv);
68 casadi::SX Jdist_expr = substitute(gradient(dist_expr, v_all), v_all, zero_vec);
69 casadi::Function eval_Jdist(
"Jdistance", SXVector{cs_q0, cs_q1}, SXVector{Jdist_expr});
71 std::cout <<
"Jdistance func: " << eval_Jdist <<
'\n';
73 std::vector<Scalar> q0_vec(
nq);
74 ConfigVectorMap(q0_vec.data(),
model.nq, 1) =
q0;
75 std::vector<Scalar> q1_vec(
nq);
76 ConfigVectorMap(q1_vec.data(),
model.nq, 1) =
q1;
77 std::vector<Scalar> q2_vec(
nq);
78 ConfigVectorMap(q2_vec.data(),
model.nq, 1) =
q2;
80 auto J0 = eval_Jdist(casadi::DMVector{q0_vec, q0_vec})[0];
81 auto J1 = eval_Jdist(casadi::DMVector{q0_vec, q1_vec})[0];
82 auto J2 = eval_Jdist(casadi::DMVector{q0_vec, q2_vec})[0];
85 std::vector<Scalar> q_vec(
nq);
86 for (
size_t it = 0;
it < 10;
it++)
89 ConfigVectorMap(q_vec.data(),
model.nq, 1) =
q;
90 auto J_vec =
static_cast<std::vector<Scalar>
>(eval_Jdist(casadi::DMVector{q_vec, q_vec})[0]);
91 Eigen::Map<Eigen::MatrixXd> J_as_mat(J_vec.data(), 2 *
model.nv, 1);
92 BOOST_CHECK(J_as_mat.isApprox(Eigen::MatrixXd::Zero(2 *
model.nv, 1)));
96 template<
typename Derived>
100 auto M_mat =
M.toHomogeneousMatrix();
101 std::vector<Scalar> flat_M_vec(M_mat.data(), M_mat.data() + M_mat.size());
102 casadi::DM out{flat_M_vec};
103 return reshape(out, 4, 4);
109 using casadi::DMVector;
110 using casadi::SXVector;
111 using casadi::SXVectorVector;
112 typedef pin::SE3Tpl<double>
SE3;
113 typedef casadi::SX ADScalar;
114 typedef pin::SE3Tpl<ADScalar> cSE3;
115 typedef pin::MotionTpl<ADScalar> cMotion;
121 casadi::SX csm_all = vertcat(csm0, csm1);
123 cMotion::Vector6 cm0_v, cm1_v;
128 cm0_v = Eigen::Map<cMotion::Vector6>(
static_cast<SXVector
>(csm0).
data(), 6, 1);
129 cm1_v = Eigen::Map<cMotion::Vector6>(
static_cast<SXVector
>(csm1).
data(), 6, 1);
131 cSE3::Matrix4 cM0_mat, cM1_mat;
132 cM0_mat = Eigen::Map<cSE3::Matrix4>(&
static_cast<SXVector
>(csM0)[0], 4, 4);
133 cM1_mat = Eigen::Map<cSE3::Matrix4>(&
static_cast<SXVector
>(csM1)[0], 4, 4);
135 auto rot0 = cM0_mat.template block<3, 3>(0, 0);
136 auto rot1 = cM1_mat.template block<3, 3>(0, 0);
137 auto trans0 = cM0_mat.template block<3, 1>(0, 3);
139 cSE3 cM0(rot0, trans0);
140 cSE3 cM1(rot1, cM1_mat.template block<3, 1>(0, 3));
146 auto cdM(
pin::log6(cM0_i.actInv(cM1_i)).toVector());
149 casadi::SX zeros = casadi::SX::zeros(12);
151 auto dist = cdM.squaredNorm();
152 auto grad_cdM_expr = gradient(
dist, csm_all);
153 auto hess_cdM_expr =
jacobian(grad_cdM_expr, csm_all);
155 casadi::Function grad_cdM_eval(
156 "gdM", SXVector{csM0, csM1}, SXVector{substitute(grad_cdM_expr, csm_all, zeros)});
157 casadi::Function hess_cdM_eval(
158 "gdM", SXVector{csM0, csM1}, SXVector{substitute(hess_cdM_expr, csm_all, zeros)});
160 std::cout <<
"dM_eval: " << grad_cdM_eval <<
'\n';
162 SE3 M_neutral(SE3::Identity());
163 SE3 M0(SE3::Random()), M1(SE3::Random());
169 std::cout << M0_dm <<
"\n\n";
170 std::cout << M2_dm <<
'\n';
172 auto J0 = grad_cdM_eval(DMVector{M0_dm, M1_dm})[0];
173 std::cout << J0 <<
'\n';
175 auto J1 = grad_cdM_eval(DMVector{M0_dm, M2_dm})[0];
176 std::cout << J1 <<
'\n';
178 auto J2 = grad_cdM_eval(DMVector{M2_dm, M2_dm})[0];
179 std::cout << J2 <<
'\n';
181 std::cout << hess_cdM_eval(DMVector{M_n_dm, M_n_dm})[0];
182 std::cout << hess_cdM_eval(DMVector{M0_dm, M0_dm})[0];
183 std::cout << hess_cdM_eval(DMVector{M0_dm, M1_dm})[0];
184 std::cout << hess_cdM_eval(DMVector{M2_dm, M2_dm})[0];
187 BOOST_AUTO_TEST_SUITE_END()