第一个机器人
30 秒 Hello World
Section titled “30 秒 Hello World”import asynciofrom threewe import Robot
async def main(): async with Robot(backend="mock") as robot: image = robot.get_image() await robot.move_to(x=2.0, y=1.0) pose = robot.get_pose() print(f"Arrived at: ({pose.x:.2f}, {pose.y:.2f})")
asyncio.run(main())无需 ROS2,无需 Gazebo,无需 GPU。mock 后端在本地模拟一切,除 numpy 外零依赖。
更改一个参数即可在不同后端上运行:
# Local simulation (no dependencies)Robot(backend="mock")
# Full physics simulation (Gazebo Harmonic)Robot(backend="gazebo")
# GPU-accelerated training (Isaac Sim)Robot(backend="isaac_sim")
# Real hardware (ROS2 Jazzy + Pi5 + ESP32)Robot(backend="real")所有后端共享完全相同的 API——零代码改动。
使用 Gymnasium 进行 RL 训练
Section titled “使用 Gymnasium 进行 RL 训练”import gymnasium as gymimport threewe.gym # Registers environments
env = gym.make("3we/Navigation-v1", render_mode="rgb_array")obs, info = env.reset()
for _ in range(1000): action = env.action_space.sample() obs, reward, terminated, truncated, info = env.step(action) if terminated or truncated: obs, info = env.reset()
env.close()VLM 驱动的导航
Section titled “VLM 驱动的导航”import asynciofrom threewe import Robot
async def main(): async with Robot(backend="mock") as robot: result = await robot.execute_instruction("find the red bottle and stop near it") print(f"Success: {result.success}") print(f"Description: {result.description}")
asyncio.run(main())需要 pip install threewe[ai] 和 OPENAI_API_KEY 环境变量。
模仿学习数据采集
Section titled “模仿学习数据采集”import asynciofrom threewe import Robotfrom threewe.data import TrajectoryRecorder
async def main(): recorder = TrajectoryRecorder(output_dir="trajectories/")
async with Robot(backend="mock") as robot: recorder.start(robot) await robot.move_to(x=3.0, y=2.0) await robot.move_forward(1.0) await robot.rotate(1.57) recorder.stop()
asyncio.run(main())API 速查表
Section titled “API 速查表”| 方法 | 描述 |
|---|---|
robot.get_image() | 相机图像 (480, 640, 3) uint8 |
robot.get_camera_image() | 与 get_image() 相同 |
robot.get_lidar_scan() | 360 度 LiDAR 扫描 |
robot.get_pose() | 地图坐标系中的位置 (x, y, theta) |
robot.get_velocity() | 当前机体坐标系速度 |
robot.get_imu() | IMU 传感器数据 |
robot.get_observation() | ML 模型的标准化字典 |
await robot.move_to(x, y) | 使用路径规划导航到目标 |
await robot.move_forward(d) | 直线前进 d 米 |
await robot.rotate(a) | 旋转 a 弧度 |
robot.set_velocity(vx, vy, omega) | 直接速度指令 |
robot.stop() | 停止所有运动 |
await robot.execute_instruction(text) | VLM 驱动的任务执行 |
robot.execute_action(action) | 执行策略网络输出 |