Exemplo n.º 1
0
    def run(self, drone: CarObject, agent: MyHivemind):
        car_to_ball, distance = (agent.ball.location - drone.location).normalize(True)
        ball_to_target = (self.target - agent.ball.location).normalize()

        relative_velocity = car_to_ball.dot(drone.velocity - agent.ball.velocity)
        if relative_velocity != 0.0:
            eta = cap(distance / cap(relative_velocity, 400, 2300), 0.0, 1.5)
        else:
            eta = 1.5

        # If we are approaching the ball from the wrong side the car will try to only hit the very edge of the ball
        left_vector = car_to_ball.cross((0, 0, 1))
        right_vector = car_to_ball.cross((0, 0, -1))
        target_vector = -ball_to_target.clamp(left_vector, right_vector)
        final_target = agent.ball.location + (target_vector * (distance / 2))

        # Some adjustment to the final target to ensure we don't try to drive through any goalposts to reach it
        if abs(drone.location[1]) > 5150:
            final_target[0] = cap(final_target[0], -750, 750)

        agent.line(final_target - Vector3(0, 0, 100), final_target + Vector3(0, 0, 100), [255, 255, 255])

        angles = defaultPD(drone, drone.local(final_target - drone.location))
        defaultThrottle(drone, 2300 if distance > 1600 else 2300 - cap(1600 * abs(angles[1]), 0, 2050))
        drone.controller.boost = False if drone.airborne or abs(angles[1]) > 0.3 else drone.controller.boost
        drone.controller.handbrake = True if abs(angles[1]) > 2.3 else drone.controller.handbrake

        if abs(angles[1]) < 0.05 and (eta < 0.45 or distance < 150):
            drone.pop()
            drone.push(Flip(drone.local(car_to_ball)))
Exemplo n.º 2
0
 def run(self, drone: CarObject, agent: MyHivemind):
     target = agent.ball.location + Vector3(0, 200 * agent.side(), 0)
     local_target = drone.local(target - drone.location)
     defaultPD(drone, local_target)
     defaultThrottle(drone, 2300)
     if local_target.magnitude() < 650:
         drone.pop()
         drone.push(Flip(drone.local(agent.foe_goal.location - drone.location)))
Exemplo n.º 3
0
 def run(self, drone: CarObject, agent: MyHivemind):
     target = Vector3(0, 3116 * agent.side(), 0)
     local_target = drone.local(target - drone.location)
     defaultPD(drone, local_target)
     defaultThrottle(drone, 2300)
     if local_target.magnitude() < 400:
         drone.pop()
         drone.push(DiagonalKickoff())
         drone.push(Flip(drone.local(agent.ball.location - drone.location)))
Exemplo n.º 4
0
def defaultPD(drone: CarObject,
              local_target: Vector3,
              upside_down=False,
              up=None) -> []:
    # points the car towards a given local target.
    # Direction can be changed to allow the car to steer towards a target while driving backwards

    if up is None:
        up = drone.local(
            Vector3(0, 0, -1) if upside_down else Vector3(
                0, 0, 1))  # where "up" is in local coordinates
    target_angles = (
        math.atan2(local_target[2],
                   local_target[0]),  # angle required to pitch towards target
        math.atan2(local_target[1],
                   local_target[0]),  # angle required to yaw towards target
        math.atan2(up[1], up[2])  # angle required to roll upright
    )
    # Once we have the angles we need to rotate, we feed them into PD loops to determining the controller inputs
    drone.controller.steer = steerPD(target_angles[1], 0)
    drone.controller.pitch = steerPD(target_angles[0],
                                     drone.angular_velocity[1] / 4)
    drone.controller.yaw = steerPD(target_angles[1],
                                   -drone.angular_velocity[2] / 4)
    drone.controller.roll = steerPD(target_angles[2],
                                    drone.angular_velocity[0] / 4)
    # Returns the angles, which can be useful for other purposes
    return target_angles
Exemplo n.º 5
0
 def run(self, drone: CarObject, agent: MyHivemind):
     if self.time == -1:
         elapsed = 0
         self.time = agent.time
     else:
         elapsed = agent.time - self.time
     if elapsed >= self.duration:
         drone.pop()
         drone.push(Flip(drone.local(agent.ball.location - drone.location)))
Exemplo n.º 6
0
def defaultThrottle(drone: CarObject,
                    target_speed: float,
                    direction: float = 1.0) -> float:
    # accelerates the car to a desired speed using throttle and boost
    car_speed = drone.local(drone.velocity)[0]
    t = (target_speed * direction) - car_speed
    drone.controller.throttle = cap((t**2) * sign(t) / 1000, -1.0, 1.0)
    drone.controller.boost = True if t > 150 and car_speed < 2275 and drone.controller.throttle == 1.0 else False
    return car_speed
