💾 数据(SceneData)#
在上一章节中,我们了解了 SceneModel 用于描述静态的物理模型。本章节将重点介绍 SceneData的创建和使用方法。
基本概念#
SceneData 是 MotrixSim 仿真系统中的动态数据容器,存储了系统在运行过程中的所有变化量。这些数据不限于关节的位置和速度,还包括物体在空间中的位置和姿态、传感器的数值等。
SceneData 中的状态更新需要通过调用运动学函数来更新系统状态。
创建数据#
基本创建#
# tag::load_model_from_file[]
# The scene description file
path = "examples/assets/empty.xml"
# Load the scene model
model = load_model(path)
# end::load_model_from_file[]
# Create the render instance of the model
render.launch(model)
# Create the physics data of the model
data = SceneData(model)
完整的示例代码请参见 examples/empty.py。
多数据实例#
MotrixSim 支持基于同一个 SceneModel 创建多个独立的 SceneData 实例,适用于并行实验、状态备份、参数对比等场景。
# The scene description file
path = "examples/assets/model.xml"
# Load the scene model
model = load_model(path)
# Create the render instance of the model
# Try to create 3 model data
repeat = 3
render_offset_1 = [0, 0, 0]
render_offset_2 = [0, 1, 0]
render_offset_3 = [0, -1, 0]
render.launch(model, repeat, [render_offset_1, render_offset_2, render_offset_3])
# Create the physics data of the model
data_1 = SceneData(model)
data_2 = SceneData(model)
data_3 = SceneData(model)
各数据实例之间互不影响,可以独立进行状态更新。
完整的示例代码请参见 examples/model.py。
状态访问#
直接数组访问#
SceneData 提供了直接访问系统状态数组的方式:
pos = data.dof_pos_array
vel = data.dof_vel_array
详细的 SceneData 属性,请参阅:API 快速参考 - SceneData
通过组件访问#
结合 SceneModel 的 Named Access,可以通过组件对象来访问或设置特定的状态:
# Get position from body directly
dof_pos = cube_fb.get_dof_pos(data)
# Get velocity from body directly
dof_vel = cube_fb.get_dof_vel(data)
# Get position and rotation directly
# Note: This data will be delayed by one frame
pose = cube.get_pose(data)
print(f"dof_pos is : {dof_pos}")
print(f"dof_vel is : {dof_vel}")
print(f"pose is {pose}")
完整的示例代码请参见 examples/body.py。
API Reference#
更多与 SceneData 相关的 API,请参考 SceneData API