def execute(s, drone): drone.role.timer += s.dt if s.strategy == Strategy.KICKOFF: distance = np.linalg.norm(drone.pos - s.ball.pos) time_to_hit = distance / np.linalg.norm(drone.vel) if time_to_hit <= KO_DODGE_TIME: drone.controller = Dodge(drone, local(drone.orient_m, drone.pos, s.ball.pos)) drone.role.timer = 0 if drone.controller is None: if drone.kickoff == 'r_back' or drone.kickoff == 'l_back': drone.controller = AB_Control(drone, a3l([0.0, -2816.0, 70.0])*team_sign(s.team)) drone.role.timer = 0 #drone.controller = LINE_PD_Control(drone, a3l([0,-6000,0])*team_sign(s.team), s.ball.pos) else: drone.controller = AB_Control(drone, s.ball.pos) elif isinstance(drone.controller,AB_Control): if drone.role.timer >= KO_PAD_TIME: AB_Control(drone, s.ball.pos) else: drone.controller = AB_Control(drone, s.ball.pos) if drone.controller is not None: if isinstance(drone.controller,Dodge): drone.controller.run(drone.role.timer) else: drone.controller.run()
def run(self, hive, drone, target): """Runs the controller. Arguments: hive {Hivemind} -- The hivemind. drone {Drone} -- Drone being controlled. target {np.ndarray} -- World coordinates of where to dodge towards. """ # Calculates local target and direction. local_target = local(drone.orient_m, drone.pos, target) direction = normalise(local_target) # First jump if self.timer <= self.FST_JUMP_DURATION: drone.ctrl.jump = True # Second jump, i.e. dodge. if self.timer >= self.FST_JUMP_DURATION + self.SND_JUMP_DELAY: drone.ctrl.jump = True drone.ctrl.pitch = -direction[0] drone.ctrl.paw = direction[1] # Expiration of the controller. if self.timer >= self.FST_JUMP_DURATION + self.SND_JUMP_DELAY + self.SND_JUMP_DURATION: drone.controller = None super().run(hive)
def run(self, agent, player, target): """Runs the controller. Arguments: agent {BaseAgent} -- The agent. player {Car} -- Car object for which to generate controls. target {np.ndarray} -- World coordinates of where to dodge towards. """ # Calculates local target and direction. local_target = local(player.orient_m, player.pos, target) direction = normalise(local_target) # First jump if self.timer <= self.FST_JUMP_DURATION: agent.ctrl.jump = True # Second jump, i.e. dodge. if self.timer >= self.FST_JUMP_DURATION + self.SND_JUMP_DELAY: agent.ctrl.jump = True agent.ctrl.pitch = -direction[0] agent.ctrl.paw = direction[1] # Expiration of the controller. if self.timer >= self.FST_JUMP_DURATION + self.SND_JUMP_DELAY + self.SND_JUMP_DURATION: agent.controller = None super().run(agent)
def run(self): local_target = local(self.drone.orient_m, self.drone.pos, self.target) angle = np.arctan2(local_target[1], local_target[0]) if abs(angle) > GK_MIN_ANGLE or np.pi - abs(angle) > GK_MIN_ANGLE: self.drone.ctrl.steer = 1 if angle > 0 else -1 distance = abs(local_target[0]) if abs(angle) > GK_BACK_ANGLE: self.drone.ctrl.throttle = cap(-1 * GK_THROTTLE * distance, -1, 1) else: self.drone.ctrl.throttle = cap(1 * GK_THROTTLE * distance, -1, 1)
def run(self): local_target = local(self.drone.orient_m, self.drone.pos, self.target) angle = np.arctan2(local_target[1], local_target[0]) if abs(angle) > AB_MIN_ANGLE: self.drone.ctrl.steer = 1 if angle > 0 else -1 if abs(angle) < AB_BOOST_ANGLE: self.drone.ctrl.boost = True if abs(angle) > AB_DRIFT_ANGLE: self.drone.ctrl.handbrake = True self.drone.ctrl.throttle = 1
def precise(agent, target, time, ctrl=SimpleControllerState()): # Calculates angle to target. local_target = local(agent.player.orient_m, agent.player.pos, target) angle = np.arctan2(local_target[1], local_target[0]) # Steer using special sauce. ctrl.steer = special_sauce(angle, -5) # Throttle 1 but brakes at last moment. towards_target = target - agent.player.pos distance = np.linalg.norm(towards_target) vel = np.dot(towards_target / distance, agent.player.vel) ETA = distance / vel + agent.game_time ctrl.throttle = 1 if ETA > time else -1 return ctrl
def run(self): line_v = self.line[1] - self.line[0] theta = angle_between_vectors(line_v, self.line[1] - self.drone.pos) distance = np.linalg.norm(self.line[1] - self.drone.pos) print("theta", theta) phi = angle_between_vectors(line_v, self.drone.vel) print("phi", phi) self.drone.ctrl.throttle = 1 self.drone.ctrl.steer = cap( LINE_PD_ALPHA * (np.sin(theta) * distance) + LINE_PD_BETA * phi, -1, 1) local_target = local(self.drone.orient_m, self.drone.pos, self.line[1]) angle = np.arctan2(local_target[1], local_target[0]) if abs(angle) < LINE_PD_BOOST_ANGLE: self.drone.ctrl.boost = True
def simple(agent, target, ctrl=SimpleControllerState()): # Calculates angle to target. local_target = local(agent.player.orient_m, agent.player.pos, target) angle = np.arctan2(local_target[1], local_target[0]) # Steer using special sauce. ctrl.steer = special_sauce(angle, -5) # Throttle always 1. ctrl.throttle = 1 # Handbrake if large angle. if abs(angle) > 1.85: ctrl.handbrake = True # Boost if small angle. elif abs(angle) < 0.5: ctrl.boost = 1 return ctrl
def drift_render(self): """Renders information on the screen.""" self.renderer.begin_rendering() car = self.agent # Calculates a vector from the car to the position 1000 uu in the front direction of the car. front = world(car.orient_m, car.pos, a3l([1000, 0, 0])) - car.pos # Calculated the velocity vector in local coordinates. local_v = local(car.orient_m, a3l([0, 0, 0]), car.vel) # Uses two methods to calculate angle. (The were just for testing which produces better results.) angle2D = np.arctan2(local_v[1], local_v[0]) angle_pure = angle_between_vectors(car.vel, front) # Rendering front vector and velocity vector. self.renderer.draw_line_3d(car.pos, car.pos + front, self.renderer.yellow()) self.renderer.draw_line_3d(car.pos, car.pos + car.vel, self.renderer.cyan()) # Rendering angles. self.renderer.draw_string_2d(10, 10, 2, 2, "angle 2D: {}".format(angle2D), self.renderer.pink()) self.renderer.draw_string_2d(10, 50, 2, 2, "angle 3D: {}".format(angle_pure), self.renderer.pink()) # Rendering position and velocity. self.renderer.draw_string_2d(10, 110, 2, 2, "pos: {}".format(car.pos), self.renderer.cyan()) self.renderer.draw_string_2d(10, 150, 2, 2, "vel: {}".format(car.vel), self.renderer.cyan()) # Rendering test related stuff. self.renderer.draw_string_2d( 10, 210, 2, 2, "test: {}/{}".format(self.count, self.tests), self.renderer.white()) self.renderer.draw_string_2d(10, 250, 2, 2, "timer: {}".format(self.timer), self.renderer.white()) self.renderer.end_rendering()
def run(self, agent, player, target): """Runs the controller. Arguments: agent {BaseAgent} -- The agent. player {Car} -- Car object for which to generate controls. target {np.ndarray} -- World coordinates of the point to drive towards. """ # Calculates angle to target. local_target = local(player.orient_m, player.pos, target) angle = np.arctan2(local_target[1], local_target[0]) # Creates controller inputs. agent.ctrl.steer = special_sauce(angle, -5) if abs(angle) < self.BOOST_ANGLE: agent.ctrl.boost = True if abs(angle) > self.DRIFT_ANGLE: agent.ctrl.handbrake = True agent.ctrl.throttle = 1
def run(self, agent, player): """Runs the controller. Arguments: agent {BaseAgent} -- The agent. player {Car} -- Car object for which to generate controls. """ if self.type == self.Type.DEFAULT: # Calculates angle to target. local_target = local(player.orient_m, player.pos, agent.ball.pos) angle = np.arctan2(local_target[1], local_target[0]) agent.ctrl.steer = special_sauce(angle, -5) agent.ctrl.throttle = 1 agent.ctrl.boost = 1 ETA = np.linalg.norm(agent.ball.pos - player.pos) / np.linalg.norm( player.vel) if ETA <= self.DODGE_TIME: agent.controller = Dodge() super().run(agent)
def run(self, hive, drone, target): """Runs the controller. Arguments: hive {Hivemind} -- The hivemind. drone {Drone} -- Drone being controlled. target {np.ndarray} -- World coordinates of the point to drive towards. """ # Calculates angle to target. local_target = local(drone.orient_m, drone.pos, target) angle = np.arctan2(local_target[1], local_target[0]) # Creates controller inputs. if abs(angle) > self.MIN_ANGLE: drone.ctrl.steer = 1 if angle > 0 else -1 if abs(angle) < self.BOOST_ANGLE: drone.ctrl.boost = True if abs(angle) > self.DRIFT_ANGLE: drone.ctrl.handbrake = True drone.ctrl.throttle = 1