16 #include <casadi/casadi.hpp>
19 #include <boost/test/unit_test.hpp>
20 #include <boost/utility/binary.hpp>
22 BOOST_AUTO_TEST_SUITE(BOOST_TEST_MODULE)
27 typedef casadi::SX ADScalar;
37 model.lowerPositionLimit.head<3>().
fill(-1.);
38 model.upperPositionLimit.head<3>().
fill(1.);
41 typedef Model::ConfigVectorType ConfigVector;
42 typedef Model::TangentVectorType TangentVector;
45 TangentVector
v(TangentVector::Random(
model.nv));
46 TangentVector
tau(TangentVector::Random(
model.nv));
48 typedef ADModel::ConfigVectorType ConfigVectorAD;
49 typedef ADModel::TangentVectorType TangentVectorAD;
50 ADModel ad_model =
model.cast<ADScalar>();
51 ADData ad_data(ad_model);
58 q_ad = Eigen::Map<ConfigVectorAD>(
static_cast<std::vector<ADScalar>
>(cs_q).
data(),
model.nq, 1);
60 Eigen::Map<ConfigVectorAD>(
static_cast<std::vector<ADScalar>
>(cs_v_int).
data(),
model.nv, 1);
63 casadi::SX cs_q_int(
model.nq, 1);
65 std::vector<double> q_vec((
size_t)
model.nq);
66 Eigen::Map<ConfigVector>(q_vec.data(),
model.nq, 1) =
q;
68 std::vector<double> v_int_vec((
size_t)
model.nv);
69 Eigen::Map<TangentVector>(v_int_vec.data(),
model.nv, 1).setZero();
72 TangentVectorAD v_ad(
model.nv);
73 v_ad = Eigen::Map<TangentVectorAD>(
static_cast<std::vector<ADScalar>
>(cs_v).
data(),
model.nv, 1);
76 TangentVectorAD tau_ad(
model.nv);
78 Eigen::Map<TangentVectorAD>(
static_cast<std::vector<ADScalar>
>(cs_tau).
data(),
model.nv, 1);
82 casadi::SX cs_ddq(
model.nv, 1);
83 for (Eigen::DenseIndex k = 0; k <
model.nv; ++k)
84 cs_ddq(k) = ad_data.ddq[k];
85 casadi::Function eval_aba(
86 "eval_aba", casadi::SXVector{cs_q, cs_v_int, cs_v, cs_tau}, casadi::SXVector{cs_ddq});
88 std::vector<double> v_vec((
size_t)
model.nv);
89 Eigen::Map<TangentVector>(v_vec.data(),
model.nv, 1) =
v;
91 std::vector<double> tau_vec((
size_t)
model.nv);
92 Eigen::Map<TangentVector>(tau_vec.data(),
model.nv, 1) =
tau;
94 casadi::DM ddq_res = eval_aba(casadi::DMVector{q_vec, v_int_vec, v_vec, tau_vec})[0];
95 Data::TangentVectorType ddq_mat = Eigen::Map<Data::TangentVectorType>(
96 static_cast<std::vector<double>
>(ddq_res).
data(),
model.nv, 1);
98 BOOST_CHECK(ddq_mat.isApprox(
data.ddq));
103 ddq_dq_ref.setZero();
104 ddq_dv_ref.setZero();
105 ddq_dtau_ref.setZero();
108 ddq_dtau_ref.triangularView<Eigen::StrictlyLower>() =
109 ddq_dtau_ref.transpose().triangularView<Eigen::StrictlyLower>();
113 casadi::Function eval_ddq_dq(
114 "eval_ddq_dq", casadi::SXVector{cs_q, cs_v_int, cs_v, cs_tau}, casadi::SXVector{
ddq_dq});
116 casadi::DM ddq_dq_res = eval_ddq_dq(casadi::DMVector{q_vec, v_int_vec, v_vec, tau_vec})[0];
117 std::vector<double> ddq_dq_vec(
static_cast<std::vector<double>
>(ddq_dq_res));
119 Eigen::Map<Data::MatrixXs>(ddq_dq_vec.data(),
model.nv,
model.nv).isApprox(ddq_dq_ref));
123 casadi::Function eval_ddq_dv(
124 "eval_ddq_dv", casadi::SXVector{cs_q, cs_v_int, cs_v, cs_tau}, casadi::SXVector{
ddq_dv});
126 casadi::DM ddq_dv_res = eval_ddq_dv(casadi::DMVector{q_vec, v_int_vec, v_vec, tau_vec})[0];
127 std::vector<double> ddq_dv_vec(
static_cast<std::vector<double>
>(ddq_dv_res));
129 Eigen::Map<Data::MatrixXs>(ddq_dv_vec.data(),
model.nv,
model.nv).isApprox(ddq_dv_ref));
133 casadi::Function eval_ddq_da(
134 "eval_ddq_da", casadi::SXVector{cs_q, cs_v_int, cs_v, cs_tau}, casadi::SXVector{
ddq_dtau});
136 casadi::DM ddq_dtau_res = eval_ddq_da(casadi::DMVector{q_vec, v_int_vec, v_vec, tau_vec})[0];
137 std::vector<double> ddq_dtau_vec(
static_cast<std::vector<double>
>(ddq_dtau_res));
139 Eigen::Map<Data::MatrixXs>(ddq_dtau_vec.data(),
model.nv,
model.nv).isApprox(ddq_dtau_ref));
147 ad_data.Minv.triangularView<Eigen::StrictlyLower>() =
148 ad_data.Minv.transpose().triangularView<Eigen::StrictlyLower>();
154 casadi::Function eval_aba_derivatives_dq(
155 "eval_aba_derivatives_dq", casadi::SXVector{cs_q, cs_v, cs_tau}, casadi::SXVector{cs_ddq_dq});
157 casadi::DM ddq_dq_res_direct =
158 eval_aba_derivatives_dq(casadi::DMVector{q_vec, v_vec, tau_vec})[0];
159 Data::MatrixXs ddq_dq_res_direct_map = Eigen::Map<Data::MatrixXs>(
160 static_cast<std::vector<double>
>(ddq_dq_res_direct).
data(),
model.nv,
model.nv);
161 BOOST_CHECK(ddq_dq_ref.isApprox(ddq_dq_res_direct_map));
163 casadi::Function eval_aba_derivatives_dv(
164 "eval_aba_derivatives_dv", casadi::SXVector{cs_q, cs_v, cs_tau}, casadi::SXVector{cs_ddq_dv});
166 casadi::DM ddq_dv_res_direct =
167 eval_aba_derivatives_dv(casadi::DMVector{q_vec, v_vec, tau_vec})[0];
168 Data::MatrixXs ddq_dv_res_direct_map = Eigen::Map<Data::MatrixXs>(
169 static_cast<std::vector<double>
>(ddq_dv_res_direct).
data(),
model.nv,
model.nv);
170 BOOST_CHECK(ddq_dv_ref.isApprox(ddq_dv_res_direct_map));
172 casadi::Function eval_aba_derivatives_dtau(
173 "eval_aba_derivatives_dtau", casadi::SXVector{cs_q, cs_v, cs_tau},
174 casadi::SXVector{cs_ddq_dtau});
176 casadi::DM ddq_dtau_res_direct =
177 eval_aba_derivatives_dtau(casadi::DMVector{q_vec, v_vec, tau_vec})[0];
178 Data::MatrixXs ddq_dtau_res_direct_map = Eigen::Map<Data::MatrixXs>(
179 static_cast<std::vector<double>
>(ddq_dtau_res_direct).
data(),
model.nv,
model.nv);
180 BOOST_CHECK(ddq_dtau_ref.isApprox(ddq_dtau_res_direct_map));
188 typedef typename Model::ConfigVectorType ConfigVector;
189 typedef typename Model::TangentVectorType TangentVector;
193 model.lowerPositionLimit.head<3>().
fill(-1.);
194 model.upperPositionLimit.head<3>().
fill(1.);
199 TangentVector
v(TangentVector::Random(
model.nv));
200 TangentVector
tau(TangentVector::Random(
model.nv));
204 data.Minv.triangularView<Eigen::StrictlyLower>() =
205 data.Minv.transpose().triangularView<Eigen::StrictlyLower>();
209 std::cout <<
"Number of operations in the ABA function = " << ad_casadi.getFunOperationCount()
211 std::cout <<
"Number of operations in the ABA derivs function = "
212 << ad_casadi.getFunDerivsOperationCount() <<
"\n";
215 ad_casadi.evalFunction(
q,
v,
tau);
216 ad_casadi.evalJacobian(
q,
v,
tau);
217 BOOST_CHECK(ad_casadi.ddq.isApprox(
data.ddq));
218 BOOST_CHECK(ad_casadi.ddq_dq.isApprox(
data.ddq_dq));
219 BOOST_CHECK(ad_casadi.ddq_dv.isApprox(
data.ddq_dv));
220 BOOST_CHECK(ad_casadi.ddq_dtau.isApprox(
data.Minv));
223 BOOST_AUTO_TEST_SUITE_END()