Joint Trajectory Playback Example

Jump to: navigation , search

Enable the robot joint trajectory interface, parse a file created using the joint position recorder example, and send the resulting joint trajectory to the action server.

Overview

A commonly used ROS method for robot arm motion control is the joint trajectory action interface. The trajectory_controller and it's corresponding joint trajectory action server is the intera_interface implementation to support this action interface. This example shows usage for launching the joint trajectory action server, and parsing of a text file describing timestamped joint position goals into a joint trajectory action call to be commanded to the joint trajectory action server.

Introduction

This example demonstrates the usage of the Joint Trajectory Action server to command raw joint position commands. The main() creates and instance of the Trajectory class and calls the parse_file() method. This method is responsible for parsing the input file and packing them as Request message for the Joint Trajectory Action server. The start() method is then called, which sends the Request messages to the action server. Finally, wait() is then called, which verifies if the trajectory execution was successful. Action Servers - robot/limb/right/follow_joint_trajectory. If you would like to follow along with the actual source code for the example on GitHub, it can be found through this link for joint trajectory file playback example.

Usage

Verify that the robot is enabled from an SDK terminal session, if the robot is not enabled run:

$ rosrun intera_interface robot_enable.py

Record a joint position file using the recorder.py example, ex:

$ rosrun intera_examples recorder.py -f <position_file_name>

The recorder is now recording joint positions with corresponding timestamps for robot arm. Move the arms while holding the cuffs. If the position file name exists, the function will overwrite existing file.

NOTE: You can open and close the gripper while recording by using the robot's cuff buttons: Oval(lower) = Close, Circle(Upper) = Open

Start the joint trajectory controller, ex:

$ rosrun intera_interface joint_trajectory_action_server.py --mode velocity

In another RSDK terminal session, Run the joint trajectory playback example program, ex:

$ rosrun intera_examples joint_trajectory_file_playback.py -f <position_file_name>

The robot arm will then be commanded to repeat the trajectory recorded during the joint position recording. The difference between this example and the joint_position playback example is that the trajectory controller has the ability to honor the velocities (due to the timestamps) to more accurately repeating the recorded trajectory.

Arguments

Important Arguments: Arguments for joint_trajectory_action_server.py

See the trajectory controller's usage on the command line by passing trajectory_controller.py the -h, help argument:

$ rosrun intera_interface joint_trajectory_action_server.py -h
Intera SDK Joint Trajectory Controller

    Unlike other robots running ROS, this is not a Motor Controller plugin,
    but a regular node using the SDK interface.    

optional arguments:
  -h, --help            show this help message and exit
  -l {right}, --limb {right}
                        joint trajectory action server limb (default: right)
  -r RATE, --rate RATE  trajectory control rate (Hz) (default: 100.0)
  -m {position_w_id,position,velocity}, --mode {position_w_id,position,velocity}
                        control mode for trajectory execution (default:
                        position_w_id)

Important Arguments: Arguments for joint_trajectory_file_playback.py

See the joint_trajectory_file_playback's usage on the command line by passing joint_trajectory_file_playback.py the -h, help argument:

$ rosrun intera_interface joint_trajectory_file_playback.py -h
RSDK Joint Trajectory Example: File Playback

    Plays back joint positions honoring timestamps recorded
    via the joint_recorder example.

    Run the joint_recorder.py example first to create a recording
    file for use with this example. Then make sure to start the
    joint_trajectory_action_server before running this example.

    This example will use the joint trajectory action server
    with velocity control to follow the positions and times of
    the recorded motion, accurately replicating movement speed
    necessary to hit each trajectory point on time.

required arguments:
  -f FILE, --file FILE   path to input file
optional arguments:
  -h, --help            show this help message and exit
  -l LOOPS, --loops LOOPS
                         number of playback loops. 0=infinite. (default: 1)

Interfaces

ROS APIs:

See the API Reference page for details: ROS APIs

Joint Trajectory Action Server - /robot/limb/right/follow_joint_trajectory [control_msgs/FollowJointTrajectoryAction]

Intera_interface APIs:

JointTrajectoryActionServer class: joint_trajectory_action_server.py

