Mujoco robosuite 机器人模型

news2025/4/24 4:15:16
import ctypes
import os

# 获取当前脚本所在的目录
script_dir = os.path.dirname(os.path.abspath(__file__))

# 构建库文件的相对路径
lib_relative_path = os.path.join('dynamic_models', 'UR5e', 'Jb.so')

# 拼接成完整的路径
lib_path = os.path.join(script_dir, lib_relative_path)

try:
    fun_grav = ctypes.CDLL(lib_path)
    print("库文件加载成功!")
except OSError as e:
    print(f"加载库文件时出错: {e}")

<mujoco model="ur5">
    <compiler angle="radian" meshdir="ur5/collision/" />
    <size njmax="500" nconmax="100" />
    <asset>
        <mesh name="base" file="base.stl" />
        <mesh name="shoulder" file="shoulder.stl" />
        <mesh name="upperarm" file="upperarm.stl" />
        <mesh name="forearm" file="forearm.stl" />
        <mesh name="wrist1" file="wrist1.stl" />
        <mesh name="wrist2" file="wrist2.stl" />
        <mesh name="wrist3" file="wrist3.stl" />
    </asset>
    <worldbody>
        <geom type="mesh" contype="0" conaffinity="0" group="1" rgba="0.7 0.7 0.7 1" mesh="base" />
        <geom type="mesh" rgba="0.7 0.7 0.7 1" mesh="base" />
        <body name="shoulder_link" pos="0 0 0.089159">
            <inertial pos="0 0 0" mass="3.7" diaginertia="0.0102675 0.0102675 0.00666" />
            <joint name="shoulder_pan_joint" pos="0 0 0" axis="0 0 1" limited="true" range="-3.14159 3.14159" />
            <geom type="mesh" contype="0" conaffinity="0" group="1" rgba="0.7 0.7 0.7 1" mesh="shoulder" />
            <geom type="mesh" rgba="0.7 0.7 0.7 1" mesh="shoulder" />
            <body name="upper_arm_link" pos="0 0.13585 0" quat="0.707107 0 0.707107 0">
                <inertial pos="0 0 0.28" mass="8.393" diaginertia="0.226891 0.226891 0.0151074" />
                <joint name="shoulder_lift_joint" pos="0 0 0" axis="0 1 0" limited="true" range="-3.14159 3.14159" />
                <geom type="mesh" contype="0" conaffinity="0" group="1" rgba="0.7 0.7 0.7 1" mesh="upperarm" />
                <geom type="mesh" rgba="0.7 0.7 0.7 1" mesh="upperarm" />
                <body name="forearm_link" pos="0 -0.1197 0.425">
                    <inertial pos="0 0 0.196125" mass="2.275" diaginertia="0.0312168 0.0312168 0.004095" />
                    <joint name="elbow_joint" pos="0 0 0" axis="0 1 0" limited="true" range="-3.14159 3.14159" />
                    <geom type="mesh" contype="0" conaffinity="0" group="1" rgba="0.7 0.7 0.7 1" mesh="forearm" />
                    <geom type="mesh" rgba="0.7 0.7 0.7 1" mesh="forearm" />
                    <body name="wrist_1_link" pos="0 0 0.39225" quat="0.707107 0 0.707107 0">
                        <inertial pos="0 0.093 0" mass="1.219" diaginertia="0.0025599 0.0025599 0.0021942" />
                        <joint name="wrist_1_joint" pos="0 0 0" axis="0 1 0" limited="true" range="-3.14159 3.14159" />
                        <geom type="mesh" contype="0" conaffinity="0" group="1" rgba="0.7 0.7 0.7 1" mesh="wrist1" />
                        <geom type="mesh" rgba="0.7 0.7 0.7 1" mesh="wrist1" />
                        <body name="wrist_2_link" pos="0 0.093 0">
                            <inertial pos="0 0 0.09465" mass="1.219" diaginertia="0.0025599 0.0025599 0.0021942" />
                            <joint name="wrist_2_joint" pos="0 0 0" axis="0 0 1" limited="true" range="-3.14159 3.14159" />
                            <geom type="mesh" contype="0" conaffinity="0" group="1" rgba="0.7 0.7 0.7 1" mesh="wrist2" />
                            <geom type="mesh" rgba="0.7 0.7 0.7 1" mesh="wrist2" />
                            <body name="wrist_3_link" pos="0 0 0.09465">
                                <inertial pos="0 0.06505 0" quat="1.73123e-12 0.707107 -0.707107 1.73123e-12" mass="0.1879" diaginertia="0.000132117 8.46959e-05 8.46959e-05" />
                                <joint name="wrist_3_joint" pos="0 0 0" axis="0 1 0" limited="true" range="-3.14159 3.14159" />
                                <geom type="mesh" contype="0" conaffinity="0" group="1" rgba="0.7 0.7 0.7 1" mesh="wrist3" />
                                <geom type="mesh" rgba="0.7 0.7 0.7 1" mesh="wrist3" />
                                <site name="ee" pos="0 0 0" rgba="1 0 0 1" size="0.01" type="sphere"/>
                            </body>
                        </body>
                    </body>
                </body>
            </body>
        </body>
    </worldbody>
    <actuator>
        <motor name='motor1' ctrllimited="true" ctrlrange="-100 100" joint='shoulder_pan_joint' gear="1"/>
        <motor name='motor2' ctrllimited="true" ctrlrange="-100 100" joint='shoulder_lift_joint' gear="1"/>
        <motor name='motor3' ctrllimited="true" ctrlrange="-100 100" joint='elbow_joint' gear="1"/>
        <motor name='motor4' ctrllimited="true" ctrlrange="-100 100" joint='wrist_1_joint' gear="1"/>
        <motor name='motor5' ctrllimited="true" ctrlrange="-100 100" joint='wrist_2_joint' gear="1"/>
        <motor name='motor6' ctrllimited="true" ctrlrange="-100 100" joint='wrist_3_joint' gear="1"/>
    </actuator>
