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 2013 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  std::tr1::tie(this->physicsEngine, this->jointType) = GetParam();
77  }
78  }
79 
83  public: void SpawnJointTypes(const std::string &_physicsEngine,
84  const std::string &_jointType);
85 
90  public: void SpawnJointRotational(const std::string &_physicsEngine,
91  const std::string &_jointType);
92 
97  public: void SpawnJointRotationalWorld(const std::string &_physicsEngine,
98  const std::string &_jointType);
99 
101  public: class SpawnJointOptions
102  {
104  public: SpawnJointOptions() : worldChild(false), worldParent(false),
105  axis(math::Vector3(1, 0, 0))
106  {
107  }
108 
110  public: ~SpawnJointOptions()
111  {
112  }
113 
115  public: std::string type;
116 
118  public: bool worldChild;
119 
121  public: bool worldParent;
122 
126 
129 
132 
135 
138 
141  };
142 
153  public: physics::JointPtr SpawnJoint(const std::string &_type,
154  bool _worldChild = false,
155  bool _worldParent = false,
156  common::Time _wait = common::Time(99, 0))
157  {
158  SpawnJointOptions opt;
159  opt.type = _type;
160  opt.worldChild = _worldChild;
161  opt.worldParent = _worldParent;
162  opt.wait = _wait;
163  return SpawnJoint(opt);
164  }
165 
172  public: physics::JointPtr SpawnJoint(const SpawnJointOptions &_opt)
173  {
174  msgs::Factory msg;
175  std::ostringstream modelStr;
176  std::ostringstream modelName;
177  modelName << "joint_model" << this->spawnCount++;
178 
179  modelStr
180  << "<sdf version='" << SDF_VERSION << "'>"
181  << "<model name ='" << modelName.str() << "'>"
182  << " <pose>" << _opt.modelPose << "</pose>";
183  if (!_opt.worldParent)
184  {
185  modelStr
186  << " <link name='parent'>"
187  << " <pose>" << _opt.parentLinkPose << "</pose>"
188  << " </link>";
189  }
190  if (!_opt.worldChild)
191  {
192  modelStr
193  << " <link name='child'>"
194  << " <pose>" << _opt.childLinkPose << "</pose>"
195  << " </link>";
196  }
197  modelStr
198  << " <joint name='joint' type='" << _opt.type << "'>"
199  << " <pose>" << _opt.jointPose << "</pose>";
200  if (_opt.worldParent)
201  modelStr << " <parent>world</parent>";
202  else
203  modelStr << " <parent>parent</parent>";
204  if (_opt.worldChild)
205  modelStr << " <child>world</child>";
206  else
207  modelStr << " <child>child</child>";
208  modelStr
209  << " <axis>"
210  << " <xyz>" << _opt.axis << "</xyz>"
211  << " </axis>";
212  modelStr
213  << " </joint>"
214  << "</model>";
215 
216  msg.set_sdf(modelStr.str());
217  this->factoryPub->Publish(msg);
218 
219  physics::JointPtr joint;
220  if (_opt.wait != common::Time::Zero)
221  {
223  unsigned int waitCount = 0;
224  while (_opt.wait > (common::Time::GetWallTime() - wallStart) &&
225  !this->HasEntity(modelName.str()))
226  {
228  if (++waitCount % 10 == 0)
229  {
230  gzwarn << "Waiting " << waitCount / 10 << " seconds for "
231  << _opt.type << " joint to spawn." << std::endl;
232  }
233  }
234  if (this->HasEntity(modelName.str()) && waitCount >= 10)
235  gzwarn << _opt.type << " joint has spawned." << std::endl;
236 
237  physics::WorldPtr world = physics::get_world("default");
238  if (world != NULL)
239  {
240  physics::ModelPtr model = world->GetModel(modelName.str());
241  if (model != NULL)
242  joint = model->GetJoint("joint");
243  }
244  }
245  return joint;
246  }
247 
249  protected: std::string physicsEngine;
250 
252  protected: std::string jointType;
253 
255  private: unsigned int spawnCount;
256 };
257 #endif