publish_trajectory.py
Go to the documentation of this file.
1 from __future__ import print_function, division
2 from time import sleep
3 import matplotlib.pyplot as plt
4 import signal
5 
6 __all__ = ["sig_int_handler", "publish_pose", "publish_trajectory",
7  "publish_time_indexed_trajectory", "plot"]
8 
9 
10 def sig_int_handler(signal, frame):
11  raise KeyboardInterrupt
12 
13 
14 def publish_pose(q, problem, t=0.0):
15  problem.get_scene().update(q, t)
16  problem.get_scene().get_kinematic_tree().publish_frames()
17 
18 
19 def publish_trajectory(traj, T, problem, once=False):
20  '''
21  Plays back a trajectory by updating the scene in a problem and publishing the corresponding tf transforms.
22 
23  Parameters:
24  traj (list[list[]]): A trajectory consisting of a list of configurations which can be either Python lists or np.array.
25  T (float): Time taken for playback. The timestep between configurations is assumed constant and is calculated as T/len(traj)
26  problem (pyexotica.PlanningProblem): The planning problem whose Scene will be used for playback
27  once (bool): Whether to play the trajectory once or in a loop. By default, will play back the trajectory until a KeyboardInterrupt has been received.
28  '''
29  if len(traj) == 0:
30  raise ValueError("Trajectory has zero elements")
31  signal.signal(signal.SIGINT, sig_int_handler)
32  print('Playing back trajectory ' + str(T) + 's')
33  dt = float(T) / float(len(traj))
34  t = 0
35  while True:
36  try:
37  publish_pose(traj[t], problem, float(t) * dt)
38  sleep(dt)
39  if t >= len(traj) - 1 and once:
40  return
41  t = (t + 1) % len(traj)
42  except KeyboardInterrupt:
43  return False
44 
45 
46 def publish_time_indexed_trajectory(traj, Ts, problem, once=False):
47  if len(traj) == 0:
48  raise ValueError("Trajectory has zero elements")
49  signal.signal(signal.SIGINT, sig_int_handler)
50  print('Playing back trajectory ' + str(len(Ts)) +
51  ' states in ' + str(Ts[len(Ts) - 1]))
52 
53  while True:
54  try:
55  for i in range(1, len(Ts) - 1):
56  publish_pose(traj[i], problem, Ts[i])
57  sleep(Ts[i] - Ts[i-1])
58  if once:
59  break
60  except KeyboardInterrupt:
61  return False
62  return True
63 
64 
65 def plot(solution, labels=None, yscale=None):
66  print('Plotting the solution')
67  plt.plot(solution, '.-')
68  if labels is not None:
69  plt.legend(labels)
70  if yscale is not None:
71  plt.yscale(yscale)
72  plt.show()
pyexotica.publish_trajectory.publish_pose
def publish_pose(q, problem, t=0.0)
Definition: publish_trajectory.py:14
pyexotica.publish_trajectory.publish_time_indexed_trajectory
def publish_time_indexed_trajectory(traj, Ts, problem, once=False)
Definition: publish_trajectory.py:46
pyexotica.publish_trajectory.sig_int_handler
def sig_int_handler(signal, frame)
Definition: publish_trajectory.py:10
pyexotica.publish_trajectory.publish_trajectory
def publish_trajectory(traj, T, problem, once=False)
Definition: publish_trajectory.py:19
pyexotica.publish_trajectory.plot
def plot(solution, labels=None, yscale=None)
Definition: publish_trajectory.py:65


exotica_python
Author(s):
autogenerated on Fri Aug 2 2024 08:44:00