Created
April 14, 2015 14:53
-
-
Save rethink-imcmahon/0affaf2767476b17f73f to your computer and use it in GitHub Desktop.
JTAS Waypoint Tester
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Copyright (c) 2013-2015, Rethink Robotics | |
# All rights reserved. | |
# This script is meant to how the JTAS handles various numbers of points | |
# It executes a set of trajectories with 4 waypoints, then 3, then 2, then 1 waypoints. To run it: | |
# # one baxter.sh sourced terminal | |
# $ rosrun baxter_tools enable_robot.py -e | |
# $ rosrun baxter_interface joint_trajectory_action_server.py --mode velocity | |
# # another sourced terminal | |
# $ ./test_joint_trajectory_client.py -l <left/right> | |
""" | |
Baxter RSDK Joint Trajectory Action Client Test | |
""" | |
import argparse | |
import sys | |
from copy import copy | |
import rospy | |
import actionlib | |
from control_msgs.msg import ( | |
FollowJointTrajectoryAction, | |
FollowJointTrajectoryGoal, | |
) | |
from trajectory_msgs.msg import ( | |
JointTrajectoryPoint, | |
) | |
import baxter_interface | |
from baxter_interface import CHECK_VERSION | |
class Trajectory(object): | |
def __init__(self, limb): | |
ns = 'robot/limb/' + limb + '/' | |
self._client = actionlib.SimpleActionClient( | |
ns + "follow_joint_trajectory", | |
FollowJointTrajectoryAction, | |
) | |
self._goal = FollowJointTrajectoryGoal() | |
self._goal_time_tolerance = rospy.Time(0.1) | |
self._goal.goal_time_tolerance = self._goal_time_tolerance | |
server_up = self._client.wait_for_server(timeout=rospy.Duration(10.0)) | |
if not server_up: | |
rospy.logerr("Timed out waiting for Joint Trajectory" | |
" Action Server to connect. Start the action server" | |
" before running example.") | |
rospy.signal_shutdown("Timed out waiting for Action Server") | |
sys.exit(1) | |
self.clear(limb) | |
def add_point(self, positions, time): | |
point = JointTrajectoryPoint() | |
point.positions = copy(positions) | |
point.time_from_start = rospy.Duration(time) | |
self._goal.trajectory.points.append(point) | |
def start(self): | |
self._goal.trajectory.header.stamp = rospy.Time.now() | |
self._client.send_goal(self._goal) | |
def stop(self): | |
self._client.cancel_goal() | |
def wait(self, timeout=15.0): | |
self._client.wait_for_result(timeout=rospy.Duration(timeout)) | |
def result(self): | |
return self._client.get_result() | |
def clear(self, limb): | |
self._goal = FollowJointTrajectoryGoal() | |
self._goal.goal_time_tolerance = self._goal_time_tolerance | |
self._goal.trajectory.joint_names = [limb + '_' + joint for joint in \ | |
['s0', 's1', 'e0', 'e1', 'w0', 'w1', 'w2']] | |
def main(): | |
"""RSDK Joint Trajectory Example: Simple Action Client | |
Creates a client of the Joint Trajectory Action Server | |
to send commands of standard action type, | |
control_msgs/FollowJointTrajectoryAction. | |
Make sure to start the joint_trajectory_action_server.py | |
first. Then run this example on a specified limb to | |
command a short series of trajectory points for the arm | |
to follow. | |
""" | |
arg_fmt = argparse.RawDescriptionHelpFormatter | |
parser = argparse.ArgumentParser(formatter_class=arg_fmt, | |
description=main.__doc__) | |
required = parser.add_argument_group('required arguments') | |
required.add_argument( | |
'-l', '--limb', required=True, choices=['left', 'right'], | |
help='send joint trajectory to which limb' | |
) | |
args = parser.parse_args(rospy.myargv()[1:]) | |
limb = args.limb | |
print("Initializing node... ") | |
rospy.init_node("rsdk_joint_trajectory_client_%s" % (limb,)) | |
print("Getting robot state... ") | |
rs = baxter_interface.RobotEnable(CHECK_VERSION) | |
print("Enabling robot... ") | |
rs.enable() | |
print("Running. Ctrl-c to quit") | |
positions = { | |
'left': [-0.11, -0.62, -1.15, 1.32, 0.80, 1.27, 2.39], | |
'right': [0.11, -0.62, 1.15, 1.32, -0.80, 1.27, -2.39], | |
} | |
traj = Trajectory(limb) | |
rospy.on_shutdown(traj.stop) | |
# Command Current Joint Positions first | |
limb_interface = baxter_interface.limb.Limb(limb) | |
print("Executing four waypoint JTAS Path....") | |
current_angles = [limb_interface.joint_angle(joint) for joint in limb_interface.joint_names()] | |
traj.add_point(current_angles, 0.0) | |
p1 = positions[limb] | |
traj.add_point(p1, 7.0) | |
traj.add_point([x * 0.75 for x in p1], 9.0) | |
traj.add_point([x * 1.25 for x in p1], 12.0) | |
traj.start() | |
traj.wait(15.0) | |
print("Joint Trajectory Action Four Waypoint Test Complete") | |
print("Executing three waypoint JTAS Path...") | |
traj = Trajectory(limb) | |
rospy.on_shutdown(traj.stop) | |
current_angles = [limb_interface.joint_angle(joint) for joint in limb_interface.joint_names()] | |
traj.add_point(current_angles, 0.0) | |
traj.add_point([x * 0.75 for x in p1], 9.0) | |
traj.add_point([x * 1.25 for x in p1], 12.0) | |
traj.start() | |
traj.wait(15.0) | |
print("Joint Trajectory Action Three Waypoint Test Complete") | |
print("Executing two waypoint JTAS Path...") | |
traj = Trajectory(limb) | |
rospy.on_shutdown(traj.stop) | |
current_angles = [limb_interface.joint_angle(joint) for joint in limb_interface.joint_names()] | |
traj.add_point(current_angles, 0.0) | |
traj.add_point([x * 0.75 for x in p1], 9.0) | |
traj.start() | |
traj.wait(15.0) | |
print("Joint Trajectory Action Two Waypoint Test Complete") | |
print("Executing one waypoint JTAS Path..") | |
traj = Trajectory(limb) | |
rospy.on_shutdown(traj.stop) | |
traj.add_point([x * 1.25 for x in p1], 12.0) | |
traj.start() | |
traj.wait(15.0) | |
print("Exiting - Joint Trajectory Action One Waypoint Test Complete") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment