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 498
/
Copy pathPhysicsEngine_TEST.cc
252 lines (220 loc) · 9.24 KB
/
PhysicsEngine_TEST.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
/*
* Copyright (C) 2012 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 <any>
#include "gazebo/test/ServerFixture.hh"
#include "gazebo/test/helper_physics_generator.hh"
#include "gazebo/msgs/msgs.hh"
using namespace gazebo;
class PhysicsEngineTest : public ServerFixture,
public testing::WithParamInterface<const char*>
{
public: void OnPhysicsMsgResponse(ConstResponsePtr &_msg);
public: void PhysicsEngineParam(const std::string &_physicsEngine);
public: void PhysicsEngineGetParamBool(const std::string &_physicsEngine);
public: static msgs::Physics physicsPubMsg;
public: static msgs::Physics physicsResponseMsg;
};
msgs::Physics PhysicsEngineTest::physicsPubMsg;
msgs::Physics PhysicsEngineTest::physicsResponseMsg;
/////////////////////////////////////////////////
void PhysicsEngineTest::OnPhysicsMsgResponse(ConstResponsePtr &_msg)
{
if (_msg->type() == physicsPubMsg.GetTypeName())
physicsResponseMsg.ParseFromString(_msg->serialized_data());
}
/////////////////////////////////////////////////
void PhysicsEngineTest::PhysicsEngineParam(const std::string &_physicsEngine)
{
physicsPubMsg.Clear();
physicsResponseMsg.Clear();
Load("worlds/empty.world", false, _physicsEngine);
physics::WorldPtr world = physics::get_world("default");
ASSERT_TRUE(world != NULL);
transport::NodePtr physicsNode;
physicsNode = transport::NodePtr(new transport::Node());
physicsNode->Init();
transport::PublisherPtr physicsPub
= physicsNode->Advertise<msgs::Physics>("~/physics");
transport::PublisherPtr requestPub
= physicsNode->Advertise<msgs::Request>("~/request");
transport::SubscriberPtr responsePub = physicsNode->Subscribe("~/response",
&PhysicsEngineTest::OnPhysicsMsgResponse, this);
msgs::Physics_Type type;
if (_physicsEngine == "ode")
type = msgs::Physics::ODE;
else if (_physicsEngine == "bullet")
type = msgs::Physics::BULLET;
else if (_physicsEngine == "dart")
type = msgs::Physics::DART;
else if (_physicsEngine == "simbody")
type = msgs::Physics::SIMBODY;
else
type = msgs::Physics::ODE;
physicsPubMsg.set_type(type);
physicsPubMsg.set_max_step_size(0.01);
physicsPubMsg.set_real_time_update_rate(500);
physicsPubMsg.set_real_time_factor(1.2);
physicsPub->Publish(physicsPubMsg);
msgs::Request *requestMsg = msgs::CreateRequest("physics_info", "");
requestPub->Publish(*requestMsg);
int waitCount = 0, maxWaitCount = 3000;
#if GOOGLE_PROTOBUF_VERSION < 3001000
while (physicsResponseMsg.ByteSize() == 0 && ++waitCount < maxWaitCount)
#else
// ByteSizeLong appeared in version 3.1 of Protobuf, and ByteSize
// became deprecated.
while (physicsResponseMsg.ByteSizeLong() == 0 && ++waitCount < maxWaitCount)
#endif
{
common::Time::MSleep(10);
}
ASSERT_LT(waitCount, maxWaitCount);
EXPECT_DOUBLE_EQ(physicsResponseMsg.max_step_size(),
physicsPubMsg.max_step_size());
EXPECT_DOUBLE_EQ(physicsResponseMsg.real_time_update_rate(),
physicsPubMsg.real_time_update_rate());
EXPECT_DOUBLE_EQ(physicsResponseMsg.real_time_factor(),
physicsPubMsg.real_time_factor());
// Test PhysicsEngine::[GS]etParam()
{
physics::PhysicsEnginePtr physics = world->Physics();
boost::any dt = physics->GetParam("max_step_size");
EXPECT_DOUBLE_EQ(boost::any_cast<double>(dt),
physicsPubMsg.max_step_size());
EXPECT_NO_THROW(physics->GetParam("fake_param_name"));
EXPECT_NO_THROW(physics->SetParam("fake_param_name", 0));
// Try SetParam with wrong type
EXPECT_NO_THROW(physics->SetParam("iters", std::string("wrong")));
}
{
// Test SetParam for non-implementation-specific parameters
physics::PhysicsEnginePtr physics = world->Physics();
try
{
boost::any value;
double maxStepSize = 0.02;
double realTimeUpdateRate = 3.03;
double realTimeFactor = 0.04;
ignition::math::Vector3d gravity(0, 0, 0);
ignition::math::Vector3d magField(0.1, 0.1, 0.1);
ignition::math::Vector3d gravity2(0.2, 0.2, 0.2);
ignition::math::Vector3d magField2(0.3, 0.3, 0.3);
gzdbg << "Set and Get max_step_size" << std::endl;
EXPECT_TRUE(physics->SetParam("max_step_size", maxStepSize));
EXPECT_TRUE(physics->GetParam("max_step_size", value));
EXPECT_NEAR(boost::any_cast<double>(value), maxStepSize, 1e-6);
gzdbg << "Set and Get real_time_update_rate" << std::endl;
EXPECT_TRUE(physics->SetParam("real_time_update_rate",
realTimeUpdateRate));
EXPECT_TRUE(physics->GetParam("real_time_update_rate", value));
EXPECT_NEAR(boost::any_cast<double>(value), realTimeUpdateRate, 1e-6);
gzdbg << "Set and Get real_time_factor" << std::endl;
EXPECT_TRUE(physics->SetParam("real_time_factor",
realTimeFactor));
EXPECT_TRUE(physics->GetParam("real_time_factor", value));
EXPECT_NEAR(boost::any_cast<double>(value), realTimeFactor, 1e-6);
gzdbg << "Set gravity as ignition::math::Vector3d" << std::endl;
EXPECT_TRUE(physics->SetParam("gravity", gravity));
EXPECT_TRUE(physics->GetParam("gravity", value));
EXPECT_EQ(boost::any_cast<ignition::math::Vector3d>(value), gravity);
gzdbg << "Set gravity as ignition::math::Vector3d" << std::endl;
EXPECT_TRUE(physics->SetParam("gravity", gravity2));
EXPECT_TRUE(physics->GetParam("gravity", value));
EXPECT_EQ(boost::any_cast<ignition::math::Vector3d>(value),
gravity2);
gzdbg << "Set magnetic_field as ignition::math::Vector3d" << std::endl;
EXPECT_TRUE(physics->SetParam("magnetic_field", magField));
EXPECT_TRUE(physics->GetParam("magnetic_field", value));
EXPECT_EQ(boost::any_cast<ignition::math::Vector3d>(value), magField);
gzdbg << "Set magnetic_field as ignition::math::Vector3d" << std::endl;
EXPECT_TRUE(physics->SetParam("magnetic_field", magField2));
EXPECT_TRUE(physics->GetParam("magnetic_field", value));
EXPECT_EQ(boost::any_cast<ignition::math::Vector3d>(value),
magField2);
// test with max_step_size wrapped in std::any
maxStepSize = 0.04;
EXPECT_TRUE(physics->SetParam("max_step_size", std::any(maxStepSize)));
EXPECT_TRUE(physics->GetParam("max_step_size", value));
EXPECT_DOUBLE_EQ(boost::any_cast<double>(value), maxStepSize);
// test with incorrect type in std::any and ensure that it doesn't throw
EXPECT_FALSE(physics->SetParam("max_step_size", std::any("wrong type")));
}
catch(boost::bad_any_cast &_e)
{
std::cout << "Bad any_cast in PhysicsEngine::SetParam test: " << _e.what()
<< std::endl;
FAIL();
}
}
physicsNode->Fini();
}
/////////////////////////////////////////////////
TEST_P(PhysicsEngineTest, PhysicsEngineParam)
{
PhysicsEngineParam(GetParam());
}
/////////////////////////////////////////////////
void PhysicsEngineTest::PhysicsEngineGetParamBool
(const std::string &_physicsEngine)
{
Load("worlds/empty.world", false, _physicsEngine);
physics::WorldPtr world = physics::get_world("default");
ASSERT_TRUE(world != NULL);
physics::PhysicsEnginePtr physics = world->Physics();
// Initialize to failure conditions
boost::any value;
// Test shared physics engine parameter(s)
EXPECT_TRUE(physics->GetParam("gravity", value));
EXPECT_EQ(boost::any_cast<ignition::math::Vector3d>(value),
ignition::math::Vector3d(0, 0, -9.8));
EXPECT_TRUE(physics->GetParam("max_step_size", value));
EXPECT_NEAR(boost::any_cast<double>(value), 0.001, 1e-6);
EXPECT_TRUE(physics->GetParam("real_time_factor", value));
EXPECT_NEAR(boost::any_cast<double>(value), 1.0, 1e-6);
EXPECT_TRUE(physics->GetParam("real_time_update_rate", value));
EXPECT_NEAR(boost::any_cast<double>(value), 1000.0, 1e-6);
EXPECT_TRUE(physics->GetParam("type", value));
EXPECT_EQ(boost::any_cast<std::string>(value), _physicsEngine);
if (_physicsEngine == "ode" || _physicsEngine == "bullet")
{
EXPECT_TRUE(physics->GetParam("iters", value));
EXPECT_EQ(boost::any_cast<int>(value), 50);
}
else if (_physicsEngine == "dart")
{
gzwarn << "DARTPhysics::GetParam not yet implemented." << std::endl;
return;
}
else if (_physicsEngine == "simbody")
{
EXPECT_TRUE(physics->GetParam("accuracy", value));
EXPECT_NEAR(boost::any_cast<double>(value), 1e-3, 1e-6);
}
EXPECT_FALSE(physics->GetParam("param_does_not_exist", value));
}
/////////////////////////////////////////////////
TEST_P(PhysicsEngineTest, PhysicsEngineGetParamBool)
{
PhysicsEngineGetParamBool(GetParam());
}
INSTANTIATE_TEST_CASE_P(PhysicsEngines, PhysicsEngineTest,
PHYSICS_ENGINE_VALUES,); // NOLINT
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}