Directory: | ./ |
---|---|
File: | unittest/test_contact_constraints.cpp |
Date: | 2025-01-16 08:47:40 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 51 | 51 | 100.0% |
Branches: | 111 | 212 | 52.4% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | ||
2 | // BSD 3-Clause License | ||
3 | // | ||
4 | // Copyright (C) 2021-2023, University of Edinburgh, Heriot-Watt University | ||
5 | // Copyright note valid unless otherwise stated in individual files. | ||
6 | // All rights reserved. | ||
7 | /////////////////////////////////////////////////////////////////////////////// | ||
8 | |||
9 | #define BOOST_TEST_NO_MAIN | ||
10 | #define BOOST_TEST_ALTERNATIVE_INIT_API | ||
11 | |||
12 | #include "factory/contact_constraint.hpp" | ||
13 | #include "unittest_common.hpp" | ||
14 | |||
15 | using namespace boost::unit_test; | ||
16 | using namespace crocoddyl::unittest; | ||
17 | |||
18 | //----------------------------------------------------------------------------// | ||
19 | |||
20 | 13 | void test_partial_derivatives_against_contact_numdiff( | |
21 | ContactConstraintModelTypes::Type constraint_type, | ||
22 | PinocchioModelTypes::Type model_type, | ||
23 | ActuationModelTypes::Type actuation_type) { | ||
24 | // create the model | ||
25 | const boost::shared_ptr<crocoddyl::DifferentialActionModelAbstract> &model = | ||
26 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | ContactConstraintModelFactory().create(constraint_type, model_type, |
27 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | actuation_type); |
28 | |||
29 | // create the corresponding data object and set the constraint to nan | ||
30 | const boost::shared_ptr<crocoddyl::DifferentialActionDataAbstract> &data = | ||
31 |
1/2✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
13 | model->createData(); |
32 | |||
33 |
1/2✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
13 | crocoddyl::DifferentialActionModelNumDiff model_num_diff(model); |
34 | const boost::shared_ptr<crocoddyl::DifferentialActionDataAbstract> | ||
35 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | &data_num_diff = model_num_diff.createData(); |
36 | |||
37 | // Generating random values for the state and control | ||
38 |
1/2✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
|
13 | Eigen::VectorXd x = model->get_state()->rand(); |
39 |
2/4✓ Branch 3 taken 13 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
|
13 | const Eigen::VectorXd u = Eigen::VectorXd::Random(model->get_nu()); |
40 | |||
41 | // Computing the action derivatives | ||
42 |
3/6✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 13 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 13 times.
✗ Branch 9 not taken.
|
13 | model->calc(data, x, u); |
43 |
3/6✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 13 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 13 times.
✗ Branch 9 not taken.
|
13 | model->calcDiff(data, x, u); |
44 |
3/6✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 13 times.
✗ Branch 8 not taken.
|
13 | model_num_diff.calc(data_num_diff, x, u); |
45 |
3/6✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 13 times.
✗ Branch 8 not taken.
|
13 | model_num_diff.calcDiff(data_num_diff, x, u); |
46 | // Tolerance defined as in | ||
47 | // http://www.it.uom.gr/teaching/linearalgebra/NumericalRecipiesInC/c5-7.pdf | ||
48 | 13 | double tol = std::pow(model_num_diff.get_disturbance(), 1. / 3.); | |
49 |
8/16✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 13 times.
✗ Branch 6 not taken.
✓ Branch 11 taken 13 times.
✗ Branch 12 not taken.
✓ Branch 16 taken 13 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 13 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 13 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 13 times.
✗ Branch 26 not taken.
✗ Branch 30 not taken.
✓ Branch 31 taken 13 times.
|
13 | BOOST_CHECK((data->Gx - data_num_diff->Gx).isZero(tol)); |
50 |
8/16✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 13 times.
✗ Branch 6 not taken.
✓ Branch 11 taken 13 times.
✗ Branch 12 not taken.
✓ Branch 16 taken 13 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 13 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 13 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 13 times.
✗ Branch 26 not taken.
✗ Branch 30 not taken.
✓ Branch 31 taken 13 times.
|
13 | BOOST_CHECK((data->Gu - data_num_diff->Gu).isZero(tol)); |
51 |
8/16✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 13 times.
✗ Branch 6 not taken.
✓ Branch 11 taken 13 times.
✗ Branch 12 not taken.
✓ Branch 16 taken 13 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 13 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 13 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 13 times.
✗ Branch 26 not taken.
✗ Branch 30 not taken.
✓ Branch 31 taken 13 times.
|
13 | BOOST_CHECK((data->Hx - data_num_diff->Hx).isZero(tol)); |
52 |
8/16✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 13 times.
✗ Branch 6 not taken.
✓ Branch 11 taken 13 times.
✗ Branch 12 not taken.
✓ Branch 16 taken 13 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 13 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 13 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 13 times.
✗ Branch 26 not taken.
✗ Branch 30 not taken.
✓ Branch 31 taken 13 times.
|
13 | BOOST_CHECK((data->Hu - data_num_diff->Hu).isZero(tol)); |
53 | |||
54 | // Computing the action derivatives | ||
55 |
1/2✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
|
13 | x = model->get_state()->rand(); |
56 |
2/4✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 13 times.
✗ Branch 6 not taken.
|
13 | model->calc(data, x); |
57 |
2/4✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 13 times.
✗ Branch 6 not taken.
|
13 | model->calcDiff(data, x); |
58 |
2/4✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
|
13 | model_num_diff.calc(data_num_diff, x); |
59 |
2/4✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
|
13 | model_num_diff.calcDiff(data_num_diff, x); |
60 |
8/16✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 13 times.
✗ Branch 6 not taken.
✓ Branch 11 taken 13 times.
✗ Branch 12 not taken.
✓ Branch 16 taken 13 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 13 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 13 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 13 times.
✗ Branch 26 not taken.
✗ Branch 30 not taken.
✓ Branch 31 taken 13 times.
|
13 | BOOST_CHECK((data->Gx - data_num_diff->Gx).isZero(tol)); |
61 |
8/16✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 13 times.
✗ Branch 6 not taken.
✓ Branch 11 taken 13 times.
✗ Branch 12 not taken.
✓ Branch 16 taken 13 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 13 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 13 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 13 times.
✗ Branch 26 not taken.
✗ Branch 30 not taken.
✓ Branch 31 taken 13 times.
|
13 | BOOST_CHECK((data->Hx - data_num_diff->Hx).isZero(tol)); |
62 | 13 | } | |
63 | |||
64 | //----------------------------------------------------------------------------// | ||
65 | |||
66 | 13 | void register_contact_constraint_model_unit_tests( | |
67 | ContactConstraintModelTypes::Type constraint_type, | ||
68 | PinocchioModelTypes::Type model_type, | ||
69 | ActuationModelTypes::Type actuation_type) { | ||
70 |
2/4✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
|
13 | boost::test_tools::output_test_stream test_name; |
71 |
5/10✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 13 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 13 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 13 times.
✗ Branch 14 not taken.
|
13 | test_name << "test_" << constraint_type << "_" << actuation_type << "_" |
72 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | << model_type; |
73 |
4/8✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 13 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 13 times.
✗ Branch 11 not taken.
|
13 | std::cout << "Running " << test_name.str() << std::endl; |
74 |
4/8✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 13 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 13 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 13 times.
✗ Branch 12 not taken.
|
13 | test_suite *ts = BOOST_TEST_SUITE(test_name.str()); |
75 |
5/10✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 13 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 13 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 13 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 13 times.
✗ Branch 15 not taken.
|
13 | ts->add(BOOST_TEST_CASE( |
76 | boost::bind(&test_partial_derivatives_against_contact_numdiff, | ||
77 | constraint_type, model_type, actuation_type))); | ||
78 |
3/6✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
|
13 | framework::master_test_suite().add(ts); |
79 | 13 | } | |
80 | |||
81 | 1 | bool init_function() { | |
82 | // Test all the contact constraint model. Note that we can do it only with | ||
83 | // humanoids as it needs to test the contact wrench cone | ||
84 | 6 | for (size_t constraint_type = 0; | |
85 |
2/2✓ Branch 1 taken 5 times.
✓ Branch 2 taken 1 times.
|
6 | constraint_type < ContactConstraintModelTypes::all.size(); |
86 | ++constraint_type) { | ||
87 | 5 | register_contact_constraint_model_unit_tests( | |
88 | 5 | ContactConstraintModelTypes::all[constraint_type], | |
89 | PinocchioModelTypes::Talos, | ||
90 | ActuationModelTypes::ActuationModelFloatingBase); | ||
91 | 5 | register_contact_constraint_model_unit_tests( | |
92 | 5 | ContactConstraintModelTypes::all[constraint_type], | |
93 | PinocchioModelTypes::RandomHumanoid, | ||
94 | ActuationModelTypes::ActuationModelFloatingBase); | ||
95 | 5 | if (ContactConstraintModelTypes::all[constraint_type] == | |
96 | ContactConstraintModelTypes:: | ||
97 | 4 | ConstraintModelResidualContactForceEquality || | |
98 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 times.
|
4 | ContactConstraintModelTypes::all[constraint_type] == |
99 | ContactConstraintModelTypes:: | ||
100 |
4/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 2 times.
|
9 | ConstraintModelResidualContactFrictionConeInequality || |
101 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
|
3 | ContactConstraintModelTypes::all[constraint_type] == |
102 | ContactConstraintModelTypes:: | ||
103 | ConstraintModelResidualContactControlGravInequality) { | ||
104 | 3 | register_contact_constraint_model_unit_tests( | |
105 | 3 | ContactConstraintModelTypes::all[constraint_type], | |
106 | PinocchioModelTypes::HyQ, | ||
107 | ActuationModelTypes::ActuationModelFloatingBase); | ||
108 | } | ||
109 | } | ||
110 | 1 | return true; | |
111 | } | ||
112 | |||
113 | 1 | int main(int argc, char **argv) { | |
114 | 1 | return ::boost::unit_test::unit_test_main(&init_function, argc, argv); | |
115 | } | ||
116 |