Skip to content

Commit 7e9d289

Browse files
authored
Zane kaber/movement speed (UBC-Thunderbots#3473)
1 parent 9b39930 commit 7e9d289

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

src/software/ai/hl/stp/tactic/goalie/goalie_fsm.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "software/ai/hl/stp/tactic/move_primitive.h"
55
#include "software/math/math_functions.h"
66

7+
78
Point GoalieFSM::getGoaliePositionToBlock(
89
const Ball &ball, const Field &field,
910
TbotsProto::GoalieTacticConfig goalie_tactic_config)
@@ -182,6 +183,55 @@ void GoalieFSM::panic(const Update &event)
182183
Angle goalie_orientation =
183184
(event.common.world_ptr->ball().position() - goalie_pos).orientation();
184185

186+
Point save_pos = goalie_pos;
187+
// First we are trying to determine how much time do we have to reach the ball
188+
Duration ball_intercept_time = Duration::fromSeconds(
189+
(event.common.world_ptr->ball().position() - save_pos).length() /
190+
(std::max(std::numeric_limits<double>::epsilon(),
191+
event.common.world_ptr->ball().velocity().length())));
192+
193+
if (event.common.robot.getTimeToPosition(goalie_pos) > ball_intercept_time)
194+
{
195+
Point new_destination = save_pos;
196+
double final_speed = GOALIE_STEP_SPEED_M_PER_S;
197+
bool finished = false;
198+
double max_speed = event.common.robot.robotConstants().robot_max_speed_m_per_s;
199+
double max_acc =
200+
event.common.robot.robotConstants().robot_max_acceleration_m_per_s_2;
201+
while (!finished)
202+
{
203+
Vector final_velocity =
204+
(new_destination - event.common.robot.position()).normalize(final_speed);
205+
206+
// What's happening here is we are using v_o^2=2*d*a to determine the extra
207+
// distance the goalie will be forced to travel if they intercept the ball
208+
// with final_speed and then immediately decelerate
209+
double extra_length = (final_speed * final_speed) / (2.0 * max_acc);
210+
new_destination = save_pos + final_velocity.normalize(extra_length);
211+
if (event.common.world_ptr->field().pointInFriendlyDefenseArea(
212+
new_destination))
213+
{
214+
goalie_pos = new_destination;
215+
if (event.common.robot.getTimeToPosition(save_pos, final_velocity) <
216+
ball_intercept_time)
217+
{
218+
// If we found that the robot can make it before the ball we consider
219+
// the while loop finished
220+
finished = true;
221+
}
222+
}
223+
else
224+
{
225+
finished = true;
226+
}
227+
final_speed += GOALIE_STEP_SPEED_M_PER_S;
228+
if (final_speed > max_speed)
229+
{
230+
finished = true;
231+
}
232+
}
233+
}
234+
185235
event.common.set_primitive(std::make_unique<MovePrimitive>(
186236
event.common.robot, goalie_pos, goalie_orientation, max_allowed_speed_mode,
187237
TbotsProto::ObstacleAvoidanceMode::AGGRESSIVE, TbotsProto::DribblerMode::OFF,

src/software/ai/hl/stp/tactic/goalie/goalie_fsm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ struct GoalieFSM
253253

254254
private:
255255
static constexpr double BALL_RETRIEVED_THRESHOLD = 0.2;
256+
// The step amount between speeds we check that the goalie is observed to
257+
// go at during the save
258+
static constexpr double GOALIE_STEP_SPEED_M_PER_S = 0.2;
256259
// The goalie tactic config
257260
TbotsProto::GoalieTacticConfig goalie_tactic_config;
258261
// Configuration values for inflated obstacles

src/software/ai/hl/stp/tactic/goalie/goalie_tactic_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@
2929
),
3030
# test ball very fast misses net
3131
(tbots_cpp.Point(0, 0), tbots_cpp.Vector(-5, 1), tbots_cpp.Point(-4.5, 0)),
32+
# test ball very fast get saved
33+
(
34+
tbots_cpp.Point(-2.5, 0),
35+
tbots_cpp.Vector(-4.8, 1.1),
36+
tbots_cpp.Point(-4.5, 0),
37+
),
38+
# test ball very fast with the goalie out of position saved
39+
(
40+
tbots_cpp.Point(-2, 0),
41+
tbots_cpp.Vector(-5.5, 1),
42+
tbots_cpp.Point(-4.5, -0.1),
43+
),
3244
# ball slow inside friendly defense area
3345
(tbots_cpp.Point(-4, 0.8), tbots_cpp.Vector(-0.2, 0), tbots_cpp.Point(0, 0)),
3446
# ball slow inside friendly defense area

0 commit comments

Comments
 (0)