|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright (c) 2015, Carnegie Mellon University |
| 4 | +# All rights reserved. |
| 5 | +# Authors: Siddhartha Srinivasa <siddh@cs.cmu.edu> |
| 6 | +# |
| 7 | +# Redistribution and use in source and binary forms, with or without |
| 8 | +# modification, are permitted provided that the following conditions are met: |
| 9 | +# |
| 10 | +# - Redistributions of source code must retain the above copyright notice, this |
| 11 | +# list of conditions and the following disclaimer. |
| 12 | +# - Redistributions in binary form must reproduce the above copyright notice, |
| 13 | +# this list of conditions and the following disclaimer in the documentation |
| 14 | +# and/or other materials provided with the distribution. |
| 15 | +# - Neither the name of Carnegie Mellon University nor the names of its |
| 16 | +# contributors may be used to endorse or promote products derived from this |
| 17 | +# software without specific prior written permission. |
| 18 | +# |
| 19 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 23 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | +# POSSIBILITY OF SUCH DAMAGE. |
| 30 | + |
| 31 | +import logging |
| 32 | +import numpy |
| 33 | +import openravepy |
| 34 | +import time |
| 35 | +from base import BasePlanner, PlanningError, PlanningMethod |
| 36 | +import prpy.util |
| 37 | + |
| 38 | +logger = logging.getLogger('planning') |
| 39 | + |
| 40 | + |
| 41 | +class VectorfieldPlanner(BasePlanner): |
| 42 | + def __init__(self): |
| 43 | + super(VectorfieldPlanner, self).__init__() |
| 44 | + |
| 45 | + def __str__(self): |
| 46 | + return 'VectorfieldPlanner' |
| 47 | + |
| 48 | + def _geodesictwist(t1, t2): |
| 49 | + ''' |
| 50 | + Computes the twist in global coordinates that corresponds |
| 51 | + to the gradient of the geodesic distance between two transforms. |
| 52 | +
|
| 53 | + @param t1 current transform |
| 54 | + @param t2 goal transform |
| 55 | + @return twist in se(3) |
| 56 | + ''' |
| 57 | + trel = numpy.dot(numpy.linalg.inv(t1), t2) |
| 58 | + trans = numpy.dot(t1[0:3, 0:3], trel[0:3, 3]) |
| 59 | + omega = numpy.dot(t1[0:3, 0:3], |
| 60 | + openravepy.axisAngleFromRotationMatrix( |
| 61 | + trel[0:3, 0:3])) |
| 62 | + return numpy.hstack((trans, omega)) |
| 63 | + |
| 64 | + @PlanningMethod |
| 65 | + def PlanToEndEffectorPose(self, robot, goal_pose, timelimit=5.0, |
| 66 | + dq_tol=0.0001, **kw_args): |
| 67 | + """ |
| 68 | + Plan to an end effector pose by following a geodesic loss function |
| 69 | + in SE(3) via an optimized Jacobian. |
| 70 | +
|
| 71 | + @param robot |
| 72 | + @param goal_pose desired end-effector pose |
| 73 | + @param timelimit time limit before giving up |
| 74 | + @param dq_tol velocity tolerance for termination |
| 75 | + @return traj |
| 76 | + """ |
| 77 | + |
| 78 | + start_time = time.time() |
| 79 | + |
| 80 | + # save all active bodies so we only check collision with those |
| 81 | + active_bodies = [] |
| 82 | + for body in self.env.GetBodies(): |
| 83 | + if body.IsEnabled(): |
| 84 | + active_bodies.append(body) |
| 85 | + |
| 86 | + with robot: |
| 87 | + manip = robot.GetActiveManipulator() |
| 88 | + robot.SetActiveDOFs(manip.GetArmIndices()) |
| 89 | + qtraj = openravepy.RaveCreateTrajectory(self.env, '') |
| 90 | + qtraj.Init(manip.GetArmConfigurationSpecification()) |
| 91 | + |
| 92 | + dt = min(robot.GetDOFResolutions()/robot.GetDOFVelocityLimits()) |
| 93 | + while True: |
| 94 | + # Check for a timeout. |
| 95 | + current_time = time.time() |
| 96 | + if (timelimit is not None and |
| 97 | + current_time - start_time > timelimit): |
| 98 | + raise PlanningError('Reached time limit.') |
| 99 | + |
| 100 | + q_curr = robot.GetActiveDOFValues() |
| 101 | + # Check for collisions. |
| 102 | + for body in active_bodies: |
| 103 | + if self.env.CheckCollision(robot, body): |
| 104 | + raise PlanningError('Encountered collision.') |
| 105 | + if robot.CheckSelfCollision(): |
| 106 | + raise PlanningError('Encountered self-collision.') |
| 107 | + |
| 108 | + # Add to trajectory |
| 109 | + qtraj.Insert(qtraj.GetNumWaypoints(), q_curr) |
| 110 | + |
| 111 | + t_curr = manip.GetEndEffectorTransform() |
| 112 | + twist = self._geodesictwist(t_curr, goal_pose) |
| 113 | + dqout, tout = prpy.util.ComputeJointVelocityFromTwist( |
| 114 | + robot, twist) |
| 115 | + if (numpy.linalg.norm(dqout) < dq_tol): |
| 116 | + break |
| 117 | + qnew = q_curr + dqout*dt |
| 118 | + robot.SetActiveDOFValues(qnew) |
| 119 | + |
| 120 | + return qtraj |
0 commit comments