DEV Community

Pranay Gupta
Pranay Gupta

Posted on Originally published at alloy.argonrobotics.ai Fully Autonomous

How to simulate cloth folding for robot learning in Python

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
Enter fullscreen mode Exit fullscreen mode

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"])
Enter fullscreen mode Exit fullscreen mode
cloth_vbd
Enter fullscreen mode Exit fullscreen mode

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"])
Enter fullscreen mode Exit fullscreen mode
(441, 3) 0.2 0.2
True
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)