Exemplo n.º 7
0
    def run(self, drone: CarObject, agent: MyHivemind):
        if self.target is not None:
            if self.target_local:
                local_target = self.target
            else:
                local_target = drone.local((self.target - drone.location).flatten())
        else:
            local_target = drone.local(drone.velocity.flatten())

        defaultPD(drone, local_target)
        drone.controller.throttle = 1
        t = (-drone.velocity.z - (
                drone.velocity.z ** 2 + 2 * -650 * -(max(drone.location.z - 17.01, 0.01))) ** 0.5) / -650
        if self.target is not None:
            robbies_constant = (self.target.normalize() * t * 2200 - drone.velocity * t) * 2 * t ** -2
        else:
            robbies_constant = (drone.velocity.normalize() * t * 2200 - drone.velocity * t) * 2 * t ** -2
        agent.line(drone.location, robbies_constant, color=[255, 255, 255])
        robbies_boost_constant = drone.forward.normalize().dot(robbies_constant.normalize()) > 0.5
        drone.controller.boost = robbies_boost_constant and self.boost and not drone.supersonic
        if not drone.airborne:
            drone.pop()
Exemplo n.º 8
0
    def run(self, drone: CarObject, agent: MyHivemind):
        if self.boost is None:
            drone.pop()
            return
        car_to_boost = self.boost.location - drone.location
        distance_remaining = car_to_boost.flatten().magnitude()

        agent.line(self.boost.location - Vector3(0, 0, 500), self.boost.location + Vector3(0, 0, 500), [0, 255, 0])

        if self.target is not None:
            vector = (self.target - self.boost.location).normalize()
            side_of_vector = sign(vector.cross((0, 0, 1)).dot(car_to_boost))
            car_to_boost_perp = car_to_boost.cross((0, 0, side_of_vector)).normalize()
            adjustment = car_to_boost.angle2D(vector) * distance_remaining / 3.14
            final_target = self.boost.location + (car_to_boost_perp * adjustment)
            car_to_target = (self.target - drone.location).magnitude()
        else:
            adjustment = 9999
            car_to_target = 0
            final_target = self.boost.location

        # Some adjustment to the final target to ensure it's inside the field and
        # we don't try to dirve through any goalposts to reach it
        if abs(drone.location[1]) > 5150:
            final_target[0] = cap(final_target[0], -750, 750)

        local_target = drone.local(final_target - drone.location)

        angles = defaultPD(drone, local_target)
        defaultThrottle(drone, 2300)

        drone.controller.boost = self.boost.large if abs(angles[1]) < 0.3 else False
        drone.controller.handbrake = True if abs(angles[1]) > 2.3 else drone.controller.handbrake

        velocity = 1 + drone.velocity.magnitude()
        if not self.boost.active or drone.boost >= 99.0 or distance_remaining < 350:
            drone.pop()
        elif drone.airborne:
            drone.push(Recovery(self.target))
        elif abs(angles[1]) < 0.05 and 600 < velocity < 2150 and (
                distance_remaining / velocity > 2.0 or (adjustment < 90 and car_to_target / velocity > 2.0)):
            drone.push(Flip(local_target))
Exemplo n.º 9
0
    def run(self, drone: CarObject, agent: MyHivemind):
        target = agent.friend_goal.location + (agent.ball.location - agent.friend_goal.location) / 2
        car_to_target = target - drone.location
        distance_remaining = car_to_target.flatten().magnitude()

        agent.line(target - Vector3(0, 0, 500), target + Vector3(0, 0, 500), [255, 0, 255])

        if self.vector is not None:
            # See commends for adjustment in jump_shot or aerial for explanation
            side_of_vector = sign(self.vector.cross((0, 0, 1)).dot(car_to_target))
            car_to_target_perp = car_to_target.cross((0, 0, side_of_vector)).normalize()
            adjustment = car_to_target.angle(self.vector) * distance_remaining / 3.14
            final_target = target + (car_to_target_perp * adjustment)
        else:
            final_target = target

        # Some adjustment to the final target to ensure it's inside the field and
        # we don't try to drive through any goalposts to reach it
        if abs(drone.location[1]) > 5150:
            final_target[0] = cap(final_target[0], -750, 750)

        local_target = drone.local(final_target - drone.location)

        angles = defaultPD(drone, local_target, self.direction)
        defaultThrottle(drone, 2300, self.direction)

        drone.controller.boost = False
        drone.controller.handbrake = True if abs(angles[1]) > 2.3 else drone.controller.handbrake

        velocity = 1 + drone.velocity.magnitude()
        if distance_remaining < 350:
            drone.pop()
        elif abs(angles[1]) < 0.05 and 600 < velocity < 2150 and distance_remaining / velocity > 2.0:
            drone.push(Flip(local_target))
        # TODO Halfflip
        # elif abs(angles[1]) > 2.8 and velocity < 200:
        #     agent.push(flip(local_target, True))
        elif drone.airborne:
            drone.push(Recovery(target))
