Skip to content
Snippets Groups Projects
Commit 98fe3e77 authored by Pierre-Alexandre Leziart's avatar Pierre-Alexandre Leziart Committed by Pierre-Alexandre Leziart
Browse files

Files to test triggering the main loop with a periodic signal

parent f2ce01bc
No related branches found
No related tags found
No related merge requests found
loop.py 0 → 100644
import signal, time
from abc import ABCMeta, abstractmethod
## Code copied from mlp to avoid large dependency. Should be put in smaller package instead
class Loop(metaclass=ABCMeta):
"""
Astract Class to allow users to execute self.loop at a given frequency
with a timer while self.run can do something else.
"""
def __init__(self, period):
self.period = period
signal.signal(signal.SIGALRM, self.loop)
signal.setitimer(signal.ITIMER_REAL, period, period)
self.run()
def stop(self):
signal.setitimer(signal.ITIMER_REAL, 0)
raise KeyboardInterrupt # our self.run is waiting for this.
def run(self):
# Default implementation: don't do anything
try:
time.sleep(1e9)
except KeyboardInterrupt:
pass
@abstractmethod
def loop(self, signum, frame):
...
import numpy as np
import time
from loop import Loop
class SimulatorLoop(Loop):
"""
Class used to call pybullet at a given frequency
"""
def __init__(self, period, t_max):
"""
Constructor
:param period: the time step
:param t_max: maximum simulation time
"""
self.t = 0.0
self.t_max = t_max
self.period = period
def trigger(self):
super().__init__(self.period)
def loop(self, signum, frame):
self.t += self.period
if self.t > self.t_max:
self.stop()
print("- Start loop -")
print("- End loop -")
if __name__ == "__main__":
# Start the control loop:
sim_loop = SimulatorLoop(1.0, 10.0)
sim_loop.trigger()
print("-- FINAL --")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment