RdlCalcVelocitiesTests.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include "rdl_dynamics/Model.h"
5 
6 #include <gtest/gtest.h>
7 
8 #include "UnitTestUtils.hpp"
9 
10 using namespace std;
11 using namespace RobotDynamics;
12 using namespace RobotDynamics::Math;
13 
14 class RdlCalcVelocitiesTests : public testing::Test
15 {
16  public:
18  {
19  }
20 };
21 
22 struct RdlModelVelocitiesFixture : public testing::Test
23 {
25  {
26  }
27 
28  void SetUp()
29  {
30  model = new Model;
31 
32  body_a = Body(1., Vector3d(1., 0., 0.), Vector3d(1., 1., 1.));
33  Joint joint_a(SpatialVector(0., 0., 1., 0., 0., 0.));
34 
35  body_a_id = model->addBody(0, Xtrans(Vector3d(0., 0., 0.)), joint_a, body_a);
36 
37  body_b = Body(1., Vector3d(0., 1., 0.), Vector3d(1., 1., 1.));
38  Joint joint_b(SpatialVector(0., 1., 0., 0., 0., 0.));
39 
40  body_b_id = model->addBody(1, Xtrans(Vector3d(1., 0., 0.)), joint_b, body_b);
41 
42  body_c = Body(1., Vector3d(1., 0., 0.), Vector3d(1., 1., 1.));
43  Joint joint_c(SpatialVector(1., 0., 0., 0., 0., 0.));
44 
45  body_c_id = model->addBody(2, Xtrans(Vector3d(0., 1., 0.)), joint_c, body_c);
46 
47  Q = VectorNd::Constant((size_t)model->dof_count, 0.);
48  QDot = VectorNd::Constant((size_t)model->dof_count, 0.);
49 
50  point_position = Vector3d::Zero(3);
51  point_velocity.setToZero();
52 
53  ref_body_id = 0;
54  }
55 
56  void TearDown()
57  {
58  delete model;
59  }
60 
62 
63  unsigned int body_a_id, body_b_id, body_c_id, ref_body_id;
64  Body body_a, body_b, body_c;
65  Joint joint_a, joint_b, joint_c;
66 
69 
72 };
73 
74 TEST_F(RdlModelVelocitiesFixture, TestCalcPointSimple)
75 {
76  ref_body_id = 1;
77  QDot[0] = 1.;
78  point_position = Vector3d(1., 0., 0.);
79  FrameVector point_velocity = calcPointVelocity(*model, Q, QDot, ref_body_id, point_position);
80 
81  EXPECT_NEAR(0., point_velocity[0], unit_test_utils::TEST_PREC);
82  EXPECT_NEAR(1., point_velocity[1], unit_test_utils::TEST_PREC);
83  EXPECT_NEAR(0., point_velocity[2], unit_test_utils::TEST_PREC);
84 }
85 
86 TEST_F(RdlModelVelocitiesFixture, TestCalcPointRotatedBaseSimple)
87 {
88  // rotated first joint
89 
90  ref_body_id = 1;
91  Q[0] = M_PI * 0.5;
92  QDot[0] = 1.;
93  point_position = Vector3d(1., 0., 0.);
94  point_velocity = calcPointVelocity(*model, Q, QDot, ref_body_id, point_position);
95 
96  EXPECT_NEAR(-1., point_velocity[0], unit_test_utils::TEST_PREC);
97  EXPECT_NEAR(0., point_velocity[1], unit_test_utils::TEST_PREC);
98  EXPECT_NEAR(0., point_velocity[2], unit_test_utils::TEST_PREC);
99 }
100 
101 TEST_F(RdlModelVelocitiesFixture, TestCalcPointRotatingBodyB)
102 {
103  // rotating second joint, point at third body
104 
105  ref_body_id = 3;
106  QDot[1] = 1.;
107  point_position = Vector3d(1., 0., 0.);
108  point_velocity = calcPointVelocity(*model, Q, QDot, ref_body_id, point_position);
109 
110  EXPECT_NEAR(0., point_velocity[0], unit_test_utils::TEST_PREC);
111  EXPECT_NEAR(0., point_velocity[1], unit_test_utils::TEST_PREC);
112  EXPECT_NEAR(-1., point_velocity[2], unit_test_utils::TEST_PREC);
113 }
114 
115 TEST_F(RdlModelVelocitiesFixture, TestCalcPointRotatingBaseXAxis)
116 {
117  // also rotate the first joint and take a point that is
118  // on the X direction
119 
120  ref_body_id = 3;
121  QDot[0] = 1.;
122  QDot[1] = 1.;
123  point_position = Vector3d(1., -1., 0.);
124  point_velocity = calcPointVelocity(*model, Q, QDot, ref_body_id, point_position);
125 
126  EXPECT_NEAR(2., point_velocity[1], unit_test_utils::TEST_PREC);
127  EXPECT_NEAR(0., point_velocity[0], unit_test_utils::TEST_PREC);
128  EXPECT_NEAR(-1., point_velocity[2], unit_test_utils::TEST_PREC);
129 }
130 
131 TEST_F(RdlModelVelocitiesFixture, TestCalcPointRotatedBaseXAxis)
132 {
133  // perform the previous test with the first joint rotated by pi/2
134  // upwards
135 
136  ref_body_id = 3;
137  point_position = Vector3d(1., -1., 0.);
138 
139  Q[0] = M_PI * 0.5;
140  QDot[0] = 1.;
141  QDot[1] = 1.;
142  point_velocity = calcPointVelocity(*model, Q, QDot, ref_body_id, point_position);
143 
144  EXPECT_NEAR(-2., point_velocity[0], unit_test_utils::TEST_PREC);
145  EXPECT_NEAR(0., point_velocity[1], unit_test_utils::TEST_PREC);
146  EXPECT_NEAR(-1., point_velocity[2], unit_test_utils::TEST_PREC);
147 }
148 
149 TEST_F(RdlModelVelocitiesFixture, TestCalcPointBodyOrigin)
150 {
151  // Checks whether the computation is also correct for points at the origin
152  // of a body
153 
154  ref_body_id = body_b_id;
155  point_position = Vector3d(0., 0., 0.);
156 
157  Q[0] = 0.;
158  QDot[0] = 1.;
159 
160  point_velocity = calcPointVelocity(*model, Q, QDot, ref_body_id, point_position);
161 
162  EXPECT_NEAR(0., point_velocity[0], unit_test_utils::TEST_PREC);
163  EXPECT_NEAR(1., point_velocity[1], unit_test_utils::TEST_PREC);
164  EXPECT_NEAR(0., point_velocity[2], unit_test_utils::TEST_PREC);
165 }
166 
167 TEST_F(RdlCalcVelocitiesTests, FixedJointCalcPointVelocity)
168 {
169  // the standard modeling using a null body
170  Body body(1., Vector3d(1., 0.4, 0.4), Vector3d(1., 1., 1.));
171  Body fixed_body(1., Vector3d(1., 0.4, 0.4), Vector3d(1., 1., 1.));
172 
173  Model model;
174 
175  Joint joint_rot_z(SpatialVector(0., 0., 1., 0., 0., 0.));
176  model.addBody(0, Xtrans(Vector3d(0., 0., 0.)), joint_rot_z, body);
177 
178  SpatialTransform transform = Xtrans(Vector3d(1., 0., 0.));
179  unsigned int fixed_body_id = model.appendBody(transform, Joint(JointTypeFixed), fixed_body, "fixed_body");
180 
181  VectorNd Q = VectorNd::Zero(model.dof_count);
182  VectorNd QDot = VectorNd::Zero(model.dof_count);
183 
184  QDot[0] = 1.;
185 
186  FrameVector point0_velocity = calcPointVelocity(model, Q, QDot, fixed_body_id, Vector3d(0., 0., 0.));
187 
188  FrameVector point1_velocity = calcPointVelocity(model, Q, QDot, fixed_body_id, Vector3d(1., 0., 0.));
189 
190  EXPECT_TRUE(unit_test_utils::checkVector3dEpsilonClose(Vector3d(0., 1., 0.), point0_velocity, unit_test_utils::TEST_PREC));
191  EXPECT_TRUE(unit_test_utils::checkVector3dEpsilonClose(Vector3d(0., 2., 0.), point1_velocity, unit_test_utils::TEST_PREC));
192 }
193 
194 TEST_F(RdlCalcVelocitiesTests, FixedJointCalcPointVelocityRotated)
195 {
196  // the standard modeling using a null body
197  Body body(1., Vector3d(1., 0.4, 0.4), Vector3d(1., 1., 1.));
198  Body fixed_body(1., Vector3d(1., 0.4, 0.4), Vector3d(1., 1., 1.));
199 
200  Model model;
201 
202  Joint joint_rot_z(SpatialVector(0., 0., 1., 0., 0., 0.));
203  model.addBody(0, Xtrans(Vector3d(0., 0., 0.)), joint_rot_z, body);
204 
205  SpatialTransform transform = Xtrans(Vector3d(1., 0., 0.));
206  unsigned int fixed_body_id = model.appendBody(transform, Joint(JointTypeFixed), fixed_body, "fixed_body");
207 
208  VectorNd Q = VectorNd::Zero(model.dof_count);
209  VectorNd QDot = VectorNd::Zero(model.dof_count);
210 
211  Q[0] = M_PI * 0.5;
212  QDot[0] = 1.;
213 
214  FrameVector point0_velocity = calcPointVelocity(model, Q, QDot, fixed_body_id, Vector3d(0., 0., 0.));
215 
216  FrameVector point1_velocity = calcPointVelocity(model, Q, QDot, fixed_body_id, Vector3d(1., 0., 0.));
217 
218  EXPECT_TRUE(unit_test_utils::checkVector3dEpsilonClose(Vector3d(-1., 0., 0.), point0_velocity, unit_test_utils::TEST_PREC));
219  EXPECT_TRUE(unit_test_utils::checkVector3dEpsilonClose(Vector3d(-2., 0., 0.), point1_velocity, unit_test_utils::TEST_PREC));
220 }
221 
222 int main(int argc, char** argv)
223 {
224  ::testing::InitGoogleTest(&argc, argv);
225  return RUN_ALL_TESTS();
226 }
Joint joint_rot_z(SpatialVector(0., 0., 1., 0., 0., 0.))
Describes all properties of a single body.
Definition: Body.h:30
int main(int argc, char **argv)
VectorNd QDot
const double TEST_PREC
VectorNd Q
SpatialTransform Xtrans(const Vector3d &r)
Get pure translation transform.
Fixed joint which causes the inertial properties to be merged with.
Definition: Joint.h:205
unsigned int appendBody(const Math::SpatialTransform &joint_frame, const Joint &joint, const Body &body, std::string body_name="")
Adds a Body to the model such that the previously added Body is the Parent.
Definition: Model.cc:517
unsigned int addBody(const unsigned int parent_id, const Math::SpatialTransform &joint_frame, const Joint &joint, const Body &body, std::string body_name="")
Connects a given body to the model.
Definition: Model.cc:271
Math::FrameVector calcPointVelocity(Model &model, const Math::VectorNd &Q, const Math::VectorNd &QDot, unsigned int body_id, const Math::Vector3d &point_position, bool update_kinematics=true)
Computes the velocity of a point on a body.
Definition: Kinematics.cc:1422
Compact representation of spatial transformations.
TEST_F(RdlModelVelocitiesFixture, TestCalcPointSimple)
A FrameVector is a 3D vector with a ReferenceFrame, and all operations between FrameVectors and other...
Definition: FrameVector.hpp:33
Describes a joint relative to the predecessor body.
Definition: Joint.h:228
Eigen::VectorXd VectorNd
Definition: rdl_eigenmath.h:25
Contains all information about the rigid body model.
Definition: Model.h:121
Math types such as vectors and matrices and utility functions.
Definition: ForceVector.hpp:21
static bool checkVector3dEpsilonClose(const RobotDynamics::Math::Vector3d &v1, const RobotDynamics::Math::Vector3d &v2, const double epsilon)
unsigned int dof_count
number of degrees of freedoms of the model
Definition: Model.h:169
Namespace for all structures of the RobotDynamics library.
Definition: Body.h:21


rdl_dynamics
Author(s):
autogenerated on Tue Apr 20 2021 02:25:27