Algorithm

Algorithm

The algorithm we use consists of a series of actions executed following specific triggers. Let’s go over it in more detail:

  • First, we store the initial angle at which the robot is positioned. This is crucial because our calculations are based on this alignment.

    We know there’s an obstacle in the middle of the arena, so we avoid it from the start with a hardcoded move. This involves turning 30 degrees right or left (chosen randomly to prevent opponents from predicting our strategy) and moving forward for 3 seconds. This time was calibrated for the arena. Note that while moving forward, we align the robot with the angle it started with when advancing, not the initial one, but a local variable in the forward function. The alignment is done by checking our deviation from the initial angle and adjusting the wheel speeds accordingly.

  • Next, we detect an obstacle, typically the wall. We align the robot with the very first angle stored as a global variable (the gyro’s zero).

    As we move forward, we align to keep the robot straight. Upon detecting the green line, we trigger the centering protocol, where the robot centers itself relative to the green line.

    Centering is achieved using sonar to measure distances from both sides to the wall. When the distances are equal, we’re centered.

  • We then move forward, stopping at the flag located in the middle. To avoid touching the wall and dropping the flag when lifting it, we grab the flag, move backward, and then lift it.

  • After a U-turn, we follow the same path as before. The difference is that we don’t trigger the centering protocol at the green line. Instead, we slow down, advance until close to the wall, place the flag down without releasing it, push it to the wall, release it, and then move backward to avoid contact with the flag.

In extreme cases and when handling obstacles:

  • If there’s an obstacle in front of the robot:

    • On the arena’s right side, we move back a bit and turn left.(we detect on which side we are by checking the sign of the angle of the turn when we left the initial position).
    • On the left side, we move back a bit and turn right. This approach works for most obstacles, even with obstacles on three sides (e.g., when following the same route as another robot with a middle obstacle to the left, a wall to the right, and an adversary in front).
  • If there’s an obstacle on the green line during centering, the robot continues forward until the path is clear on both sides, then triggers the centering protocol.

Pseudocode:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Initialize and store initial angle
initial_angle = get_current_angle()

# Start with a hardcoded move to avoid middle obstacle
turn_angle = choose_randomly(30 degrees left, 30 degrees right)
turn_robot(turn_angle)
move_forward(3 seconds)

# Align robot during forward movement
while moving forward:
    current_angle = get_current_angle()
    align_robot_based_on_angle(current_angle)

# Detecting and aligning to wall
if obstacle_detected():
    align_robot_to_angle(initial_angle)

# Move forward and centering protocol
while moving forward:
    if green_line_detected():
        center_robot_on_green_line()
        break

# Centering protocol
function center_robot_on_green_line():
    left_distance = measure_distance_to_wall(left)
    right_distance = measure_distance_to_wall(right)
    while left_distance != right_distance:
        adjust_robot_position()
        left_distance = measure_distance_to_wall(left)
        right_distance = measure_distance_to_wall(right)

# Move to flag and handling flag
move_forward_to_flag()
grab_flag()
move_backward()
lift_flag()

# Return path with U-turn
perform_U_turn()
follow_same_path()

# Different handling at green line
if green_line_detected():
    slow_down()
    move_forward_until_close_to_wall()
    place_flag_down()
    push_flag_to_wall()
    release_flag()
    move_backward_to_avoid_contact()

# Handling extreme cases and obstacles
if obstacle_in_front():
    if on_right_side_of_arena():
        move_backward_slightly()
        turn_left()
    elif on_left_side_of_arena():
        move_backward_slightly()
        turn_right()

# Handling obstacle on green line during centering
if obstacle_on_green_line():
    while not path_clear_on_both_sides():
        move_forward()
    trigger_centering_protocol()

© . Some rights reserved.

Using the Chirpy theme for Jekyll.