3
3
from proto .import_all_protos import *
4
4
from proto .message_translation import tbots_protobuf
5
5
from software .py_constants import SECONDS_PER_MILLISECOND
6
+ from software .thunderscope .constants import ProtoUnixIOTypes
6
7
from software .thunderscope .proto_unix_io import ProtoUnixIO
7
8
from software .thunderscope .thread_safe_buffer import ThreadSafeBuffer
8
9
from software .thunderscope .thunderscope import Thunderscope
@@ -100,6 +101,7 @@ def realtime_sim_ticker(
100
101
"""
101
102
simulation_state_buffer = ThreadSafeBuffer (5 , SimulationState )
102
103
sim_proto_unix_io .register_observer (SimulationState , simulation_state_buffer )
104
+ per_tick_delay_s = tick_rate_ms * SECONDS_PER_MILLISECOND
103
105
104
106
# Tick simulation if Thundersocpe is open
105
107
while tscope .is_open ():
@@ -109,18 +111,19 @@ def realtime_sim_ticker(
109
111
tick = SimulatorTick (milliseconds = tick_rate_ms )
110
112
sim_proto_unix_io .send_proto (SimulatorTick , tick )
111
113
112
- time .sleep (
113
- (tick_rate_ms * SECONDS_PER_MILLISECOND )
114
- / simulation_state_message .simulation_speed
115
- )
114
+ time .sleep (per_tick_delay_s / simulation_state_message .simulation_speed )
116
115
117
116
118
- def sync_simulation (sim_proto_unix_io : ProtoUnixIO , num_robots : int ) -> None :
117
+ def sync_simulation (
118
+ tscope : Thunderscope , num_robots : int , timeout_s : float = 0.1
119
+ ) -> None :
119
120
"""Ensure that simulator has synchronized with the default world state.
120
121
121
- :param sim_proto_unix_io : ProtoUnixIO for the Simulation
122
+ :param tscope : Thunderscope instance that is tied to this instance of the simulation
122
123
:param num_robots: Number of robots to initialize the simulator with
124
+ :param timeout_s: How long to wait before we retry our attempt to synchronize with the simulator
123
125
"""
126
+ sim_proto_unix_io = tscope .proto_unix_io_map [ProtoUnixIOTypes .SIM ]
124
127
world_state_received_buffer = ThreadSafeBuffer (1 , WorldStateReceivedTrigger )
125
128
sim_proto_unix_io .register_observer (
126
129
WorldStateReceivedTrigger , world_state_received_buffer
@@ -132,11 +135,15 @@ def sync_simulation(sim_proto_unix_io: ProtoUnixIO, num_robots: int) -> None:
132
135
133
136
try :
134
137
world_state_received = world_state_received_buffer .get (
135
- block = True , timeout = 0.1
138
+ block = True , timeout = timeout_s
136
139
)
137
140
except queue .Empty :
138
141
# Did not receive a response within timeout period
139
142
continue
140
143
else :
141
144
# Received a response from the simulator
142
145
break
146
+
147
+ # Wait for Thunderscope to launch
148
+ while not tscope .is_open ():
149
+ time .sleep (timeout_s )
0 commit comments