AttributeError:'bool'对象没有属性'items'

时间:2016-06-21 02:56:56

标签: python attributeerror

我是python的初学者。我目前正试图处理使用IK来移动机器人手臂。当我尝试运行程序时,手臂可以移动到我设置的起始位置,但是当它进入下一步时,它会向我显示此错误:AttributeError: 'bool' object has no attribute 'items'

这是我的计划:

class Pick_Place (object):

    #def __init__(self,limb,hover_distance = 0.15):
    def __init__(self,limb):
       self._limb = baxter_interface.Limb(limb)
       self._gripper = baxter_interface.Gripper(limb)
       self._gripper.calibrate(limb)
       #self.gripper_open()
       #self._verbose = verbose
       ns = "ExternalTools/" + limb + "/PositionKinematicsNode/IKService"
       self._iksvc = rospy.ServiceProxy(ns,SolvePositionIK) 
       rospy.wait_for_service(ns, 5.0)

    def move_to_start (self,start_angles = None):

            print ("moving.....")
            if not start_angles:
                print ("it is 0")
                start_angles = dict(zip(self._joint_names, [0]*7))

            self._guarded_move_to_joint_position(start_angles)
            self.gripper_open()
            rospy.sleep(1.0)
            print ("moved!!!")

#########################IK_Server################################################
    def ik_request (self,pose):
        hdr = Header(stamp=rospy.Time.now(),frame_id='base')
        ikreq = SolvePositionIKRequest()
        ikreq.pose_stamp.append(PoseStamped(header=hdr, pose=pose))
        try:
            resp = self._iksvc (ikreq)

        except (rospy.ServiceException, rospy.ROSException), e:
            rospy.logerr("Service call failed: %s" % (e,))
        return False

        limb_joints = {}
        limb_joints = dict(zip(resp.joints[0].name, resp.joints[0].position))

        return limb_joints
###################################################################################

    def _guarded_move_to_joint_position(self,joint_angles):
          print ("joint position.....")
          self._limb.move_to_joint_positions(joint_angles)

    def gripper_open (self):
        self._gripper.open()
        rospy.sleep(1.0)

    def gripper_close (self):
        self._gripper.close()
        rospy.sleep(1.0)
#################################Individual_Motion####################################
    def _approach (self, pose):
        print ("\nApproaching.....")

        approach = copy.deepcopy(pose)
        approach.position.z = approach.position.z #+ self._hover_distance
        joint_angles = self.ik_request(approach)
        self._guarded_move_to_joint_position(joint_angles)

        print ("\nApproached.....")

    def _retract (self):
        print ("\nRetracting.....")
        current_pose = self._limb.endpoint_pose()
        ik_pose = Pose()
        ik_pose.position.x = current_pose['position'].x
        ik_pose.position.y = current_pose['position'].y
        ik_pose.position.z = current_pose['position'].z #+ self._hover_distance
        ik_pose.orientation.x = current_pose['orientation'].x
        ik_pose.orientation.y = current_pose['orientation'].y
        ik_pose.orientation.z = current_pose['orientation'].z
        ik_pose.orientation.w = current_pose['orientation'].w
        joint_angles = self.ik_request(ik_pose)

        self._guarded_move_to_joint_position(joint_angles)
        print ("\nRetracted......")

    def _servo_to_pose (self, pose):
        print ("\nPosing.....")
        joint_angles = self.ik_request(pose)
        self._guarded_move_to_joint_position(joint_angles)
        print ("\nPosed.....")


##########################Motion_of_pick_and_place#####################################

    def pick (self,pose):
        print ("\nPicking_1.....")
        # open the gripper
        self.gripper_open()
        # servo above pose
        self._approach(pose)
        # servo to pose
        self._servo_to_pose(pose)
        # close gripper
        self.gripper_close()
        # retract to clear object
        self._retract()
        print ("\nPicked")

    def place (self,pose):
        print ("\nPlacing_1.....")
        # servo above pose
        self._approach(pose)
        # servo to pose
        self._servo_to_pose(pose)
        # open the gripper
        self.gripper_open()
        # retract to clear object
        self._retract()
        print ("\nPlaced")

