TraveSim Adapters  0.1
Protobuf adapters for TraveSim project
replacer_adapter.cpp
Go to the documentation of this file.
1 /**
2  * @file replacer_adapter.cpp
3  *
4  * @author Felipe Gomes de Melo <felipe.gomes@thunderatz.org>
5  * @author Lucas Haug <lucas.haug@thunderatz.org>
6  *
7  * @brief Replacer adapter execution file
8  *
9  * @date 06/2021
10  *
11  * @copyright MIT License - Copyright (c) 2021 ThundeRatz
12  *
13  */
14 
15 #include <queue>
16 #include <memory>
17 
18 #include <ros/ros.h>
19 #include <ros/console.h>
20 #include <gazebo_msgs/ModelStates.h>
21 
26 
27 int main(int argc, char** argv) {
28  ros::init(argc, argv, "replacer_adapter");
29  ros::NodeHandle nh;
30 
31  // Get config
32  travesim::ReplacerConfigurer replacer_configurer;
33 
34  int32_t receiver_port = replacer_configurer.get_port();
35  std::string receiver_address = replacer_configurer.get_address();
36 
37  bool specific_source = replacer_configurer.get_specific_source();
38 
39  // Initialize sender, receivers and commands
40  travesim::ros_side::ReplacerSender replacer_sender;
41  travesim::proto::ReplacerReceiver replacer_receiver(receiver_address, receiver_port, specific_source);
42 
43  // Define data structures
44  travesim::ros_side::state_vector_t model_state_vector;
45  std::queue<std::shared_ptr<travesim::EntityState>> states_queue;
46 
47  // Start
48  ROS_INFO_STREAM("Replacer adapter config:" << std::endl << replacer_configurer);
49 
50  while (ros::ok()) {
51  bool received_new_msg = replacer_receiver.receive(&states_queue);
52 
53  if (received_new_msg) {
54  replacer_sender.send_command(travesim::ros_side::PAUSE);
55 
56  while (!states_queue.empty()) {
57  std::shared_ptr<travesim::RobotState> state = std::dynamic_pointer_cast<travesim::RobotState>(
58  states_queue.front());
59 
60  gazebo_msgs::ModelState gazebo_state_msg;
61 
62  if (state != nullptr) {
63  gazebo_state_msg = travesim::converter::RobotState_to_ModelState(state.get());
64  } else {
65  gazebo_state_msg = travesim::converter::EntityState_to_ModelState(states_queue.front().get());
66  }
67 
68  model_state_vector.push_back(gazebo_state_msg);
69 
70  states_queue.pop();
71  }
72 
73  replacer_sender.set_models_state(&model_state_vector);
74 
76 
77  model_state_vector.clear();
78  }
79 
80  if (replacer_configurer.get_reset()) {
81  // Get config
82  receiver_port = replacer_configurer.get_port();
83  receiver_address = replacer_configurer.get_address();
84 
85  specific_source = replacer_configurer.get_specific_source();
86 
87  // Reconfigure receiver
88  replacer_receiver.set_receiver_endpoint(receiver_address, receiver_port);
89  replacer_receiver.force_specific_source(specific_source);
90  replacer_receiver.reset();
91 
92  // Show config
93  ROS_INFO_STREAM("Replacer adapter config:" << std::endl << replacer_configurer);
94  }
95 
96  ros::spinOnce();
97  }
98 }
std::string get_address(void)
Get the replacer configured address.
bool send_command(simulation_command_t command)
Send command to gazebo.
bool get_specific_source(void)
Get the specific_source config.
Collection of data converters between ROS and local formats.
Replacer receiver with UDP and protobuf.
bool set_models_state(state_vector_t *model_states)
Set state of multiple entities.
bool get_reset(void)
Get the reset config.
std::vector< gazebo_msgs::ModelState > state_vector_t
Helper type to set the states of multiple entites at once.
Configurer for the Replacer.
uint16_t get_port(void)
Get the replacer configured port.
Replacer receiver class with UDP and protobuf.
ReplacerConfigurer class definition.
gazebo_msgs::ModelState RobotState_to_ModelState(RobotState *robot_state, double z=DEFAULT_Z_VALUE_ROBOT)
Function to convert travesim::RobotState to gazebo_msgs::ModelState.
Definition: ros_side.cpp:108
void force_specific_source(bool force_specific_source)
Set wheter to enable any source or source specific multicast. True for specific source,...
int main(int argc, char **argv)
bool receive(std::queue< std::shared_ptr< EntityState >> *p_replament_queue)
Receive the replacement commands.
void reset(void)
Reset the receiver.
gazebo_msgs::ModelState EntityState_to_ModelState(EntityState *entity_state, double z=DEFAULT_Z_VALUE_BALL)
Function to convert travesim::EntityState to gazebo_msgs::ModelState.
Definition: ros_side.cpp:86
void set_receiver_endpoint(const std::string receiver_address, const short receiver_port)
Set the receiver endpoint.