TraveSim Adapters  0.1
Protobuf adapters for TraveSim project
teams_sender.cpp
Go to the documentation of this file.
1 /**
2  * @file teams_sender.cpp
3  *
4  * @author Felipe Gomes de Melo <felipe.gomes@thunderatz.org>
5  * @author Lucas Haug <lucas.haug@thunderatz.org>
6  *
7  * @brief ROS teams commands sender class definition
8  *
9  * @date 06/2021
10  *
11  * @copyright MIT License - Copyright (c) 2021 ThundeRatz
12  *
13  */
14 
15 #include <ros/ros.h>
16 #include <std_msgs/Float64.h>
17 
19 
20 /*****************************************
21  * Private Constants
22  *****************************************/
23 
24 #define BUFFER_SIZE 2
25 
26 /*****************************************
27  * Private MAcros
28  *****************************************/
29 
30 #define ROBOT_WHEEL_TOPIC(color, num, side) \
31  "/" color "_team/robot_" + std::to_string(num) + "/" side "_controller/command"
32 
33 /*****************************************
34  * Class Definition
35  *****************************************/
36 
37 namespace travesim {
38 namespace ros_side {
39 TeamsSender::TeamsSender(TeamsFormation teams_formation) : robots_per_team(teams_formation) {
40  ros::NodeHandle nh;
41 
42  this->yellow_pubs = std::vector<robot_command_pub_t>(this->robots_per_team);
43  this->blue_pubs = std::vector<robot_command_pub_t>(this->robots_per_team);
44 
45  for (uint8_t i = 0; i < this->robots_per_team; i++) {
46  this->yellow_pubs[i] = {
47  .left = nh.advertise<std_msgs::Float64>(ROBOT_WHEEL_TOPIC("yellow", i, "left"), BUFFER_SIZE),
48  .right = nh.advertise<std_msgs::Float64>(ROBOT_WHEEL_TOPIC("yellow", i, "right"), BUFFER_SIZE)
49  };
50 
51  this->blue_pubs[i] = {
52  .left = nh.advertise<std_msgs::Float64>(ROBOT_WHEEL_TOPIC("blue", i, "left"), BUFFER_SIZE),
53  .right = nh.advertise<std_msgs::Float64>(ROBOT_WHEEL_TOPIC("blue", i, "right"), BUFFER_SIZE)
54  };
55  }
56 }
57 
58 void TeamsSender::send(TeamCommand* yellow_team_command, TeamCommand* blue_team_command) {
59  std_msgs::Float64 yellow_left_msg, yellow_right_msg;
60  std_msgs::Float64 blue_left_msg, blue_right_msg;
61 
62  for (uint8_t i = 0; i < this->robots_per_team; i++) {
63  yellow_left_msg.data = yellow_team_command->robot_command[i].left_speed;
64  yellow_right_msg.data = yellow_team_command->robot_command[i].right_speed;
65 
66  blue_left_msg.data = blue_team_command->robot_command[i].left_speed;
67  blue_right_msg.data = blue_team_command->robot_command[i].right_speed;
68 
69  this->yellow_pubs[i].left.publish(yellow_left_msg);
70  this->yellow_pubs[i].right.publish(yellow_right_msg);
71 
72  this->blue_pubs[i].left.publish(blue_left_msg);
73  this->blue_pubs[i].right.publish(blue_right_msg);
74  }
75 }
76 } // namespace ros_side
77 } // namespace travesim
std::vector< robot_command_pub_t > yellow_pubs
Yellow team ROS publishers.
std::vector< robot_command_pub_t > blue_pubs
Blue team ROS publishers.
ROS teams commands sender class definition.
std::vector< RobotCommand > robot_command
Data structure to hold the command for a team.
TeamsSender(TeamsFormation teams_formation=TeamsFormation::THREE_ROBOTS_PER_TEAM)
Construct a new Teams Sender object.
void send(TeamCommand *yellow_team_command, TeamCommand *blue_team_command)
Send the stored commands to Gazebo controllers.
#define ROBOT_WHEEL_TOPIC(color, num, side)
TeamsFormation
Formation of the teams.
Definition: data_common.hpp:40
#define BUFFER_SIZE