|
3 | 3 | import numpy as np |
4 | 4 | from typing import Tuple, Optional, Dict, Union, List |
5 | 5 |
|
| 6 | +from entities.game.game import Game |
6 | 7 | from global_utils.math_utils import rotate_vector |
7 | 8 | from entities.data.command import RobotSimCommand |
8 | 9 | from entities.data.vision import RobotData, BallData |
|
13 | 14 | PID_PARAMS, |
14 | 15 | YELLOW_START, |
15 | 16 | ) |
| 17 | +from team_controller.src.generated_code.ssl_simulation_robot_control_pb2 import ( |
| 18 | + RobotControl, |
| 19 | +) |
16 | 20 |
|
17 | 21 | # TODO: To be moved to a High-level Descision making repo |
18 | | - |
19 | 22 | class StartUpController: |
20 | 23 | def __init__( |
21 | 24 | self, |
22 | | - vision_receiver: VisionDataReceiver, |
| 25 | + game: Game, |
23 | 26 | debug=False, |
24 | 27 | ): |
25 | | - self.vision_receiver = vision_receiver |
26 | | - self.sim_robot_controller = SimRobotController(is_team_yellow=True, debug=debug) |
| 28 | + self.game = game |
| 29 | + self.sim_robot_controller = SimRobotController(debug=debug) |
27 | 30 |
|
28 | 31 | # TODO: Tune PID parameters further when going from sim to real(it works for Grsim) |
29 | 32 | # potentially have a set tunig parameters for each robot |
30 | 33 | self.pid_oren = PID(0.0167, 8, -8, 5, 0, 0.03, num_robots=6) |
31 | 34 | self.pid_trans = PID(0.0167, 1.5, -1.5, 5, 0, 0.02, num_robots=6) |
32 | 35 |
|
33 | | - self.lock = threading.Lock() |
34 | | - |
35 | 36 | self.debug = debug |
36 | 37 |
|
37 | | - def startup(self): |
38 | | - while True: |
39 | | - start_time = time.time() |
40 | | - |
41 | | - robots, balls = self._get_positions() |
42 | | - |
43 | | - if robots and balls: |
44 | | - for robot_id, robot_data in enumerate(robots): |
45 | | - if robot_data is None: |
46 | | - continue |
47 | | - target_coords = YELLOW_START[robot_id] |
48 | | - command = self._calculate_robot_velocities( |
49 | | - robot_id, target_coords, robots, balls, face_ball=True |
50 | | - ) |
51 | | - self.sim_robot_controller.add_robot_commands(command, robot_id) |
52 | | - |
53 | | - self.sim_robot_controller.send_robot_commands() |
54 | | - self.sim_robot_controller.robot_has_ball(robot_id=3) |
55 | | - |
56 | | - time_to_sleep = max(0, 0.0167 - (time.time() - start_time)) |
57 | | - time.sleep(time_to_sleep) |
58 | | - |
59 | | - def _get_positions(self) -> tuple: |
60 | | - # Fetch the latest positions of robots and balls with thread locking. |
61 | | - with self.lock: |
62 | | - robots = self.vision_receiver.get_robots_pos(is_yellow=True) |
63 | | - balls = self.vision_receiver.get_ball_pos() |
64 | | - return robots, balls |
| 38 | + def make_decision(self): |
| 39 | + robots = self.game.get_robots_pos(is_yellow=True) |
| 40 | + balls = self.game.get_ball_pos() |
| 41 | + |
| 42 | + if robots and balls: |
| 43 | + out_packet = RobotControl() |
| 44 | + for robot_id, robot_data in enumerate(robots): |
| 45 | + if robot_data is None: |
| 46 | + continue |
| 47 | + target_coords = YELLOW_START[robot_id] |
| 48 | + command = self._calculate_robot_velocities( |
| 49 | + robot_id, target_coords, robots, balls, face_ball=True |
| 50 | + ) |
| 51 | + self.sim_robot_controller.add_robot_command(command) |
| 52 | + |
| 53 | + if self.debug: |
| 54 | + print(out_packet) |
| 55 | + self.sim_robot_controller.send_robot_commands(team_is_yellow=True) |
| 56 | + self.sim_robot_controller.robot_has_ball(robot_id=3, team_is_yellow=True) |
65 | 57 |
|
66 | 58 | def _calculate_robot_velocities( |
67 | 59 | self, |
|
0 commit comments