TraveSim Adapters  0.1
Protobuf adapters for TraveSim project
replacer_receiver.cpp
Go to the documentation of this file.
1 /**
2  * @file replacer_receiver.cpp
3  *
4  * @author Lucas Haug <lucas.haug@thunderatz.org>
5  *
6  * @brief Replacer receiver with UDP and protobuf
7  *
8  * @date 04/2021
9  *
10  * @copyright MIT License - Copyright (c) 2021 ThundeRatz
11  */
12 
13 #include <ros/console.h>
14 
16 
17 /*****************************************
18  * Private Constants
19  *****************************************/
20 
21 #define BUFFER_SIZE 1024U
22 
23 /*****************************************
24  * Public Methods Bodies Definitions
25  *****************************************/
26 
27 namespace travesim {
28 namespace proto {
29 ReplacerReceiver::ReplacerReceiver(const std::string receiver_address, const short receiver_port,
30  bool force_specific_source) {
31  this->unicast_receiver =
32  std::unique_ptr<udp::UnicastReceiver>(new udp::UnicastReceiver(receiver_address, receiver_port));
33  this->unicast_receiver->force_specific_source(force_specific_source);
34 }
35 
36 bool ReplacerReceiver::receive(std::queue<std::shared_ptr<EntityState>>* p_replament_queue) {
37  char buffer[BUFFER_SIZE];
38 
39  size_t data_size = 0;
40 
41  try {
42  data_size = this->unicast_receiver->receive(buffer, BUFFER_SIZE);
43  } catch (std::exception& e) {
44  ROS_ERROR_STREAM("Replacer receiver: " << e.what());
45 
46  return false;
47  }
48 
49  if (data_size > 0) {
50  fira_message::sim_to_ref::Packet packet_data;
51  packet_data.ParseFromArray(buffer, BUFFER_SIZE);
52 
53  if (packet_data.has_replace()) {
54  for (const auto& robot_replacement : packet_data.replace().robots()) {
55  RobotState robot_state = this->robot_rplcmt_pb_to_robot_state(&robot_replacement);
56  p_replament_queue->push(std::make_shared<RobotState>(robot_state));
57  }
58 
59  if (packet_data.replace().has_ball()) {
60  const fira_message::sim_to_ref::BallReplacement ball = packet_data.replace().ball();
61  EntityState ball_state = this->ball_rplcmt_pb_to_entity_state(&ball);
62  p_replament_queue->push(std::make_shared<EntityState>(ball_state));
63  }
64 
65  return true;
66  }
67  }
68 
69  return false;
70 }
71 
72 void ReplacerReceiver::set_receiver_endpoint(const std::string receiver_address, const short receiver_port) {
73  this->unicast_receiver->set_receiver_endpoint(receiver_address, receiver_port);
74 }
75 
76 void ReplacerReceiver::force_specific_source(bool force_specific_source) {
77  this->unicast_receiver->force_specific_source(force_specific_source);
78 }
79 
81  try {
82  this->unicast_receiver->reset();
83  } catch (std::exception& e) {
84  ROS_ERROR_STREAM("Replacer receiver: " << e.what());
85  }
86 }
87 
89  const fira_message::sim_to_ref::BallReplacement* p_ball_pb_msg) {
90  EntityState ball_state;
91 
92  ball_state.position.x = p_ball_pb_msg->x();
93  ball_state.position.y = p_ball_pb_msg->y();
94  ball_state.velocity.x = p_ball_pb_msg->vx();
95  ball_state.velocity.y = p_ball_pb_msg->vy();
96 
97  return ball_state;
98 }
99 
101  const fira_message::sim_to_ref::RobotReplacement* p_robot_pb_msg) {
102  RobotState robot_state;
103 
104  robot_state.is_yellow = p_robot_pb_msg->yellowteam();
105  robot_state.id = p_robot_pb_msg->position().robot_id();
106 
107  robot_state.position.x = p_robot_pb_msg->position().x();
108  robot_state.position.y = p_robot_pb_msg->position().y();
109  robot_state.angular_position = p_robot_pb_msg->position().orientation();
110 
111  robot_state.velocity.x = p_robot_pb_msg->position().vx();
112  robot_state.velocity.y = p_robot_pb_msg->position().vy();
113  robot_state.angular_velocity = p_robot_pb_msg->position().vorientation();
114 
115  return robot_state;
116 }
117 } // namespace proto
118 } // namespace travesim
double angular_velocity
Angular velocity in rad/s.
Vector2D velocity
Velocity in m/s.
double x
Public attributes.
EntityState ball_rplcmt_pb_to_entity_state(const fira_message::sim_to_ref::BallReplacement *p_ball_pb_msg)
Convert a BallReplacement protobuf message to a EntityState.
Data structure to hold the state of a robot in the simulation.
Definition: robot_state.hpp:23
double angular_position
Angular position in radinans.
Receiver class using UDP in unicast mode.
Replacer receiver with UDP and protobuf.
Vector2D position
Position in meters.
ReplacerReceiver(const std::string receiver_address, const short receiver_port, bool force_specific_source=false)
Construct a new ReplacerReceiver object.
void force_specific_source(bool force_specific_source)
Set wheter to enable any source or source specific multicast. True for specific source,...
bool is_yellow
Public attributes.
Definition: robot_state.hpp:64
bool receive(std::queue< std::shared_ptr< EntityState >> *p_replament_queue)
Receive the replacement commands.
Data structure to hold the state of a entity in the simulation.
void reset(void)
Reset the receiver.
RobotState robot_rplcmt_pb_to_robot_state(const fira_message::sim_to_ref::RobotReplacement *p_robot_pb_msg)
Convert a RobotReplacement protobuf message to a RobotState.
#define BUFFER_SIZE
std::unique_ptr< udp::UnicastReceiver > unicast_receiver
void set_receiver_endpoint(const std::string receiver_address, const short receiver_port)
Set the receiver endpoint.