示例#1
0
    def generateToppTrajectoryCallback(self, req):
        print("Generating TOPP trajectory.")
        res = GenerateTrajectoryResponse()
        dof = len(req.waypoints.points[0].positions)
        n = len(req.waypoints.points)

        # If there is not enough waypoints to generate a trajectory return false
        if (n <= 1 or dof == 0):
            print(
                "You must provide at least 2 points to generate a valid trajectory."
            )
            res.trajectory.success = False
            return res

        # Generate trajectory
        # Path is of dimensions DOF x n_waypoints
        path = np.zeros([dof, n])

        for i in range(0, n):
            for j in range(0, dof):
                path[j][i] = req.waypoints.points[i].positions[j]

        traj0 = Utilities.InterpolateViapoints(
            path)  # Interpolate using splines

        # Constraints
        vmax = zeros(dof)  # Velocity limits
        amax = zeros(dof)  # Acceleration limits
        for i in range(0, dof):
            vmax[i] = req.waypoints.points[0].velocities[i]
            amax[i] = req.waypoints.points[0].accelerations[i]

        # Set up the TOPP instance
        trajectorystring = str(traj0)
        discrtimestep = 0.01
        uselegacy = False
        t0 = time.time()
        if uselegacy:  #Using the legacy KinematicLimits (a bit faster but not fully supported)
            constraintstring = str(discrtimestep)
            constraintstring += "\n" + string.join([str(v) for v in vmax])
            constraintstring += "\n" + string.join([str(a) for a in amax])
            x = TOPPbindings.TOPPInstance(None, "KinematicLimits",
                                          constraintstring, trajectorystring)
        else:  #Using the general QuadraticConstraints (fully supported)
            constraintstring = str(discrtimestep)
            constraintstring += "\n" + string.join([str(v) for v in vmax])
            constraintstring += TOPPpy.ComputeKinematicConstraints(
                traj0, amax, discrtimestep)
            x = TOPPbindings.TOPPInstance(None, "QuadraticConstraints",
                                          constraintstring, trajectorystring)

        # Run TOPP
        t1 = time.time()
        ret = x.RunComputeProfiles(0, 0)
        x.ReparameterizeTrajectory()
        t2 = time.time()

        print("Using legacy:", uselegacy)
        print("Discretization step:", discrtimestep)
        print("Setup TOPP:", t1 - t0)
        print("Run TOPP:", t2 - t1)
        print("Total:", t2 - t0)

        x.WriteProfilesList()
        x.WriteSwitchPointsList()
        profileslist = TOPPpy.ProfilesFromString(x.resprofilesliststring)
        switchpointslist = TOPPpy.SwitchPointsFromString(
            x.switchpointsliststring)
        TOPPpy.PlotProfiles(profileslist, switchpointslist, 4)
        x.WriteResultTrajectory()
        traj1 = Trajectory.PiecewisePolynomialTrajectory.FromString(
            x.restrajectorystring)
        dtplot = 0.01
        if self.plot_flag == True:
            ion()
            TOPPpy.PlotKinematics(traj0, traj1, dtplot, vmax, amax)

        res.trajectory = self.TOPP2JointTrajectory(traj1,
                                                   req.sampling_frequency)
        res.success = True
        return res
示例#2
0
amax = 10 * ones(ndof)  # Acceleration limits

# Set up the TOPP instance
discrtimestep = 0.01
uselegacy = True
t0 = time.time()
if uselegacy:  #Using the legacy KinematicLimits (a bit faster but not fully supported)
    constraintstring = str(discrtimestep)
    constraintstring += "\n" + " ".join([str(v) for v in vmax])
    constraintstring += "\n" + " ".join([str(a) for a in amax])
    x = TOPPbindings.TOPPInstance(None, "KinematicLimits", constraintstring,
                                  trajectorystring)
else:  #Using the general QuadraticConstraints (fully supported)
    constraintstring = str(discrtimestep)
    constraintstring += "\n" + " ".join([str(v) for v in vmax])
    constraintstring += TOPPpy.ComputeKinematicConstraints(
        traj0, amax, discrtimestep)
    x = TOPPbindings.TOPPInstance(None, "QuadraticConstraints",
                                  constraintstring, trajectorystring)

# Run TOPP
t1 = time.time()
ret = x.RunComputeProfiles(0, 0)
x.ReparameterizeTrajectory()
t2 = time.time()

