Skip to content

Commit

Permalink
Has Open Shot (#2178)
Browse files Browse the repository at this point in the history
Co-authored-by: sanatd33 <[email protected]>
  • Loading branch information
sanatd33 and sanatd33 authored Feb 1, 2024
1 parent 7dc2b0f commit 3748541
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
48 changes: 47 additions & 1 deletion soccer/src/soccer/strategy/agent/position/offense.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Offense::State Offense::update_state() {

std::optional<RobotIntent> Offense::state_to_task(RobotIntent intent) {
float dist{0.0f};
SPDLOG_INFO(current_state_);
// SPDLOG_INFO(current_state_);
if (current_state_ == IDLING) {
// Do nothing
auto empty_motion_cmd = planning::MotionCommand{};
Expand Down Expand Up @@ -215,6 +215,52 @@ std::optional<RobotIntent> Offense::state_to_task(RobotIntent intent) {
return std::nullopt;
}

bool Offense::has_open_shot() {
// Goal location
rj_geometry::Point their_goal_pos = field_dimensions_.their_goal_loc();
double goal_width = field_dimensions_.goal_width(); // 1.0 meters

// Ball location
rj_geometry::Point ball_position = this->last_world_state_->ball.position;

double best_distance = -1.0;
rj_geometry::Point increment(0.05, 0);
rj_geometry::Point curr_point =
their_goal_pos - rj_geometry::Point(goal_width / 2.0, 0) + increment;
for (int i = 0; i < 19; i++) {
double distance = distance_from_their_robots(ball_position, curr_point);
if (distance > best_distance) {
best_distance = distance;
}
curr_point = curr_point + increment;
}

return best_distance > max_receive_distance;
}

double Offense::distance_from_their_robots(rj_geometry::Point tail, rj_geometry::Point head) {
rj_geometry::Point vec = head - tail;
auto their_robots = this->last_world_state_->their_robots;
double min_angle = -0.5;
for (auto enemy : their_robots) {
rj_geometry::Point enemy_vec = enemy.pose.position() - tail;
if (enemy_vec.dot(vec) < 0) {
continue;
}
auto projection = (enemy_vec.dot(vec) / vec.dot(vec));
enemy_vec = enemy_vec - (projection)*vec;
double distance = enemy_vec.mag();
if (distance < (kRobotRadius + kBallRadius)) {
return -1.0;
}
double angle = distance / projection;
if ((min_angle < 0) || (angle < min_angle)) {
min_angle = angle;
}
}
return min_angle;
}

void Offense::receive_communication_response(communication::AgentPosResponseWrapper response) {
Position::receive_communication_response(response);

Expand Down
4 changes: 4 additions & 0 deletions soccer/src/soccer/strategy/agent/position/offense.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ class Offense : public Position {
bool scorer_ = false;
bool last_scorer_ = false;

bool has_open_shot();

double distance_from_their_robots(rj_geometry::Point tail, rj_geometry::Point head);

/**
* @brief Send request to the other robots to see if this robot should be the scorer
*
Expand Down

0 comments on commit 3748541

Please sign in to comment.