Skip to content

Commit 962a2ad

Browse files
author
earlaud
committed
Factorize tests
1 parent afd614d commit 962a2ad

File tree

1 file changed

+65
-51
lines changed

1 file changed

+65
-51
lines changed

tests/inverse_dynamics.cpp

Lines changed: 65 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -25,70 +25,92 @@ Eigen::VectorXd solo_q_start(const RobotModelHandler & model_handler)
2525
return q_start;
2626
}
2727

28-
void check_joint_limits(
29-
const RobotModelHandler & model_handler,
30-
const Eigen::VectorXd & q,
31-
const Eigen::VectorXd & v,
32-
const Eigen::VectorXd & tau)
28+
// Helper class to create the problem and run it
29+
class TestKinoID
3330
{
34-
const pinocchio::Model & model = model_handler.getModel();
35-
for (int i = 0; i < model.nv - 6; i++)
31+
public:
32+
TestKinoID(RobotModelHandler model_handler_, KinodynamicsID::Settings settings_)
33+
: model_handler(model_handler_)
34+
, data_handler(model_handler)
35+
, settings(settings_)
36+
, solver(model_handler, dt, settings)
37+
, q(model_handler.getReferenceState().head(model_handler.getModel().nq))
38+
, dq(Eigen::VectorXd::Zero(model_handler.getModel().nv))
39+
, ddq(Eigen::VectorXd::Zero(model_handler.getModel().nv))
40+
, tau(Eigen::VectorXd::Zero(model_handler.getModel().nv - 6))
3641
{
37-
BOOST_CHECK_LE(q[7 + i], model.upperPositionLimit[7 + i]);
38-
BOOST_CHECK_GE(q[7 + i], model.lowerPositionLimit[7 + i]);
39-
BOOST_CHECK_LE(v[6 + i], model.upperVelocityLimit[6 + i]);
40-
// Do not use lower velocity bound as TSID cannot handle it
41-
BOOST_CHECK_GE(v[6 + i], -model.upperVelocityLimit[6 + i]);
42-
BOOST_CHECK_LE(tau[i], model.upperEffortLimit[6 + i]);
43-
BOOST_CHECK_GE(tau[i], model.lowerEffortLimit[6 + i]);
4442
}
45-
}
43+
44+
void step()
45+
{
46+
// Solve
47+
solver.solve(t, q, dq, tau);
48+
solver.getAccelerations(ddq);
49+
50+
// Integrate
51+
t += dt;
52+
q = pinocchio::integrate(model_handler.getModel(), q, (dq + ddq / 2. * dt) * dt);
53+
dq += ddq * dt;
54+
55+
// Check common to all tests
56+
check_joint_limits();
57+
}
58+
59+
protected:
60+
void check_joint_limits()
61+
{
62+
const pinocchio::Model & model = model_handler.getModel();
63+
for (int i = 0; i < model.nv - 6; i++)
64+
{
65+
BOOST_CHECK_LE(q[7 + i], model.upperPositionLimit[7 + i]);
66+
BOOST_CHECK_GE(q[7 + i], model.lowerPositionLimit[7 + i]);
67+
BOOST_CHECK_LE(dq[6 + i], model.upperVelocityLimit[6 + i]);
68+
// Do not use lower velocity bound as TSID cannot handle it
69+
BOOST_CHECK_GE(dq[6 + i], -model.upperVelocityLimit[6 + i]);
70+
BOOST_CHECK_LE(tau[i], model.upperEffortLimit[6 + i]);
71+
BOOST_CHECK_GE(tau[i], model.lowerEffortLimit[6 + i]);
72+
}
73+
}
74+
75+
public:
76+
const RobotModelHandler model_handler;
77+
RobotDataHandler data_handler;
78+
KinodynamicsID::Settings settings;
79+
double dt = 1e-3;
80+
KinodynamicsID solver;
81+
82+
double t = 0.;
83+
Eigen::VectorXd q;
84+
Eigen::VectorXd dq;
85+
Eigen::VectorXd ddq;
86+
Eigen::VectorXd tau;
87+
};
4688

4789
BOOST_AUTO_TEST_CASE(KinodynamicsID_postureTask)
4890
{
49-
RobotModelHandler model_handler = getSoloHandler();
50-
RobotDataHandler data_handler(model_handler);
51-
const double dt = 1e-3;
91+
TestKinoID test(getSoloHandler(), KinodynamicsID::Settings().set_kp_posture(20.).set_w_posture(1.));
92+
const RobotModelHandler & model_handler = test.model_handler;
5293

53-
KinodynamicsID solver(
54-
model_handler, dt, KinodynamicsID::Settings().set_kp_posture(20.).set_w_posture(1.)); // only a posture task
94+
Eigen::VectorXd error = 1e12 * Eigen::VectorXd::Ones(model_handler.getModel().nv);
5595

5696
const Eigen::VectorXd q_target = model_handler.getReferenceState().head(model_handler.getModel().nq);
57-
58-
solver.setTarget(
97+
test.solver.setTarget(
5998
q_target, Eigen::VectorXd::Zero(model_handler.getModel().nv), Eigen::VectorXd::Zero(model_handler.getModel().nv),
6099
{false, false, false, false}, {});
61100

62-
double t = 0;
63-
Eigen::VectorXd q = solo_q_start(model_handler);
64-
Eigen::VectorXd v = Eigen::VectorXd::Random(model_handler.getModel().nv);
65-
Eigen::VectorXd a = Eigen::VectorXd::Random(model_handler.getModel().nv);
66-
Eigen::VectorXd tau = Eigen::VectorXd::Zero(model_handler.getModel().nv - 6);
67-
68-
Eigen::VectorXd error = 1e12 * Eigen::VectorXd::Ones(model_handler.getModel().nv);
69-
70101
for (int i = 0; i < 10000; i++)
71102
{
72103
// Solve and get solution
73-
solver.solve(t, q, v, tau);
74-
solver.getAccelerations(a);
75-
76-
// Integrate
77-
t += dt;
78-
q = pinocchio::integrate(model_handler.getModel(), q, (v + a / 2. * dt) * dt);
79-
v += a * dt;
104+
test.step();
80105

81106
// compensate for free fall as we only care about joint posture
82-
q.head(7) = q_target.head(7);
83-
v.head(6).setZero();
107+
test.q.head(7) = q_target.head(7);
108+
test.dq.head(6).setZero();
84109

85110
// Check error is decreasing
86-
Eigen::VectorXd new_error = pinocchio::difference(model_handler.getModel(), q, q_target);
111+
Eigen::VectorXd new_error = pinocchio::difference(model_handler.getModel(), test.q, q_target);
87112
BOOST_CHECK_LE(new_error.norm(), error.norm());
88113
error = new_error;
89-
90-
// Check joints limits
91-
check_joint_limits(model_handler, q, v, tau);
92114
}
93115
}
94116

@@ -155,9 +177,6 @@ BOOST_AUTO_TEST_CASE(KinodynamicsID_contact)
155177
// Robot had time to reach permanent regime, is it robot free falling ?
156178
BOOST_CHECK_SMALL(a.head(3).norm() - model_handler.getModel().gravity.linear().norm(), 0.01);
157179
}
158-
159-
// Check joints limits
160-
check_joint_limits(model_handler, q, v, tau);
161180
}
162181
}
163182

@@ -207,8 +226,6 @@ BOOST_AUTO_TEST_CASE(KinodynamicsID_baseTask)
207226
BOOST_CHECK(new_error.norm() < 2e-2); // Should have converged by now
208227

209228
error = new_error;
210-
// Check joints limits
211-
check_joint_limits(model_handler, q, v, tau);
212229
}
213230
}
214231

@@ -264,9 +281,6 @@ BOOST_AUTO_TEST_CASE(KinodynamicsID_allTasks)
264281
Eigen::VectorXd new_error = pinocchio::difference(model_handler.getModel(), q, q_target);
265282
BOOST_CHECK_LE(new_error.norm(), error.norm());
266283
error = new_error;
267-
268-
// Check joints limits
269-
check_joint_limits(model_handler, q, v, tau);
270284
}
271285
}
272286

0 commit comments

Comments
 (0)