forked from ssl-core/ssl-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[behavior-ms] Add behavior world class (#88)
This PR add a world to behavior service processing. World contains: - Allies (RobotMessage) - Enemies (RobotMessage) - Ball (BallMessage) -> get ball with highest confidence - GameStatus (GameStatusMessage)
- Loading branch information
Showing
6 changed files
with
88 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ robocin_cpp_library( | |
SRCS behavior_processor.cpp | ||
DEPS common::output | ||
messages | ||
entities | ||
state_machine | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 51 additions & 18 deletions
69
behavior-ms/behavior-bruxo/behavior/processing/entities/world.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,65 @@ | ||
#include "behavior/processing/entities/world.h" | ||
|
||
namespace behavior { | ||
#include "world.h" | ||
|
||
void World::update(std::optional<DecisionMessage>& decision, | ||
std::optional<std::span<RobotMessage>>& allies, | ||
std::optional<std::span<RobotMessage>>& enemies, | ||
std::optional<BallMessage>& ball, | ||
std::optional<GameStatusMessage>& game_status) { | ||
#include <protocols/common/robot_id.pb.h> | ||
#include <protocols/perception/detection.pb.h> | ||
#include <vector> | ||
|
||
if (decision.has_value()) { | ||
this->decision = std::move(decision.value()); | ||
} | ||
namespace behavior { | ||
|
||
if (allies.has_value()) { | ||
this->allies = allies.value(); | ||
void World::takeBallHighConfidence(const std::vector<protocols::perception::Ball>& balls) { | ||
if (balls.empty()) { | ||
return; | ||
} | ||
|
||
if (enemies.has_value()) { | ||
this->enemies = enemies.value(); | ||
} | ||
auto max_ball | ||
= std::max_element(balls.begin(), balls.end(), [](const auto& ball, const auto& candidate) { | ||
return ball.confidence() < candidate.confidence(); | ||
}); | ||
|
||
if (ball.has_value()) { | ||
this->ball = std::move(ball.value()); | ||
this->ball.fromProto(*max_ball); | ||
} | ||
|
||
void World::takeAlliesAndEnemies(const std::vector<protocols::perception::Robot>& robots) { | ||
if (robots.empty()) { | ||
return; | ||
} | ||
|
||
if (game_status.has_value()) { | ||
this->game_status = std::move(game_status.value()); | ||
allies.clear(); | ||
enemies.clear(); | ||
|
||
for (const auto& robot : robots) { | ||
RobotMessage robot_message; | ||
robot_message.fromProto(robot); | ||
|
||
if (isAlly(robot_message)) { | ||
this->allies.emplace_back(std::move(robot_message)); | ||
} else { | ||
this->enemies.emplace_back(std::move(robot_message)); | ||
} | ||
} | ||
} | ||
|
||
void World::takeDecision(const protocols::decision::Decision& decision) { | ||
this->decision.fromProto(decision); | ||
} | ||
|
||
void World::takeGameStatus(const protocols::referee::GameStatus& game_status) { | ||
this->game_status.fromProto(game_status); | ||
} | ||
|
||
bool World::isAlly(const RobotMessage& robot) const { return robot.robot_id->color == pAllyColor; } | ||
|
||
void World::update(const protocols::decision::Decision& decision, | ||
const std::vector<protocols::perception::Robot>& robots, | ||
const std::vector<protocols::perception::Ball>& balls, | ||
const protocols::referee::GameStatus& game_status) { | ||
|
||
World::takeDecision(decision); | ||
World::takeAlliesAndEnemies(robots); | ||
World::takeBallHighConfidence(balls); | ||
World::takeGameStatus(game_status); | ||
} | ||
|
||
} // namespace behavior |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters