Basics 基础
对应目录:demo/basics/,按编号从最小可运行闭环逐步扩展到各类实体的创建与控制。
示例一览
| 目录 | 重点 | 入口脚本 |
|---|---|---|
01_load_config | 配置加载、模板导出 | main.py |
02_launch_app | 最小仿真循环 | main.py |
03_base | 地基类型:平面 / USD 环境 | main.py |
04_object | 刚体与关节体物体 | spawn_rigidbody_object.py |
05_sensor | 相机配置与图像采集 | camera.py |
06_robot | 机器人加载与关节控制 | single_gripper_arm_robot.py |
07_light | 光源类型与参数 | main.py |
01:配置加载(01_load_config)
展示 ConfigManager 的加载、导出与模板生成能力:
python
from fastsim.configs.config_manager import ConfigManager
ConfigManager.load_config("./demo_config.yaml")
# 导出为模板(含默认值展开)
ConfigManager.export_config("./output/config_template.yaml")
运行后 output/ 目录会生成 config_template.yaml 和 demo_config_saved.yaml,可对照查看默认值展开结果。
02:最小启动(02_launch_app)
FastSim 最简启动流程:
python
from fastsim.app import FastSim
from fastsim.configs.config_manager import ConfigManager
ConfigManager.load_config("./launch_config.yaml")
sim = FastSim()
sim.initialize_simulation()
sim.start_simulation_loop()
或等价的单行形式(FastSim 构造时自动读取 ConfigManager 中的配置):
python
sim = FastSim("smallest_config.yaml")
sim.start()
smallest_config.yaml 仅需包含 general + simulation 两块,无需声明任何实体。
03:地基(03_base)
base_config 支持三种 stereotype:
yaml
# 平面地基(最常用)
base_config:
stereotype: ground_plane
ground_size: [100, 100]
# 从 USD 文件加载复杂环境
base_config:
stereotype: usd
asset_path: assets://environments/room.usd
04:物体(04_object)
刚体物体可使用 USD 资产或 primitive 几何体(无需外部文件):
yaml
object_cfg_dict:
table:
stereotype: rigid
source: primitive
primitive_type: cuboid
primitive_size: [0.5, 1.0, 0.5]
mass: 1000
target_obj:
stereotype: rigid
asset_path: assets://objects/ball.usd
position: [0.4, 0.0, 0.5]
scale: [0.001, 0.001, 0.001]
关节体(articulated)同样支持,配置 stereotype: articulated 并指定 asset_path。
05:传感器(05_sensor)
相机配置示例(多路、不同视角):
yaml
sensor_cfg_dict:
front_camera:
stereotype: camera
position: [0.8, 0.0, 0.8]
data_types: [rgb, depth, normals]
width: 1280
height: 720
fix_camera: true
wrist_cam:
stereotype: camera
data_types: [rgb, depth]
width: 640
height: 480
attach_to: arm # 绑定到机器人,随机器人移动
采集图像:
python
from fastsim.unisim.scene_manager import SceneManager
camera = SceneManager.get_sensor("front_camera")
sim.step()
rgb = camera.get_rgb()
depth = camera.get_depth()
06:机器人(06_robot)
在主线程中直接驱动关节(适合简单脚本):
python
from fastsim.app import FastSim
from fastsim.unisim.scene_manager import SceneManager
import numpy as np
sim = FastSim("single_gripper_arm_robot_config.yaml")
sim.setup_all_before_loop()
robot = SceneManager.get_robot("franka_panda_2f85")
joint_names = robot.get_joint_names()
limits = robot.get_joint_position_limit()
# 在关节极限内插值运动
for pos in np.linspace(limits[0][0], limits[0][1], 100):
robot.set_joint_position_target([pos], [joint_names[0]])
sim.step()
sim.close()
在控制线程中操作机器人应使用
SpawnableController或RobotController,见 Controller 示例。
07:光源(07_light)
yaml
light_cfg_dict:
main_light:
stereotype: general_light
light_type: distant # distant / sphere / disk
intensity: 3000
color: [1.0, 1.0, 1.0]