Code Walkthrough

Now, let's break down the code.

  1. import argparse
  2. import operator
  3. import sys
  4. import threading
  5.  
  6. from bisect import bisect
  7. from copy import copy
  8. from os import path
  9.  
  10. import rospy
  11.  
  12. import actionlib
  13.  
  14. from control_msgs.msg import (
  15.     FollowJointTrajectoryAction,
  16.     FollowJointTrajectoryGoal,
  17. )
  18. from trajectory_msgs.msg import (
  19.     JointTrajectoryPoint,
  20. )
  21.  
  22. import intera_interface
  23.  
  24. from intera_interface import CHECK_VERSION

This imports the intera_interface for accessing the limb and the gripper class. The actionlib is imported to use its Action Server class.The CHECK_VERSION is imported to check if the software running on the robot would be compatible with this local version. It is not necessary to check the version in custom programs.

  1. class Trajectory(object):
  2.     def __init__(self, limb="right"):
  3.         #create our action server clients
  4.         self._limb_client = actionlib.SimpleActionClient(
  5.             'robot/limb/right/follow_joint_trajectory',
  6.             FollowJointTrajectoryAction,
  7.         )
  8.  
  9.         #verify joint trajectory action servers are available
  10.         is_server_up = self._limb_client.wait_for_server(rospy.Duration(10.0))
  11.         if not is_server_up:
  12.             msg = ("Action server not available."
  13.                    " Verify action server availability.")
  14.             rospy.logerr(msg)
  15.             rospy.signal_shutdown(msg)
  16.             sys.exit(1)

Action server clients for the right limb is created. The existence of action server for right limb is checked with a timeout of 10 seconds. If it is not available, a shutdown signal is sent and the program exits.

  1.         #create our goal request
  2.         self.goal = FollowJointTrajectoryGoal()
  3.  
  4.         #limb interface - current angles needed for start move
  5.         self.arm = intera_interface.Limb(limb)
  6.  
  7.         self.limb = limb
  8.         self.gripper_name = '_'.join([limb, 'gripper'])
  9.         #gripper interface - for gripper command playback
  10.         try:
  11.             self.gripper = intera_interface.Gripper(limb)
  12.             self.has_gripper = True
  13.         except:
  14.             self.has_gripper = False
  15.             rospy.loginfo("Did not detect a connected electric gripper.")
  16.            
  17.         #flag to signify the arm trajectories have begun executing
  18.         self._arm_trajectory_started = False
  19.         #reentrant lock to prevent same-thread lockout
  20.         self._lock = threading.RLock()

The request message that will hold the goal position for right limbs' action server is created. The current joint is also captured. If there is a electric gripper connected, the gripper positions will be captured as well.

  1.         # Verify Grippers Have No Errors and are Calibrated
  2.         if self.has_gripper:
  3.             if self.gripper.has_error():
  4.                 self.gripper.reboot()
  5.             if not self.gripper.is_calibrated():
  6.                 self.gripper.calibrate()
  7.  
  8.             #gripper goal trajectories
  9.             self.grip = FollowJointTrajectoryGoal()
  10.  
  11.             #gripper control rate
  12.             self._gripper_rate = 20.0  # Hz
  13.  
  14.         # Timing offset to prevent gripper playback before trajectory has started
  15.         self._slow_move_offset = 0.0
  16.         self._trajectory_start_offset = rospy.Duration(0.0)
  17.         self._trajectory_actual_offset = rospy.Duration(0.0)
  18.  
  19.         #param namespace
  20.         self._param_ns = '/rsdk_joint_trajectory_action_server/'

The error() method returns whether or not the gripper is in an error state. The possible cause of errors might be over/under-voltage, over/under-current, motor faults, etc. The calibrated() method returns a boolean, true if the gripper is already calibrated. The request message that will hold the goal position for the gripper is created. The namespace is modified and the gripper's control rates is modified.

  1.     def _execute_gripper_commands(self):
  2.         start_time = rospy.get_time() - self._trajectory_actual_offset.to_sec()
  3.         grip_cmd = self.grip.trajectory.points
  4.         pnt_times = [pnt.time_from_start.to_sec() for pnt in grip_cmd]
  5.         end_time = pnt_times[-1]
  6.         rate = rospy.Rate(self._gripper_rate)
  7.         now_from_start = rospy.get_time() - start_time
  8.         while(now_from_start < end_time + (1.0 / self._gripper_rate) and
  9.               not rospy.is_shutdown()):
  10.             idx = bisect(pnt_times, now_from_start) - 1
  11.             if self.has_gripper:
  12.                 self.gripper.set_position(grip_cmd[idx].positions[0])
  13.             rate.sleep()
  14.             now_from_start = rospy.get_time() - start_time

The grip_cmd variable holds the gripper' position that was parsed from the file. The corresponding time stamps are read into pnt_times variable. idx holds the index of the timestamp from the file, relative to the current one. It is important to note that the velocity at which the Joint Positions was traversed is preserved during the playback. The corresponding gripper's position is then commanded to the action server.

  1.     def _clean_line(self, line, joint_names):
  2.         """
  3.        Cleans a single line of recorded joint positions
  4.  
  5.        @param line: the line described in a list to process
  6.        @param joint_names: joint name keys
  7.  
  8.        @return command: returns dictionary {joint: value} of valid commands
  9.        @return line: returns list of current line values stripped of commas
  10.        """
  11.         def try_float(x):
  12.             try:
  13.                 return float(x)
  14.             except ValueError:
  15.                 return None
  16.         #convert the line of strings to a float or None
  17.         line = [try_float(x) for x in line.rstrip().split(',')]
  18.         #zip the values with the joint names
  19.         combined = zip(joint_names[1:], line[1:])
  20.         #take out any tuples that have a none value
  21.         cleaned = [x for x in combined if x[1] is not None]
  22.         #convert it to a dictionary with only valid commands
  23.         command = dict(cleaned)
  24.         return (command, line,)

This try_float() function replaces the contents of the variable passed with float and none values. Then, a tuple is constructed by zipping the names passed with the values from the variable line. This may contain valid values as well as none values . After removing the none values, a dictionary is constructed. This dictionary is then parsed to create valid Joint commands.

  1.     def _add_point(self, positions, side, time):
  2.         """
  3.        Appends trajectory with new point
  4.  
  5.        @param positions: joint positions
  6.        @param side: limb to command point
  7.        @param time: time from start for point in seconds
  8.        """
  9.         #creates a point in trajectory with time_from_start and positions
  10.         point = JointTrajectoryPoint()
  11.         point.positions = copy(positions)
  12.         point.time_from_start = rospy.Duration(time)
  13.         if side == self.limb:
  14.             self.goal.trajectory.points.append(point)
  15.         elif self.has_gripper and side == self.gripper_name:
  16.             self.grip.trajectory.points.append(point)

This method creates a request message of the JointTrajectoryPoint type and appends the goal position based on the side of the limb/gripper that is requesting. It compiles a list of Joint Positions as the trajectory.

  1.     def parse_file(self, filename):
  2.         """
  3.        Parses input file into FollowJointTrajectoryGoal format
  4.  
  5.        @param filename: input filename
  6.        """
  7.         #open recorded file
  8.         with open(filename, 'r') as f:
  9.             lines = f.readlines()
  10.         #read joint names specified in file
  11.         joint_names = lines[0].rstrip().split(',')
  12.         #parse joint names for right limb
  13.         for name in joint_names:
  14.             if self.limb == name[:-3]:
  15.                 self.goal.trajectory.joint_names.append(name)

The lines are split into a list with ',' as the delimiter to extract the joint names. The arm of the current joint, found by stripping the "_<joint>" (last three characters) from the joint_name, is checked and stored.

  1.         def find_start_offset(pos):
  2.             #create empty lists
  3.             cur = []
  4.             cmd = []
  5.             dflt_vel = []
  6.             vel_param = self._param_ns + "%s_default_velocity"
  7.             #for all joints find our current and first commanded position
  8.             #reading default velocities from the parameter server if specified
  9.             for name in joint_names:
  10.                 if self.limb == name[:-3]:
  11.                     cmd.append(pos[name])
  12.                     cur.append(self.arm.joint_angle(name))
  13.                     prm = rospy.get_param(vel_param % name, 0.25)
  14.                     dflt_vel.append(prm)
  15.             diffs = map(operator.sub, cmd, cur)
  16.             diffs = map(operator.abs, diffs)
  17.             #determine the largest time offset necessary across all joints
  18.             offset = max(map(operator.div, diffs, dflt_vel))
  19.             return offset

