OSC_ROS2/test/rampfunction.py
Alexander Schaefer 472cbc6b08 AS
2025-05-12 20:20:00 +02:00

31 lines
1.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import numpy as np
def symmetric_timestamps(total_duration: float, num_points: int, flat_ratio: float = 0.3) -> list:
"""
Generate symmetric timestamps with increasing → decreasing → flat → increasing spacing pattern.
Args:
total_duration (float): The total duration (last timestamp).
num_points (int): Total number of timestamps (must be >= 3).
flat_ratio (float): Fraction of timestamps in the constant-spacing center (0.00.9).
Returns:
List[float]: List of timestamps from 0 to total_duration.
"""
n = int(num_points*(1-flat_ratio)/2)
start = np.cos(np.linspace(0, np.pi, n))+2
end = np.cos(np.linspace(-np.pi, 0, n))+2
flat = np.ones(num_points-2*n)
timestamps = np.concatenate((start, flat, end))
timestamps *= total_duration / timestamps.sum()
timestamps = np.cumsum(timestamps)
return timestamps.tolist()
a = symmetric_timestamps(7.5, 30, 0.7)
for i in range(len(a)-1):
print(f"{a[i]:.2f} - {a[i+1]:.2f} = {a[i+1]-a[i]}")