跳到正文
DEV Community· Pranay Gupta·· 5 天前AI 評分51

如何用 Alloy 和 Python 模擬機械人學習中的布料摺疊

How to simulate cloth folding for robot learning in Python

AI 導讀

這篇教學示範如何用 Alloy 在 Python 中模擬供機械人學習使用的布料摺疊,涵蓋布料越過方塊、檢查模擬狀態,以及載入 ClothFold-v0 環境。布料使用 Newton 的 VBD solver,機械人使用 MuJoCo Warp;範例以 sim.check() 檢查模擬狀態。

正文

Pranay Gupta

Folding a towel is a classic robot learning task, and the hard part starts before any learning: you need a simulator where cloth bends, rests on itself as two layers and sits in the same scene as a robot. This walkthrough does it in a few lines of Python with Alloy, an open-source physics engine that runs cloth on Newton's VBD solver next to robots on MuJoCo Warp.

pip install alloy-engine

1. Drape a sheet over a block

import alloy

scene = alloy.Scene(dt=0.01)
scene.add_ground()
scene.add_box("bar", size=(0.12, 0.5, 0.1), pos=(0, 0, 0.15), static=True)   # top at z = 0.2
scene.add_cloth("sheet", size=(0.5, 0.5), pos=(0, 0, 0.3), resolution=20, mass=0.1)
sim = alloy.build(scene)
print(sim.routes()["sheet"]["route"])
cloth_vbd

sim.routes() tells you which solver each object went to: the sheet runs on the cloth solver. Without a GPU it runs on the CPU, slower but fine for a scene this small.

2. Run it and check the fold

sim.run(1.0)
points = sim.state(["sheet"], world=0)["sheet.points"]
left, right = points[points[:, 0] < 0], points[points[:, 0] > 0]
print(points.shape, (0.2 - left[:, 2].min()).round(1), (0.2 - right[:, 2].min()).round(1))
print(sim.check()["ok"])
(441, 3) 0.2 0.2
True

The sheet bends over the block and each half hangs down to the ground (both halves end 0.2 m below the block top). sim.check() grades the world, so a run that blew up never makes it into your training data.

Keep the cloth cells smaller than the objects it touches: cloth meets rigid shapes at its vertices, and Alloy warns when a sheet is too coarse for a thin bar.

3. Fold with a robot

For a robot doing the folding, there is a ready-made environment:

from alloy.envs import make

env = make("ClothFold-v0", num_envs=64)   # pip install "alloy-engine[envs]"; needs a GPU
obs, info = env.reset(seed=0)

It folds a towel along its diagonal, in many parallel worlds at once. The default grasp="friction" holds the towel by friction alone; grasp="attach" is a shortcut that pins the towel to the closed fingers.

Keep in mind

  • ClothFold-v0 is experimental and slow, and needs a GPU. Time a short run first.
  • A friction grasp that carries a cotton sheet is not verified yet, and sheets that fall onto each other can pass through. The docs keep a full list of limits.
  • If you only need rigid robots, MuJoCo Warp on its own is the leaner choice.

Links

來源:DEV Community · dev.to