TraveSim Adapters  0.1
Protobuf adapters for TraveSim project
replacer_sender.cpp
Go to the documentation of this file.
1 /**
2  * @file replacer_sender.cpp
3  *
4  * @author Felipe Gomes de Melo <felipe.gomes@thunderatz.org>
5  *
6  * @brief ROS replacer sender for gazebo
7  *
8  * @date 04/2021
9  *
10  * @copyright MIT License - Copyright (c) 2021 ThundeRatz
11  *
12  */
13 
14 #include <ros/console.h>
15 #include <std_srvs/Empty.h>
16 
17 #include <unordered_map>
18 
20 
21 namespace travesim {
22 namespace ros_side {
24  _nh = ros::NodeHandlePtr(new ros::NodeHandle());
26 }
27 
29  /**
30  * @brief If the connection dropped, we try to reconnect
31  */
32  if (!this->gz_service.isValid()) {
33  this->gz_service = this->_nh->serviceClient<gazebo_msgs::SetModelState>("/gazebo/set_model_state");
34  }
35 }
36 
37 bool ReplacerSender::set_model_state(gazebo_msgs::ModelState model_state) {
38  static gazebo_msgs::SetModelStateRequest request;
39  static gazebo_msgs::SetModelStateResponse response;
40 
42  request.model_state = model_state;
43 
44  bool status = this->gz_service.call(request, response);
45 
46  if (!(status && response.success)) {
47  ROS_ERROR_STREAM("Error while calling set_model_state " << model_state);
48  return false;
49  }
50 
51  return true;
52 }
53 
55  for (uint32_t i = 0; i < model_states->size(); i++) {
56  if (!this->set_model_state(model_states->at(i))) {
57  ROS_ERROR_STREAM("Error while calling set_team_state at i = " << i);
58  return false;
59  }
60  }
61 
62  return true;
63 }
64 
66  static std::unordered_map<simulation_command_t, std::string> topics_table({
67  {travesim::ros_side::PAUSE, "/gazebo/pause_physics" },
68  {travesim::ros_side::RESUME, "/gazebo/unpause_physics"},
69  {travesim::ros_side::RESET_WORLD, "/gazebo/reset_world"},
70  {travesim::ros_side::RESET_SIMULATION, "/gazebo/reset_simulation"},
71  });
72 
73  std_srvs::Empty msg;
74 
75  if (topics_table[command] == "") {
76  return false;
77  } else {
78  ros::service::call(topics_table[command], msg);
79  return true;
80  }
81 }
82 } // namespace ros_side
83 } // namespace travesim
void reconnect_service_client()
Attempts to reconnect gz_service ServiceClient if connection dropped.
bool set_model_state(gazebo_msgs::ModelState model_state)
Set the state of one entity.
bool send_command(simulation_command_t command)
Send command to gazebo.
bool set_models_state(state_vector_t *model_states)
Set state of multiple entities.
std::vector< gazebo_msgs::ModelState > state_vector_t
Helper type to set the states of multiple entites at once.
ReplacerSender()
Construct a new Replacer Sender object.
simulation_command_t
All possible commands to send to Gazebo.