</mujoco>

雅克比

from dm_control.mujoco.wrapper import mjbindings
import numpy as np

mjlib = mjbindings.mjlib

def get_site_jac(model, data, site_id):
    """Return the Jacobian' translational component of the end-effector of
    the corresponding site id.
    """
    jacp = np.zeros((3, model.nv))
    jacr = np.zeros((3, model.nv))
    mjlib.mj_jacSite(model, data, jacp, jacr, site_id)
    jac = np.vstack([jacp, jacr])

    return jac

def get_fullM(model, data):
    M = np.zeros((model.nv, model.nv))
    mjlib.mj_fullM(model, M, data.qM)
    return M

任务空间惯量矩阵

def task_space_inertia_matrix(M, J, threshold=1e-3):
    """Generate the task-space inertia matrix

    Parameters
    ----------
    M: np.array
        the generalized coordinates inertia matrix
    J: np.array
        the task space Jacobian
    threshold: scalar, optional (Default: 1e-3)
        singular value threshold, if the detminant of Mx_inv is less than
        this value then Mx is calculated using the pseudo-inverse function
        and all singular values < threshold * .1 are set = 0
    """

    # calculate the inertia matrix in task space
    M_inv = np.linalg.inv(M)
    Mx_inv = np.dot(J, np.dot(M_inv, J.T))
    if abs(np.linalg.det(Mx_inv)) >= threshold:
        # do the linalg inverse if matrix is non-singular
        # because it's faster and more accurate
        Mx = np.linalg.inv(Mx_inv)
    else:
        # using the rcond to set singular values < thresh to 0
        # singular values < (rcond * max(singular_values)) set to 0
        Mx = np.linalg.pinv(Mx_inv, rcond=threshold * 0.1)

    return Mx, M_inv
def set_state(model, data, qpos, qvel):
    assert qpos.shape == (model.nq, ) and qvel.shape == (model.nv, )
    # old_state = data.get_state()
    # new_state = mujoco.MjSimState(old_state.time, qpos, qvel, old_state.act,
    #                                  old_state.udd_state)
    # data.set_state(new_state)
    # model.forward()
    data.qpos[:] = qpos
    data.qvel[:] = qvel


def get_contact_force(mj_model, mj_data, body_name):
    bodyId = mujoco.mj_name2id(mj_model, MJ_BODY_OBJ, body_name)
    force_com = mj_data.cfrc_ext[bodyId, :]
    trn_force = force_com.copy()

    return np.hstack((trn_force[3:], trn_force[:3]))


