drake

How to serialize trajectories to file?


I would like to serialize a Drake Trajectory to file using the Python API. It appears that some trajectory classes (BsplineTrajectory, PiecewisePolynomial) have serialization capabilities. However, I can't find any examples of writing a trajectory object to file; the unit tests only show loading the trajectory from file. Is there a way that I can save trajectories to a file?

I tried the following commands without success:

import pickle
from pydrake.common.yaml import yaml_dump, yaml_dump_typed
from pydrake.all import PiecewisePolynomial


t = PiecewisePolynomial.ZeroOrderHold([0.0, 1.0], [[0.0, 0.0], [1.0, 1.0]])
pickle.dump(t, '/tmp/trajectory.drake')  # Fails - TypeError: file must have a 'write' attribute
yaml_dump(t, '/tmp/trajectory.yaml')  # Fails - 'cannot represent an object', <pydrake.trajectories.PiecewisePolynomial`
yaml_dump_typed(t, '/tmp/trajectory.yaml')  # Fails - AttributeError: 'pydrake.trajectories.PiecewisePolynomial' object has no attribute 'breaks'

System:


Solution

  • Per this comment:

    At the moment, PiecewisePolynomial is only compatible with yaml_load_typed but not yaml_dump_typed.

    The work-around is to ask the PP for its information manually (get_segment_times(), getPolynomialMatrix(...)), and create the necessary output by hand.