Example #1
0
    def follow_joint_trajectory(self, traj):
        traj = np.r_[np.atleast_2d(self.get_joint_positions()), traj]
        for i in [2,4,6]:
            traj[:,i] = np.unwrap(traj[:,i])

        times = retiming.retime_with_vel_limits(traj, self.vel_limits)
        times_up = np.arange(0,times[-1],.1)
        traj_up = mu.interp2d(times_up, times, traj)
        vels = resampling.get_velocities(traj_up, times_up, .001)
        self.follow_timed_joint_trajectory(traj_up, vels, times_up)
def retime_hmats(lhmats, rhmats, max_cart_vel=.02, upsample_time=.1):
    """
    retimes hmats (4x4 transforms) for left and right grippers
    """
    assert len(lhmats) == len(rhmats)
    cart_traj = np.empty((len(rhmats), 6))
    for i in xrange(len(lhmats)):
        cart_traj[i,:3] = lhmats[i][:3,3]
        cart_traj[i,3:] = rhmats[i][:3,3]
    times    = retiming.retime_with_vel_limits(cart_traj, np.repeat(max_cart_vel, 6))
    times_up = np.linspace(0, times[-1], times[-1]/upsample_time) if times[-1] > upsample_time else times
    lhmats_up = resampling.interp_hmats(times_up, times, lhmats)
    rhmats_up = resampling.interp_hmats(times_up, times, rhmats)
    return (lhmats_up, rhmats_up)
Example #3
0
def retime_traj(robot, inds, traj, base_hmats, max_cart_vel=.02, upsample_time=.1):
    """retime a trajectory so that it executes slowly enough for the simulation"""
    cart_traj = np.empty((len(traj), 6))
    leftarm, rightarm = robot.GetManipulator("leftarm"), robot.GetManipulator("rightarm")
    with robot:
        for i in range(len(traj)):
            robot.SetDOFValues(traj[i], inds)
            cart_traj[i,:3] = leftarm.GetTransform()[:3,3]
            cart_traj[i,3:] = rightarm.GetTransform()[:3,3]

    times = retiming.retime_with_vel_limits(cart_traj, np.repeat(max_cart_vel, 6))
    times_up = np.linspace(0, times[-1], times[-1]/upsample_time) if times[-1] > upsample_time else times
    traj_up = math_utils.interp2d(times_up, times, traj)
    
    if base_hmats != None:
        base_traj = [mat_to_base_pose(base_hmat) for base_hmat in base_hmats]
        base_traj_up = math_utils.interp2d(times_up, times, base_traj)
        base_hmats_up = [base_pose_to_mat(base_pose) for base_pose in base_traj_up]
    else:
        base_hmats_up = None
    
    return traj_up, base_hmats_up
def follow_body_traj(pr2, bodypart2traj, wait=True, base_frame = "/base_footprint", speed_factor=1):    

    LOG.info("following trajectory with bodyparts %s", " ".join(bodypart2traj.keys()))
    
    name2part = {"lgrip":pr2.lgrip, 
                 "rgrip":pr2.rgrip, 
                 "larm":pr2.larm, 
                 "rarm":pr2.rarm,
                 "base":pr2.base}
    for partname in bodypart2traj:
        if partname not in name2part:
            raise Exception("invalid part name %s"%partname)
            
            
    #### Go to initial positions #######
            
            
    for (name, part) in name2part.items():
        if name in bodypart2traj:
            part_traj = bodypart2traj[name]        
            if name == "lgrip" or name == "rgrip":
                part.set_angle(np.squeeze(part_traj)[0])
            elif name == "larm" or name == "rarm":
                part.goto_joint_positions(part_traj[0])
            elif name == "base":
                part.goto_pose(part_traj[0], base_frame)          
    pr2.join_all()  


    #### Construct total trajectory so we can retime it #######

            
    n_dof = 0            
    trajectories = []
    vel_limits = []
    acc_limits = []
    bodypart2inds = {}
    for (name, part) in name2part.items():
        if name in bodypart2traj:
            traj = bodypart2traj[name]
            if traj.ndim == 1: traj = traj.reshape(-1,1)
            trajectories.append(traj)
            vel_limits.extend(part.vel_limits)
            acc_limits.extend(part.acc_limits)
            bodypart2inds[name] = range(n_dof, n_dof+part.n_joints)
            n_dof += part.n_joints
                        
    trajectories = np.concatenate(trajectories, 1)    
    
    vel_limits = np.array(vel_limits)*speed_factor
    

    times = retiming.retime_with_vel_limits(trajectories, vel_limits)
    times_up = np.linspace(0, times[-1], int(np.ceil(times[-1]/.1)))
    traj_up = mu.interp2d(times_up, times, trajectories)
    
    
    #### Send all part trajectories ###########
    
    for (name, part) in name2part.items():
        if name in bodypart2traj:
            part_traj = traj_up[:,bodypart2inds[name]]
            if name == "lgrip" or name == "rgrip":
                part.follow_timed_trajectory(times_up, part_traj.flatten())
            elif name == "larm" or name == "rarm":
                vels = resampling.get_velocities(part_traj, times_up, .001)
                part.follow_timed_joint_trajectory(part_traj, vels, times_up)
            elif name == "base":
                part.follow_timed_trajectory(times_up, part_traj, base_frame)
                
    if wait: pr2.join_all()    
    
    return True