Skip to content

Commit ab4af6a

Browse files
author
Ian McMahon
committed
Merge pull request #55 from RethinkRobotics/add-trajectory-points-when-few-points
Prevent JTAS Error with few Points
2 parents 1a50ec7 + 839a03b commit ab4af6a

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

src/joint_trajectory_action/bezier.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,25 @@ def de_boor_control_pts(points_array, d0=None,
135135
# Construct de Boor Control Points from A matrix
136136
d_pts = np.zeros((N+3, k))
137137
for col in range(0, k):
138-
x = np.zeros((N-1, 1))
139-
# Compute start / end conditions
140-
if natural:
141-
x[N-2, 0] = 6*points_array[-2, col] - points_array[-1, col]
142-
x[0, 0] = 6*points_array[1, col] - points_array[0, col]
143-
else:
144-
x[N-2, 0] = 6*points_array[-2, col] - 1.5*dN[0, col]
145-
x[0, 0] = 6*points_array[1, col] - 1.5*d0[0, col]
146-
x[range(1, N-3+1), 0] = 6*points_array[range(2, N-2+1), col]
147-
# Solve bezier interpolation
138+
x = np.zeros((max(N-1, 1), 1))
148139
if N > 2:
140+
# Compute start / end conditions
141+
if natural:
142+
x[N-2, 0] = 6*points_array[-2, col] - points_array[-1, col]
143+
x[0, 0] = 6*points_array[1, col] - points_array[0, col]
144+
else:
145+
x[N-2, 0] = 6*points_array[-2, col] - 1.5*dN[0, col]
146+
x[0, 0] = 6*points_array[1, col] - 1.5*d0[0, col]
147+
x[range(1, N-3+1), 0] = 6*points_array[range(2, N-2+1), col]
148+
# Solve bezier interpolation
149149
d_pts[2:N+1, col] = np.linalg.solve(A, x).T
150150
else:
151+
# Compute start / end conditions
152+
if natural:
153+
x[0, 0] = 6*points_array[1, col] - points_array[0, col]
154+
else:
155+
x[0, 0] = 6*points_array[1, col] - 1.5*d0[col]
156+
# Solve bezier interpolation
151157
d_pts[2, col] = x / A
152158
# Store off start and end positions
153159
d_pts[0, :] = points_array[0, :]

src/joint_trajectory_action/joint_trajectory_action.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,24 @@ def _on_trajectory_action(self, goal):
354354
control_rate = rospy.Rate(self._control_rate)
355355

356356
dimensions_dict = self._determine_dimensions(trajectory_points)
357-
# Force Velocites/Accelerations to zero at the final timestep
357+
358+
if num_points == 1:
359+
# Add current position as trajectory point
360+
first_trajectory_point = JointTrajectoryPoint()
361+
first_trajectory_point.positions = self._get_current_position(joint_names)
362+
# To preserve desired velocities and accelerations, copy them to the first
363+
# trajectory point if the trajectory is only 1 point.
364+
if dimensions_dict['velocities']:
365+
first_trajectory_point.velocities = deepcopy(trajectory_points[0].velocities)
366+
if dimensions_dict['accelerations']:
367+
first_trajectory_point.accelerations = deepcopy(trajectory_points[0].accelerations)
368+
first_trajectory_point.time_from_start = rospy.Duration(0)
369+
trajectory_points.insert(0, first_trajectory_point)
370+
num_points = len(trajectory_points)
371+
372+
# Force Velocites/Accelerations to zero at the final timestep
358373
# if they exist in the trajectory
359-
# Remove this behavior if you are stringing together trajectories,
374+
# Remove this behavior if you are stringing together trajectories,
360375
# and want continuous, non-zero velocities/accelerations between
361376
# trajectories
362377
if dimensions_dict['velocities']:
@@ -366,9 +381,18 @@ def _on_trajectory_action(self, goal):
366381

367382
# Compute Full Bezier Curve Coefficients for all 7 joints
368383
pnt_times = [pnt.time_from_start.to_sec() for pnt in trajectory_points]
369-
b_matrix = self._compute_bezier_coeff(joint_names,
370-
trajectory_points,
371-
dimensions_dict)
384+
try:
385+
b_matrix = self._compute_bezier_coeff(joint_names,
386+
trajectory_points,
387+
dimensions_dict)
388+
except Exception as ex:
389+
rospy.logerr(("{0}: Failed to compute a Bezier trajectory for {1}"
390+
" arm with error \"{2}: {3}\"").format(
391+
self._action_name,
392+
self._name,
393+
type(ex).__name__, ex))
394+
self._server.set_aborted()
395+
return
372396
# Wait for the specified execution time, if not provided use now
373397
start_time = goal.trajectory.header.stamp.to_sec()
374398
if start_time == 0.0:

0 commit comments

Comments
 (0)