###########################Main_Program############################################
def main():

    print ("Initializing....")
    rospy.init_node("ylj_ik_traTest")
    print("Getting the robot state.....")
    rs= baxter_interface.RobotEnable()
    print ("Enabling....")
    rs.enable()


    limb = 'left'
    #hover_distance = 0.15

    starting_joint_angles = {'left_s0': -0.50,
                             'left_s1': -1.30,
                             'left_e0': -0.60,
                             'left_e1':  1.30,
                             'left_w0':  0.20,
                             'left_w1':  1.60,
                             'left_w2': -0.30}

    #pnp = Pick_Place(limb,hover_distance)
    pnp = Pick_Place(limb)

    overhead_orientation = Quaternion(
                             x=-0.0249590815779,
                             y=0.999649402929,
                             z=0.00737916180073,
                             w=0.00486450832011)
    ball_poses = list()
    #1st ball point
    ball_poses.append(Pose(
        position = Point(x=0.7, y=0.15, z=-0.1),
        orientation = overhead_orientation))

    #2nd ball point
    ball_poses.append(Pose(
        position = Point(x=0.75, y=0.0, z=-0.1),
        orientation = overhead_orientation))

    pnp.move_to_start(starting_joint_angles)
    idx = 0
    while not rospy.is_shutdown():
        print ("\nPicking.....")
        pnp.pick(ball_poses[idx])

        print ("\nPlacing.....")
        idx = (idx+1) % len(ball_poses)#?????
        pnp.place(ball_poses[idx])
    return 0

if __name__ == '__main__':
    sys.exit(main())

这就是错误告诉我:

Initializing....
Getting the robot state.....
Enabling....
[INFO] [WallTime: 1466477391.621678] Robot Enabled
moving.....
joint position.....
moved!!!

Picking.....

Picking_1.....

Approaching.....
joint position.....
Traceback (most recent call last):
  File "/home/baxter/ros_ws/src/baxter_examples/scripts/ylj_research/ylj_ik_traTest.py", line 197, in <module>
    sys.exit(main())
  File "/home/baxter/ros_ws/src/baxter_examples/scripts/ylj_research/ylj_ik_traTest.py", line 188, in main
    pnp.pick(ball_poses[idx])
  File "/home/baxter/ros_ws/src/baxter_examples/scripts/ylj_research/ylj_ik_traTest.py", line 122, in pick
    self._approach(pose)
  File "/home/baxter/ros_ws/src/baxter_examples/scripts/ylj_research/ylj_ik_traTest.py", line 88, in _approach
    self._guarded_move_to_joint_position(joint_angles)
  File "/home/baxter/ros_ws/src/baxter_examples/scripts/ylj_research/ylj_ik_traTest.py", line 72, in _guarded_move_to_joint_position
    self._limb.move_to_joint_positions(joint_angles)
  File "/home/baxter/ros_ws/src/baxter_interface/src/baxter_interface/limb.py", line 368, in move_to_joint_positions
    diffs = [genf(j, a) for j, a in positions.items() if
AttributeError: 'bool' object has no attribute 'items'

之前有没有人遇到过这种错误?请帮帮我,谢谢。

2 个答案:

答案 0 :(得分:2)

错误告诉您布尔值(无论是真还是假)都没有属性&#34; items&#34;。

致电时

self._limb.move_to_joint_positions(joint_angles)你正在传递参数&#34; joint_angles&#34;成为&#34;职位&#34;在功能中 move_to_joint_positions()

查看source code of the library you're using,它会告诉您它想要的位置: @type positions: dict({str:float})

简而言之,它希望joint_angles成为将字符串映射到浮点数的字典,并且您传递了一个布尔值。让我们来看看你是如何得到joint_angles的:

joint_angles = self.ik_request(ik_pose)

在方法体中,每次都返回False:

def ik_request (self,pose): ... except (rospy.ServiceException, rospy.ROSException), e: rospy.logerr("Service call failed: %s" % (e,)) return False

返回布尔值显然不是你想要做的,而且是造成这个错误的原因。

答案 1 :(得分:0)

您尝试访问布尔值的元素,这是不允许的。在您的示例中,positions是真/假值,而不是您期望的值。代码的某些部分将positions分配给布尔值。您应该浏览代码并查找包含positions = something的任何行。