Trajectory Interpolation

Use interpolators to obtain trajectory functions where you input a time (or array of times) and return the trajectory at that timepoint (or timepoints).

Interpolator Class

This is the primary way to create an interpolation function. This handles many edge-cases to ensure behavior is the same as the real-life Ctrl-P control system.

class sorotraj.interpolator.Interpolator(trajectory)

Trajectory interpolator

trajectory

Trajectory to interpolate

Type

dict

Examples

>>> interp = sorotraj.Interpolator(traj)
... actuation_fn = interp.get_interp_function(
...     num_reps=1,
...     speed_factor=1.0,
...     invert_direction=False)
... interp.get_final_time()
8.0
get_cycle_function(num_reps=1, speed_factor=1.0, invert_direction=False, as_list=None)

Get a function to return the current cycle number given time as an input

Parameters
  • num_reps (int) – Number of times to repeat the “main” trajectory segment

  • speed_factor (float) – Speed multiplier (times are multiplied by inverse of this)

  • invert_direction (Union[bool, list]) – Invert the sign of the interpolated values. If True, all signs are flipped. If list, invert_direction is treated as a list of indices.

Returns

  • cycle_function (function) – The cycle function

  • final_time (float) – The end time of the trajectory

get_final_time()

Get the final time of the most-recent interpolator

(This function exists for backward compatibillity. In the future, obtain the final time from the “get_traj_function” instead.)

get_interp_function(num_reps=1, speed_factor=1.0, invert_direction=False, **kwargs)

Get a trajectory interpolation function with the specified parameters

(This function exists for backward compatibillity. In the future, use “get_traj_function” instead.)

Parameters
  • num_reps (int) – Number of times to repeat the “main” trajectory segment

  • speed_factor (float) – Speed multiplier (times are multiplied by inverse of this)

  • invert_direction (Union[bool, list]) – Invert the sign of the interpolated values. If True, all signs are flipped. If list, invert_direction is treated as a list of indices.

Returns

The trajectory interpolation function

Return type

traj_function

Raises

ValueError – If num_reps is less than 0, or if speed_factor is 0 or less

get_traj_function(num_reps=1, speed_factor=1.0, invert_direction=False)

Get a trajectory interpolation function with the specified parameters

Parameters
  • num_reps (int) – Number of times to repeat the “main” trajectory segment

  • speed_factor (float) – Speed multiplier (times are multiplied by inverse of this)

  • invert_direction (Union[bool, list]) – Invert the sign of the interpolated values. If True, all signs are flipped. If list, invert_direction is treated as a list of indices.

Returns

  • traj_function (function) – The trajectory interpolation function

  • final_time (float) – The end time of the trajectory

Custom Back-End Interpolators

Several custom interpolation classes are used under the hood to make the functions behave like the physical control system. Below you can find documentation for these classes

class sorotraj.interpolator.TrajectoryInterpolator(traj_unpacked, num_reps=1, speed_factor=1.0, invert_direction=False, fill_value=None)

A trajectory interpolator based on specified parameters

Parameters
  • traj_unpacked (dict) – Unpacked trajectory object (dict where keys are trajectory components with fields “time” and “”values”)

  • num_reps (int, optional) – Number of times to repeat the “main” trajectory segment

  • speed_factor (float, optional) – Speed multiplier (times are multiplied by inverse of this)

  • invert_direction (Union[bool, list], optional) – Invert the sign of the interpolated values. If True, all signs are flipped. If list, invert_direction is treated as a list of indices.

  • fill_value (Union[list, np.ndarray], optional) – Default value of signals (only used when prefix and main are empty in the trajectory)

Raises

ValueError – If all trajectory components are empty

get_final_time()

Get the final time of the trajectory

Returns

final_time – The final time

Return type

float

get_traj_function()

Get the trajectory function

Returns

traj_function – The trajectory interpolation meta-function.

Return type

function

traj_function(x0)

The trajectory interpolation function

Parameters

x0 (Union[float, list, np.ndarray]) –

Returns

output – The trajectory at the given time point(s). If x0 is a float, output is a 1D np.ndarray. If x0 is a list or np.ndarray, output is a 2D np.ndarray.

Return type

np.ndarray

Raises
  • ValueError – If input is not a 1D array-like object

  • RuntimeError – If the length of the output does not equal the length of the input

class sorotraj.interpolator.WrappedInterp1d(x, y, **kwargs)

Create a wrapping 1D interpolator

Parameters
  • x (dict) – x points to use in interpolation

  • y (int, optional) – Values to use for interpolation

  • **kwargs (optional) – kwargs to pass to scipy.interpolate.interp1d().

get_function()

Get the wrapped interpolation function

Returns

wrapped_interp1d – The wrapped interpolator function

Return type

function

max_wrap(x0)

Calculate wrapped x values when x is greater than the wrapping bounds

Parameters

x0 (np.ndarray) – Values of x

Returns

wrapped_x0 – Values of x wrapped.

Return type

np.ndarray

min_wrap(x0)

Calculate wrapped x values when x is less than the wrapping bounds

Parameters

x0 (np.ndarray) – Values of x

Returns

wrapped_x0 – Values of x wrapped.

Return type

np.ndarray

wrapped_interp1d(x0)

The wrapped interp1d function. Input x0, return interpolated cyclic values

Parameters

x0 (Union[float, list, np.ndarray]) – Values of x where you want to interpolate

Returns

output – The interpolated values of y at the given time point(s)

Return type

np.ndarray

Raises

ValueError – If the input is not 1D

sorotraj.interpolator.interp1d_patched(x, y, **kwargs)

Get a 1D interpolation function where single-length input data are handled

When the length of x and y is greater than 1, interp1d is used. When the legnth of x and y is 1, use the value of y for all values of x0.

Parameters

x0 (Union[float, list, np.ndarray]) – Values of x where you want to interpolate

Returns

patched_interp1d – The patched interp1d function (same way the regular interp1d works)

Return type

function