def perform(self, comms): rot_angle = utils.defender_get_rotation_to_catch_point( Vector(self.robot.x, self.robot.y, self.robot.angle, 0), utils.get_defence_point(self.world), 0)[0] logging.info("Facing direction to move to defence area. Rotating %f degrees.", math.degrees(rot_angle)) comms.turn(rot_angle) return utils.defender_turn_delay(rot_angle)
class GoToStaticBall(Action): ''' Move defender to the ball when static ''' preconditions = [(lambda w, r: abs( utils.defender_get_rotation_to_catch_point( Vector(r.x, r.y, r.angle, 0), Vector(w.ball.x, w.ball.y, 0, 0), r. catch_distance)[0]) < ROTATION_THRESHOLD, "Defender is facing ball"), (lambda w, r: r.catcher == 'OPEN', "Grabbers are open."), (lambda w, r: utils.ball_is_static(w), "Ball is static.")] def perform(self, comms): # reset dynamic threshold # TODO: has to be restarted everywhere! global ROTATION_THRESHOLD ROTATION_THRESHOLD = 0.1 dx = self.world.ball.x - self.robot.x dy = self.world.ball.y - self.robot.y displacement = math.hypot(dx, dy) if displacement == 0: alpha = 0 else: # TODO: has to handle this edge case better try: alpha = math.degrees( math.asin(self.robot.catch_distance / float(displacement))) except ValueError as er: print(er) x = self.world.ball.x y = self.world.ball.y angle = utils.get_rotation_to_point(self.robot.vector, Vector(x, y, 0, 0)) logging.info("Wants to close-rotate by: " + str(angle)) comms.turn(angle) return 1 # TODO: NO FIXED DELAY beta = 90 - alpha if math.sin(math.radians(alpha)) == 0: distance_to_move = 15 # something is obviously wrong so we have to move a bit else: distance_to_move = math.sin( math.radians(beta)) * self.robot.catch_distance / math.sin( math.radians(alpha)) # Find the movement side angle, side = utils.defender_get_rotation_to_catch_point( Vector(self.robot.x, self.robot.y, self.robot.angle, 0), Vector(self.world.ball.x, self.world.ball.y, 0, 0), self.robot.catch_distance) if side == "left": distance_to_move = -distance_to_move logging.info("Wants to move by: " + str(distance_to_move)) comms.move(distance_to_move) return utils.defender_move_delay(distance_to_move)
class KickToScoreZone(Action): ''' Pass ball to our attacker's score zone ''' preconditions = [(lambda w, r: abs(r=utils.defender_get_rotation_to_catch_point(Vector(r.x, r.y, r.angle, 0)[0], Vector(w.score_zone.x, w.score_zone.y, 0, 0), r.catch_distance)) < FACING_ROTATION_THRESHOLD, "Defender is facing attacker's score zone"), (lambda w, r: r.has_ball(w.ball), "Defender has ball")] def perform(self, comms): raise NotImplementedError
class MoveToDefenceArea(Action): preconditions = [(lambda w, r: abs(utils.defender_get_rotation_to_catch_point(Vector(r.x, r.y, r.angle, 0), utils.get_defence_point(w), 0)[0]) < ROTATION_THRESHOLD, "Aligned to move to defence point.")] def perform(self, comms): dist = utils.dist(self.robot, utils.get_defence_point(self.world)) dist *= utils.get_movement_direction_from_vector(self.robot, utils.get_defence_point(self.world)) logging.info("Moving to defence area. Moving %f right.", dist) comms.move(dist) return utils.defender_move_delay(abs(dist))
def perform(self, comms): global ROTATION_THRESHOLD logging.info('---- ' + str(ROTATION_THRESHOLD)) x = self.world.ball.x y = self.world.ball.y angle = utils.defender_get_rotation_to_catch_point(Vector(self.robot.x, self.robot.y, self.robot.angle, 0), Vector(x, y, 0, 0), self.robot.catch_distance)[0] logging.info("Wants to rotate by: " + str(angle)) comms.turn(angle) if ROTATION_THRESHOLD < 0.30: ROTATION_THRESHOLD += 0.07 return utils.defender_turn_delay(angle)
def perform(self, comms): logging.info('---- ' + str(ROTATION_THRESHOLD)) x = self.world.ball.x y = self.world.ball.y angle = utils.defender_get_rotation_to_catch_point(Vector(self.robot.x, self.robot.y, self.robot.angle, 0), Vector(x, y, 0, 0), self.robot.catch_distance)[0] logging.info("Wants to rotate by: " + str(angle)) comms.turn(angle) global ROTATION_THRESHOLD if ROTATION_THRESHOLD < 0.30: ROTATION_THRESHOLD += 0.07 return utils.defender_turn_delay(angle)
def perform(self, comms): # reset dynamic threshold # TODO: has to be restarted everywhere! global ROTATION_THRESHOLD ROTATION_THRESHOLD = 0.1 dx = self.world.ball.x - self.robot.x dy = self.world.ball.y - self.robot.y displacement = math.hypot(dx, dy) if displacement == 0: alpha = 0 else: # TODO: has to handle this edge case better try: alpha = math.degrees( math.asin(self.robot.catch_distance / float(displacement))) except ValueError as er: print(er) x = self.world.ball.x y = self.world.ball.y angle = utils.get_rotation_to_point(self.robot.vector, Vector(x, y, 0, 0)) logging.info("Wants to close-rotate by: " + str(angle)) comms.turn(angle) return 1 # TODO: NO FIXED DELAY beta = 90 - alpha if math.sin(math.radians(alpha)) == 0: distance_to_move = 15 # something is obviously wrong so we have to move a bit else: distance_to_move = math.sin( math.radians(beta)) * self.robot.catch_distance / math.sin( math.radians(alpha)) # Find the movement side angle, side = utils.defender_get_rotation_to_catch_point( Vector(self.robot.x, self.robot.y, self.robot.angle, 0), Vector(self.world.ball.x, self.world.ball.y, 0, 0), self.robot.catch_distance) if side == "left": distance_to_move = -distance_to_move logging.info("Wants to move by: " + str(distance_to_move)) comms.move(distance_to_move) return utils.defender_move_delay(distance_to_move)
def perform(self, comms): # reset dynamic threshold # TODO: has to be restarted everywhere! global ROTATION_THRESHOLD ROTATION_THRESHOLD = 0.1 dx = self.world.ball.x - self.robot.x dy = self.world.ball.y - self.robot.y displacement = math.hypot(dx, dy) if displacement == 0: alpha = 0 else: # TODO: has to handle this edge case better try: alpha = math.degrees(math.asin(self.robot.catch_distance / float(displacement))) except ValueError as er: print(er) x = self.world.ball.x y = self.world.ball.y angle = utils.get_rotation_to_point(self.robot.vector, Vector(x, y, 0, 0)) logging.info("Wants to close-rotate by: " + str(angle)) comms.turn(angle) return 1 # TODO: NO FIXED DELAY beta = 90 - alpha if math.sin(math.radians(alpha)) == 0: distance_to_move = 15 # something is obviously wrong so we have to move a bit else: distance_to_move = math.sin(math.radians(beta)) * self.robot.catch_distance / math.sin(math.radians(alpha)) # Find the movement side angle, side = utils.defender_get_rotation_to_catch_point(Vector(self.robot.x, self.robot.y, self.robot.angle, 0), Vector(self.world.ball.x, self.world.ball.y, 0, 0), self.robot.catch_distance) if side == "left": distance_to_move = -distance_to_move logging.info("Wants to move by: " + str(distance_to_move)) comms.move(distance_to_move) return utils.defender_move_delay(distance_to_move)
def rotation_precondition(self, w, r): possessing = w.robot_in_posession rotation = utils.defender_get_rotation_to_catch_point(Vector(r.x, r.y, r.angle, 0), Vector(possessing.x, possessing.y, 0, 0), r.catch_distance)[0] return abs(rotation) < FACING_ROTATION_THRESHOLD