Exemplo n.º 10
0
def push_shot(drone: CarObject, agent: MyHivemind):
    left = Vector3(4200 * -agent.side(), agent.side() * 5120, 0)
    right = Vector3(4200 * agent.side(), agent.side() * 5120, 0)
    targets = {"goal": (agent.foe_goal.left_post, agent.foe_goal.right_post)}
    if not agent.conceding:
        drones = copy(agent.drones)
        drones.remove(drone)
        team = agent.friends + drones
        for teammate in team:
            a = teammate.location
            b = teammate.location + 2000 * teammate.forward
            local_a = drone.local(a)
            angle_a = math.atan2(local_a.y, local_a.x)
            if angle_a > 0:
                targets["teammate" + str(team.index(teammate))] = (b, a)
            else:
                targets["teammate" + str(team.index(teammate))] = (a, b)
    targets["upfield"] = (left, right)
    shots = find_hits(drone, agent, targets)
    if len(shots["goal"]) > 0:
        drone.clear()
        drone.push(shots["goal"][0])
        drone.action = Action.Going
    elif shots.get("teammate0") is not None and len(
            shots.get("teammate0")) > 0:
        drone.clear()
        drone.push(shots["teammate0"][0])
        drone.action = Action.Going
    elif shots.get("teammate1") is not None and len(
            shots.get("teammate1")) > 0:
        drone.clear()
        drone.push(shots["teammate1"][0])
        drone.action = Action.Going
    elif len(shots["upfield"]) > 0:
        drone.clear()
        drone.push(shots["upfield"][0])
        drone.action = Action.Going
Exemplo n.º 11
0
    def run(self, drone: CarObject, agent: MyHivemind):
        raw_time_remaining = self.intercept_time - agent.time
        # Capping raw_time_remaining above 0 to prevent division problems
        time_remaining = cap(raw_time_remaining, 0.01, 10.0)

        car_to_ball = self.ball_location - drone.location
        # whether we are to the left or right of the shot vector
        side_of_shot = sign(self.shot_vector.cross((0, 0, 1)).dot(car_to_ball))

        car_to_intercept = self.intercept - drone.location
        car_to_intercept_perp = car_to_intercept.cross((0, 0, side_of_shot))  # perpendicular
        distance_remaining = car_to_intercept.flatten().magnitude()

        speed_required = distance_remaining / time_remaining
        # When still on the ground we pretend gravity doesn't exist, for better or worse
        acceleration_required = backsolve(self.intercept, drone, time_remaining, 0 if self.jump_time == 0 else 325)
        local_acceleration_required = drone.local(acceleration_required)

        # The adjustment causes the car to circle around the dodge point in an effort to line up with the shot vector
        # The adjustment slowly decreases to 0 as the bot nears the time to jump
        adjustment = car_to_intercept.angle(self.shot_vector) * distance_remaining / 1.57  # size of adjustment
        adjustment *= (cap(self.jump_threshold - (acceleration_required[2]), 0.0,
                           self.jump_threshold) / self.jump_threshold)  # factoring in how close to jump we are
        # we don't adjust the final target if we are already jumping
        final_target = self.intercept + ((car_to_intercept_perp.normalize() * adjustment) if self.jump_time == 0 else 0)

        # Some extra adjustment to the final target to ensure it's inside the field and
        # we don't try to drive through any goalposts to reach it
        if abs(drone.location[1] > 5150):
            final_target[0] = cap(final_target[0], -750, 750)

        local_final_target = drone.local(final_target - drone.location)

        # drawing debug lines to show the dodge point and final target (which differs due to the adjustment)
        agent.line(drone.location, self.intercept)
        agent.line(self.intercept - Vector3(0, 0, 100), self.intercept + Vector3(0, 0, 100), [255, 0, 0])
        agent.line(final_target - Vector3(0, 0, 100), final_target + Vector3(0, 0, 100), [0, 255, 0])

        angles = defaultPD(drone, local_final_target)

        if self.jump_time == 0:
            defaultThrottle(drone, speed_required)
            drone.controller.boost = False if abs(angles[1]) > 0.3 or drone.airborne else drone.controller.boost
            drone.controller.handbrake = True if abs(angles[1]) > 2.3 else drone.controller.handbrake
            if acceleration_required[2] > self.jump_threshold:
                # Switch into the jump when the upward acceleration required reaches our threshold,
                # hopefully we have aligned already...
                self.jump_time = agent.time
        else:
            time_since_jump = agent.time - self.jump_time

            # While airborne we boost if we're within 30 degrees of our local acceleration requirement
            if drone.airborne and local_acceleration_required.magnitude() * time_remaining > 100:
                angles = defaultPD(drone, local_acceleration_required)
                if abs(angles[0]) + abs(angles[1]) < 0.5:
                    drone.controller.boost = True
            if self.counter == 0 and (time_since_jump <= 0.2 and local_acceleration_required[2] > 0):
                # hold the jump button up to 0.2 seconds to get the most acceleration from the first jump
                drone.controller.jump = True
            elif time_since_jump > 0.2 and self.counter < 3:
                # Release the jump button for 3 ticks
                drone.controller.jump = False
                self.counter += 1
            elif local_acceleration_required[2] > 300 and self.counter == 3:
                # the acceleration from the second jump is instant, so we only do it for 1 frame
                drone.controller.jump = True
                drone.controller.pitch = 0
                drone.controller.yaw = 0
                drone.controller.roll = 0
                self.counter += 1

        if raw_time_remaining < -0.25 or not shot_valid(agent, self):
            drone.pop()
            drone.push(Recovery())
