import distutils.util import subprocess if subprocess.run('nvidia-smi').returncode: raise RuntimeError( 'Cannot communicate with GPU. ' 'Make sure you are using a GPU Colab runtime. ' 'Go to the Runtime menu and select Choose runtime type.') # Configure MuJoCo to use the EGL rendering backend (requires GPU) print('Setting environment variable to use GPU rendering:') %env MUJOCO_GL=egl
try: print('Checking that the installation succeeded:') import mujoco mujoco.MjModel.from_xml_string('<mujoco/>') except Exception as e: raise e from RuntimeError( 'Something went wrong during installation. Check the shell output above ' 'for more information.\n' 'If using a hosted Colab runtime, make sure you enable GPU acceleration ' 'by going to the Runtime menu and selecting "Choose runtime type".')
print('Installation successful.')
其它导入包与帮助函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#@title Other imports and helper functions import numpy as np from typing importCallable, Optional, Union, List import scipy.linalg
# Graphics and plotting. print('Installing mediapy:') !command -v ffmpeg >/dev/null || (apt update && apt install -y ffmpeg) !pip install -q mediapy import mediapy as media import matplotlib.pyplot as plt
# More legible printing from numpy. np.set_printoptions(precision=3, suppress=True, linewidth=100)
加载与渲染标准人体模型
1 2 3 4
print('Getting MuJoCo humanoid XML description from GitHub:') !git clone https://github.com/deepmind/mujoco withopen('mujoco/model/humanoid/humanoid.xml', 'r') as f: xml = f.read()
# Make a new camera, move it to a closer distance. camera = mujoco.MjvCamera() mujoco.mjv_defaultFreeCamera(model, camera) camera.distance = 2
mujoco.mj_resetDataKeyframe(model, data, 1)
frames = [] while data.time < DURATION: # Set control vector. data.ctrl = np.random.randn(model.nu)
# Step the simulation. mujoco.mj_step(model, data)
# Render and save frames. iflen(frames) < data.time * FRAMERATE: # Set the lookat point to the humanoid's center of mass. camera.lookat = data.body('torso').subtree_com
mujoco.mj_resetDataKeyframe(model, data, 1) mujoco.mj_forward(model, data) data.qacc = 0# Assert that there is no the acceleration. mujoco.mj_inverse(model, data) print(data.qfrc_inverse)
# Set the state and controls to their setpoints. mujoco.mj_resetData(model, data) data.qpos = qpos0 data.ctrl = ctrl0
frames = [] while data.time < DURATION: # Step the simulation. mujoco.mj_step(model, data)
# Render and save frames. iflen(frames) < data.time * FRAMERATE: # Set the lookat point to the humanoid's center of mass. camera.lookat = data.body('torso').subtree_com renderer.update_scene(data, camera) pixels = renderer.render() frames.append(pixels)
# Get the Jacobian for the root body (torso) CoM. mujoco.mj_resetData(model, data) data.qpos = qpos0 mujoco.mj_forward(model, data) jac_com = np.zeros((3, nv)) mujoco.mj_jacSubtreeCom(model, data, jac_com, model.body('torso').id)
# Get the Jacobian for the left foot. jac_foot = np.zeros((3, nv)) mujoco.mj_jacBodyCom(model, data, jac_foot, None, model.body('foot_left').id)
# Get all joint names. joint_names = [model.joint(i).name for i inrange(model.njnt)]
# Get indices into relevant sets of joints. root_dofs = range(6) body_dofs = range(6, nv) abdomen_dofs = [ model.joint(name).dofadr[0] for name in joint_names if'abdomen'in name andnot'z'in name ] left_leg_dofs = [ model.joint(name).dofadr[0] for name in joint_names if'left'in name and ('hip'in name or'knee'in name or'ankle'in name) andnot'z'in name ] balance_dofs = abdomen_dofs + left_leg_dofs other_dofs = np.setdiff1d(body_dofs, balance_dofs)
# Set the initial state and control. mujoco.mj_resetData(model, data) data.ctrl = ctrl0 data.qpos = qpos0
# Allocate the A and B matrices, compute them. A = np.zeros((2*nv, 2*nv)) B = np.zeros((2*nv, nu)) epsilon = 1e-6 centered = True mujoco.mjd_transitionFD(model, data, epsilon, centered, A, B, None, None)