The first commanded position and the current position for all the joints are captured. The default values that were loaded into the param server are read. The largest time offset necessary across all the joints is calculated.

  1.         for idx, values in enumerate(lines[1:]):
  2.             #clean each line of file
  3.             cmd, values = self._clean_line(values, joint_names)
  4.             #find allowable time offset for move to start position
  5.             if idx == 0:
  6.                 # Set the initial position to be the current pose.
  7.                 # This ensures we move slowly to the starting point of the
  8.                 # trajectory from the current pose - The user may have moved
  9.                 # arm since recording
  10.                 cur_cmd = [self.arm.joint_angle(jnt) for jnt in self.goal.trajectory.joint_names]
  11.                 self._add_point(cur_cmd, self.limb, 0.0)
  12.                 start_offset = find_start_offset(cmd)
  13.                 # Gripper playback won't start until the starting movement's
  14.                 # duration has passed, and the actual trajectory playback begins
  15.                 self._slow_move_offset = start_offset
  16.                 self._trajectory_start_offset = rospy.Duration(start_offset + values[0])
  17.             #add a point for this set of commands with recorded time
  18.             cur_cmd = [cmd[jnt] for jnt in self.goal.trajectory.joint_names]
  19.             self._add_point(cur_cmd, self.limb, values[0] + start_offset)
  20.             if self.has_gripper:
  21.                 cur_cmd = [cmd[self.gripper_name]]
  22.                 self._add_point(cur_cmd, self.gripper_name, values[0] + start_offset)

The non-float values are cleaned and a dictionary of valid Joint Commands is returned in the _clean_line() function as explained above. The time offset for move to start position is also calculated. The parsed set of recorded commands along with their recorded time is added to the goal list as well as gripper's command and time.

  1.     def _feedback(self, data):
  2.         # Test to see if the actual playback time has exceeded
  3.         # the move-to-start-pose timing offset
  4.         if (not self._get_trajectory_flag() and
  5.               data.actual.time_from_start >= self._trajectory_start_offset):
  6.             self._set_trajectory_flag(value=True)
  7.             self._trajectory_actual_offset = data.actual.time_from_start

Called in start function, test to see if the actual playback time has exceeded the move-to-start-pose timing offset.

  1.     def _set_trajectory_flag(self, value=False):
  2.         with self._lock:
  3.             # Assign a value to the flag
  4.             self._arm_trajectory_started = value
  5.  
  6.     def _get_trajectory_flag(self):
  7.         temp_flag = False
  8.         with self._lock:
  9.             # Copy to external variable
  10.             temp_flag = self._arm_trajectory_started
  11.         return temp_flag

Assign the value to the trajectory flag function and get the trajectory flag value function.

  1.     def start(self):
  2.         """
  3.        Sends FollowJointTrajectoryAction request
  4.        """
  5.         self._limb_client.send_goal(self.goal, feedback_cb=self._feedback)
  6.         # Syncronize playback by waiting for the trajectories to start
  7.         while not rospy.is_shutdown() and not self._get_trajectory_flag():
  8.             rospy.sleep(0.05)
  9.         if self.has_gripper:
  10.             self._execute_gripper_commands()

This function sends the FollowJointTrajectoryAction request message which includes the recorded positions and their corresponding time, to the Joint Trajectory Action Server.

  1.     def stop(self):
  2.         """
  3.        Preempts trajectory execution by sending cancel goals
  4.        """
  5.         if (self._limb_client.gh is not None and
  6.             self._limb_client.get_state() == actionlib.GoalStatus.ACTIVE):
  7.             self._limb_client.cancel_goal()
  8.  
  9.         #delay to allow for terminating handshake
  10.         rospy.sleep(0.1)

