Created
July 20, 2015 19:25
-
-
Save rethink-imcmahon/33c34ef39c7aabc85c3e to your computer and use it in GitHub Desktop.
Script for retrieving the IK solution of Baxter's current Joint Pose
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. | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions are met: | |
# | |
# 1. Redistributions of source code must retain the above copyright notice, | |
# this list of conditions and the following disclaimer. | |
# 2. Redistributions in binary form must reproduce the above copyright | |
# notice, this list of conditions and the following disclaimer in the | |
# documentation and/or other materials provided with the distribution. | |
# 3. Neither the name of the Rethink Robotics nor the names of its | |
# contributors may be used to endorse or promote products derived from | |
# this software without specific prior written permission. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
# POSSIBILITY OF SUCH DAMAGE. | |
""" | |
Baxter RSDK Inverse Kinematics Example | |
""" | |
import argparse | |
import struct | |
import sys | |
import rospy | |
import baxter_interface | |
from geometry_msgs.msg import ( | |
PoseStamped, | |
Pose, | |
Point, | |
Quaternion, | |
) | |
from std_msgs.msg import Header | |
from baxter_core_msgs.srv import ( | |
SolvePositionIK, | |
SolvePositionIKRequest, | |
) | |
def ik_test(limb): | |
rospy.init_node("rsdk_ik_service_client") | |
ns = "ExternalTools/" + limb + "/PositionKinematicsNode/IKService" | |
iksvc = rospy.ServiceProxy(ns, SolvePositionIK) | |
ikreq = SolvePositionIKRequest() | |
hdr = Header(stamp=rospy.Time.now(), frame_id='base') | |
limb_interface = baxter_interface.Limb(limb) | |
current_limb_dict = limb_interface.joint_angles() | |
current_pose = limb_interface.endpoint_pose() | |
ik_pose = PoseStamped() | |
ik_pose.pose.position.x = current_pose['position'].x | |
ik_pose.pose.position.y = current_pose['position'].y | |
ik_pose.pose.position.z = current_pose['position'].z | |
ik_pose.pose.orientation.x = current_pose['orientation'].x | |
ik_pose.pose.orientation.y = current_pose['orientation'].y | |
ik_pose.pose.orientation.z = current_pose['orientation'].z | |
ik_pose.pose.orientation.w = current_pose['orientation'].w | |
ik_pose.header = hdr | |
ikreq.pose_stamp.append(ik_pose) | |
print ikreq | |
try: | |
rospy.wait_for_service(ns, 5.0) | |
resp = iksvc(ikreq) | |
except (rospy.ServiceException, rospy.ROSException), e: | |
rospy.logerr("Service call failed: %s" % (e,)) | |
return 1 | |
# Check if result valid, and type of seed ultimately used to get solution | |
# convert rospy's string representation of uint8[]'s to int's | |
resp_seeds = struct.unpack('<%dB' % len(resp.result_type), | |
resp.result_type) | |
print resp_seeds | |
if (resp_seeds[0] != resp.RESULT_INVALID): | |
seed_str = { | |
ikreq.SEED_USER: 'User Provided Seed', | |
ikreq.SEED_CURRENT: 'Current Joint Angles', | |
ikreq.SEED_NS_MAP: 'Nullspace Setpoints', | |
}.get(resp_seeds[0], 'None') | |
print("SUCCESS - Valid Joint Solution Found from Seed Type: %s" % | |
(seed_str,)) | |
# Format solution into Limb API-compatible dictionary | |
limb_joints = dict(zip(resp.joints[0].name, resp.joints[0].position)) | |
print "\nIK Joint Solution:\n", limb_joints | |
print "------------------" | |
print "Response Message:\n", resp | |
else: | |
print("INVALID POSE - No Valid Joint Solution Found.") | |
limb_interface.move_to_joint_positions(limb_joints) | |
return 0 | |
def main(): | |
"""RSDK Inverse Kinematics Example | |
A simple example of using the Rethink Inverse Kinematics | |
Service which returns the joint angles and validity for | |
a requested Cartesian Pose. | |
Run this example, passing the *limb* to test, and the | |
example will call the Service with a sample Cartesian | |
Pose, using the current joint angles, printing the | |
response of whether a valid joint solution was found, | |
and if so, the corresponding joint angles. | |
""" | |
arg_fmt = argparse.RawDescriptionHelpFormatter | |
parser = argparse.ArgumentParser(formatter_class=arg_fmt, | |
description=main.__doc__) | |
parser.add_argument( | |
'-l', '--limb', choices=['left', 'right'], required=True, | |
help="the limb to test" | |
) | |
args = parser.parse_args(rospy.myargv()[1:]) | |
return ik_test(args.limb) | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment