[Python] How do i create a chain from a urdf file using IKpy?

Discussão em 'Python' iniciado por Stack, Maio 14, 2025.

  1. Stack

    Stack Membro Participativo

    So i'm programming a robot arm, and its structure is explained in a urdf file

    <robot name="Patrick">
    <link name="Base">
    <visual>
    <origin xyz="0.0 0.0 0.0017" rpy="0.0 0.0 0.0"/>
    <geometry>
    <cylinder radius="0.20" length="0.035"/>
    </geometry>
    </visual>
    </link>

    <link name="Arm_base">
    <visual>
    <color rgba="0.0 0.0 1.0 0.0"/>
    <geometry>
    <cylinder radius="0.06" length="0.10"/>
    </geometry>
    </visual>
    </link>

    <joint name="BaseJoint" type="revolute">
    <parent link="Base"/>
    <child link="Arm_base"/>
    <origin xyz="0.0 0.0 0.067" rpy="0.0 0.0 0.0"/>
    <axis xyz="0.0 0.0 -1"/>
    </joint>

    <link name="Lower_arm">
    <visual>
    <origin xyz="0.0 0.0 0.075" rpy="0.0 0.0 0.0"/>
    <geometry>
    <cylinder radius="0.03" length="0.15"/>
    </geometry>
    </visual>
    </link>

    <joint name="Lower_joint" type="revolute">
    <parent link="Arm_base"/>
    <child link="Lower_arm"/>
    <origin xyz="0.0 0.0 0.05"/>
    <axis xyz="1 0.0 0.0"/>
    <limit lower="-1.57" upper="1.57"/>
    </joint>

    <link name="Upper_arm">
    <visual>
    <origin xyz="0.0 0.0 0.075" rpy="0.0 0.0 0.0"/>
    <geometry>
    <cylinder radius="0.03" length="0.15"/>
    </geometry>
    </visual>
    </link>

    <joint name="Upper_joint" type="revolute">
    <parent link="Lower_arm"/>
    <child link="Upper_arm"/>
    <origin xyz="0.0 0.0 0.15" rpy="0.0 0.0 0.0"/>
    <axis xyz="1 0.0 0.0"/>
    <limit lower="-1.57" upper="1.57"/>
    </joint>

    <link name="Gripper">
    <visual>
    <origin xyz="0.0 0.0 0.0250" rpy="0.0 0.0 0.0"/>
    <geometry>
    <box size="0.05 0.05 0.05"/>
    </geometry>
    </visual>
    </link>

    <joint name="Arm_joint" type="revolute">
    <parent link="Upper_arm"/>
    <child link="Gripper"/>
    <origin xyz="0.0 0.0 0.15" rpy="0.0 0.0 0.0"/>
    <axis xyz="1 0.0 0"/>
    <limit lower="-1.57" upper="1.57"/>
    </joint>

    <link name="Gripper_rotation">
    <visual>
    <origin xyz="0.0 0.0 0.001" rpy="0.0 0.0 0.0"/>
    <geometry>
    <box size="0.06 0.07 0.02"/>
    </geometry>
    </visual>
    </link>

    <joint name="Arm_rotation" type="revolute">
    <parent link="Gripper"/>
    <child link="Gripper_rotation"/>
    <origin xyz="0.0 0.0 0.06" rpy="0.0 0.0 0.0"/>
    <axis xyz="0 0.0 1"/>
    <limit lower="-1.57" upper="1.57"/>
    </joint>

    </robot>


    This arm would be programmed using python IKpy, given code was a testcode.

    import ikpy.chain
    from ikpy.chain import Chain
    import numpy as np
    import ikpy.utils.plot as plot_utils

    import matplotlib.pyplot
    from mpl_toolkits.mplot3d import Axes3D

    my_chain = Chain.from_urdf_file("BITL.URDF")
    target_position = [ 0.1, -0.2, 0.1]
    print("The angles of each joints are : ", my_chain.inverse_kinematics(target_position))

    real_frame = my_chain.forward_kinematics(my_chain.inverse_kinematics(target_position))
    print("Computed position vector : %s, original position vector : %s" % (real_frame[:3, 3], target_position))

    ax = matplotlib.pyplot.figure().add_subplot(111, projection='3d')

    my_chain.plot(my_chain.inverse_kinematics([2, 2, 2]), ax)
    matplotlib.pyplot.show()


    but when ran i get this error:

    Traceback (most recent call last):
    File "c:\Users\Nick.Driesen\Documents\GitHub\BITL\IK\test.py", line 9, in <module>
    my_chain = Chain.from_urdf_file("BITL.URDF")
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\Nick.Driesen\AppData\Roaming\Python\Python312\site-packages\ikpy\chain.py", line 324, in from_urdf_file
    links = URDF.get_urdf_parameters(urdf_file, base_elements=base_elements, last_link_vector=last_link_vector, base_element_type=base_element_type, symbolic=symbolic)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\Nick.Driesen\AppData\Roaming\Python\Python312\site-packages\ikpy\urdf\URDF.py", line 184, in get_urdf_parameters
    tree = ET.parse(urdf_file)
    ^^^^^^^^^^^^^^^^^^^
    File "C:\Program Files\Python312\Lib\xml\etree\ElementTree.py", line 1204, in parse
    tree.parse(source, parser)
    File "C:\Program Files\Python312\Lib\xml\etree\ElementTree.py", line 558, in parse
    source = open(source, "rb")
    ^^^^^^^^^^^^^^^^^^
    FileNotFoundError: [Errno 2] No such file or directory: 'BITL.URDF'


    I'm using visual studio code on windows, don't know if this matters. Anyone know how to fix this ?

    First i added the from ikpy.chain import Chain, because at first Chain wasn't defined. but then i got this error.

    Continue reading...

Compartilhe esta Página