def get_geom_pose(model, geom_name):
    """Return the geom pose (relative to parent body).

    :param mujoco_py.MjModel model:
    :param str geom_name:
    :return: position, quaternion
    :rtype: tuple(np.array(3), np.array(4))
    """
    geom_id = mujoco.mj_name2id(model, MJ_GEOM_OBJ, geom_name)
    pos = model.geom_pos[geom_id, :]
    quat = model.geom_quat[geom_id, :]
    return pos, quat


def get_geom_size(model, geom_name):
    """Return the geom size.

    :param mujoco_py.MjModel model:
    :param str geom_name:
    :return: (radius, half-length, _) for cylinder geom, and
             (X half-size; Y half-size; Z half-size) for box geom
    :rtype: np.array(3)
    """
    geom_id = mujoco.mj_name2id(model, MJ_GEOM_OBJ, geom_name)
    if model.geom_type[geom_id] == MJ_BOX or model.geom_type[
            geom_id] == MJ_CYLINDER:
        return model.geom_size[geom_id, :]
    else:
        return None


def get_geom_friction(model, geom_name):
    geom_id = mujoco.mj_name2id(model, MJ_GEOM_OBJ, geom_name)
    return model.geom_friction[geom_id, :]

def get_body_mass(model, body_name):
    body_id = mujoco.mj_name2id(model, MJ_BODY_OBJ, body_name)
    return model.body_mass[body_id]


def get_body_pose(model, body_name):
    body_id = mujoco.mj_name2id(model, MJ_BODY_OBJ, body_name)
    return model.body_pos[body_id], model.body_quat[body_id]


def get_mesh_vertex_pos(model, geom_name):
    geom_id = mujoco.mj_name2id(model, MJ_GEOM_OBJ, geom_name)
    assert model.geom_type[geom_id] == MJ_MESH
    mesh_id = model.geom_dataid[geom_id]
    first_vertex_id = model.mesh_vertadr[mesh_id]
    no_vertex = model.mesh_vertnum[mesh_id]
    vertex_pos = model.mesh_vert[first_vertex_id:first_vertex_id + no_vertex]
    return vertex_pos


def set_geom_size(model, geom_name, size):
    geom_id = mujoco.mj_name2id(model, MJ_GEOM_OBJ, geom_name)
    model.geom_size[geom_id, :] = size


def set_body_mass(model, body_name, mass):
    body_id = mujoco.mj_name2id(model, MJ_BODY_OBJ, body_name)
    model.body_mass[body_id] = mass


def set_geom_friction(model, geom_name, friction):
    geom_id = mujoco.mj_name2id(model, MJ_GEOM_OBJ, geom_name)
    model.geom_friction[geom_id, :] = friction


def set_body_pose(model, body_name, pos, quat):
    body_id = mujoco.mj_name2id(model, MJ_BODY_OBJ, body_name)
    model.body_pos[body_id, :] = pos
    model.body_quat[body_id, :] = quat

def set_body_pose_rotm(model, body_name, pos, R):
    quat = mat2quat(R)
    body_id = mujoco.mj_name2id(model, MJ_BODY_OBJ, body_name)
    model.body_pos[body_id, :] = pos
    model.body_quat[body_id, :] = quat


# -------- GEOMETRY TOOLs
def quat_error(q1, q2):
    """Compute the rotation vector (expressed in the base frame), that if follow
        in a unit time, will transform a body with orientation `q1` to
        orientation `q2`

    :param list/np.ndarray q1: Description of parameter `q1`.
    :param list/np.ndarray q2: Description of parameter `q2`.
    :return: a 3D rotation vector
    :rtype: np.ndarray

    """
    if isinstance(q1, list):
        q1 = np.array(q1)

    if isinstance(q2, list):
        q2 = np.array(q2)

    dtype = q1.dtype
    neg_q1 = np.zeros(4, dtype=dtype)
    err_rot_quat = np.zeros(4, dtype=dtype)
    err_rot = np.zeros(3, dtype=dtype)

    if q1.dot(q2) < 0:
        q1 = -q1

    mujoco.mju_negQuat(neg_q1, q1)
    mujoco.mju_mulQuat(err_rot_quat, q2, neg_q1)
    mujoco.mju_quat2Vel(err_rot, err_rot_quat, 1)
    return err_rot


