knyazer

Untitled

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