Skip to content

Commit 3175cb5

Browse files
committed
Adding logic to make IsAtTrajectoryStart work for affine dofs.
1 parent b8d45b9 commit 3175cb5

File tree

2 files changed

+69
-32
lines changed

2 files changed

+69
-32
lines changed

src/prpy/base/robot.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,13 @@ def ExecuteTrajectory(self, traj, defer=False, timeout=None, period=0.01, **kwar
447447
if traj.GetNumWaypoints() <= 0:
448448
raise ValueError('Trajectory must contain at least one waypoint.')
449449

450+
# Check if this trajectory contains both affine and joint DOFs
451+
cspec = traj.GetConfigurationSpecification()
452+
needs_base = util.HasAffineDOFs(cspec)
453+
needs_joints = util.HasJointDOFs(cspec)
454+
if needs_base and needs_joints:
455+
raise ValueError('Trajectories with affine and joint DOFs are not supported')
456+
450457
# Check that the current configuration of the robot matches the
451458
# initial configuration specified by the trajectory.
452459
if not util.IsAtTrajectoryStart(self, traj):
@@ -475,9 +482,6 @@ def ExecuteTrajectory(self, traj, defer=False, timeout=None, period=0.01, **kwar
475482
' function that produced this trajectory to return a'
476483
' single-waypoint trajectory.', FutureWarning)
477484

478-
# TODO: Check if this trajectory contains the base.
479-
needs_base = util.HasAffineDOFs(traj.GetConfigurationSpecification())
480-
481485
self.GetController().SetPath(traj)
482486

483487
active_manipulators = self.GetTrajectoryManipulators(traj)

src/prpy/util.py

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,23 @@ def CreatePlannerParametersString(options, params=None,
9696

9797
return lxml.etree.tostring(params_xml)
9898

99+
def HasGroup(cspec, group_name):
100+
try:
101+
cspec.GetGroupFromName(group_name)
102+
return True
103+
except openravepy.openrave_exception:
104+
return False
105+
99106
def HasAffineDOFs(cspec):
100-
def has_group(cspec, group_name):
101-
try:
102-
cspec.GetGroupFromName(group_name)
103-
return True
104-
except openravepy.openrave_exception:
105-
return False
107+
return (HasGroup(cspec, 'affine_transform')
108+
or HasGroup(cspec, 'affine_velocities')
109+
or HasGroup(cspec, 'affine_accelerations'))
106110

107-
return (has_group(cspec, 'affine_transform')
108-
or has_group(cspec, 'affine_velocities')
109-
or has_group(cspec, 'affine_accelerations'))
111+
def HasJointDOFs(cspec):
112+
return (HasGroup(cspec, 'joint_values')
113+
or HasGroup(cspec, 'joint_velocities')
114+
or HasGroup(cspec, 'joint_accelerations')
115+
or HasGroup(cspec, 'joint_torques'))
110116

111117
def GetTrajectoryIndices(traj):
112118
try:
@@ -710,29 +716,56 @@ def IsAtTrajectoryStart(robot, trajectory):
710716
@returns: True if the robot's active DOFs match the given trajectory
711717
False if one or more active DOFs differ by DOF resolution
712718
"""
713-
# Get used indices and starting configuration from trajectory.
714719
cspec = trajectory.GetConfigurationSpecification()
715-
dof_indices, _ = cspec.ExtractUsedIndices(robot)
716-
traj_values = cspec.ExtractJointValues(
717-
trajectory.GetWaypoint(0), robot, dof_indices)
718-
719-
# Get current configuration of robot for used indices.
720-
with robot.GetEnv():
721-
robot_values = robot.GetDOFValues(dof_indices)
722-
dof_resolutions = robot.GetDOFResolutions(dof_indices)
723-
724-
# Check deviation in each DOF, using OpenRAVE's SubtractValue function.
725-
dof_infos = zip(dof_indices, traj_values, robot_values, dof_resolutions)
726-
for dof_index, traj_value, robot_value, dof_resolution in dof_infos:
727-
# Look up the Joint and Axis of the DOF from the robot.
728-
joint = robot.GetJointFromDOFIndex(dof_index)
729-
axis = dof_index - joint.GetDOFIndex()
730-
731-
# If any joint deviates too much, return False.
732-
delta_value = abs(joint.SubtractValue(traj_value, robot_value, axis))
733-
if delta_value > dof_resolution:
720+
needs_base = HasAffineDOFs(cspec)
721+
needs_joints = HasJointDOFs(cspec)
722+
723+
if needs_base and needs_joints:
724+
raise ValueError('Trajectories with affine and joint DOFs are not supported')
725+
726+
if needs_base:
727+
doft = openravepy.DOFAffine.X | openravepy.DOFAffine.Y | openravepy.DOFAffine.RotationAxis
728+
current_pose = openravepy.RaveGetAffineDOFValuesFromTransform(robot.GetTransform(), doft)
729+
start_transform = numpy.eye(4)
730+
start_transform = cspec.ExtractTransform(start_transform, trajectory.GetWaypoint(0), robot)
731+
traj_start = openravepy.RaveGetAffineDOFValuesFromTransform(start_transform, doft)
732+
733+
# Compare translation distance
734+
trans_delta_value = abs(current_pose[:2] - traj_start[:2])
735+
trans_resolution = robot.GetAffineTranslationResolution()[:2]
736+
if trans_delta_value[0] > trans_resolution[0] or \
737+
trans_delta_value[1] > trans_resolution[1]:
738+
return False
739+
740+
# Compare rotation distance
741+
rot_delta_value = (current_pose[2] - traj_start[2] + numpy.pi) % (2.*numpy.pi) - numpy.pi
742+
rot_resolution = robot.GetAffineRotationAxisResolution()[2] # Rotation about z?
743+
if rot_delta_value > rot_resolution:
734744
return False
735745

746+
else:
747+
# Get used indices and starting configuration from trajectory.
748+
dof_indices, _ = cspec.ExtractUsedIndices(robot)
749+
traj_values = cspec.ExtractJointValues(
750+
trajectory.GetWaypoint(0), robot, dof_indices)
751+
752+
# Get current configuration of robot for used indices.
753+
with robot.GetEnv():
754+
robot_values = robot.GetDOFValues(dof_indices)
755+
dof_resolutions = robot.GetDOFResolutions(dof_indices)
756+
757+
# Check deviation in each DOF, using OpenRAVE's SubtractValue function.
758+
dof_infos = zip(dof_indices, traj_values, robot_values, dof_resolutions)
759+
for dof_index, traj_value, robot_value, dof_resolution in dof_infos:
760+
# Look up the Joint and Axis of the DOF from the robot.
761+
joint = robot.GetJointFromDOFIndex(dof_index)
762+
axis = dof_index - joint.GetDOFIndex()
763+
764+
# If any joint deviates too much, return False.
765+
delta_value = abs(joint.SubtractValue(traj_value, robot_value, axis))
766+
if delta_value > dof_resolution:
767+
return False
768+
736769
# If all joints match, return True.
737770
return True
738771

0 commit comments

Comments
 (0)