def quat2mat(q):
    """Tranform a quaternion to rotation amtrix.

    :param type q: Description of parameter `q`.
    :return: 3x3 rotation matrix
    :rtype: np.array
    """
    mat = np.zeros(9)
    mujoco.mju_quat2Mat(mat, q)
    return mat.reshape((3, 3))


def pose_transform(p1, q1, p21, q21):
    """Coordinate transformation between 2 frames

    :param np.ndarray p1: position in frame 1
    :param np.ndarray q1: orientation (quaternion) in frame 1
    :param np.ndarray p21: relative position between frame 1 and 2
    :param np.ndarray q21: relative orientation between frame 1 and 2
    :return: position and orientation in frame 2
    :rtype: type

    """
    # quat to rotation matrix
    R21 = quat2mat(q21)

    p2 = p21 + R21.dot(p1)
    q2 = np.zeros_like(q1)
    mujoco.mju_mulQuat(q2, q21, q1)  # q2 = q21*q1
    return p2, q2


def integrate_quat(q, r, dt):
    """Integrate quaternion by a fixed angular velocity over the duration dt.

    :param np.array(4) q: quaternion.
    :param np.array(3) r: angular velocity.
    :param float dt: duration.
    :return: result quaternion.
    :rtype: np.array(4)
    """
    qres = np.zeros(4)
    qe = np.zeros(4)
    r = r * dt
    angle = np.linalg.norm(r)
    if angle < 1e-9:
        # if angle too small then return current q
        return q.copy()
    axis = r / angle
    mujoco.mju_axisAngle2Quat(qe, axis, angle)
    mujoco.mju_mulQuat(qres, qe, q)
    return qres


def transform_spatial(v1, q21):
    """Coordinate transformation of a spatial vector. The spatial vector can be either
    twist (linear + angular velocity) or wrench (force + torque)

    :param type v1: Spatial vector in frame 1
    :param type q21: transformation matrix (in terms of quaternion)
    :return: Description of returned object.
    :rtype: type
    """
    R21 = quat2mat(q21)
    R = np.block([[R21, np.zeros((3, 3))], [np.zeros((3, 3)), R21]])
    return R.dot(v1)


def similarity_transform(A1, q21):
    """Similarity transformation of a matrix from frame 1 to frame 2
            A2 = R21 * A1 * R12

    :param np.array((3, 3)) A1: 3x3 matrix.
    :param np.array(4) q21: quaternion representation.
    :return: 3x3 matrix
    :rtype: np.array

    """
    R21 = quat2mat(q21)
    return R21.dot(A1.dot(R21.T))


# NOTE: there are infinite rotation vector solutions for a particular
# orientation, the `ref` is to find the closest solution to a reference.
# Is there another minimal representation that could avoid this?
def quat2vec(q, ref=None):
    """Transform quaternion representation to rotation vector representation"""
    r = np.zeros(3)
    scale = 1
    mujoco.mju_quat2Vel(r, q, scale)
    if ref is not None:
        if r.dot(ref) < 0:
            angle = np.linalg.norm(r)
            r = r / angle
            angle = angle - 2 * np.pi
            r = r * angle
    return r


def inverse_frame(p, q):
    pi, qi = np.zeros(3), np.zeros(4)
    mujoco.mju_negPose(pi, qi, p, q)
    return pi, qi


def mat2quat(R):
    R = R.flatten()
    q = np.zeros(4)
    mujoco.mju_mat2Quat(q, R)
    return q


def mul_quat(q1, q2):
    q = np.zeros(4)
    mujoco.mju_mulQuat(q, q1, q2)
    return q

https://github.com/google-deepmind/mujoco_menageriehttps://github.com/google-deepmind/mujoco_menagerie

https://github.com/ARISE-Initiative/robosuitehttps://github.com/ARISE-Initiative/robosuite

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2341198.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

K8s:概念、特点、核心组件与简单应用

