Skip to content

Commit

Permalink
thread safe animation
Browse files Browse the repository at this point in the history
  • Loading branch information
pfandzelter committed Jun 18, 2024
1 parent cb10abc commit c69c5c2
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions celestial/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,16 @@ def __init__(
"""

def _updateAnimation(self, obj: typing.Any, event: typing.Any) -> None:
"""
This function is a wrapper to call the updateAnimation function with a lock.
:param obj: The object that generated the event, probably vtk render window.
:param event: The event that triggered this function.
"""
with self.lock:
self.updateAnimation(obj, event)

def updateAnimation(self, obj: typing.Any, event: typing.Any) -> None:
"""
This function takes in new position data and updates the render window
Expand Down Expand Up @@ -450,7 +460,8 @@ def makeRenderWindow(self) -> None:
self.interactor.Initialize()
# set up a timer to call the update function at a max rate
# of every 7 ms (~144 hz)
self.interactor.AddObserver("TimerEvent", self.updateAnimation)
self.lock = td.Lock()
self.interactor.AddObserver("TimerEvent", self._updateAnimation)
self.interactor.CreateRepeatingTimer(self.frequency)

# start the model
Expand Down Expand Up @@ -867,21 +878,23 @@ def controlThreadHandler(self) -> None:
# or update status of a shell
command = received_data["type"]
if command == "time":
self.current_simulation_time = received_data["time"]
# print(f"Animation: time is now {self.current_simulation_time}")
with self.lock:
self.current_simulation_time = received_data["time"]
# print(f"Animation: time is now {self.current_simulation_time}")
if command == "shell":
shell = received_data["shell"]
# print(f"Animation: updating shell {shell}")
self.sat_positions[shell] = received_data["sat_positions"]
# print(
# f"Animation: updated shell {shell} sat positions: {self.sat_positions[shell]}"
# )
self.links[shell] = received_data["links"]
# print(f"Animation: updated shell {shell} links: {self.links[shell]}")
self.gst_links[shell] = received_data["gst_links"]
# print(
# f"Animation: updated shell {shell} gst links: {self.gst_links[shell]}"
# )

if shell == 0:
self.gst_positions = received_data["gst_positions"]
with self.lock:
shell = received_data["shell"]
# print(f"Animation: updating shell {shell}")
self.sat_positions[shell] = received_data["sat_positions"]
# print(
# f"Animation: updated shell {shell} sat positions: {self.sat_positions[shell]}"
# )
self.links[shell] = received_data["links"]
# print(f"Animation: updated shell {shell} links: {self.links[shell]}")
self.gst_links[shell] = received_data["gst_links"]
# print(
# f"Animation: updated shell {shell} gst links: {self.gst_links[shell]}"
# )

if shell == 0:
self.gst_positions = received_data["gst_positions"]

0 comments on commit c69c5c2

Please sign in to comment.