TraveSim Adapters  0.1
Protobuf adapters for TraveSim project
travesim::proto::ReplacerReceiver Class Reference

Replacer receiver class with UDP and protobuf. More...

#include "replacer_receiver.hpp"

Collaboration diagram for travesim::proto::ReplacerReceiver:

Public Member Functions

 ReplacerReceiver (const std::string receiver_address, const short receiver_port, bool force_specific_source=false)
 Construct a new ReplacerReceiver object. More...
 
bool receive (std::queue< std::shared_ptr< EntityState >> *p_replament_queue)
 Receive the replacement commands. More...
 
void set_receiver_endpoint (const std::string receiver_address, const short receiver_port)
 Set the receiver endpoint. More...
 
void force_specific_source (bool force_specific_source)
 Set wheter to enable any source or source specific multicast. True for specific source, false for any source, default is false. More...
 
void reset (void)
 Reset the receiver. More...
 
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. More...
 
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. More...
 

Private Attributes

std::unique_ptr< udp::UnicastReceiverunicast_receiver
 

Detailed Description

Replacer receiver class with UDP and protobuf.

Definition at line 29 of file replacer_receiver.hpp.

Constructor & Destructor Documentation

◆ ReplacerReceiver()

travesim::proto::ReplacerReceiver::ReplacerReceiver ( const std::string  receiver_address,
const short  receiver_port,
bool  force_specific_source = false 
)

Construct a new ReplacerReceiver object.

Parameters
receiver_addressReplacer receiver address
receiver_portReplacer receiver port
force_specific_sourceWhether to enable source specific or not, default false
Note
The unicast addresses must be in the block 127.0.0.0/8, see IANA IPv4 Address Space Registry or the RFC6890 for more informations.

Definition at line 29 of file replacer_receiver.cpp.

30  {
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 }
void force_specific_source(bool force_specific_source)
Set wheter to enable any source or source specific multicast. True for specific source,...
std::unique_ptr< udp::UnicastReceiver > unicast_receiver

References unicast_receiver.

Member Function Documentation

◆ ball_rplcmt_pb_to_entity_state()

EntityState travesim::proto::ReplacerReceiver::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.

Parameters
p_ball_pb_msgPointer the ball replacement protobuf message
Returns
Converted EntityState

Definition at line 88 of file replacer_receiver.cpp.

89  {
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 }

References travesim::EntityState::position, travesim::EntityState::velocity, travesim::Vector2D::x, and travesim::Vector2D::y.

Here is the caller graph for this function:

◆ force_specific_source()

void travesim::proto::ReplacerReceiver::force_specific_source ( bool  force_specific_source)

Set wheter to enable any source or source specific multicast. True for specific source, false for any source, default is false.

Parameters
force_specific_sourceWhether to enable source specific or not.

Definition at line 76 of file replacer_receiver.cpp.

76  {
77  this->unicast_receiver->force_specific_source(force_specific_source);
78 }
void force_specific_source(bool force_specific_source)
Set wheter to enable any source or source specific multicast. True for specific source,...
std::unique_ptr< udp::UnicastReceiver > unicast_receiver

References unicast_receiver.

Here is the caller graph for this function:

◆ receive()

bool travesim::proto::ReplacerReceiver::receive ( std::queue< std::shared_ptr< EntityState >> *  p_replament_queue)

Receive the replacement commands.

Parameters
p_replament_queuePointer to a queue where to store the desired replacements
Returns
true if a new message was received, false otherwise

Definition at line 36 of file replacer_receiver.cpp.

36  {
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 }
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.
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

References ball_rplcmt_pb_to_entity_state(), BUFFER_SIZE, robot_rplcmt_pb_to_robot_state(), and unicast_receiver.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reset()

void travesim::proto::ReplacerReceiver::reset ( void  )

Reset the receiver.

Definition at line 80 of file replacer_receiver.cpp.

80  {
81  try {
82  this->unicast_receiver->reset();
83  } catch (std::exception& e) {
84  ROS_ERROR_STREAM("Replacer receiver: " << e.what());
85  }
86 }
std::unique_ptr< udp::UnicastReceiver > unicast_receiver

References unicast_receiver.

Here is the caller graph for this function:

◆ robot_rplcmt_pb_to_robot_state()

RobotState travesim::proto::ReplacerReceiver::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.

Parameters
p_robot_pb_msgPointer the robot replacement protobuf message
Returns
Converted EntityState

Definition at line 100 of file replacer_receiver.cpp.

101  {
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 }

References travesim::EntityState::angular_position, travesim::EntityState::angular_velocity, travesim::RobotState::id, travesim::RobotState::is_yellow, travesim::EntityState::position, travesim::EntityState::velocity, travesim::Vector2D::x, and travesim::Vector2D::y.

Here is the caller graph for this function:

◆ set_receiver_endpoint()

void travesim::proto::ReplacerReceiver::set_receiver_endpoint ( const std::string  receiver_address,
const short  receiver_port 
)

Set the receiver endpoint.

Parameters
receiver_addressReplacer receiver address
receiver_portReplacer receiver port
Note
The unicast addresses must be in the block 127.0.0.0/8, see IANA IPv4 Address Space Registry or the RFC6890 for more informations.

Definition at line 72 of file replacer_receiver.cpp.

72  {
73  this->unicast_receiver->set_receiver_endpoint(receiver_address, receiver_port);
74 }
std::unique_ptr< udp::UnicastReceiver > unicast_receiver

References unicast_receiver.

Here is the caller graph for this function:

Member Data Documentation

◆ unicast_receiver

std::unique_ptr<udp::UnicastReceiver> travesim::proto::ReplacerReceiver::unicast_receiver
private

UDP unicast receiver

Definition at line 100 of file replacer_receiver.hpp.


The documentation for this class was generated from the following files: