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 class Joint_TEST : public ServerFixture
32 {
33  protected: Joint_TEST() : ServerFixture(), spawnCount(0)
34  {
35  }
36 
39  public: void SpawnJointTypes(const std::string &_physicsEngine);
40 
51  public: physics::JointPtr SpawnJoint(const std::string &_type,
52  bool _worldChild = false,
53  bool _worldParent = false,
54  common::Time _wait = common::Time(99, 0))
55  {
56  msgs::Factory msg;
57  std::ostringstream modelStr;
58  std::ostringstream modelName;
59  modelName << "joint_model" << this->spawnCount++;
60 
61  modelStr
62  << "<sdf version='" << SDF_VERSION << "'>"
63  << "<model name ='" << modelName.str() << "'>";
64  if (!_worldParent)
65  {
66  modelStr
67  << " <link name='parent'>"
68  << " </link>";
69  }
70  if (!_worldChild)
71  {
72  modelStr
73  << " <link name='child'>"
74  << " </link>";
75  }
76  modelStr
77  << " <joint name='joint' type='" << _type << "'>"
78  << " <pose>0 0 0 0 0 0</pose>";
79  if (_worldParent)
80  modelStr << " <parent>world</parent>";
81  else
82  modelStr << " <parent>parent</parent>";
83  if (_worldChild)
84  modelStr << " <child>world</child>";
85  else
86  modelStr << " <child>child</child>";
87  modelStr
88  << " <axis>"
89  << " <xyz>0 0 1</xyz>"
90  << " </axis>";
91  modelStr
92  << " </joint>"
93  << "</model>";
94 
95  msg.set_sdf(modelStr.str());
96  this->factoryPub->Publish(msg);
97 
98  physics::JointPtr joint;
99  if (_wait != common::Time::Zero)
100  {
102  unsigned int waitCount = 0;
103  while (_wait > (common::Time::GetWallTime() - wallStart) &&
104  !this->HasEntity(modelName.str()))
105  {
107  if (++waitCount % 10 == 0)
108  {
109  gzwarn << "Waiting " << waitCount / 10 << " seconds for "
110  << _type << " joint to spawn." << std::endl;
111  }
112  }
113  if (this->HasEntity(modelName.str()) && waitCount >= 10)
114  gzwarn << _type << " joint has spawned." << std::endl;
115 
116  physics::WorldPtr world = physics::get_world("default");
117  if (world != NULL)
118  {
119  physics::ModelPtr model = world->GetModel(modelName.str());
120  if (model != NULL)
121  joint = model->GetJoint("joint");
122  }
123  }
124  return joint;
125  }
126 
128  private: unsigned int spawnCount;
129 };
130 
131 #endif