The Trajectory execution is stopped by sending cancel goals to the Joint trajectory action server.

  1.     def wait(self):
  2.         """
  3.        Waits for and verifies trajectory execution result
  4.        """
  5.         #create a timeout for our trajectory execution
  6.         #total time trajectory expected for trajectory execution plus a buffer
  7.         last_time = self.goal.trajectory.points[-1].time_from_start.to_sec()
  8.         time_buffer = rospy.get_param(self._param_ns + 'goal_time', 0.0) + 1.5
  9.         timeout = rospy.Duration(self._slow_move_offset +
  10.                                  last_time +
  11.                                  time_buffer)
  12.  
  13.         finish = self._limb_client.wait_for_result(timeout)
  14.         result = (self._limb_client.get_result().error_code == 0)
  15.  
  16.         #verify result
  17.         if all([finish, result]):
  18.             return True
  19.         else:
  20.             msg = ("Trajectory action failed or did not finish before "
  21.                    "timeout/interrupt.")
  22.             rospy.logwarn(msg)
  23.             return False

This method waits for the completion of the trajectory execution by Joint trajectory action server.

  1. def main():
  2.     """RSDK Joint Trajectory Example: File Playback
  3.  
  4.    Plays back joint positions honoring timestamps recorded
  5.    via the joint_recorder example.
  6.  
  7.    Run the joint_recorder.py example first to create a recording
  8.    file for use with this example. Then make sure to start the
  9.    joint_trajectory_action_server before running this example.
  10.  
  11.    This example will use the joint trajectory action server
  12.    with velocity control to follow the positions and times of
  13.    the recorded motion, accurately replicating movement speed
  14.    necessary to hit each trajectory point on time.
  15.    """
  16.     epilog = """
  17. Related examples:
  18.  joint_recorder.py; joint_position_file_playback.py.
  19.    """
  20.     arg_fmt = argparse.RawDescriptionHelpFormatter
  21.     parser = argparse.ArgumentParser(formatter_class=arg_fmt,
  22.                                      description=main.__doc__,
  23.                                      epilog=epilog)
  24.     parser.add_argument(
  25.         '-f', '--file', metavar='PATH', required=True,
  26.         help='path to input file'
  27.     )
  28.     parser.add_argument(
  29.         '-l', '--loops', type=int, default=1,
  30.         help='number of playback loops. 0=infinite.'
  31.     )
  32.     # remove ROS args and filename (sys.arv[0]) for argparse
  33.     args = parser.parse_args(rospy.myargv()[1:])

The name source file to read the Joint Position is captured along with the number of times the trajectory has to be looped from the command line.

  1.     print("Initializing node... ")
  2.     rospy.init_node("sdk_joint_trajectory_file_playback")
  3.     print("Getting robot state... ")
  4.     rs = intera_interface.RobotEnable(CHECK_VERSION)
  5.     print("Enabling robot... ")
  6.     rs.enable()
  7.     print("Running. Ctrl-c to quit")
  8.  
  9.     traj = Trajectory()
  10.     traj.parse_file(path.expanduser(args.file))
  11.     #for safe interrupt handling
  12.     rospy.on_shutdown(traj.stop)
  13.     result = True
  14.     loop_cnt = 1
  15.     loopstr = str(args.loops)
  16.     if args.loops == 0:
  17.         args.loops = float('inf')
  18.         loopstr = "forever"
  19.     while (result == True and loop_cnt <= args.loops
  20.            and not rospy.is_shutdown()):
  21.         print("Playback loop %d of %s" % (loop_cnt, loopstr,))
  22.         traj.start()
  23.         result = traj.wait()
  24.         loop_cnt = loop_cnt + 1
  25.     print("Exiting - File Playback Complete")
  26.  
  27. if __name__ == "__main__":
  28.     main()

The node is initialized. An instance of the Trajectory class is created. The parse_file() method is called to extract the recorded Joint Positions and their corresponding timestamps as explained above. The start() method is called to send the FollowJointTrajectory request message to the Joint Trajectory Action server. This loops for the user specified number of times.