一、引言 在当今云计算和容器技术蓬勃发展的时代&#xff0c;Kubernetes&#xff08;简称 K8s&#xff09;已成为容器编排领域的事实标准。它为管理容器化应用提供了高效、可靠的解决方案&#xff0c;极大地简化了应用的部署、扩展和运维过程。无论是小型初创公司还是大型企业…

STM32的定时器输出PWM时,死区时间(DTR)如何计算

在 STM32F429&#xff08;以及所有 STM32F4 “高级定时器”&#xff09;中&#xff0c;死区时间由 TIMx_BDTR 寄存器的 8 位 “Dead‑Time Generator” 字段 DTG[7:0] 来配置。其计算分三步&#xff1a; 计算死区时钟周期 tDTS TIM1 时钟源为 APB2 定时器时钟&#xff08;PCL…

STC32G12K128单片机GPIO模式SPI操作NorFlash并实现FatFS文件系统

STC32G12K128单片机GPIO模式SPI操作NorFlash并实现FatFS文件系统 NorFlash简介NorFlash操作驱动代码文件系统测试代码 NorFlash简介 NOR Flash是一种类型的非易失性存储器&#xff0c;它允许在不移除电源的情况下保留数据。NOR Flash的名字来源于其内部结构中使用的NOR逻辑门。…

ClickHouse 设计与细节

1. 引言 ClickHouse 是一款备受欢迎的开源列式在线分析处理 (OLAP) 数据库管理系统&#xff0c;专为在海量数据集上实现高性能实时分析而设计&#xff0c;并具备极高的数据摄取速率 1。其在各种行业中得到了广泛应用&#xff0c;包括众多知名企业&#xff0c;例如超过半数的财…

智能体MCP 实现数据可视化分析

参考: 在线体验 https://www.doubao.com/chat/ 下载安装离线体验 WPS软件上的表格分析 云上创建 阿里mcp:https://developer.aliyun.com/article/1661198 (搜索加可视化) 案例 用cline 或者cherry studio实现 mcp server:excel-mcp-server、quickchart-mcp-server

再看开源多模态RAG的视觉文档(OCR-Free)检索增强生成方案-VDocRAG

前期几个工作提到&#xff0c;基于OCR的文档解析RAG的方式进行知识库问答&#xff0c;受限文档结构复杂多样&#xff0c;各个环节的解析泛化能力较差&#xff0c;无法完美的对文档进行解析。因此出现了一些基于多模态大模型的RAG方案。如下&#xff1a; 【RAG&多模态】多模…

深入浅出 NVIDIA CUDA 架构与并行计算技术

&#x1f407;明明跟你说过&#xff1a;个人主页 &#x1f3c5;个人专栏&#xff1a;《深度探秘&#xff1a;AI界的007》 &#x1f3c5; &#x1f516;行路有良友&#xff0c;便是天堂&#x1f516; 目录 一、引言 1、CUDA为何重要&#xff1a;并行计算的时代 2、NVIDIA在…

FPGA系列之DDS信号发生器设计(DE2-115开发板)

一、IP核 IP(Intellectual Property)原指知识产权、著作权等&#xff0c;在IC设计领域通常被理解为实现某种功能的设计。IP模块则是完成某种比较复杂算法或功能&#xff08;如FIR滤波器、FFT、SDRAM控制器、PCIe接口、CPU核等&#xff09;并且参数可修改的电路模块&#xff0c…

【Dv3Admin】从零搭建Git项目安装·配置·初始化

项目采用 Django 与 Vue3 技术栈构建&#xff0c;具备强大的后端扩展能力与现代前端交互体验。完整实现了权限管理、任务队列、WebSocket 通信、系统配置等功能&#xff0c;适用于构建中后台管理系统与多租户平台。 本文章内容涵盖环境搭建、虚拟环境配置、前后端部署、项目结…

P3416-图论-法1.BFS / 法2.Floyd

这道题虽然标签有floyd但是直接bfs也能过 其实事实证明还是bfs快&#xff0c;因为bfs只需要遍历特定的点&#xff0c;但是floyd需要考虑遍历所有可能的中介点 法1.BFS 用字典存储每个点所能普及的范围&#xff0c;然后用对每个点bfs进行拓展 nint(input())temp[]#xmax0;yma…

极狐GitLab 议题和史诗创建的速率限制如何设置?