print("Using legacy:", uselegacy)
print("Discretization step:", discrtimestep)
print("Setup TOPP:", t1 - t0)
print("Run TOPP:", t2 - t1)
print("Total:", t2 - t0)
def call_TOPP(req):

    ### Get the request path and prepare for TOPP
    t0 = time.time()
    print("= Get the request path...")
    path_points = req.path
    vel_limits = req.vel_limits
    acc_limits = req.acc_limits
    discrtimestep = req.timestep

    # create a path going through viapoints
    print("== Create a path going through viapoints...")
    dim = len(path_points[0].positions)
    ndata = len(path_points)
    path_array = np.zeros((dim, ndata))
    for i in range(ndata):
        path_array[:, i] = np.array(path_points[i].positions)
    print("=== Interpolate viapoints...")
    #import pdb
    #pdb.set_trace()
    path = Utilities.InterpolateViapoints(
        path_array)  # Interpolate using splines

    # Constraints
    vmax = np.array(vel_limits)
    amax = np.array(acc_limits)

    ### Set up the TOPP instance
    print("==== Set up a TOPP instance...")
    t1 = time.time()
    trajectorystring = str(path)
    uselegacy = True  # True to use KinematicLimits(vel & acc)
    if uselegacy:  #Using the legacy KinematicLimits (a bit faster but not fully supported)
        constraintstring = str(discrtimestep)
        constraintstring += "\n" + string.join([str(v) for v in vmax])
        constraintstring += "\n" + string.join([str(a) for a in amax])
        x = TOPPbindings.TOPPInstance(None, "KinematicLimits",
                                      constraintstring, trajectorystring)
    else:  #Using the general QuadraticConstraints (fully supported)
        constraintstring = str(discrtimestep)
        constraintstring += "\n" + string.join([str(v) for v in vmax])
        constraintstring += TOPPpy.ComputeKinematicConstraints(
            path, amax, discrtimestep)
        x = TOPPbindings.TOPPInstance(None, "QuadraticConstraints",
                                      constraintstring, trajectorystring)

    ### Run TOPP
    print("===== Run TOPP...")
    t2 = time.time()
    #import pdb
    #pdb.set_trace()
    ret = x.RunComputeProfiles(0, 0)
    x.ReparameterizeTrajectory()

    ### Convert resultant array to plan
    print("====== Convert resultant array to plan...")
    t3 = time.time()
    x.WriteResultTrajectory(
    )  # be sure to write the result before reading it!!!
    traj = Trajectory.PiecewisePolynomialTrajectory.FromString(
        x.restrajectorystring)
    traj_point = JointTrajectoryPoint()
    traj_points = []  #PathToTrajResponse() # just an empty list
    t = 0.0
    while t < traj.duration:
        traj_point.positions = traj.Eval(t).tolist()
        traj_point.velocities = traj.Evald(t).tolist()
        traj_point.accelerations = traj.Evaldd(t).tolist()
        traj_point.time_from_start = rospy.Duration(t)
        traj_points.append(copy.deepcopy(traj_point))
        t += discrtimestep
    # the last point
    traj_point.positions = traj.Eval(traj.duration).tolist()
    traj_point.velocities = traj.Evald(traj.duration).tolist()
    traj_point.accelerations = traj.Evaldd(traj.duration).tolist()
    traj_point.time_from_start = rospy.Duration(traj.duration)
    traj_points.append(copy.deepcopy(traj_point))

    ### Display statistics
    t4 = time.time()
    print(">>>>> Statistics of TOPP <<<<<")
    print("Dimension of path point: " + str(path_array.shape[0]))
    print("Number of data points: " + str(path_array.shape[1]))
    print("Using legacy: " + str(uselegacy))
    print("Discretization step: " + str(discrtimestep) + " s")
    print("Obtain request path and get prepared: " + str(t1 - t0) + " s")
    print("Setup TOPP: " + str(t2 - t1) + " s")
    print("Run TOPP: " + str(t3 - t2) + " s")
    print("Convert resultant array to plan: " + str(t4 - t3) + " s")
    print("Total: " + str(t4 - t0) + " s")
    print("Trajectory duration before TOPP: " + str(path.duration) + " s")
    print("Trajectory duration after TOPP: " + str(traj.duration) + " s")
    print(">>>>> End <<<<<")

    ### Return result
    res = PathToTrajResponse()
    res.traj = traj_points
    return res