Exemplo n.º 12
0
    def run(self, drone: CarObject, agent: MyHivemind):
        raw_time_remaining = self.intercept_time - agent.time
        # Capping raw_time_remaining above 0 to prevent division problems
        time_remaining = cap(raw_time_remaining, 0.001, 10.0)
        car_to_ball = self.ball_location - drone.location
        # whether we are to the left or right of the shot vector
        side_of_shot = sign(self.shot_vector.cross((0, 0, 1)).dot(car_to_ball))

        car_to_dodge_point = self.dodge_point - drone.location
        car_to_dodge_perp = car_to_dodge_point.cross((0, 0, side_of_shot))  # perpendicular
        distance_remaining = car_to_dodge_point.magnitude()

        speed_required = distance_remaining / time_remaining
        acceleration_required = backsolve(self.dodge_point, drone, time_remaining, 0 if not self.jumping else 650)
        local_acceleration_required = drone.local(acceleration_required)

        # The adjustment causes the car to circle around the dodge point in an effort to line up with the shot vector
        # The adjustment slowly decreases to 0 as the bot nears the time to jump
        adjustment = car_to_dodge_point.angle(self.shot_vector) * distance_remaining / 2.0  # size of adjustment
        adjustment *= (cap(self.jump_threshold - (acceleration_required[2]), 0.0,
                           self.jump_threshold) / self.jump_threshold)  # factoring in how close to jump we are
        # we don't adjust the final target if we are already jumping
        final_target = self.dodge_point + (
            (car_to_dodge_perp.normalize() * adjustment) if not self.jumping else 0) + Vector3(0, 0, 50)
        # Ensuring our target isn't too close to the sides of the field,
        # where our car would get messed up by the radius of the curves

        # Some adjustment to the final target to ensure it's inside the field and
        # we don't try to dirve through any goalposts to reach it
        if abs(drone.location[1]) > 5150:
            final_target[0] = cap(final_target[0], -750, 750)

        local_final_target = drone.local(final_target - drone.location)

        # drawing debug lines to show the dodge point and final target (which differs due to the adjustment)
        agent.line(drone.location, self.dodge_point)
        agent.line(self.dodge_point - Vector3(0, 0, 100), self.dodge_point + Vector3(0, 0, 100), [255, 0, 0])
        agent.line(final_target - Vector3(0, 0, 100), final_target + Vector3(0, 0, 100), [0, 255, 0])

        # Calling our drive utils to get us going towards the final target
        angles = defaultPD(drone, local_final_target, self.direction)
        defaultThrottle(drone, speed_required, self.direction)

        agent.line(drone.location, drone.location + (self.shot_vector * 200), [255, 255, 255])

        drone.controller.boost = False if abs(angles[1]) > 0.3 or drone.airborne else drone.controller.boost
        drone.controller.handbrake = True if abs(
            angles[1]) > 2.3 and self.direction == 1 else drone.controller.handbrake

        if not self.jumping:
            if raw_time_remaining <= 0.0 or (speed_required - 2300) * time_remaining > 45 or not shot_valid(agent,
                                                                                                            self):
                # If we're out of time or not fast enough to be within 45 units of target at the intercept time, we pop
                drone.pop()
                if drone.airborne:
                    drone.push(Recovery())
            elif local_acceleration_required[2] > self.jump_threshold \
                    and local_acceleration_required[2] > local_acceleration_required.flatten().magnitude():
                # Switch into the jump when the upward acceleration required reaches our threshold,
                # and our lateral acceleration is negligible
                self.jumping = True
        else:
            if (raw_time_remaining > 0.2 and not shot_valid(agent, self, 150)) or raw_time_remaining <= -0.9 or (
                    not drone.airborne and self.counter > 0):
                drone.pop()
                drone.push(Recovery())
            elif self.counter == 0 and local_acceleration_required[2] > 0.0 and raw_time_remaining > 0.083:
                # Initial jump to get airborne + we hold the jump button for extra power as required
                drone.controller.jump = True
            elif self.counter < 3:
                # make sure we aren't jumping for at least 3 frames
                drone.controller.jump = False
                self.counter += 1
            elif 0.1 >= raw_time_remaining > -0.9:
                # dodge in the direction of the shot_vector
                drone.controller.jump = True
                if not self.dodging:
                    vector = drone.local(self.shot_vector)
                    self.p = abs(vector[0]) * -sign(vector[0])
                    self.y = abs(vector[1]) * sign(vector[1]) * self.direction
                    self.dodging = True
                # simulating a deadzone so that the dodge is more natural
                drone.controller.pitch = self.p if abs(self.p) > 0.2 else 0
                drone.controller.yaw = self.y if abs(self.y) > 0.3 else 0
Exemplo n.º 13
0
 def run(self, drone: CarObject, agent: MyHivemind):
     relative_target = agent.ball.location - drone.location
     local_target = drone.local(relative_target)
     defaultPD(drone, local_target)
     defaultThrottle(drone, 2300)
Exemplo n.º 14
0
    def run(self, drone: CarObject, agent: MyHivemind):
        if self.time == -1:
            elapsed = 0
            self.time = agent.time
        else:
            elapsed = agent.time - self.time
        T = self.intercept_time - agent.time
        xf = drone.location + drone.velocity * T + 0.5 * gravity * T ** 2
        vf = drone.velocity + gravity * T
        if self.jumping:
            if self.jump_time == -1:
                jump_elapsed = 0
                self.jump_time = agent.time
            else:
                jump_elapsed = agent.time - self.jump_time
            tau = jump_max_duration - jump_elapsed
            if jump_elapsed == 0:
                vf += drone.up * jump_speed
                xf += drone.up * jump_speed * T

            vf += drone.up * jump_acc * tau
            xf += drone.up * jump_acc * tau * (T - 0.5 * tau)

            vf += drone.up * jump_speed
            xf += drone.up * jump_speed * (T - tau)

            if jump_elapsed < jump_max_duration:
                drone.controller.jump = True
            elif elapsed >= jump_max_duration and self.counter < 3:
                drone.controller.jump = False
                self.counter += 1
            elif elapsed < 0.3:
                drone.controller.jump = True
            else:
                self.jumping = jump_elapsed <= 0.3
        else:
            drone.controller.jump = 0

        delta_x = self.ball_location - xf
        direction = delta_x.normalize()
        if delta_x.magnitude() > 50:
            defaultPD(drone, drone.local(delta_x))
        else:
            if self.target is not None:
                defaultPD(drone, drone.local(self.target))
            else:
                defaultPD(drone, drone.local(self.ball_location - drone.location))

        if jump_max_duration <= elapsed < 0.3 and self.counter == 3:
            drone.controller.roll = 0
            drone.controller.pitch = 0
            drone.controller.yaw = 0
            drone.controller.steer = 0

        if drone.forward.angle3D(direction) < 0.3:
            if delta_x.magnitude() > 50:
                drone.controller.boost = 1
                drone.controller.throttle = 0
            else:
                drone.controller.boost = 0
                drone.controller.throttle = cap(0.5 * throttle_accel * T ** 2, 0, 1)
        else:
            drone.controller.boost = 0
            drone.controller.throttle = 0

        if T <= 0 or not shot_valid(agent, self, threshold=150):
            drone.pop()
            drone.push(Recovery(agent.friend_goal.location))