spawn_box.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /* Author: John Hsu */
36 
37 #include <string>
38 #include <gtest/gtest.h>
39 #include "gazebo/SpawnModel.h"
40 #include "gazebo/GetWorldProperties.h"
41 #include "gazebo/DeleteModel.h"
42 #include "geometry_msgs/Pose.h"
43 #include "std_srvs/Empty.h"
44 
45 #include <sstream>
46 #include <tinyxml.h>
47 
48 // Including ros, just to be able to call ros::init(), to remove unwanted
49 // args from command-line.
50 #include <ros/ros.h>
51 
52 int g_argc;
53 char** g_argv;
54 
55 TEST(SpawnTest, spawnSingleBox)
56 {
57  ros::NodeHandle nh("");
58  // make the service call to spawn model
59  ros::service::waitForService("gazebo/spawn_urdf_model");
60  ros::ServiceClient spawn_model_client = nh.serviceClient<gazebo::SpawnModel>("gazebo/spawn_urdf_model");
61  gazebo::SpawnModel spawn_model;
62  spawn_model.request.model_name = "box1";
63 
64  // load urdf file
65  std::string urdf_filename = std::string(g_argv[1]);
66  ROS_DEBUG_NAMED("spawn_box", "loading file: %s",urdf_filename.c_str());
67  // read urdf / gazebo model xml from file
68  TiXmlDocument xml_in(urdf_filename);
69  xml_in.LoadFile();
70  std::ostringstream stream;
71  stream << xml_in;
72  spawn_model.request.model_xml = stream.str(); // load xml file
73  ROS_DEBUG_NAMED("spawn_box", "XML string: %s",stream.str().c_str());
74 
75  spawn_model.request.robot_namespace = "";
76  geometry_msgs::Pose pose;
77  pose.position.x = pose.position.y = 0; pose.position.z = 1;
78  pose.orientation.w = 1.0; pose.orientation.x = pose.orientation.y = pose.orientation.z = 0;
79  spawn_model.request.initial_pose = pose;
80  spawn_model.request.reference_frame = "";
81 
82  ASSERT_TRUE(spawn_model_client.call(spawn_model));
83 
84  SUCCEED();
85 }
86 
87 
88 TEST(SpawnTest, spawnBoxStack)
89 {
90  ros::NodeHandle nh("");
91  // make the service call to spawn model
92  ros::service::waitForService("gazebo/spawn_urdf_model");
93  ros::ServiceClient spawn_model_client = nh.serviceClient<gazebo::SpawnModel>("gazebo/spawn_urdf_model");
94  gazebo::SpawnModel spawn_model;
95 
96  // load urdf file
97  std::string urdf_filename = std::string(g_argv[1]);
98  ROS_DEBUG_NAMED("spawn_box", "loading file: %s",urdf_filename.c_str());
99  // read urdf / gazebo model xml from file
100  TiXmlDocument xml_in(urdf_filename);
101  xml_in.LoadFile();
102  std::ostringstream stream;
103  stream << xml_in;
104  spawn_model.request.model_xml = stream.str(); // load xml file
105  ROS_DEBUG_NAMED("spawn_box", "XML string: %s",stream.str().c_str());
106 
107  spawn_model.request.robot_namespace = "";
108  spawn_model.request.reference_frame = "";
109 
110  // spawn 10 boxes
111  for (int i=0;i<10;i++)
112  {
113  std::ostringstream mn_stream;
114  mn_stream << "box_" << i;
115  spawn_model.request.model_name = mn_stream.str();
116  geometry_msgs::Pose pose;
117  pose.position.x = pose.position.y = 0; pose.position.z = 1 + 2.0*(i+1);
118  pose.orientation.w = 1.0; pose.orientation.x = pose.orientation.y = pose.orientation.z = 0;
119  spawn_model.request.initial_pose = pose;
120  ASSERT_TRUE(spawn_model_client.call(spawn_model));
121  }
122 
123  SUCCEED();
124 }
125 
126 TEST(DeleteTest, deleteAllModels)
127 {
128  ros::NodeHandle nh("");
129  // make the service call to delete model
130  ros::service::waitForService("gazebo/delete_model");
131  ros::ServiceClient delete_model_client = nh.serviceClient<gazebo::DeleteModel>("gazebo/delete_model");
132  gazebo::DeleteModel delete_model;
133 
134  // model names to delete
135  ros::service::waitForService("gazebo/get_world_properties");
136  ros::ServiceClient check_model_client = nh.serviceClient<gazebo::GetWorldProperties>("gazebo/get_world_properties");
137  gazebo::GetWorldProperties world_properties;
138  // get all models in the world and loop through deleting each one
139  check_model_client.call(world_properties);
140  for (std::vector<std::string>::iterator mit = world_properties.response.model_names.begin();
141  mit != world_properties.response.model_names.end();
142  mit++)
143  {
144  delete_model.request.model_name = *mit;
145  ASSERT_TRUE(delete_model_client.call(delete_model));
146  }
147 
148  // should have no more models in the world
149  check_model_client.call(world_properties);
150  ASSERT_TRUE(world_properties.response.model_names.empty());
151 
152  SUCCEED();
153 }
154 
155 
156 int main(int argc, char** argv)
157 {
158  ros::init(argc, argv, "test", ros::init_options::AnonymousName);
159  testing::InitGoogleTest(&argc, argv);
160  g_argc = argc;
161  g_argv = argv;
162  return RUN_ALL_TESTS();
163 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
char ** g_argv
Definition: spawn_box.cpp:53
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
#define ROS_DEBUG_NAMED(name,...)
int SUCCEED
int main(int argc, char **argv)
Definition: spawn_box.cpp:156
TEST(SpawnTest, spawnSingleBox)
Definition: spawn_box.cpp:55
int g_argc
Definition: spawn_box.cpp:52
ROSCPP_DECL bool waitForService(const std::string &service_name, int32_t timeout)


gazebo_plugins
Author(s): John Hsu
autogenerated on Tue Mar 26 2019 02:31:27