不能给dict函数(AttributeError)

时间:2017-08-02 15:25:15

标签: python dictionary ros

我正在从rethinkrobotics编程机器人Sawyer 我想用字典limb_joints调用函数set_joint_position(),如下所示:

    ...
    limb_joints = dict(zip(resp.joints[0].name, resp.joints[0].position))
    #-print("Joints: " + str(tuple(resp.joints[0].position)))
    #-rospy.loginfo("\nIK Joint Solution:\n%s", limb_joints)
    else:
        rospy.logerr("INVALID POSE - No Valid Joint Solution Found.")
        rospy.logerr("Result Error %d", resp.result_type[0])
        return False

    while not rospy.is_shutdown():
        print("Moving robot to joint solution...")
        limb.set_joint_positions(limb_joints)
    ...

我得到的错误是:AttributeError:'str'对象没有属性'set_joint_positions'

有什么用处: 功能def

def set_joint_positions(self, positions): 
      """ 
      Commands the joints of this limb to the specified positions. 
      ...
      @type positions: dict({str:float}) 
      @param positions: joint_name:angle command 
      ...

输出,如果我取消注释rospy.loginfo并让程序运行:

IK Joint Solution:
{'right_j6': 3.00655557165754, 'right_j5': 0.542373122535165, 'right_j4': 0.3206092859358142, 'right_j3': 2.1274699085764, 'right_j2': 0.33896690311766786, 'right_j1': -1.062786744876718, 'right_j0': 0.2720637178544216}

我是python的新手,不知道为什么它不起作用。 在tutorial on their website中,函数被调用,就像我一样。

完整代码

    def __init__(self):

    # Verify limb parameters
    print("Validating limbs...")
    rp = intera_interface.RobotParams()
    valid_limbs = rp.get_limb_names()
    if not valid_limbs:
        rp.log_message(("Cannot detect any limb parameters on this robot. "
                        "Exiting."), "ERROR")
        return

    # Verify robot is enabled
    print("Getting robot state... ")
    rs = intera_interface.RobotEnable()
    print("Enabling robot... ")
    rs.enable()
    print("Done.")

    #Move in neutral position
    print("Moving to neutral position... ")
    limb = intera_interface.Limb('right')
    #limb.move_to_neutral()
    print("Done.")  


def doit(self, limb = "right"):
    # Initialising IK services
    ns = "ExternalTools/" + limb + "/PositionKinematicsNode/IKService"
    iksvc = rospy.ServiceProxy(ns, SolvePositionIK)
    ikreq = SolvePositionIKRequest()
    hdr = Header(stamp=rospy.Time.now(), frame_id='base')

    # Defining a new pose
    poses = {
        'right': PoseStamped(
            header=hdr,
            pose=Pose(
                position=Point(
                    x=0.450628752997,
                    y=0.161615832271,
                    z=0.217447307078,
                ),
                orientation=Quaternion(
                    x=0.704020578925,
                    y=0.710172716916,
                    z=0.00244101361829,
                    w=0.00194372088834,
                ),
            ),
        ),
    }

    # Add desired pose for inverse kinematics
    ikreq.pose_stamp.append(poses[limb])
    # Request inverse kinematics from base to "right_hand" link
    ikreq.tip_names.append('right_hand')

    try:
        rospy.wait_for_service(ns, 5.0)
        # resp enthaelt momentane joint positions
        resp = iksvc(ikreq)
    except (rospy.ServiceException, rospy.ROSException), e:
        rospy.logerr("Service call failed: %s" % (e,))
        return False

    # Check if result valid, and type of seed ultimately used to get solution
    if(resp.result_type[0] > 0):
        # Seed is optional, default mode: each strategy will be used
        seed_str = {
            ikreq.SEED_USER: 'User Provided Seed',
            ikreq.SEED_CURRENT: 'Current Joint Angles',
            ikreq.SEED_NS_MAP: 'Nullspace Setpoints',
        }.get(resp.result_type[0], 'None')
        rospy.loginfo("SUCCESS\nValid Joint Solution Found from Seed Type:\n%s" %(seed_str,))
        # Format solution into Limb API-compatible dictionary
        limb_joints = dict(zip(resp.joints[0].name, resp.joints[0].position))
        #-print("Joints: " + str(tuple(resp.joints[0].position)))
        # Formatted print of joints (instead of rospy.loginfo())
        #print("\nIK Joint Solution:")
        #pp = pprint.PrettyPrinter(indent=4)
        #pp.pprint(limb_joints)
        rospy.loginfo("\nIK Joint Solution:\n%s", limb_joints)
        rospy.loginfo("------------------")
    else:
        rospy.logerr("INVALID POSE - No Valid Joint Solution Found.")
        rospy.logerr("Result Error %d", resp.result_type[0])
        return False

    while not rospy.is_shutdown():
        print("Moving robot to joint solution...")
        limb.set_joint_positions(limb_joints)
        rospy.sleep(0.01) 

    return True     

if __name__ == '__main__':
# Init Node for communication with Master
rospy.init_node("PickAndPlace", anonymous=True)

# Calling init
pickandplace = PickAndPlace()
print("--- Initialisation complete. ---")

print("Calling doit()...")
pickandplace.doit()

0 个答案:

没有答案