Skip to content

Basic Usage Tutorial

This tutorial explains the fundamental usage of the SoftRealtimeLoop class.

Simple One-Shot Function

The most basic usage is running a function once in the loop. The function returns 0 to stop the loop:

def main():
    # Create loop with 1ms timestep
    rt_loop = SoftRealtimeLoop(dt=0.001)

    # Example 1: Simple function that runs once
    def basic_function() -> int:
        print("Basic loop iteration")
        return 0  # Stop after one iteration

    print("Running basic function...")

Timed Execution

You can run a function for a specific duration by tracking elapsed time:

    print("\nRunning timed function for 3 seconds...")
    start_time = time.monotonic()

    def timed_function() -> Union[int, None]:
        elapsed = time.monotonic() - start_time
        print(f"Time elapsed: {elapsed:.2f} seconds")
        if elapsed > 3:
            return 0  # Stop after 3 seconds
        return None

Key Concepts

  1. The run() method is blocking and continues until the function returns or a signal interrupts the loop.
  2. The time step (dt) determines the loop frequency
  3. The loop maintains precise timing automatically

If you have any questions or need further assistance, please post on the Open Source Leg community forum.