6 #include <gtest/gtest.h> 7 #include "../../models/testhyq/RobCoGenTestHyQ.h" 18 template <
size_t state_dim,
size_t control_dim>
20 CostFunctionQuadratic<state_dim, control_dim>& costFunction2)
22 ASSERT_NEAR(costFunction.evaluateIntermediate(), costFunction2.evaluateIntermediate(), 1e-9);
23 ASSERT_NEAR(costFunction.evaluateTerminal(), costFunction2.evaluateTerminal(), 1e-9);
25 ASSERT_TRUE(costFunction.stateDerivativeIntermediate().isApprox(costFunction2.stateDerivativeIntermediate()));
26 ASSERT_TRUE(costFunction.stateDerivativeTerminal().isApprox(costFunction2.stateDerivativeTerminal()));
29 costFunction.stateSecondDerivativeIntermediate().isApprox(costFunction2.stateSecondDerivativeIntermediate()));
30 ASSERT_TRUE(costFunction.stateSecondDerivativeTerminal().isApprox(costFunction2.stateSecondDerivativeTerminal()));
32 ASSERT_TRUE(costFunction.controlDerivativeIntermediate().isApprox(costFunction2.controlDerivativeIntermediate()));
33 ASSERT_TRUE(costFunction.controlDerivativeTerminal().isApprox(costFunction2.controlDerivativeTerminal()));
35 ASSERT_TRUE(costFunction.controlSecondDerivativeIntermediate().isApprox(
36 costFunction2.controlSecondDerivativeIntermediate()));
38 costFunction.controlSecondDerivativeTerminal().isApprox(costFunction2.controlSecondDerivativeTerminal()));
41 costFunction.stateControlDerivativeIntermediate().isApprox(costFunction2.stateControlDerivativeIntermediate()));
42 ASSERT_TRUE(costFunction.stateControlDerivativeTerminal().isApprox(costFunction2.stateControlDerivativeTerminal()));
45 ASSERT_TRUE(costFunction.stateSecondDerivativeIntermediate().isApprox(
46 costFunction.stateSecondDerivativeIntermediate().transpose()));
47 ASSERT_TRUE(costFunction.controlSecondDerivativeIntermediate().isApprox(
48 costFunction.controlSecondDerivativeIntermediate().transpose()));
52 TEST(TaskspaceCostFunctionTests, TestTaskSpacePositionTerm)
55 typedef TestHyQ::tpl::Kinematics<size_type>
KinTpl_t;
63 Eigen::Matrix<double, 3, 3> Q;
66 std::shared_ptr<optcon::CostFunctionAD<hyqStateDim, hyqControlDim>> Adcf(
67 new optcon::CostFunctionAD<hyqStateDim, hyqControlDim>());
68 std::shared_ptr<TermTaskspacePosition<KinTpl_t, true, hyqStateDim, hyqControlDim>> term1(
69 new TermTaskspacePosition<KinTpl_t, true, hyqStateDim, hyqControlDim>(eeId, Q, Eigen::Vector3d::Random()));
71 Adcf->addFinalADTerm(term1,
true);
72 Adcf->addIntermediateADTerm(term1,
true);
76 Eigen::Matrix<double, hyqStateDim, 1>
x;
77 Eigen::Matrix<double, hyqControlDim, 1>
u;
83 Adcf->setCurrentStateAndControl(x, u, t);
86 ASSERT_TRUE(Adcf->stateDerivativeIntermediateTest());
87 ASSERT_TRUE(Adcf->controlDerivativeIntermediateTest());
90 std::shared_ptr<optcon::CostFunctionAD<hyqStateDim, hyqControlDim>> Adcf_cloned(Adcf->clone());
94 Adcf->setCurrentStateAndControl(x, u, t);
95 Adcf_cloned->setCurrentStateAndControl(x, u, t);
101 TEST(TaskspaceCostFunctionTests, TestTaskSpacePoseTerm)
104 typedef TestHyQ::tpl::Kinematics<size_type>
KinTpl_t;
113 Eigen::Matrix<double, 3, 3>
Qpos;
120 Eigen::Matrix<double, 3, 1> w_pos_ee;
121 w_pos_ee.setRandom();
124 Eigen::Quaternion<double> w_q_ee;
125 w_q_ee.setIdentity();
128 Eigen::Matrix<double, 3, 1> w_euler_ee = w_q_ee.toRotationMatrix().eulerAngles(0, 1, 2);
130 std::shared_ptr<optcon::CostFunctionAD<hyqStateDim, hyqControlDim>> Adcf(
131 new optcon::CostFunctionAD<hyqStateDim, hyqControlDim>());
134 std::shared_ptr<TermTaskspacePose<KinTpl_t, true, hyqStateDim, hyqControlDim>> term1(
135 new TermTaskspacePose<KinTpl_t, true, hyqStateDim, hyqControlDim>(eeId, Qpos, Qrot, w_pos_ee, w_q_ee));
138 std::shared_ptr<TermTaskspacePose<KinTpl_t, true, hyqStateDim, hyqControlDim>> term2(
139 new TermTaskspacePose<KinTpl_t, true, hyqStateDim, hyqControlDim>(eeId, Qpos, Qrot, w_pos_ee, w_euler_ee));
141 Adcf->addFinalADTerm(term1,
true);
142 Adcf->addIntermediateADTerm(term1,
true);
146 Eigen::Matrix<double, hyqStateDim, 1>
x;
147 Eigen::Matrix<double, hyqControlDim, 1>
u;
153 Adcf->setCurrentStateAndControl(x, u, t);
156 ASSERT_TRUE(Adcf->stateDerivativeIntermediateTest());
157 ASSERT_TRUE(Adcf->controlDerivativeIntermediateTest());
160 std::shared_ptr<optcon::CostFunctionAD<hyqStateDim, hyqControlDim>> Adcf_cloned(Adcf->clone());
164 Adcf->setCurrentStateAndControl(x, u, t);
165 Adcf_cloned->setCurrentStateAndControl(x, u, t);
170 TEST(TaskspaceCostFunctionTests, TestTaskSpacePoseTermCG)
173 typedef TestHyQ::tpl::Kinematics<size_type>
KinTpl_t;
176 const size_t eeId = 1;
181 const Eigen::Matrix<double, 3, 3>
Qpos = Eigen::Matrix<double, 3, 3>::Identity();
184 const double Qrot = 1.0;
187 Eigen::Matrix<double, 3, 1> w_pos_ee;
188 w_pos_ee.setRandom();
191 Eigen::Quaternion<double> w_q_ee;
192 w_q_ee.setIdentity();
195 std::shared_ptr<TermTaskspacePoseCG<KinTpl_t, true, hyqStateDim, hyqControlDim>> term1(
196 new TermTaskspacePoseCG<KinTpl_t, true, hyqStateDim, hyqControlDim>(eeId, Qpos, Qrot, w_pos_ee, w_q_ee));
199 std::shared_ptr<optcon::CostFunctionAnalytical<hyqStateDim, hyqControlDim>> costFun(
202 costFun->addIntermediateTerm(term1);
203 costFun->initialize();
205 Eigen::Matrix<double, hyqStateDim, 1>
x;
206 Eigen::Matrix<double, hyqControlDim, 1>
u;
214 ASSERT_TRUE(costFun->stateDerivativeIntermediateTest());
215 ASSERT_TRUE(costFun->controlDerivativeIntermediateTest());
219 costFun->setCurrentStateAndControl(x, u, t);
220 double cost_orig = costFun->evaluateIntermediate();
221 Eigen::VectorXd stateDerivative_orig = costFun->stateDerivativeIntermediate();
223 w_pos_ee.setRandom();
224 Eigen::Quaterniond testq = w_q_ee * Eigen::Quaterniond(0, 1, 0, 0);
225 term1->setReferencePosition(w_pos_ee);
226 term1->setReferenceOrientation(testq);
228 double cost_new = costFun->evaluateIntermediate();
229 Eigen::VectorXd stateDerivative_new = costFun->stateDerivativeIntermediate();
230 ASSERT_TRUE(cost_orig - cost_new > 1e-8);
231 ASSERT_TRUE((stateDerivative_orig - stateDerivative_new).array().maxCoeff() > 1e-8);
236 ASSERT_TRUE((w_pos_ee - w_pos_ee_retrieved).array().maxCoeff() < 1e-6);
237 Eigen::Matrix3d matOriginal = testq.toRotationMatrix();
238 Eigen::Matrix3d matRetrieved = term1->getReferenceOrientation().toRotationMatrix();
239 ASSERT_TRUE((matOriginal - matRetrieved).array().maxCoeff() < 1e-6);
243 std::shared_ptr<optcon::CostFunctionAnalytical<hyqStateDim, hyqControlDim>> costFun_cloned(costFun->clone());
247 costFun->setCurrentStateAndControl(x, u, t);
248 costFun_cloned->setCurrentStateAndControl(x, u, t);
size_t eeId
Definition: rbdJITtests.h:24
ct::core::ControlVector< control_dim > u
Cored::Vector3 Vector3d
Definition: rbd.h:137
const size_t hyqStateDim
some global variables required for the TaskSpaceTerm test
Definition: rbdJITtests.h:19
const size_t hyqControlDim
Definition: rbdJITtests.h:20
TestHyQ::tpl::Kinematics< size_type > KinTpl_t
Definition: rbdJITtests.h:22
clear all close all load ct GNMSLog0 mat reformat t
void compareCostFunctionOutput(CostFunctionQuadratic< state_dim, control_dim > &costFunction, CostFunctionQuadratic< state_dim, control_dim > &costFunction2)
This method is called from different unit tests in order to compare the cost, first and second order ...
Definition: TaskspaceCostFunctionTest.h:19
KinTpl_t kynTpl
Definition: rbdJITtests.h:23
TEST(TaskspaceCostFunctionTests, TestTaskSpacePositionTerm)
Definition: TaskspaceCostFunctionTest.h:52
double Qrot
Definition: rbdJITtests.h:28
ct::core::StateVector< state_dim > x
const Eigen::Matrix< double, 3, 3 > Qpos
Definition: rbdJITtests.h:27
ct::core::ADCGScalar size_type
Definition: rbdJITtests.h:21