极狐GitLab 是 GitLab 在中国的发行版&#xff0c;关于中文参考文档和资料有&#xff1a; 极狐GitLab 中文文档极狐GitLab 中文论坛极狐GitLab 官网 议题和史诗创建的速率限制 (BASIC SELF) 速率限制是为了控制新史诗和议题的创建速度。例如&#xff0c;如果您将限制设置为 …

提交到Gitee仓库

文章目录 注册配置公钥创建空白的码云仓库把本地项目上传到码云对应的空白仓库中 注册 注册并激活码云账号&#xff08; 注册页面地址&#xff1a;https://gitee.com/signup &#xff09; 可以在自己C盘/用户/用户名/.ssh 可以看到 有id_rsa.pub 以前在GitHub注册时搞过&…

oracle中错误总结

oracle中给表起别名不能用as&#xff0c;用as报错 在 Oracle 数据库中&#xff0c;​​WITH 子句&#xff08;即 CTE&#xff0c;公共表表达式&#xff09;允许后续定义的子查询引用前面已经定义的 CTE​​&#xff0c;但 ​​前面的 CTE 无法引用后面的 CTE​​。这种设计类似…

纽约大学具身智能体在城市空间中的视觉导航之旅!CityWalker:从海量网络视频中学习城市导航

作者&#xff1a;Xinhao Liu, Jintong Li, Yicheng Jiang, Niranjan Sujay, Zhicheng Yang, Juexiao Zhang, John Abanes, Jing Zhang, Chen Feng单位&#xff1a;纽约大学论文标题&#xff1a;CityWalker: Learning Embodied Urban Navigation from Web-Scale Videos论文链接&…

OpenCV颜色变换cvtColor

OpenCV计算机视觉开发实践&#xff1a;基于Qt C - 商品搜索 - 京东 颜色变换是imgproc模块中一个常用的功能。我们生活中看到的大多数彩色图片都是RGB类型的&#xff0c;但是在进行图像处理时需要用到灰度图、二值图、HSV&#xff08;六角锥体模型&#xff0c;这个模型中颜色的…

Manus技术架构、实现内幕及分布式智能体项目实战

Manus技术架构、实现内幕及分布式智能体项目实战 模块一&#xff1a; 剖析Manus分布式多智能体全生命周期、九大核心模块及MCP协议&#xff0c;构建低幻觉、高效且具备动态失败处理能力的Manus系统。 模块二&#xff1a; 解析Manus大模型Agent操作电脑的原理与关键API&#xf…

下载油管视频 - yt-dlp

文章目录 1. yt-dlp与you-get介绍1.1 主要功能对比1.2 使用场景1.3 安装 2. 基本命令介绍2.1 默认下载视频2.2 指定画质和格式规则2.3 下载播放列表2.4 备注 3. 参考资料 之前只使用you-get下载b站视频&#xff0c;当时了解you-get也可下载油管视频&#xff0c;但之前无此需求&…

济南通过首个备案生活服务大模型,打造行业新标杆

近日&#xff0c;一则振奋人心的消息在人工智能领域传开&#xff1a;济南本土企业丽阳神州智能科技有限公司自主研发的 “丽阳雨露” 大模型成功通过国家网信办的备案。这一成果不仅是济南企业在科技创新道路上的重大突破&#xff0c;更标志着我国在生活服务领域的人工智能应用…

第6次课 贪心算法 A

向日葵朝着太阳转动&#xff0c;时刻追求自身成长的最大可能。 贪心策略在一轮轮的简单选择中&#xff0c;逐步导向最佳答案。 课堂学习 引入 贪心算法&#xff08;英语&#xff1a;greedy algorithm&#xff09;&#xff0c;是用计算机来模拟一个「贪心」的人做出决策的过程…

Hexo+Github+gitee图床零成本搭建自己的专属博客

一个详细、完善的 Hexo 博客部署教程&#xff0c;不仅涵盖了基本的安装、配置、生成与部署步骤&#xff0c;还增加了常见问题的解决、主题设置、图片上传等 在开始之前可以看看我最终搭建出来的成果&#xff1a;https://liangjh.blog 1.安装git和nodejs 在Windows上使用Git&a…