Skip to content

Commit 8f4b5cb

Browse files
Shortcut for pausing/resuming simulation in thunderscope (UBC-Thunderbots#2827)
* first commit on adding pause shortcut for thunderscope. still needs to add register_observer for proto * last commit for pause simulation shortcut. fully functional. will add a button as a part of a new pr later * Update src/software/thunderscope/field/world_layer.py Co-authored-by: Nima Zareian <[email protected]> * second commit. removed unnecessary comments --------- Co-authored-by: Nima Zareian <[email protected]>
1 parent ef0384d commit 8f4b5cb

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

src/proto/ssl_simulation_control.proto

-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ message SimulatorControl
7777
// Change the simulation speed
7878
optional float simulation_speed = 3;
7979
}
80-
8180
// Command from the connected client to the simulator
8281
message SimulatorCommand
8382
{
@@ -86,7 +85,6 @@ message SimulatorCommand
8685
// Configure the simulation
8786
optional SimulatorConfig config = 2;
8887
}
89-
9088
// Response of the simulator to the connected client
9189
message SimulatorResponse
9290
{

src/proto/world.proto

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ message SimulatorTick
5151
required double milliseconds = 1;
5252
}
5353

54+
message SimulationState
55+
{
56+
required bool is_playing = 1 [default = true];
57+
}
5458
message Pass
5559
{
5660
// The location of the passer

src/software/thunderscope/field/world_layer.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ def __init__(self, simulator_io, friendly_colour_yellow, buffer_size=5):
5050
self.key_pressed = {}
5151
self.display_robot_id = False
5252

53-
self.accepted_keys = [Qt.Key.Key_Control, Qt.Key.Key_I]
53+
self.is_playing = True
54+
55+
self.accepted_keys = [Qt.Key.Key_Control, Qt.Key.Key_I, QtCore.Qt.Key.Key_Space]
5456
for key in self.accepted_keys:
5557
self.key_pressed[key] = False
5658

@@ -69,6 +71,17 @@ def keyPressEvent(self, event):
6971
if event.key() == QtCore.Qt.Key.Key_I:
7072
self.display_robot_id = not self.display_robot_id
7173

74+
# if user is holding ctrl + space, send a command to simulator to pause the gameplay
75+
if (
76+
self.key_pressed[QtCore.Qt.Key.Key_Control]
77+
and self.key_pressed[QtCore.Qt.Key.Key_Space]
78+
):
79+
80+
simulator_state = SimulationState(is_playing=not self.is_playing)
81+
self.is_playing = not self.is_playing
82+
83+
self.simulator_io.send_proto(SimulationState, simulator_state)
84+
7285
def keyReleaseEvent(self, event):
7386
"""Detect when a key has been released (override)
7487

src/software/thunderscope/thunderscope_main.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import argparse
55
import numpy
66

7+
from software.thunderscope.thread_safe_buffer import ThreadSafeBuffer
78
from software.thunderscope.thunderscope import Thunderscope
89
from software.thunderscope.binary_context_managers import *
910
from proto.message_translation import tbots_protobuf
@@ -337,10 +338,20 @@ def __async_sim_ticker(tick_rate_ms):
337338
)
338339
tscope.simulator_proto_unix_io.send_proto(WorldState, world_state)
339340

341+
simulation_state_buffer = ThreadSafeBuffer(1, SimulationState)
342+
tscope.simulator_proto_unix_io.register_observer(
343+
SimulationState, simulation_state_buffer
344+
)
345+
340346
# Tick Simulation
341347
while True:
342-
tick = SimulatorTick(milliseconds=tick_rate_ms)
343-
tscope.simulator_proto_unix_io.send_proto(SimulatorTick, tick)
348+
349+
simulation_state_message = simulation_state_buffer.get()
350+
351+
if simulation_state_message.is_playing:
352+
tick = SimulatorTick(milliseconds=tick_rate_ms)
353+
tscope.simulator_proto_unix_io.send_proto(SimulatorTick, tick)
354+
344355
time.sleep(tick_rate_ms / 1000)
345356

346357
# Launch all binaries

0 commit comments

Comments
 (0)