tools.py
Go to the documentation of this file.
1 from __future__ import print_function, division
2 import numpy as np
3 from time import time
4 import matplotlib.pyplot as plt
5 from collections import OrderedDict
6 
7 __all__ = [
8  "check_trajectory_continuous_time",
9  "check_whether_trajectory_is_collision_free_by_subsampling",
10  "get_colliding_links",
11  "plot_task_cost_over_time",
12 ]
13 
14 
15 def check_trajectory_continuous_time(scene, trajectory):
16  start_time = time()
17  all_good = True
18  robot_links = scene.get_collision_robot_links()
19  world_links = scene.get_collision_world_links()
20  for t in range(1, trajectory.shape[0]):
21  q_t_1 = trajectory[t - 1, :]
22  q_t_2 = trajectory[t, :]
23  for r_l in robot_links:
24  for w_l in world_links:
25  scene.update(q_t_1)
26  T_r_l_1 = scene.fk(r_l)
27  T_w_l_1 = scene.fk(w_l)
28  scene.update(q_t_2)
29  T_r_l_2 = scene.fk(r_l)
30  T_w_l_2 = scene.fk(w_l)
31  p = scene.get_collision_scene().continuous_collision_check(
32  r_l, T_r_l_1, T_r_l_2, w_l, T_w_l_1, T_w_l_2
33  )
34  if p.in_collision:
35  print(t, p)
36  all_good = False
37  end_time = time()
38  print("Continuous-time collision verification took", end_time - start_time)
39  return all_good
40 
41 
43  scene, trajectory, num_subsamples=10, debug=False
44 ):
45  """
46  num_subsamples specifies how many steps are checked between two configurations. Returns True if trajectory is collision-free, and False otherwise.
47 
48  TODO: Support setting time for Scene update.
49  """
50  start_time = time()
51  trajectory_length = trajectory.shape[0]
52  for t in range(1, trajectory_length):
53  q_t_1 = trajectory[t - 1, :]
54  q_t_2 = trajectory[t, :]
55  q_t_interpolation = np.linspace(q_t_1, q_t_2, num_subsamples)
56  for i in range(num_subsamples):
57  scene.update(q_t_interpolation[i, :])
58  if not scene.is_state_valid(True):
59  return False
60  end_time = time()
61  if debug:
62  print("Trajectory transition collision check took", end_time - start_time)
63  return True
64 
65 
67  scene, margin=0.0, safe_distance=0.0, check_self_collision=True, debug=False
68 ):
69  robotLinks = scene.get_collision_robot_links()
70  world_links = scene.get_collision_world_links()
71  collisions = []
72  for r_l in robotLinks:
73  for w_l in world_links:
74  if scene.is_allowed_to_collide(r_l, w_l, True):
75  if not scene.is_collision_free(r_l, w_l, margin):
76  d = scene.get_collision_distance(r_l, w_l)
77  if abs(d[0].distance) > safe_distance:
78  collisions.append((r_l, w_l, d[0].distance))
79  if debug:
80  print(r_l, "-", w_l, "d=", d[0].distance)
81  if check_self_collision:
82  for w_l in robotLinks:
83  if w_l != r_l:
84  if scene.is_allowed_to_collide(r_l, w_l, True):
85  if not scene.is_collision_free(r_l, w_l, margin):
86  d = scene.get_collision_distance(r_l, w_l)
87  if abs(d[0].distance) > safe_distance:
88  collisions.append((r_l, w_l, d[0].distance))
89  if debug:
90  print(r_l, "-", w_l, d[0].distance)
91  return collisions
92 
93 
95  """
96  Plots the task cost (task maps) over time given a problem.
97  """
98  costs = OrderedDict()
99  for task_name in problem.cost.task_maps:
100  costs[task_name] = np.zeros((problem.T,))
101  for t in range(problem.T):
102  ydiff = problem.cost.get_task_error(task_name, t)
103  S = problem.cost.get_S(task_name, t)
104  cost = np.dot(np.dot(ydiff, S), ydiff.T)
105  costs[task_name][t] = cost
106  fig = plt.figure()
107  for task_name in costs:
108  plt.plot(costs[task_name], label=task_name)
109  plt.title("Task cost over time")
110  plt.legend()
111  plt.tight_layout()
112  plt.show()
def plot_task_cost_over_time(problem)
Definition: tools.py:94
def check_whether_trajectory_is_collision_free_by_subsampling(scene, trajectory, num_subsamples=10, debug=False)
Definition: tools.py:44
def check_trajectory_continuous_time(scene, trajectory)
Definition: tools.py:15
def get_colliding_links(scene, margin=0.0, safe_distance=0.0, check_self_collision=True, debug=False)
Definition: tools.py:68


exotica_python
Author(s):
autogenerated on Sat Apr 10 2021 02:35:59