Tetrapod Project
terrain_plugin.cpp
Go to the documentation of this file.
2 
3 namespace gazebo{
4 
5 // Constructor
7 
8 // Destructor
10 {
11  this->rosNode->shutdown();
12 
13  this->rosProcessQueue.clear();
14  this->rosProcessQueue.disable();
15  this->rosProcessQueueThread.join();
16 }
17 
18 // Initialize ROS Publish and Process Queue Threads
20 {
21  this->rosProcessQueueThread = std::thread(
22  std::bind(&TerrainPlugin::ProcessQueueThread, this)
23  );
24 }
25 
26 // Setup thread to process messages
28 {
29  static const double timeout = 0.000001;
30  while (this->rosNode->ok())
31  {
32  this->rosProcessQueue.callAvailable(ros::WallDuration(timeout));
33  }
34 }
35 
36 
37 void TerrainPlugin::Load(physics::WorldPtr _world, sdf::ElementPtr _sdf)
38 {
39  this->world = _world;
40 
41  InitRos();
42 
44 }
45 
47 {
48  if (!ros::isInitialized())
49  {
50  int argc = 0;
51  char **argv = NULL;
52  ros::init(
53  argc,
54  argv,
55  "gazebo_client",
56  ros::init_options::NoSigintHandler
57  );
58  }
59 
60  this->rosNode.reset(new ros::NodeHandle("gazebo_client"));
61 
62  ros::AdvertiseServiceOptions reset_simulation_aso =
63  ros::AdvertiseServiceOptions::create<std_srvs::Empty>(
64  "/" + std::string("my_robot") + "/reset_simulation_terrain",
65  boost::bind(&TerrainPlugin::ResetSimulation, this, _1, _2),
66  ros::VoidPtr(),
67  &this->rosProcessQueue
68  );
69 
70  this->resetSimService = this->rosNode->advertiseService(reset_simulation_aso);
71 
72  //this -> resetSimService = this->rosNode->advertiseService("/my_robot/reset_simulation_terrain", &TerrainPlugin::ResetSimulation, this);
73 
74  this->tetrapodResetService = this->rosNode->serviceClient<std_srvs::Empty>("/my_robot/reset_simulation");
75 }
76 
77 bool TerrainPlugin::ResetSimulation(const std_srvs::Empty::Request &_req,
78  std_srvs::Empty::Response &_res)
79 {
80  //physics::ModelPtr terrain = this->world->ModelByName("terrain");
81 
82  physics::Model_V models = this->world->Models();
83 
84  //sdf::SDF terrain_sdf;
85 
86  /*std::string sdf_string = "<sdf version='1.7'>\
87  <model name='terrain'>\
88  <pose>0 0 0 0 0 0</pose>\
89  <include>\
90  <uri>model://terrain_description</uri>\
91  </include>\
92  </model>\
93  </sdf>";
94 
95  terrain_sdf.SetFromString(sdf_string);*/
96 
97  for (auto ptr : models)
98  {
99  std::string name = ptr->GetName();
100  std::cout << name << std::endl;
101  }
102  //this->world->RemoveModel(old_terrain);
103  models = this->world->Models();
104 
105  for (auto ptr: models)
106  {
107  std::string name = ptr->GetName();
108  std::cout << name << std::endl;
109  }
110  //ros::Duration(0.001).sleep();
111  this->world->RemoveModel("terrain");
112  this->world->InsertModelFile("model://terrain_description");
113  this->world->DisableAllModels();
114  this->world->EnableAllModels();
115  this->world->Reset();
116 
117  std_srvs::Empty srv_call;
118 
119  this->tetrapodResetService.call(srv_call);
120 
121  return true;
122 }
123 }
virtual void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf)
The Load function is called by Gazebo when the plugin is inserted into simulation.
ros::CallbackQueue rosProcessQueue
ROS callbackque that helps process messages.
ros::ServiceServer resetSimService
ROS Reset Simulation Service.
physics::WorldPtr world
Pointer to the world.
std::unique_ptr< ros::NodeHandle > rosNode
Node used for ROS transport.
std::thread rosProcessQueueThread
Thread running the rosProcessQueue.
ros::ServiceClient tetrapodResetService
bool ResetSimulation(const std_srvs::Empty::Request &_req, std_srvs::Empty::Response &_res)