This repository was archived by the owner on Feb 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 497
/
Copy path494_joint_axis_frame.cc
281 lines (255 loc) · 9.04 KB
/
494_joint_axis_frame.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* Copyright (C) 2014 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "ServerFixture.hh"
#include "test/integration/helper_physics_generator.hh"
#include "test/integration/joint_test.hh"
using namespace gazebo;
const double g_tolerance = 1e-4;
class Issue494Test : public JointTest
{
/// \brief Test for issue #494, using proper joint axis frame.
/// Also test basic joint properties.
/// \param[in] _physicsEngine Type of physics engine to use.
/// \param[in] _jointType Type of joint to test.
public: void CheckAxisFrame(const std::string &_physicsEngine,
const std::string &_jointType);
/// \brief Check joint properties.
/// \param[in] _joint Joint to check.
/// \param[in] _axis Expected axis vector in global frame.
public: void CheckJointProperties(physics::JointPtr _joint,
const math::Vector3 &_axis);
};
/////////////////////////////////////////////////
void Issue494Test::CheckAxisFrame(const std::string &_physicsEngine,
const std::string &_jointType)
{
// Load an empty world
Load("worlds/empty.world", true, _physicsEngine);
physics::WorldPtr world = physics::get_world("default");
ASSERT_TRUE(world != NULL);
// Verify physics engine type
physics::PhysicsEnginePtr physics = world->GetPhysicsEngine();
ASSERT_TRUE(physics != NULL);
EXPECT_EQ(physics->GetType(), _physicsEngine);
// disable gravity
physics->SetGravity(math::Vector3::Zero);
SpawnJointOptions opt;
opt.type = _jointType;
double Am = M_PI / 11;
double Al = M_PI / 12;
double Aj = M_PI / 13;
opt.modelPose.rot.SetFromEuler(0, 0, Am);
opt.childLinkPose.rot.SetFromEuler(0, 0, Al);
opt.jointPose.rot.SetFromEuler(0, 0, Aj);
opt.axis.Set(1, 0, 0);
// i = 0: joint between child link and parent link
// i = 1: joint between child link and world
// i = 2: joint between world and parent link
for (int i = 0; i < 3; ++i)
{
gzdbg << "SpawnJoint " << _jointType;
if (i / 2)
{
opt.worldChild = true;
std::cout << " world";
}
else
{
opt.worldChild = false;
std::cout << " child";
}
if (i % 2)
{
opt.worldParent = true;
std::cout << " world";
}
else
{
opt.worldParent = false;
std::cout << " parent";
}
std::cout << std::endl;
if (opt.worldChild && _physicsEngine == "dart")
{
gzerr << "dart seg-faults without a child link, skipping sub-test"
<< std::endl;
break;
}
// spawn joint using using parent model frame to define joint axis
if (_physicsEngine == "dart")
{
gzerr << "dart doesn't support parent model frame, skipping sub-test"
<< " per issue #1143"
<< std::endl;
}
else
{
gzdbg << "test case with joint axis specified in parent model frame.\n";
opt.useParentModelFrame = true;
physics::JointPtr jointUseParentModelFrame = SpawnJoint(opt);
ASSERT_TRUE(jointUseParentModelFrame != NULL);
if (opt.worldParent)
{
gzdbg << " where parent is world.\n";
this->CheckJointProperties(jointUseParentModelFrame, opt.axis);
}
else
{
gzdbg << " where parent is another link (not world).\n";
this->CheckJointProperties(jointUseParentModelFrame,
math::Vector3(cos(Am), sin(Am), 0));
}
}
// spawn joint using using child link frame to define joint axis
{
gzdbg << "test case with joint axis specified in child link frame.\n";
opt.useParentModelFrame = false;
physics::JointPtr joint = SpawnJoint(opt);
ASSERT_TRUE(joint != NULL);
if (opt.worldChild)
{
gzdbg << " where parent is world.\n";
this->CheckJointProperties(joint,
math::Vector3(cos(Aj), sin(Aj), 0));
}
else
{
gzdbg << " where parent is another link (not world).\n";
this->CheckJointProperties(joint,
math::Vector3(cos(Am+Al+Aj), sin(Am+Al+Aj), 0));
}
}
}
}
/////////////////////////////////////////////////
void Issue494Test::CheckJointProperties(physics::JointPtr _joint,
const math::Vector3 &_axis)
{
physics::WorldPtr world = physics::get_world();
ASSERT_TRUE(world != NULL);
physics::PhysicsEnginePtr physics = world->GetPhysicsEngine();
ASSERT_TRUE(physics != NULL);
bool isOde = physics->GetType().compare("ode") == 0;
double dt = physics->GetMaxStepSize();
// Check that Joint::GetGlobalAxis matches _axis
EXPECT_EQ(_axis, _joint->GetGlobalAxis(0));
// test GetLocalAxis, GetAxisFrame, and GetAxisFrameOffset
// get axis specified locally (in joint frame or in parent model frame)
math::Vector3 axisLocalFrame = _joint->GetLocalAxis(0);
{
// rotate axis into global frame
math::Vector3 axisGlobalFrame =
_joint->GetAxisFrame(0).RotateVector(axisLocalFrame);
// Test GetAxisFrame: check that axis in global frame is
// computed correctly.
EXPECT_EQ(axisGlobalFrame, _axis);
}
{
// rotate axis into joint frame
math::Vector3 axisJointFrame =
_joint->GetAxisFrameOffset(0).RotateVector(axisLocalFrame);
// roate axis specified in global frame into joint frame
math::Vector3 axisJointFrame2 =
_joint->GetWorldPose().rot.RotateVectorReverse(_axis);
EXPECT_EQ(axisJointFrame, axisJointFrame2);
}
if (!_joint->GetChild())
{
gzerr << "The rest of this test fails without a child link" << std::endl;
return;
}
double velocityMagnitude = 1.0;
double maxForce = velocityMagnitude / dt * 10.1;
std::vector<double> velocities;
velocities.push_back(velocityMagnitude);
velocities.push_back(0.0);
velocities.push_back(-velocityMagnitude);
for (std::vector<double>::iterator iter = velocities.begin();
iter != velocities.end(); ++iter)
{
// Use Joint::SetVelocity with different values
double vel = *iter;
_joint->SetVelocity(0, vel);
// ODE requires maxForce to be non-zero for SetVelocity to work
// See issue #964 for discussion of consistent API
if (isOde)
_joint->SetMaxForce(0, maxForce);
// Take a step and verify that Joint::GetVelocity returns the same value
world->Step(1);
EXPECT_NEAR(_joint->GetVelocity(0), vel, g_tolerance);
// Also verify that relative body motions match expected joint behavior
math::Vector3 childVelocity, parentVelocity;
{
physics::LinkPtr child = _joint->GetChild();
if (child)
{
if (_joint->HasType(physics::Base::HINGE_JOINT)
|| _joint->HasType(physics::Base::UNIVERSAL_JOINT))
childVelocity = child->GetWorldAngularVel();
else if (_joint->HasType(physics::Base::SLIDER_JOINT)
|| _joint->HasType(physics::Base::SCREW_JOINT))
{
childVelocity = child->GetWorldLinearVel();
}
}
}
{
physics::LinkPtr parent = _joint->GetParent();
if (parent)
{
if (_joint->HasType(physics::Base::HINGE_JOINT)
|| _joint->HasType(physics::Base::UNIVERSAL_JOINT))
parentVelocity = parent->GetWorldAngularVel();
else if (_joint->HasType(physics::Base::SLIDER_JOINT)
|| _joint->HasType(physics::Base::SCREW_JOINT))
{
parentVelocity = parent->GetWorldLinearVel();
}
}
}
std::cout << " joint pose: " << _joint->GetWorldPose()
<< std::endl;
std::cout << " global axis: " << _axis << std::endl;
std::cout << " axis frame: " << _joint->GetAxisFrame(0)
<< std::endl;
std::cout << " axis frame offset: " << _joint->GetAxisFrameOffset(0)
<< std::endl;
std::cout << " desired velocity: " << vel << std::endl;
std::cout << " joint velocity: " << _joint->GetVelocity(0)
<< std::endl;
std::cout << " child velocity: " << childVelocity << std::endl;
std::cout << " parent velocity: " << parentVelocity << std::endl;
std::cout << std::endl;
EXPECT_NEAR(vel, _axis.Dot(childVelocity - parentVelocity), g_tolerance);
}
}
TEST_P(Issue494Test, CheckAxisFrame)
{
CheckAxisFrame(this->physicsEngine, this->jointType);
}
INSTANTIATE_TEST_CASE_P(PhysicsEngines, Issue494Test,
::testing::Combine(PHYSICS_ENGINE_VALUES,
::testing::Values("revolute"
, "prismatic"
, "universal")));
/////////////////////////////////////////////////
/// Main
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}