Skip to content

Commit

Permalink
Merge pull request #116 from RoboTeamTwente/Update/protobuf
Browse files Browse the repository at this point in the history
Update/protobuf
  • Loading branch information
JornJorn authored Jan 29, 2024
2 parents 4dba94d + e4f75da commit ad51589
Show file tree
Hide file tree
Showing 28 changed files with 547 additions and 477 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ class GameStateManager {
* @param refMsg The data we get from the referee
* @param data The current world
*/
static void setRefereeData(proto::SSL_Referee refMsg, const rtt::world::World* data);
static void setRefereeData(proto::Referee refMsg, const rtt::world::World* data);
/**
* @brief Getter for the referee data
* @return The referee data
*/
static proto::SSL_Referee getRefereeData();
static proto::Referee getRefereeData();
/**
* @brief Converts the referee command to a RefCommand
* @param command The command received from the referee
* @param isYellow Whether our team is the yellow team
* @return RefCommand The corresponding RefCommand for the received referee command
*/
static RefCommand getCommandFromRefMsg(proto::SSL_Referee_Command command, bool isYellow);
static RefCommand getCommandFromRefMsg(proto::Referee_Command command, bool isYellow);
/**
* @brief Getter for the current game state
* @return The current game state
Expand All @@ -52,7 +52,7 @@ class GameStateManager {
static void updateInterfaceGameState(const char* name);

private:
static proto::SSL_Referee refMsg; /**< Data from the referee */
static proto::Referee refMsg; /**< Data from the referee */
static StrategyManager strategymanager; /**< Manager that updates the play according to the game state */
static std::mutex refMsgLock; /**< Synchronizer for referee data */
};
Expand Down
70 changes: 32 additions & 38 deletions roboteam_ai/include/roboteam_ai/utilities/RefCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ enum class RefCommand {
PREPARE_PENALTY_THEM = 7,
DIRECT_FREE_US = 8,
DIRECT_FREE_THEM = 9,
INDIRECT_FREE_US = 10,
INDIRECT_FREE_THEM = 11,
TIMEOUT_US = 12,
TIMEOUT_THEM = 13,
GOAL_US = 14,
Expand All @@ -49,44 +47,40 @@ enum class RefCommand {
UNDEFINED = -1
};

inline RefCommand sslRefCmdToRefCmd(proto::SSL_Referee_Command sslRefCmd, bool isYellow) {
using RefCommandMap = std::unordered_map<proto::SSL_Referee_Command, RefCommand>;
inline RefCommand sslRefCmdToRefCmd(proto::Referee_Command sslRefCmd, bool isYellow) {
using RefCommandMap = std::unordered_map<proto::Referee_Command, RefCommand>;

// static const == they are initialized only once, se we don't allocate on heap every time
static const RefCommandMap yellowMap = {{proto::SSL_Referee_Command_HALT, RefCommand::HALT},
{proto::SSL_Referee_Command_STOP, RefCommand::STOP},
{proto::SSL_Referee_Command_NORMAL_START, RefCommand::NORMAL_START},
{proto::SSL_Referee_Command_FORCE_START, RefCommand::FORCED_START},
{proto::SSL_Referee_Command_PREPARE_KICKOFF_YELLOW, RefCommand::PREPARE_KICKOFF_US},
{proto::SSL_Referee_Command_PREPARE_KICKOFF_BLUE, RefCommand::PREPARE_KICKOFF_THEM},
{proto::SSL_Referee_Command_PREPARE_PENALTY_YELLOW, RefCommand::PREPARE_PENALTY_US},
{proto::SSL_Referee_Command_PREPARE_PENALTY_BLUE, RefCommand::PREPARE_PENALTY_THEM},
{proto::SSL_Referee_Command_DIRECT_FREE_YELLOW, RefCommand::DIRECT_FREE_US},
{proto::SSL_Referee_Command_DIRECT_FREE_BLUE, RefCommand::DIRECT_FREE_THEM},
{proto::SSL_Referee_Command_INDIRECT_FREE_YELLOW, RefCommand::INDIRECT_FREE_US},
{proto::SSL_Referee_Command_INDIRECT_FREE_BLUE, RefCommand::INDIRECT_FREE_THEM},
{proto::SSL_Referee_Command_TIMEOUT_YELLOW, RefCommand::TIMEOUT_US},
{proto::SSL_Referee_Command_TIMEOUT_BLUE, RefCommand::TIMEOUT_THEM},
{proto::SSL_Referee_Command_BALL_PLACEMENT_YELLOW, RefCommand::BALL_PLACEMENT_US},
{proto::SSL_Referee_Command_BALL_PLACEMENT_BLUE, RefCommand::BALL_PLACEMENT_THEM}};
static const RefCommandMap yellowMap = {{proto::Referee_Command_HALT, RefCommand::HALT},
{proto::Referee_Command_STOP, RefCommand::STOP},
{proto::Referee_Command_NORMAL_START, RefCommand::NORMAL_START},
{proto::Referee_Command_FORCE_START, RefCommand::FORCED_START},
{proto::Referee_Command_PREPARE_KICKOFF_YELLOW, RefCommand::PREPARE_KICKOFF_US},
{proto::Referee_Command_PREPARE_KICKOFF_BLUE, RefCommand::PREPARE_KICKOFF_THEM},
{proto::Referee_Command_PREPARE_PENALTY_YELLOW, RefCommand::PREPARE_PENALTY_US},
{proto::Referee_Command_PREPARE_PENALTY_BLUE, RefCommand::PREPARE_PENALTY_THEM},
{proto::Referee_Command_DIRECT_FREE_YELLOW, RefCommand::DIRECT_FREE_US},
{proto::Referee_Command_DIRECT_FREE_BLUE, RefCommand::DIRECT_FREE_THEM},
{proto::Referee_Command_TIMEOUT_YELLOW, RefCommand::TIMEOUT_US},
{proto::Referee_Command_TIMEOUT_BLUE, RefCommand::TIMEOUT_THEM},
{proto::Referee_Command_BALL_PLACEMENT_YELLOW, RefCommand::BALL_PLACEMENT_US},
{proto::Referee_Command_BALL_PLACEMENT_BLUE, RefCommand::BALL_PLACEMENT_THEM}};

// static const == they are initialized only once, se we don't allocate on heap every time
static const RefCommandMap blueMap = {{proto::SSL_Referee_Command_HALT, RefCommand::HALT},
{proto::SSL_Referee_Command_STOP, RefCommand::STOP},
{proto::SSL_Referee_Command_NORMAL_START, RefCommand::NORMAL_START},
{proto::SSL_Referee_Command_FORCE_START, RefCommand::FORCED_START},
{proto::SSL_Referee_Command_PREPARE_KICKOFF_YELLOW, RefCommand::PREPARE_KICKOFF_THEM},
{proto::SSL_Referee_Command_PREPARE_KICKOFF_BLUE, RefCommand::PREPARE_KICKOFF_US},
{proto::SSL_Referee_Command_PREPARE_PENALTY_YELLOW, RefCommand::PREPARE_PENALTY_THEM},
{proto::SSL_Referee_Command_PREPARE_PENALTY_BLUE, RefCommand::PREPARE_PENALTY_US},
{proto::SSL_Referee_Command_DIRECT_FREE_YELLOW, RefCommand::DIRECT_FREE_THEM},
{proto::SSL_Referee_Command_DIRECT_FREE_BLUE, RefCommand::DIRECT_FREE_US},
{proto::SSL_Referee_Command_INDIRECT_FREE_YELLOW, RefCommand::INDIRECT_FREE_THEM},
{proto::SSL_Referee_Command_INDIRECT_FREE_BLUE, RefCommand::INDIRECT_FREE_US},
{proto::SSL_Referee_Command_TIMEOUT_YELLOW, RefCommand::TIMEOUT_THEM},
{proto::SSL_Referee_Command_TIMEOUT_BLUE, RefCommand::TIMEOUT_US},
{proto::SSL_Referee_Command_BALL_PLACEMENT_YELLOW, RefCommand::BALL_PLACEMENT_THEM},
{proto::SSL_Referee_Command_BALL_PLACEMENT_BLUE, RefCommand::BALL_PLACEMENT_US}};
static const RefCommandMap blueMap = {{proto::Referee_Command_HALT, RefCommand::HALT},
{proto::Referee_Command_STOP, RefCommand::STOP},
{proto::Referee_Command_NORMAL_START, RefCommand::NORMAL_START},
{proto::Referee_Command_FORCE_START, RefCommand::FORCED_START},
{proto::Referee_Command_PREPARE_KICKOFF_YELLOW, RefCommand::PREPARE_KICKOFF_THEM},
{proto::Referee_Command_PREPARE_KICKOFF_BLUE, RefCommand::PREPARE_KICKOFF_US},
{proto::Referee_Command_PREPARE_PENALTY_YELLOW, RefCommand::PREPARE_PENALTY_THEM},
{proto::Referee_Command_PREPARE_PENALTY_BLUE, RefCommand::PREPARE_PENALTY_US},
{proto::Referee_Command_DIRECT_FREE_YELLOW, RefCommand::DIRECT_FREE_THEM},
{proto::Referee_Command_DIRECT_FREE_BLUE, RefCommand::DIRECT_FREE_US},
{proto::Referee_Command_TIMEOUT_YELLOW, RefCommand::TIMEOUT_THEM},
{proto::Referee_Command_TIMEOUT_BLUE, RefCommand::TIMEOUT_US},
{proto::Referee_Command_BALL_PLACEMENT_YELLOW, RefCommand::BALL_PLACEMENT_THEM},
{proto::Referee_Command_BALL_PLACEMENT_BLUE, RefCommand::BALL_PLACEMENT_US}};

// .find() != .end() could be replaced by contains in C++20
if (isYellow && yellowMap.find(sslRefCmd) != yellowMap.end()) {
Expand All @@ -100,8 +94,8 @@ inline RefCommand sslRefCmdToRefCmd(proto::SSL_Referee_Command sslRefCmd, bool i
}

std::string refCommandToString(RefCommand command);
std::string protoRefCommandToString(proto::SSL_Referee_Command command);
std::string protoRefStageToString(proto::SSL_Referee_Stage stage);
std::string protoRefCommandToString(proto::Referee_Command command);
std::string protoRefStageToString(proto::Referee_Stage stage);

} // namespace rtt

Expand Down
4 changes: 1 addition & 3 deletions roboteam_ai/include/roboteam_ai/utilities/StrategyManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class StrategyManager {
* @param stage Stage of the game
* @param ballOpt Data about the ball
*/
void setCurrentRefGameState(RefCommand command, proto::SSL_Referee_Stage stage, std::optional<rtt::world::view::BallView> ballOpt);
void setCurrentRefGameState(RefCommand command, proto::Referee_Stage stage, std::optional<rtt::world::view::BallView> ballOpt);

/**
* @brief Forces the AI into a given game state
Expand Down Expand Up @@ -77,11 +77,9 @@ class StrategyManager {
RefGameState(RefCommand::PREPARE_SHOOTOUT_THEM, "penalty_them_prepare", Constants::RULESET_STOP(), false, RefCommand::DEFEND_SHOOTOUT),

RefGameState(RefCommand::DIRECT_FREE_THEM, "free_kick_them", Constants::RULESET_DEFAULT()),
RefGameState(RefCommand::INDIRECT_FREE_THEM, "free_kick_them", Constants::RULESET_DEFAULT()),
RefGameState(RefCommand::NORMAL_START, "normal_play", Constants::RULESET_DEFAULT()),
RefGameState(RefCommand::FORCED_START, "normal_play", Constants::RULESET_DEFAULT()),
RefGameState(RefCommand::DIRECT_FREE_US, "free_kick_us", Constants::RULESET_DEFAULT()),
RefGameState(RefCommand::INDIRECT_FREE_US, "free_kick_us", Constants::RULESET_DEFAULT()),
RefGameState(RefCommand::DO_KICKOFF, "kickoff_us", Constants::RULESET_DEFAULT(), true),
RefGameState(RefCommand::DEFEND_KICKOFF, "kickoff_them", Constants::RULESET_DEFAULT(), true),
RefGameState(RefCommand::DO_PENALTY, "penalty_us", Constants::RULESET_DEFAULT(), true),
Expand Down
2 changes: 1 addition & 1 deletion roboteam_ai/include/roboteam_ai/utilities/normalize.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void rotate(proto::World *world);
* Pointer is asserted in debug mode
* @param refereeData Referee to rotate
*/
void rotate(proto::SSL_Referee *refereeData);
void rotate(proto::Referee *refereeData);

/**
* @brief Rotates all members of a Circular arc
Expand Down
40 changes: 18 additions & 22 deletions roboteam_ai/src/utilities/GameStateManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,52 @@
namespace rtt::ai {
int GameState::cardId = -1;

proto::SSL_Referee GameStateManager::refMsg;
proto::Referee GameStateManager::refMsg;
StrategyManager GameStateManager::strategymanager;
std::mutex GameStateManager::refMsgLock;

proto::SSL_Referee GameStateManager::getRefereeData() {
proto::Referee GameStateManager::getRefereeData() {
std::lock_guard<std::mutex> lock(refMsgLock);
return GameStateManager::refMsg;
}

RefCommand GameStateManager::getCommandFromRefMsg(proto::SSL_Referee_Command command, bool isYellow) {
RefCommand GameStateManager::getCommandFromRefMsg(proto::Referee_Command command, bool isYellow) {
switch (command) {
case proto::SSL_Referee_Command_HALT:
case proto::Referee_Command_HALT:
return RefCommand::HALT;
case proto::SSL_Referee_Command_STOP:
case proto::Referee_Command_STOP:
return RefCommand::STOP;
case proto::SSL_Referee_Command_NORMAL_START:
case proto::Referee_Command_NORMAL_START:
return RefCommand::NORMAL_START;
case proto::SSL_Referee_Command_FORCE_START:
case proto::Referee_Command_FORCE_START:
return RefCommand::FORCED_START;
case proto::SSL_Referee_Command_PREPARE_KICKOFF_YELLOW:
case proto::Referee_Command_PREPARE_KICKOFF_YELLOW:
return isYellow ? RefCommand::PREPARE_KICKOFF_US : RefCommand::PREPARE_KICKOFF_THEM;
case proto::SSL_Referee_Command_PREPARE_KICKOFF_BLUE:
case proto::Referee_Command_PREPARE_KICKOFF_BLUE:
return isYellow ? RefCommand::PREPARE_KICKOFF_THEM : RefCommand::PREPARE_KICKOFF_US;
case proto::SSL_Referee_Command_PREPARE_PENALTY_YELLOW:
case proto::Referee_Command_PREPARE_PENALTY_YELLOW:
return isYellow ? RefCommand::PREPARE_PENALTY_US : RefCommand::PREPARE_PENALTY_THEM;
case proto::SSL_Referee_Command_PREPARE_PENALTY_BLUE:
case proto::Referee_Command_PREPARE_PENALTY_BLUE:
return isYellow ? RefCommand::PREPARE_PENALTY_THEM : RefCommand::PREPARE_PENALTY_US;
case proto::SSL_Referee_Command_DIRECT_FREE_YELLOW:
case proto::Referee_Command_DIRECT_FREE_YELLOW:
return isYellow ? RefCommand::DIRECT_FREE_US : RefCommand::DIRECT_FREE_THEM;
case proto::SSL_Referee_Command_DIRECT_FREE_BLUE:
case proto::Referee_Command_DIRECT_FREE_BLUE:
return isYellow ? RefCommand::DIRECT_FREE_THEM : RefCommand::DIRECT_FREE_US;
case proto::SSL_Referee_Command_INDIRECT_FREE_YELLOW:
return isYellow ? RefCommand::INDIRECT_FREE_US : RefCommand::INDIRECT_FREE_THEM;
case proto::SSL_Referee_Command_INDIRECT_FREE_BLUE:
return isYellow ? RefCommand::INDIRECT_FREE_THEM : RefCommand::INDIRECT_FREE_US;
case proto::SSL_Referee_Command_TIMEOUT_YELLOW:
case proto::Referee_Command_TIMEOUT_YELLOW:
return isYellow ? RefCommand::TIMEOUT_US : RefCommand::TIMEOUT_THEM;
case proto::SSL_Referee_Command_TIMEOUT_BLUE:
case proto::Referee_Command_TIMEOUT_BLUE:
return isYellow ? RefCommand::TIMEOUT_THEM : RefCommand::TIMEOUT_US;
case proto::SSL_Referee_Command_BALL_PLACEMENT_YELLOW:
case proto::Referee_Command_BALL_PLACEMENT_YELLOW:
return isYellow ? RefCommand::BALL_PLACEMENT_US : RefCommand::BALL_PLACEMENT_THEM;
case proto::SSL_Referee_Command_BALL_PLACEMENT_BLUE:
case proto::Referee_Command_BALL_PLACEMENT_BLUE:
return isYellow ? RefCommand::BALL_PLACEMENT_THEM : RefCommand::BALL_PLACEMENT_US;
default:
RTT_ERROR("Unknown refstate, halting all robots for safety!")
return RefCommand::HALT;
}
}

void GameStateManager::setRefereeData(proto::SSL_Referee refMsg, const rtt::world::World* data) {
void GameStateManager::setRefereeData(proto::Referee refMsg, const rtt::world::World* data) {
std::lock_guard<std::mutex> lock(refMsgLock);
GameStateManager::refMsg = refMsg;
bool isYellow = GameSettings::isYellow();
Expand Down
Loading

0 comments on commit ad51589

Please sign in to comment.