All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Joint_TEST.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2014 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 #ifndef _JOINT_TEST_HH_
19 #define _JOINT_TEST_HH_
20 
21 #include <string>
22 #include <sstream>
23 
24 #include "test/ServerFixture.hh"
25 
26 #include "gazebo/common/Time.hh"
27 #include "gazebo/physics/physics.hh"
28 
29 using namespace gazebo;
30 
31 typedef std::tr1::tuple<const char *, const char *> std_string2;
32 
33 class Joint_TEST : public ServerFixture,
34  public ::testing::WithParamInterface<std_string2>
35 {
36  protected: Joint_TEST() : ServerFixture(), spawnCount(0)
37  {
38  }
39 
43  public: void ForceTorque1(const std::string &_physicsEngine);
44 
51  public: void ForceTorque2(const std::string &_physicsEngine);
52 
57  public: void GetForceTorqueWithAppliedForce(
58  const std::string &_physicsEngine);
59 
63  public: void JointTorqueTest(const std::string &_physicsEngine);
64 
67  public: void JointCreationDestructionTest(const std::string &_physicsEngine);
68 
69  public: virtual void SetUp()
70  {
71  const ::testing::TestInfo *const test_info =
72  ::testing::UnitTest::GetInstance()->current_test_info();
73  if (test_info->value_param())
74  {
75  gzdbg << "Params: " << test_info->value_param() << std::endl;
76  this->physicsEngine = std::tr1::get<0>(GetParam());
77  this->jointType = std::tr1::get<1>(GetParam());
78  }
79  }
80 
84  public: void SpawnJointTypes(const std::string &_physicsEngine,
85  const std::string &_jointType);
86 
91  public: void SpawnJointRotational(const std::string &_physicsEngine,
92  const std::string &_jointType);
93 
98  public: void SpawnJointRotationalWorld(const std::string &_physicsEngine,
99  const std::string &_jointType);
100 
102  public: class SpawnJointOptions
103  {
105  public: SpawnJointOptions() : worldChild(false), worldParent(false),
106  wait(common::Time(99, 0)),
107  noLinkPose(false), axis(math::Vector3(1, 0, 0))
108  {
109  }
110 
112  public: ~SpawnJointOptions()
113  {
114  }
115 
117  public: std::string type;
118 
120  public: bool worldChild;
121 
123  public: bool worldParent;
124 
128 
131 
134 
137 
139  public: bool noLinkPose;
140 
143 
146  };
147 
158  public: physics::JointPtr SpawnJoint(const std::string &_type,
159  bool _worldChild = false,
160  bool _worldParent = false,
161  common::Time _wait = common::Time(99, 0))
162  {
163  SpawnJointOptions opt;
164  opt.type = _type;
165  opt.worldChild = _worldChild;
166  opt.worldParent = _worldParent;
167  opt.wait = _wait;
168  return SpawnJoint(opt);
169  }
170 
177  public: physics::JointPtr SpawnJoint(const SpawnJointOptions &_opt)
178  {
179  msgs::Factory msg;
180  std::ostringstream modelStr;
181  std::ostringstream modelName;
182  modelName << "joint_model" << this->spawnCount++;
183 
184  modelStr
185  << "<sdf version='" << SDF_VERSION << "'>"
186  << "<model name ='" << modelName.str() << "'>"
187  << " <pose>" << _opt.modelPose << "</pose>";
188  if (!_opt.worldParent)
189  {
190  modelStr << " <link name='parent'>";
191  if (!_opt.noLinkPose)
192  {
193  modelStr << " <pose>" << _opt.parentLinkPose << "</pose>";
194  }
195  modelStr << " </link>";
196  }
197  if (!_opt.worldChild)
198  {
199  modelStr << " <link name='child'>";
200  if (!_opt.noLinkPose)
201  {
202  modelStr << " <pose>" << _opt.childLinkPose << "</pose>";
203  }
204  modelStr << " </link>";
205  }
206  modelStr
207  << " <joint name='joint' type='" << _opt.type << "'>"
208  << " <pose>" << _opt.jointPose << "</pose>";
209  if (_opt.worldParent)
210  modelStr << " <parent>world</parent>";
211  else
212  modelStr << " <parent>parent</parent>";
213  if (_opt.worldChild)
214  modelStr << " <child>world</child>";
215  else
216  modelStr << " <child>child</child>";
217  modelStr
218  << " <axis>"
219  << " <xyz>" << _opt.axis << "</xyz>"
220  << " </axis>";
221  modelStr
222  << " </joint>"
223  << "</model>";
224 
225  msg.set_sdf(modelStr.str());
226  this->factoryPub->Publish(msg);
227 
228  physics::JointPtr joint;
229  if (_opt.wait != common::Time::Zero)
230  {
232  unsigned int waitCount = 0;
233  while (_opt.wait > (common::Time::GetWallTime() - wallStart) &&
234  !this->HasEntity(modelName.str()))
235  {
237  if (++waitCount % 10 == 0)
238  {
239  gzwarn << "Waiting " << waitCount / 10 << " seconds for "
240  << _opt.type << " joint to spawn." << std::endl;
241  }
242  }
243  if (this->HasEntity(modelName.str()) && waitCount >= 10)
244  gzwarn << _opt.type << " joint has spawned." << std::endl;
245 
246  physics::WorldPtr world = physics::get_world("default");
247  if (world != NULL)
248  {
249  physics::ModelPtr model = world->GetModel(modelName.str());
250  if (model != NULL)
251  joint = model->GetJoint("joint");
252  }
253  }
254  return joint;
255  }
256 
258  protected: std::string physicsEngine;
259 
261  protected: std::string jointType;
262 
264  private: unsigned int spawnCount;
265 };
266 #endif