class Patrol(Action): def init(self): self.timer = Timer() self.timer2 = Timer(2) self.straight = True def tick(self): # if not ballValid(): # return self.failure() print(self.timer2.elapsed()) self.lookAt(0, 0) self.walk(3, 0, 0) if self.straight: self.walk(3, 0, 0) else: self.walk(3, 0, 3) if self.timer2.finished(): if self.straight: self.straight = False self.timer2.restart() else: self.straight = True self.timer2.restart() return self.running()
class Lie(Action): def init(self): self.lie_timer = Timer(5) self.inited = False def tick(self): if not self.inited: self.inited = True self.lie_timer.restart() if self.lie_timer.finished(): print 'lie finished' return self.success() else: return self.running()
class Reentry(Action): def init(self): self.timer = Timer(self.bb.parameters.motionInitTime) self.timer2 = Timer(self.bb.parameters.motionInitTime) self.initial = Initial() self.cur_state = None def onEnter(self): self.timer.restart() def tick(self): gc_info = self.bb.GCInfo trackBall.tick() if not self.bb.motionConnected: self.timer.restart() if not self.timer.finished(): self.timer2.restart() self.initial.tick() self.crouch() self.lookAt(45, 0) return self.running() # if penalised, wait elif gc_info.penalised or gc_info.state is GCInfo.SET: self.lookAt(0, 0) self.crouch() return self.running() else: if gc_info.connected and gc_info.state is GCInfo.INITIAL: self.bb.reentry = False return self.success() if gc_info.connected and gc_info.state is GCInfo.PLAYING and self.bb.see_ball and self.bb.see_ball_cnt > 5: self.bb.reentry = False return self.success() self.dest = self.bb.get_default_dest() if not self.got_dest_loose(self.dest): self.gotoGlobal(self.dest) return self.running() else: self.crouch() self.bb.reentry = False return self.success()
class FindBall(Action): """ Head skill: Find ball, when robot is not turning, scan field, else look down """ def init(self): self.timer = Timer(1) self.iter = iter(GazePlats) self.curPlat = self.iter.next() def tick(self): if not self.bb.visionInfo.see_ball and self.bb.ball_lost.elapsed() < 10 and get_magnitude(self.bb.last_seen_ball_field) < 50: return self.failure() if self.gazeBall(): self.iter = iter(GazePlats) self.curPlat = self.iter.next() return self.success() else: if self.timer.finished(): self.timer.restart() if not self.next_plat(): return self.failure() self.lookAt(self.curPlat.x, self.curPlat.y, 10, 10) return self.running() def next_plat(self): try: self.curPlat = self.iter.next() # self.world.scanning = True return True except StopIteration: # GazePlats.reverse() # self.world.scanning = False self.iter = iter(GazePlats) self.curPlat = self.iter.next() return False
class ScanField_fast(Action): def init(self): self.path = [ VecPos(15, -90), # VecPos(20, -50), VecPos(15, 0), # VecPos(20, 50), VecPos(15, 90), # VecPos(40, 0) ] self.keep = False self.timer = Timer(0.75) self.iter = iter(self.path) self.curplat = self.iter.next() def tick(self): """ Success when see both goal or see circle or time out """ cur = self.curplat self.lookAt(cur.x, cur.y, 10, 10) if self.headGotDest(cur.x, cur.y): if not self.keep: self.keep = True self.timer.restart() elif self.timer.finished(): self.keep = False try: self.curplat = self.iter.next() except StopIteration: self.path.reverse() self.iter = iter(self.path) self.iter.next() self.curplat = self.iter.next() return self.success()
class GoalieDemoF(Action): def init(self): self.attackRight = True self.moveX = 0 # self.maxVelocity = 0 self.attackPointX = 0 self.robotXPre = 0 self.robotXCur = 0 self.inited = False self.lookAt(0, -80) self.leftScramble = False self.rightScramble = False self.goalie_timer = Timer(3) pass def tick(self): # if self.bb.field_angle - self.keepPoint.z > 180: # self.angleDiff = self.bb.field_angle - (self.keepPoint.z + 360) # elif self.bb.field_angle - self.keepPoint.z < -180: # self.angleDiff = (self.bb.field_angle + 360) - self.keepPoint.z # else: # self.angleDiff = self.bb.field_angle - self.keepPoint.z if self.leftScramble and not self.goalie_timer.finished(): self.goalieLeft() print 'scramble left' return self.running() elif self.leftScramble and self.goalie_timer.finished(): return self.success() elif self.rightScramble and not self.goalie_timer.finished(): self.goalieRight() print 'scramble right' return self.running() elif self.rightScramble and self.goalie_timer.finished(): return self.success() if self.bb.visionInfo.see_ball: self.gazeBall() print self.bb.ball_field print 'ballVelocity :', self.bb.ball_velocity.y # if self.bb.ball_velocity.length() > self.maxVelocity: # self.maxVelocity = self.bb.ball_velocity.length() if abs(self.bb.ball_field.y) < scrambleDist: if self.bb.ball_field.x > scramblePointX: print 'scramble left --- scrambleDist' self.leftScramble = True self.goalie_timer.restart() # time.sleep(3) return self.running() # failure elif self.bb.ball_field.x < -scramblePointX: print 'scramble right --- scrambleDist' self.rightScramble = True self.goalie_timer.restart() # time.sleep(3) return self.running() # failure else: print 'keep mid --- scrambleDist' self.crouch() return self.running() elif abs(self.bb.ball_field.y) < dangerDist: # print 'ball velocity : ', self.bb.ball_velocity # if abs(self.bb.ball_velocity.y) < ballMinMoveSpeed: # print 'lineUp --- dangerDist' # self.lineUp() # return self.running() if self.calcAttackPointX(): if abs( self.bb.ball_velocity.y ) > dangerVelocity and self.attackPointX > scramblePointX and self.attackPointX < 200: print 'scramble left --- dangerDist' self.leftScramble = True self.goalie_timer.restart() # time.sleep(3) return self.running() elif abs( self.bb.ball_velocity.y ) > dangerVelocity and self.attackPointX < -scramblePointX and self.attackPointX > -200: print 'scramble right --- dangerDist' self.rightScramble = True self.goalie_timer.restart() # time.sleep(3) return self.running() # else: # print 'lineUp --- dangerDist' # return self.running()c # self.lineUp() else: # self.lineUp() self.crouch() return self.running() elif abs(self.bb.ball_field.y) < adjustDist: # print 'adjustDist' # self.lineUp() self.crouch() return self.running() else: self.crouch() # print 'ball is too far' return self.running() # if self.calPos(): # print(self.keepPoint) # if self.bb.robot_pos.distance(self.keepPoint) > 10: # self.gotoGlobalOmni(self.keepPoint) # else: # self.crouch # return self.running() # else: # return self.running() # if self.bb.robot_pos.y - self.keepPoint.y > 20: # self.walk(0, -3, 0) # elif self.bb.robot_pos.y - self.keepPoint.y < -20: # self.walk(0, 3, 0) # elif self.angleDiff > 5: # self.turn(-5) # elif self.angleDiff < -5: # self.turn(5) else: self.lookAt(0, -80) self.crouch return self.running() def lineUp(self): print 'moveX: ', self.moveX if not self.inited: self.robotXPre = self.bb.robot_pos.x self.robotXCur = self.robotXPre self.inited = True else: self.robotXCur = self.bb.robot_pos.x self.moveX += self.robotXCur - self.robotXPre self.robotXPre = self.robotXCur if self.moveX < maxMoveUp and self.bb.ball_field.x > adjustBallX: self.walk(3, 0, 0) elif self.moveX > maxMoveDown and self.bb.ball_field.x < -adjustBallX: self.walk(-1, 0, 0) else: self.crouch() def calcAttackPointX(self): if abs(self.bb.visionInfo.ball_velocity.y) < 0.01: return False else: self.attackPointX = -self.bb.visionInfo.ball_velocity.x / self.bb.visionInfo.ball_velocity.y * self.bb.ball_field.y + self.bb.ball_field.x return True
class GCPlay(Action): def init(self): self.last_state = None self.initial = Initial() self.ready = Ready() self.set = Set() self.playing = Playing() self.finished = Finished() self.cur_state = None self.freeKickTimer = Timer(10) self.lastFreeKickBallPos = VecPos() self.enemy_freekick = False self.trackBall = TrackBall() def tick(self): #print 'GCPlay' self.trackBall.tick() gc_info = self.bb.GCInfo if gc_info is None or not gc_info.connected: self.playing.tick() else: if gc_info.ourIndirectFreeKick or gc_info.ourDirectFreeKick or gc_info.ourPenaltyKick: if gc_info.state2Freeze: self.trackBall.tick() self.crouch() elif gc_info.state2Ready: self.trackBall.tick() if self.bb.closest_to_ball(): ball = self.bb.ball_global if attackRight(): self.gotoGlobal(VecPos(ball.x - 80, ball.y, 0)) else: self.gotoGlobal(VecPos(ball.x + 80, ball.y, 0)) elif gc_info.enemyIndirectFreeKick or gc_info.enemyDirectFreeKick or gc_info.enemyPenaltyKick: self.enemy_freekick = True self.freeKickTimer.restart() self.lastFreeKickBallPos = self.bb.ball_global if gc_info.state2Freeze: self.crouch() elif gc_info.state2Ready: if self.bb.closest_to_ball(): ball = self.bb.ball_global if attackRight(): self.gotoGlobal(VecPos(ball.x - 50, ball.y, 0)) else: self.gotoGlobal(VecPos(ball.x + 50, ball.y, 0)) else: self.playing.tick() elif self.enemy_freekick: if self.bb.closest_to_ball(): if not self.freeKickTimer.finished(): # print self.freeKickTimer.elapsed() ball_now = self.bb.ball_global diff = (ball_now - self.lastFreeKickBallPos).length() # print diff if diff < 20: self.crouch() self.gazeBall() else: self.playing.tick() else: self.enemy_freekick = False else: self.enemy_freekick = False else: if gc_info.state is GCInfo.INITIAL: self.cur_state = self.initial elif gc_info.state is GCInfo.READY: self.cur_state = self.ready elif gc_info.state is GCInfo.SET: self.cur_state = self.set elif gc_info.state is GCInfo.PLAYING: self.cur_state = self.playing elif gc_info.state is GCInfo.FINISHED: self.cur_state = self.finished else: self.cur_state = self.playing if gc_info.state != self.last_state: if self.cur_state: self.cur_state.fresh() if self.cur_state: self.cur_state.tick() self.last_state = gc_info.state
class Lineup(Action): def init(self): self.diff_x = 0 self.diff_y = 0 self.sum_x = 0 self.sum_y = 0 self.avg_x = 0 self.avg_y = 0 self.line_up_cycle = 0 self.stop_timer = Timer() # FIXME(MWX): on entry self.entry = True self.timer = Timer(.5) self.left_kick = False def tick(self): if self.entry: self.entry = False self.timer.restart() self.lookAt(43, 0) ballvision = self.bb.ball_field if abs(ballvision.y) > 25 or abs(ballvision.x) > 25: return self.failure() # final_dest, dest, rub = calc_attack_point() # if not self.got_dest_tight(final_dest): # return self.failure() # TODO(MWX): step may be better if not self.timer.finished(): self.crouch() # if ballvision.y > 0: # self.bb.left_kick = True # else: # self.bb.left_kick = False return self.running() # if self.bb.left_kick: # dx = ballvision.x - self.bb.parameters.left_kick_point.x # dy = ballvision.y - self.bb.parameters.left_kick_point.y # else: dx = ballvision.x - self.bb.parameters.left_kick_point.x dy = ballvision.y - self.bb.parameters.left_kick_point.y if not abs(dx) < 3 or not abs(dy) < 3: x, y, t = [0, 0, 0] if dx > 3: x = 1 elif dx < -3: x = -1 if dy > 3: y = 1 elif dy < -3: y = -1 self.walk(x, y, t) return self.running() else: self.crouch() return self.success()
diff_pitch = [i for i in range(param.minPitch, param.maxPitch, 10)] path = [] for i in range(len(diff_pitch)): tmp = [diff_pitch[i] for j in range(len(diff_yaw))] path += zip(diff_yaw, tmp) diff_yaw.reverse() # Move(1s) --> Keep(1s) --> Capture(1s) --> Move(1s) currentPoint = (0, 0) iteration = iter(path) # wait timer = Timer(5) while not timer.finished(): timer.sleep(1) @repeatSec(0.5) class Move(Action): def tick(self): self.lookAt(pitch=currentPoint[1], yaw=currentPoint[0]) return self.running() @repeatSec(0.5) class Keep(Action): def tick(self): self.lookAt(pitch=currentPoint[1], yaw=currentPoint[0]) return self.running()