|
| 1 | +import math |
| 2 | +from pyqtgraph.Qt.QtCore import Qt |
| 3 | +from software.thunderscope.gl.helpers.extended_gl_view_widget import MouseInSceneEvent |
| 4 | +from proto.import_all_protos import * |
| 5 | +from software.thunderscope.gl.layers.gl_world_layer import tbots_cpp |
| 6 | +from software.thunderscope.proto_unix_io import ProtoUnixIO |
| 7 | +from software.logger.logger import create_logger |
| 8 | +from software.thunderscope.gl.layers.gl_layer import GLLayer |
| 9 | +from software.thunderscope.thread_safe_buffer import ThreadSafeBuffer |
| 10 | + |
| 11 | +logger = create_logger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +class GLMovementFieldTestLayer(GLLayer): |
| 15 | + def __init__( |
| 16 | + self, name: str, fullsystem_io: ProtoUnixIO, buffer_size: int = 5 |
| 17 | + ) -> None: |
| 18 | + """Initialize the GLMovementFieldTestLayer |
| 19 | +
|
| 20 | + :param name: The displayed name of the layer |
| 21 | + :param buffer_size: The buffer size we have |
| 22 | + :param fullsystem_io: The fullsystem protounix io |
| 23 | + """ |
| 24 | + super().__init__(name) |
| 25 | + |
| 26 | + self.world_buffer: ThreadSafeBuffer = ThreadSafeBuffer(buffer_size, World) |
| 27 | + self.fullsystem_io: ProtoUnixIO = fullsystem_io |
| 28 | + self.selected_robot_id = 0 |
| 29 | + self.cached_team: tbots_cpp.Team = None |
| 30 | + self.is_selected = False |
| 31 | + |
| 32 | + def select_closest_robot(self, point): |
| 33 | + """Find the closest robot to a point |
| 34 | +
|
| 35 | + :param point: the reference point to find the closest robot |
| 36 | + """ |
| 37 | + if not self.cached_team: |
| 38 | + logger.warning("No vision data received") |
| 39 | + return |
| 40 | + |
| 41 | + closest_robot = self.cached_team.getNearestRobot( |
| 42 | + tbots_cpp.Point(point.x(), point.y()) |
| 43 | + ) |
| 44 | + if closest_robot is None: |
| 45 | + logger.warning( |
| 46 | + "No robots found. Are you sure there is friendly robot on field?" |
| 47 | + ) |
| 48 | + return |
| 49 | + |
| 50 | + self.selected_robot_id = closest_robot.id() |
| 51 | + self.is_selected = True |
| 52 | + |
| 53 | + def mouse_in_scene_pressed(self, event: MouseInSceneEvent) -> None: |
| 54 | + """Move to the point clicked. |
| 55 | + If Shift+Alt+Control is pressed, clicking selects a robot based on the closest point in scene. |
| 56 | + If Shift+Alt is pressed, clicking moves a robot to the point in scene. |
| 57 | +
|
| 58 | + :param event: The event |
| 59 | + """ |
| 60 | + if not self.visible(): |
| 61 | + return |
| 62 | + |
| 63 | + if not event.mouse_event.modifiers() & Qt.KeyboardModifier.AltModifier: |
| 64 | + return |
| 65 | + |
| 66 | + point = event.point_in_scene |
| 67 | + if event.mouse_event.modifiers() & Qt.KeyboardModifier.ControlModifier: |
| 68 | + # Shift+Alt+Control is pressed |
| 69 | + self.select_closest_robot(point) |
| 70 | + else: |
| 71 | + # Shift+Alt is pressed |
| 72 | + self.move_to_point(point) |
| 73 | + |
| 74 | + def move_to_point(self, point): |
| 75 | + """Move to a point |
| 76 | +
|
| 77 | + :param point: the point we are commanding the robot to move to |
| 78 | + """ |
| 79 | + # whether the selected_robot_index would cause index out of range issues |
| 80 | + if not self.is_selected: |
| 81 | + logger.warning("No robot selected to be moved") |
| 82 | + return |
| 83 | + |
| 84 | + robot_id = self.selected_robot_id |
| 85 | + |
| 86 | + point = Point(x_meters=point.x(), y_meters=point.y()) |
| 87 | + move_tactic = MoveTactic( |
| 88 | + destination=point, |
| 89 | + dribbler_mode=DribblerMode.OFF, |
| 90 | + final_orientation=Angle(radians=-math.pi / 2), |
| 91 | + ball_collision_type=BallCollisionType.AVOID, |
| 92 | + auto_chip_or_kick=AutoChipOrKick(autokick_speed_m_per_s=0.0), |
| 93 | + max_allowed_speed_mode=MaxAllowedSpeedMode.PHYSICAL_LIMIT, |
| 94 | + obstacle_avoidance_mode=ObstacleAvoidanceMode.SAFE, |
| 95 | + ) |
| 96 | + |
| 97 | + assign_tactic = AssignedTacticPlayControlParams() |
| 98 | + assign_tactic.assigned_tactics[robot_id].move.CopyFrom(move_tactic) |
| 99 | + |
| 100 | + self.fullsystem_io.send_proto(AssignedTacticPlayControlParams, assign_tactic) |
| 101 | + |
| 102 | + def refresh_graphics(self): |
| 103 | + """Updating the world cache""" |
| 104 | + world = self.world_buffer.get(block=False, return_cached=False) |
| 105 | + |
| 106 | + if world is None: |
| 107 | + return |
| 108 | + |
| 109 | + self.cached_team = tbots_cpp.Team(world.friendly_team) |
0 commit comments