# ----------------------------------------------------------------------------
# Copyright (c) 2021-2026 DexForce Technology Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------------
"""Asset source and cache identity for Franka."""
from __future__ import annotations
import os
from copy import deepcopy
import hashlib
import json
import math
from pathlib import Path
import tempfile
from xml.etree import ElementTree
import open3d as o3d
from embodichain.data.dataset import EmbodiChainDataset, _dataset_download_lock
from embodichain.data.constants import (
EMBODICHAIN_DOWNLOAD_PREFIX,
EMBODICHAIN_DEFAULT_DATA_ROOT,
)
__all__ = ["Franka", "FrankaPandaArm", "FrankaRopePads"]
robot_assets = "robot_assets"
_FR3_WITH_HAND_SHA256 = (
"a4a2d3265dadd69c4c7d8ed9238ae44494c217db6f66f586fcc40e8754ce2315"
)
def _franka_v4_archive() -> o3d.data.DataDescriptor:
return o3d.data.DataDescriptor(
os.path.join(EMBODICHAIN_DOWNLOAD_PREFIX, robot_assets, "FrankaV4.zip"),
"0c613d884798e8b084604381641cc8e1",
)
def _materialize_hand_component(extract_dir: str, data_root: str) -> None:
"""Split the authored hand subtree without modifying its source URDF."""
source = Path(extract_dir) / "Panda/PandaWithHand.urdf"
content = source.read_bytes()
if hashlib.sha256(content).hexdigest() != _FR3_WITH_HAND_SHA256:
raise ValueError("pinned FR3/hand source changed in the asset cache")
original = ElementTree.fromstring(content)
links = ("fr3_hand", "fr3_hand_tcp", "fr3_leftfinger", "fr3_rightfinger")
joints = ("fr3_hand_tcp_joint", "fr3_finger_joint1", "fr3_finger_joint2")
hand = ElementTree.Element("robot", name="fr3_hand")
for tag, names in (("link", links), ("joint", joints)):
for name in names:
element = original.find(f"{tag}[@name='{name}']")
if element is None:
raise ValueError(f"pinned FR3 hand source is missing {tag} {name}")
hand.append(deepcopy(element))
output = ElementTree.tostring(hand, encoding="utf-8", xml_declaration=True)
report = json.dumps(
dict(
schema="fr3-hand-component/v1",
source_sha256=_FR3_WITH_HAND_SHA256,
derived_sha256=hashlib.sha256(output).hexdigest(),
links=links,
joints=joints,
),
sort_keys=True,
indent=2,
).encode()
_write_derivatives(
source,
data_root,
{"Fr3HandV1.urdf": output, "Fr3HandV1.derivation.json": report},
)
def _write_derivatives(source: Path, data_root: str, files: dict[str, bytes]) -> None:
with _dataset_download_lock(data_root, "Franka"):
for name, value in files.items():
target = source.parent / name
if target.exists():
if target.read_bytes() != value:
raise ValueError(
f"derived FR3 asset {name} differs from its pinned source"
)
continue
temporary = None
try:
with tempfile.NamedTemporaryFile(
dir=target.parent, delete=False
) as stream:
temporary = Path(stream.name)
stream.write(value)
temporary.replace(target)
finally:
if temporary is not None and temporary.exists():
temporary.unlink()
[docs]
class Franka(EmbodiChainDataset):
"""Dataset class for the Franka robots.
Reference:
https://franka.de/franka-research-3
Directory structure:
Franka/
Panda/
Panda.urdf
PandaWithHand.urdf
FR3/
fr3.urdf
Example usage:
>>> from embodichain.data.robot_dataset import Franka
>>> dataset = Franka()
or
>>> from embodichain.data import get_data_path
>>> print(get_data_path("Franka/Panda/Panda.urdf"))
>>> print(get_data_path("Franka/PandaWithHand/PandaWithHand.urdf"))
"""
[docs]
def __init__(self, data_root: str = None):
data_descriptor = _franka_v4_archive()
prefix = type(self).__name__
path = EMBODICHAIN_DEFAULT_DATA_ROOT if data_root is None else data_root
super().__init__(prefix, data_descriptor, path)
_materialize_hand_component(self.extract_dir, path)
[docs]
class FrankaRopePads(EmbodiChainDataset):
"""FR3 with flat rope-grasping pads, derived on demand in the shared V4 cache."""
[docs]
def __init__(self, data_root: str | None = None) -> None:
from .rope_pads import apply_rope_pads
root = EMBODICHAIN_DEFAULT_DATA_ROOT if data_root is None else data_root
super().__init__("Franka", _franka_v4_archive(), root)
source = Path(self.extract_dir) / "Panda/PandaWithHand.urdf"
content = source.read_bytes()
if hashlib.sha256(content).hexdigest() != _FR3_WITH_HAND_SHA256:
raise ValueError("pinned FR3/hand source changed in the asset cache")
padded = ElementTree.fromstring(content)
apply_rope_pads(padded)
model = ElementTree.tostring(padded, encoding="utf-8", xml_declaration=True)
report = json.dumps(
{
"schema": "fr3-rope-pads/v1",
"source": source.name,
"source_sha256": _FR3_WITH_HAND_SHA256,
"derived_sha256": hashlib.sha256(model).hexdigest(),
"modified_links": ["fr3_leftfinger", "fr3_rightfinger"],
},
sort_keys=True,
indent=2,
).encode()
_write_derivatives(
source,
root,
{
"Fr3RopePadsV1.urdf": model,
"Fr3RopePadsV1.derivation.json": report,
},
)
[docs]
class FrankaPandaArm(EmbodiChainDataset):
"""Pinned original Panda arm with Panda limits, distinct from FR3 assets.
The original archive preserves ``Joint1`` through ``Joint7`` and the
``ee_link`` flange. Existing Franka asset IDs keep their current defaults.
Args:
data_root: Optional ordinary EmbodiChain asset cache root.
"""
[docs]
def __init__(self, data_root: str | None = None) -> None:
descriptor = o3d.data.DataDescriptor(
[
os.path.join(
EMBODICHAIN_DOWNLOAD_PREFIX, "robot_assets", "FrankaV2.zip"
),
"https://huggingface.co/datasets/DexForceAI/embodichain_data/resolve/main/robot_assets/FrankaV2.zip",
],
"f0675b9da98126bc3d4e18c98ef5e06c",
)
super().__init__(
type(self).__name__,
descriptor,
EMBODICHAIN_DEFAULT_DATA_ROOT if data_root is None else data_root,
)
# The legacy CAD export rounded quarter-turns to 1.5708. Materialize a
# model-owned exact-geometry variant while retaining the original file.
root = Path(self.extract_dir)
original = root / "Panda/Panda.urdf"
source_bytes = original.read_bytes()
expected_source_sha = (
"ec88eb5b8bbbe2bd9ceaa0b1784a9ff0408fac118be6bf312904b7fbdb570b04"
)
if hashlib.sha256(source_bytes).hexdigest() != expected_source_sha:
raise ValueError("pinned Panda arm source changed in the asset cache")
parser = ElementTree.XMLParser(
target=ElementTree.TreeBuilder(insert_comments=True)
)
document = ElementTree.fromstring(source_bytes, parser=parser)
corrections = []
for i in range(2, 8):
joint = document.find(f"joint[@name='Joint{i}']")
origin = joint.find("origin")
angles = [float(value) for value in origin.get("rpy").split()]
if abs(abs(angles[0]) - math.pi / 2) > 5e-6 or angles[1:] != [0.0, 0.0]:
raise ValueError("unexpected Panda CAD axis convention")
angle = math.copysign(math.pi / 2, angles[0])
origin.set("rpy", f"{angle:.17g} 0 0")
corrections.append(
{
"joint": f"Joint{i}",
"original_rpy": angles,
"exact_rpy": [angle, 0.0, 0.0],
}
)
exact = ElementTree.tostring(document, encoding="utf-8", xml_declaration=True)
report = json.dumps(
{
"schema": "panda-cad-axis-precision/v1",
"original_sha256": expected_source_sha,
"exact_sha256": hashlib.sha256(exact).hexdigest(),
"corrections": corrections,
},
sort_keys=True,
indent=2,
).encode()
cache_root = EMBODICHAIN_DEFAULT_DATA_ROOT if data_root is None else data_root
with _dataset_download_lock(cache_root, type(self).__name__):
for name, content in (
("PandaExactV1.urdf", exact),
("PandaExactV1.normalization.json", report),
):
destination = original.parent / name
if destination.exists():
if destination.read_bytes() != content:
raise ValueError(
"derived Panda model differs from its pinned normalization"
)
continue
temporary = None
try:
with tempfile.NamedTemporaryFile(
dir=destination.parent, delete=False
) as stream:
temporary = Path(stream.name)
stream.write(content)
stream.flush()
os.fsync(stream.fileno())
os.link(temporary, destination)
finally:
if temporary is not None:
temporary.unlink(missing_ok=True)