首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IKPy问题: TypeError:__init__()缺少一个必需的位置参数:'origin_translation‘

IKPy问题: TypeError:__init__()缺少一个必需的位置参数:'origin_translation‘
EN

Stack Overflow用户
提问于 2022-06-12 22:53:33
回答 1查看 58关注 0票数 1

我试图在Python3.8中使用IKPy库(v3.3.3)在Mujoco平台上为我的机器人手臂编写一些演示。然而,当我尝试做的运动演示,有一些错误发生了如下所示。在这里输入图像描述下面是我的项目代码:

utils.py

代码语言:javascript
复制
from mujoco_py import MjViewer
import glfw

import numpy as np
import ikpy
from ikpy.chain import Chain
from ikpy.link import OriginLink, URDFLink

open_viewers = []  # a static list to keep track of all viewers


class MjViewerExtended(MjViewer):
    """ An extension of mujoco-py's MjViewer. MjViewerExtended does not
        terminate all other viewers and the python interpreter when closeing.
    """

    def __init__(self, sim):
        glfw.init()  # make sure glfw is initialized
        super().__init__(sim)
        open_viewers.append(self)

    def close(self):
        """ Closes the viewers glfw window. To open a new one, create a new
            instance of MjViewerExtended
        """
        # MjViewer only calls glfw.terminate() here killing glfw entierly.
        if glfw.window_should_close(self.window):
            return
        try:
            glfw.set_window_should_close(self.window, 1)
            glfw.destroy_window(self.window)
        except Exception:
            pass

        open_viewers.remove(self)
        if len(open_viewers) == 0:
            glfw.terminate()

    def key_callback(self, window, key, scancode, action, mods):
        if action == glfw.RELEASE and key == glfw.KEY_ESCAPE:
            self.close()
        else:
            super().key_callback(window, key, scancode, action, mods)


class Wam4IK(Chain):
    """ A basic kinamatic model of the MjWAM4 """

    def __init__(self, tool_orientation=None,
                 tool_translation=None,  # x, y, z
                 base_orientation=None,  # x, y, z
                 base_translation=None):

        if base_translation is None:
            base_translation = [0, 0, 0.84]
        if base_orientation is None:
            base_orientation = [0, 0, np.pi / 2]
        if tool_translation is None:
            tool_translation = [0, 0, 0]
        if tool_orientation is None:
            tool_orientation = [0, 0, 0]

        links = [OriginLink(),
                 URDFLink(name="wam/links/base",
                          translation=base_translation,  # translation of frame
                          origin_orientation=base_orientation,  # orientation of frame
                          rotation=[0, 0, 0]
                          ),  # joint axis [0, 0, 0] -> no joint
                 URDFLink(name="wam/links/shoulder_yaw",
                          translation=[0, 0, 0.16],
                          origin_orientation=[0, 0, 0],
                          rotation=[0, 0, 1]
                          ),
                 URDFLink(name="wam/links/shoulder_pitch",
                          translation=[0, 0, 0.186],
                          origin_orientation=[0, 0, 0],
                          rotation=[1, 0, 0]
                          ),
                 URDFLink(name="wam/links/shoulder_roll",
                          translation=[0, 0, 0],
                          origin_orientation=[0, 0, 0],
                          rotation=[0, 0, 1]
                          ),
                 URDFLink(name="wam/links/upper_arm",
                          translation=[0, -0.045, 0.550],
                          origin_orientation=[0, 0, 0],
                          rotation=[1, 0, 0]
                          ),
                 URDFLink(name="wam/links/tool_base_wo_plate",
                          translation=[0, 0.045, 0.350],
                          origin_orientation=[0, 0, 0],
                          rotation=[0, 0, 0]
                          ),
                 URDFLink(name="wam/links/tool_base_w_plate",
                          translation=[0, 0, 0.008],
                          origin_orientation=[0, 0, 0],
                          rotation=[0, 0, 0]
                          ),
                 URDFLink(name="wam/links/tool",
                          translation=tool_translation,
                          origin_orientation=tool_orientation,
                          rotation=[0, 0, 0]
                          )
                 ]

        self.all_joints = [False, False, True, True, True, True, False, False, False]
        self.active_joints = list(map(lambda x: x == 1, active_joints))
        self.active_links = [False, False, *self.active_joints, False, False, False]
        Chain.__init__(self, name='wam4',
                       active_links_mask=self.active_links,
                       links=links)

    def fk(self, joints, full_kinematics=False):
        joints = np.array([0, 0, *joints, 0, 0, 0])
        return Chain.forward_kinematics(self, joints, full_kinematics)

    def ik(self, target_position=None, target_orientation=None, orientation_mode=None, **kwargs):
        full = Chain.inverse_kinematics(self, target_position, target_orientation, orientation_mode, **kwargs)
        active = self.joints_from_links(full)
        return active

    def joints_from_links(self, joints):
        return np.compress(self.all_joints, joints, axis=0)

kinematics.py

代码语言:javascript
复制
import numpy as np
from mujoco_robots.utils import Wam4IK

import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d

# from juggling_wams.envs import SingleArmOneBall
from mujoco_robots.robots import MjWam4


def plot_joints(chain, qs):
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1, projection='3d', facecolor="1.0")
    for pos in qs:
        chain.plot([0, 0] + pos + [0, 0, 0], ax)
    plt.xlabel('x')
    plt.ylabel('y')
    plt.show()


def main():
    chain = Wam4IK(base_translation=[0, 0, 0.84],
                   base_orientation=[0, 0, np.pi / 2])

    links = ['wam/links/base',
             'wam/links/shoulder_yaw',
             'wam/links/shoulder_pitch',
             'wam/links/upper_arm',
             'wam/links/forearm',
             'wam/links/tool_base_wo_plate',
             'wam/links/tool_base_w_plate']

    x0 = np.array([0, 0, 0.84])
    q_test = [[0, 0, 0, 0], [1, 1, 1, 1]]

    robot = MjWam4(render=True, g_comp=True)
    for q in q_test:
        print(f'\n\ntesting for q={q}')
        robot.reset(pos=q)
        cart = chain.forward_kinematics([0, 0] + q + [0, 0, 0], full_kinematics=True)

        for i in range(7):
            print(f'\n{links[i][10:]}')
            mj_pos = robot.get_body_full_mat(links[i])[:3, 3] - x0
            ikpy_pos = cart[i + 1][:3, 3] - x0
            print(f'mj:   {mj_pos}')
            print(f'ikpy: {ikpy_pos}')
            print(f'diff: {mj_pos - ikpy_pos}')

    plot_joints(chain, q_test)

    # inverse kinematics
    x_des = [0.15, 0.86, 1.45]
    q = chain.active_from_full(chain.inverse_kinematics(x_des))
    robot.set_mocap_pos('endeff_des', x_des)
    robot.step(des_pos=q, n_steps=5000)


if __name__ == '__main__':
    main()

我认为我的环境设置和其他python文件没有问题。我认为问题应该发生在这两个文件中。如果你想看其他文件,我会很快上传它们。谢谢!

EN

回答 1

Stack Overflow用户

发布于 2022-06-13 03:00:28

你打了几个这样的电话:

代码语言:javascript
复制
                 URDFLink(name="wam/links/shoulder_yaw",
                          translation=[0, 0, 0.16],
                          origin_orientation=[0, 0, 0],
                          rotation=[0, 0, 1]
                          ),

请务必指定强制的origin_translation参数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72596431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档