knyazer

3b

Feb 9th, 2021
983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.65 KB | None | 0 0
  1. # rcj_soccer_player controller - ROBOT B1
  2.  
  3. # Feel free to import built-in libraries
  4. import math
  5. import sys
  6. from pathlib import Path
  7. sys.path.append(str(Path('.').absolute().parent))
  8. # You can now import scripts that you put into the folder with your
  9. # robot B1 controller
  10. from rcj_soccer_player_b1 import rcj_soccer_robot, utils
  11.  
  12. def sign(x):
  13.     if x > 0:
  14.         return 1
  15.     if x < 0:
  16.         return -1
  17.     return 0
  18.  
  19. def bound(val, top):
  20.     if abs(val) > top:
  21.         return sign(val) * top
  22.     return val
  23.  
  24. def dist(a, b):
  25.     return math.sqrt((a['x'] - b['x']) ** 2 + (a['y'] - b['y']) ** 2)
  26.  
  27. def norm(x):
  28.     while x <= -180:
  29.         x += 360
  30.     while x > 180:
  31.         x -= 360
  32.     return x
  33.  
  34. RAD2DEG = 180 / math.pi
  35.  
  36. BLUE_GOAL = [{'x': 0.8, 'y': -0.3}, {'x':0.8, 'y':0.3}, {'x':0.8, 'y':0}]
  37. YELLOW_GOAL = [{'x': -0.8, 'y': -0.3}, {'x':-0.8, 'y':0.3}, {'x':-0.8, 'y':0}]
  38.  
  39.  
  40. un_zero = 0
  41. class MyRobot(rcj_soccer_robot.RCJSoccerRobot):
  42.     def run(self):
  43.         while self.robot.step(rcj_soccer_robot.TIME_STEP) != -1:
  44.             if self.is_new_data():
  45.                 data = self.get_new_data()
  46.  
  47.                 # Get the position of our robot
  48.                 robot_pos = data[self.name]
  49.                 # Get the position of the ball
  50.                 ball_pos = data['ball']
  51.                
  52.                 if self.name[0] == "Y":
  53.                     ENEMY_GOAL = YELLOW_GOAL
  54.                     north_sign = -1
  55.                 else:
  56.                     ENEMY_GOAL = BLUE_GOAL
  57.                     north_sign = 1
  58.                
  59.                 # Get angle between the robot and the ball
  60.                 # and between the robot and the north
  61.                 ball_angle, robot_angle = self.get_angles(ball_pos, robot_pos)
  62.                
  63.                 ball_dist = dist(ball_pos, robot_pos)
  64.                
  65.                 abs_enemy_goal_angle = [0,0]
  66.                 abs_enemy_goal_angle[0] = norm(RAD2DEG * math.atan2(
  67.                     ENEMY_GOAL[0]['y'] - robot_pos['y'],
  68.                     ENEMY_GOAL[0]['x'] - robot_pos['x']
  69.                 ))
  70.                
  71.                 abs_enemy_goal_angle[1] = norm(RAD2DEG * math.atan2(
  72.                     ENEMY_GOAL[1]['y'] - robot_pos['y'],
  73.                     ENEMY_GOAL[1]['x'] - robot_pos['x']
  74.                 ))
  75.                
  76.                 for i in range(2):
  77.                     abs_enemy_goal_angle[i] += 15 * sign(abs_enemy_goal_angle[i])
  78.                     abs_enemy_goal_angle[i] = norm(abs_enemy_goal_angle[i])
  79.                
  80.                 abs_enemy_goal_angle_center = norm(RAD2DEG * math.atan2(
  81.                     ENEMY_GOAL[2]['y'] - robot_pos['y'],
  82.                     ENEMY_GOAL[2]['x'] - robot_pos['x']
  83.                 ))
  84.                
  85.                
  86.                 abs_ball_angle = RAD2DEG * math.atan2(
  87.                     robot_pos['y'] - ball_pos['y'],
  88.                     robot_pos['x'] - ball_pos['x']
  89.                 )
  90.                
  91.                 ball_angle = norm(ball_angle)
  92.                 robot_angle = norm(robot_angle * RAD2DEG + 90 * north_sign)
  93.                 enemy_goal_angle = norm(abs_enemy_goal_angle_center / 2 + robot_angle)
  94.                
  95.                
  96.                 target_angle = norm(abs_ball_angle - abs_enemy_goal_angle_center)
  97.                
  98.                 mul = math.sqrt(0.3 / ball_dist)
  99.                 if mul > 1.5:
  100.                     mul = 1.5
  101.                 adduction = sign(target_angle) * 40 / (2 - mul)
  102.                 adduction = bound(adduction, 110)
  103.                
  104.                 if abs(target_angle) < 10:
  105.                     un_zero = 300
  106.                 else:
  107.                     un_zero -= abs(target_angle)
  108.                    
  109.                 if abs(target_angle) < 30 and ball_dist < 0.1:
  110.                     un_zero = max(un_zero, 100)
  111.                
  112.                 if un_zero > 0:
  113.                     adduction = 0
  114.                
  115.                 if ball_dist > 0.3:
  116.                     adduction = 0
  117.                
  118.                 target_angle += adduction
  119.                 u = norm(robot_angle + target_angle)
  120.                 u = u / 3 + u * u * sign(u) / 100
  121.                 vel = -10 + abs(u) / 4
  122.                    
  123.                 left_speed = vel - u
  124.                 right_speed = vel + u
  125.                    
  126.                
  127.                 left_speed = bound(left_speed, 10)
  128.                 right_speed = bound(right_speed, 10)
  129.                
  130.                 # Set the speed to motors
  131.                 self.left_motor.setVelocity(left_speed)
  132.                 self.right_motor.setVelocity(right_speed)
  133.  
  134.  
  135. my_robot = MyRobot()
  136. my_robot.run()
  137.  
Advertisement
Add Comment
Please, Sign In to add comment