- 3.0.2 rigid body dynamics module.
rbdJITtests.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
8 #include <gtest/gtest.h>
9 #include "../../models/testhyq/RobCoGenTestHyQ.h"
10 
11 
12 using namespace ct::core;
13 using namespace ct::rbd;
14 
15 bool verbose = true;
16 
17 
19 const size_t hyqStateDim = 36;
20 const size_t hyqControlDim = 12;
21 using size_type = ct::core::ADCGScalar;
22 using KinTpl_t = TestHyQ::tpl::Kinematics<size_type>;
24 size_t eeId = 1;
25 
26 // penalties for task-space tests
27 const Eigen::Matrix<double, 3, 3> Qpos = Eigen::Matrix<double, 3, 3>::Random();
28 double Qrot = 1.0;
29 
30 // terms for task-space tests
31 std::shared_ptr<TermTaskspacePosition<KinTpl_t, true, hyqStateDim, hyqControlDim>> termTaskspace(
32  new TermTaskspacePosition<KinTpl_t, true, hyqStateDim, hyqControlDim>(eeId, Qpos, Eigen::Vector3d::Random()));
33 
34 std::shared_ptr<TermTaskspacePose<KinTpl_t, true, hyqStateDim, hyqControlDim>> termTaskspacePose(
35  new TermTaskspacePose<KinTpl_t, true, hyqStateDim, hyqControlDim>(eeId,
36  Qpos,
37  Qrot,
38  Eigen::Vector3d::Random(),
39  Eigen::Vector3d::Random()));
40 
41 
43 template <typename SCALAR>
44 Eigen::Matrix<SCALAR, 1, 1> testFunctionTaskSpacePosition(
45  const Eigen::Matrix<SCALAR, hyqStateDim + hyqControlDim, 1> xu)
46 {
47  // evaluate the task-space position term
48  Eigen::Matrix<SCALAR, 1, 1> cost;
49  SCALAR t(0.0);
50  cost(0, 0) = termTaskspace->evaluateCppadCg(xu.template head<hyqStateDim>(), xu.template tail<hyqControlDim>(), t);
51  return cost;
52 }
53 TEST(RBD_JIT_Tests, TaskspacePositionCostFunctionTest)
54 {
55  using derivativesCppadJIT = DerivativesCppadJIT<hyqStateDim + hyqControlDim, 1>;
56 
57  typename derivativesCppadJIT::FUN_TYPE_CG f = testFunctionTaskSpacePosition<ct::core::ADCGScalar>;
58 
59  derivativesCppadJIT jacCG(f);
60 
61  DerivativesCppadSettings settings;
62  settings.createJacobian_ = true;
63 
64  try
65  {
66  // compile the Jacobian
67  jacCG.compileJIT(settings, "taskSpaceCfTestLib", verbose);
68  std::cout << "testTaskSpacePositionTerm compiled!" << std::endl;
69  } catch (std::exception& e)
70  {
71  std::cout << "testTaskSpacePositionTerm failed!" << std::endl;
72  ASSERT_TRUE(false);
73  }
74 }
75 
76 
78 template <typename SCALAR>
79 Eigen::Matrix<SCALAR, 1, 1> testFunctionTaskSpacePose(const Eigen::Matrix<SCALAR, hyqStateDim + hyqControlDim, 1> xu)
80 {
81  // evaluate the task-space position term
82  Eigen::Matrix<SCALAR, 1, 1> cost;
83  SCALAR t(0.0);
84  cost(0, 0) =
85  termTaskspacePose->evaluateCppadCg(xu.template head<hyqStateDim>(), xu.template tail<hyqControlDim>(), t);
86  return cost;
87 }
88 TEST(RBD_JIT_Tests, TaskspacePoseCostFunctionTest)
89 {
90  using derivativesCppadJIT = DerivativesCppadJIT<hyqStateDim + hyqControlDim, 1>;
91 
92  typename derivativesCppadJIT::FUN_TYPE_CG f = testFunctionTaskSpacePose<ct::core::ADCGScalar>;
93 
94  derivativesCppadJIT jacCG(f);
95 
96  DerivativesCppadSettings settings;
97  settings.createJacobian_ = true;
98 
99  try
100  {
101  // compile the Jacobian
102  jacCG.compileJIT(settings, "taskSpaceCfTestLib", verbose);
103  std::cout << "testTaskSpacePoseTerm compiled!" << std::endl;
104  } catch (std::exception& e)
105  {
106  std::cout << "testTaskSpacePoseTerm failed!" << std::endl;
107  ASSERT_TRUE(false);
108  }
109 }
110 
111 
113 template <typename SCALAR>
114 Eigen::Matrix<SCALAR, 1, 1> testFunctionRBDPose(const Eigen::Matrix<SCALAR, 3 + 3, 1>& xu)
115 {
116  // construct a RigidBodyPose from state vector
117  kindr::Position<SCALAR, 3> pos(xu.template segment<3>(0));
118  kindr::EulerAnglesXyz<SCALAR> euler(xu.template segment<3>(3));
119 
121  rbdPose.position() = pos;
122  rbdPose.setFromEulerAnglesXyz(euler);
123 
124  // we make up some "artificial", non-physical quantity to be auto-diffed:
125  Eigen::Matrix<SCALAR, 1, 1> cost;
126  SCALAR t(0.0);
127 
128  cost(0, 0) = rbdPose.position().toImplementation().norm() + rbdPose.getEulerAnglesXyz().toImplementation().norm();
129 
130  return cost;
131 }
132 TEST(RBD_JIT_Tests, RigidBodyPoseTest)
133 {
134  using derivativesCppadJIT = DerivativesCppadJIT<3 + 3, 1>;
135 
136  typename derivativesCppadJIT::FUN_TYPE_CG f = testFunctionRBDPose<ct::core::ADCGScalar>;
137 
138  derivativesCppadJIT jacCG(f);
139 
140  DerivativesCppadSettings settings;
141  settings.createJacobian_ = true;
142 
143  try
144  {
145  // compile the Jacobian
146  jacCG.compileJIT(settings, "rbdPoseTestLib", verbose);
147  std::cout << "testRBDPose compiled!" << std::endl;
148  } catch (std::exception& e)
149  {
150  std::cout << "testRBDPose failed!" << std::endl;
151  ASSERT_TRUE(false);
152  }
153 }
154 
155 
157 template <typename SCALAR>
158 Eigen::Matrix<SCALAR, 1, 1> testFunctionRigidBodyState(const Eigen::Matrix<SCALAR, 12, 1>& state)
159 {
160  // construct a RigidBodyState from state vector
162  ct::rbd::tpl::RigidBodyPose<SCALAR>::EULER); //<- storage type needs to go here in order to let test pass
163 
164  rigidBodyState.velocities().getRotationalVelocity().toImplementation() = state.template segment<3>(6);
165  rigidBodyState.velocities().getTranslationalVelocity().toImplementation() = state.template segment<3>(9);
166 
167  kindr::Position<SCALAR, 3> pos(state.template segment<3>(0));
168  kindr::EulerAnglesXyz<SCALAR> euler(state.template segment<3>(3));
169 
170  rigidBodyState.pose().position() = pos;
171  rigidBodyState.pose().setFromEulerAnglesXyz(euler);
172 
173  // helps to check the copy assignment
174  ct::rbd::tpl::RigidBodyState<SCALAR> rigidBodyStateCopy = rigidBodyState;
175 
176  // we make up some "artificial", non-physical quantity to be auto-diffed:
177  Eigen::Matrix<SCALAR, 1, 1> cost;
178  SCALAR t(0.0);
179 
180  cost(0, 0) = rigidBodyStateCopy.pose().position().toImplementation().norm() +
181  rigidBodyStateCopy.pose().getEulerAnglesXyz().toImplementation().norm() +
182  rigidBodyStateCopy.velocities().getVector().norm();
183 
184  return cost;
185 }
186 TEST(RBD_JIT_Tests, RigidBodyStateTest)
187 {
188  using derivativesCppadJIT = DerivativesCppadJIT<12, 1>;
189 
190  typename derivativesCppadJIT::FUN_TYPE_CG f = testFunctionRigidBodyState<ct::core::ADCGScalar>;
191 
192  derivativesCppadJIT jacCG(f);
193 
194  DerivativesCppadSettings settings;
195  settings.createJacobian_ = true;
196 
197  try
198  {
199  // compile the Jacobian
200  jacCG.compileJIT(settings, "rbdStateTestLib", verbose);
201  std::cout << "testRigidBodyState compiled!" << std::endl;
202  } catch (std::exception& e)
203  {
204  std::cout << "testRigidBodyState failed!" << std::endl;
205  ASSERT_TRUE(false);
206  }
207 }
208 
209 
211 template <typename SCALAR>
212 Eigen::Matrix<SCALAR, 1, 1> testFunctionRBDState(const Eigen::Matrix<SCALAR, hyqStateDim, 1>& x)
213 {
214  // construct an RBDState from data in the state vector.
216  ct::rbd::tpl::RigidBodyPose<SCALAR>::EULER); //<- storage type needs to go here in order to let test pass
217 
218  baseState.velocities().getRotationalVelocity().toImplementation() = x.template segment<3>(6);
219  baseState.velocities().getTranslationalVelocity().toImplementation() = x.template segment<3>(9);
220 
221  kindr::Position<SCALAR, 3> pos(x.template segment<3>(0));
222  kindr::EulerAnglesXyz<SCALAR> euler(x.template segment<3>(3));
223  baseState.pose().position() = pos;
224  baseState.pose().setFromEulerAnglesXyz(euler);
225 
226  ct::rbd::JointState<12, SCALAR> jointState(x.template tail<24>());
227 
229  rbdState.base() = baseState;
230  rbdState.joints() = jointState;
231 
232  // we make up some "artificial", non-physical quantity to be auto-diffed:
233  Eigen::Matrix<SCALAR, 1, 1> cost;
234  SCALAR t(0.0);
235 
236  ct::rbd::RBDState<12, SCALAR> rbdStateCopy = rbdState;
237 
238  cost(0, 0) = rbdStateCopy.toStateVectorEulerXyz().norm();
239 
240  return cost;
241 }
242 TEST(RBD_JIT_Tests, RBDStateTest)
243 {
244  using derivativesCppadJIT = DerivativesCppadJIT<hyqStateDim, 1>;
245 
246  typename derivativesCppadJIT::FUN_TYPE_CG f = testFunctionRBDState<ct::core::ADCGScalar>;
247 
248  derivativesCppadJIT jacCG(f);
249 
250  DerivativesCppadSettings settings;
251  settings.createJacobian_ = true;
252 
253  try
254  {
255  // compile the Jacobian
256  jacCG.compileJIT(settings, "rbdStateTestLib", verbose);
257  std::cout << "testRBDState compiled!" << std::endl;
258  } catch (std::exception& e)
259  {
260  std::cout << "testRBDState failed!" << std::endl;
261  ASSERT_TRUE(false);
262  }
263 }
state_vector_euler_t toStateVectorEulerXyz() const
Definition: RBDState.h:161
size_t eeId
Definition: rbdJITtests.h:24
RigidBodyState_t & base()
get base states
Definition: RBDState.h:63
Pose of a single rigid body.
Definition: RigidBodyPose.h:29
RigidBodyPose< SCALAR > & pose()
return the rigid body pose
Definition: RigidBodyState.h:38
const Position3Tpl & position() const
This method returns the position of the Base frame in the inertia frame.
Definition: RigidBodyPose.h:246
joint state and joint velocity
Definition: JointState.h:21
const size_t hyqStateDim
some global variables required for the TaskSpaceTerm test
Definition: rbdJITtests.h:19
const size_t hyqControlDim
Definition: rbdJITtests.h:20
RigidBodyVelocities< SCALAR > & velocities()
return the rigid body velocities
Definition: RigidBodyState.h:42
joint states and base states
Definition: RBDState.h:27
TestHyQ::tpl::Kinematics< size_type > KinTpl_t
Definition: rbdJITtests.h:22
clear all close all load ct GNMSLog0 mat reformat t
Eigen::Matrix< SCALAR, 1, 1 > testFunctionRigidBodyState(const Eigen::Matrix< SCALAR, 12, 1 > &state)
test the RigidBodyState class for JIT-compatibility
Definition: rbdJITtests.h:158
Eigen::Matrix< SCALAR, 1, 1 > testFunctionTaskSpacePosition(const Eigen::Matrix< SCALAR, hyqStateDim+hyqControlDim, 1 > xu)
test TermTaskSpacePosition term for JIT-compatibility
Definition: rbdJITtests.h:44
kindr::EulerAnglesXyz< SCALAR > getEulerAnglesXyz() const
This method returns the Euler angles rotation (X,Y,Z / roll,pitch,yaw).
Definition: RigidBodyPose.h:119
KinTpl_t kynTpl
Definition: rbdJITtests.h:23
DerivativesCppadJIT< inDim, outDim > derivativesCppadJIT
CppAD::AD< CppAD::cg::CG< double > > SCALAR
TEST(RBD_JIT_Tests, TaskspacePositionCostFunctionTest)
Definition: rbdJITtests.h:53
bool verbose
Definition: rbdJITtests.h:15
double Qrot
Definition: rbdJITtests.h:28
std::shared_ptr< TermTaskspacePose< KinTpl_t, true, hyqStateDim, hyqControlDim > > termTaskspacePose(new TermTaskspacePose< KinTpl_t, true, hyqStateDim, hyqControlDim >(eeId, Qpos, Qrot, Eigen::Vector3d::Random(), Eigen::Vector3d::Random()))
State (pose and velocities) of a single rigid body.
Definition: RigidBodyState.h:22
const Eigen::Matrix< double, 3, 3 > Qpos
Definition: rbdJITtests.h:27
Eigen::Matrix< SCALAR, 1, 1 > testFunctionTaskSpacePose(const Eigen::Matrix< SCALAR, hyqStateDim+hyqControlDim, 1 > xu)
test TermTaskSpacePose term for JIT-compatibility
Definition: rbdJITtests.h:79
std::shared_ptr< TermTaskspacePosition< KinTpl_t, true, hyqStateDim, hyqControlDim > > termTaskspace(new TermTaskspacePosition< KinTpl_t, true, hyqStateDim, hyqControlDim >(eeId, Qpos, Eigen::Vector3d::Random()))
Eigen::Matrix< SCALAR, 1, 1 > testFunctionRBDState(const Eigen::Matrix< SCALAR, hyqStateDim, 1 > &x)
test the RBDState class for JIT-compatibility
Definition: rbdJITtests.h:212
JointState< NJOINTS, SCALAR > & joints()
get joint states
Definition: RBDState.h:94
Eigen::Matrix< SCALAR, 1, 1 > testFunctionRBDPose(const Eigen::Matrix< SCALAR, 3+3, 1 > &xu)
test the RigidBodyPose class for JIT-compatibility
Definition: rbdJITtests.h:114
ct::core::ADCGScalar size_type
Definition: rbdJITtests.h:21
void setFromEulerAnglesXyz(const kindr::EulerAnglesXyz< SCALAR > &eulerAngles)
This method sets the Euler angles rotation (X,Y,Z / roll,pitch,yaw) from a kindr type.
Definition: RigidBodyPose.h:171