Spaces:
Sleeping
Sleeping
add openarm
Browse filesFiles changed (3)
10 unmodified lines11121314151617299 unmodified lines31731831932032132232310 unmodified linesimport StatsPanel from "@/components/stats-panel";import OverviewPanel from "@/components/overview-panel";import Loading from "@/components/loading-component";import { isSO101Robot } from "@/lib/so101-robot";import {getAdjacentEpisodesVideoInfo,computeColumnMinMax,299 unmodified lines<span className="absolute bottom-0 left-0 right-0 h-0.5 bg-orange-500" />)}</button>{isSO101Robot(datasetInfo.robot_type) && (<buttonclassName={`px-6 py-2.5 text-sm font-medium transition-colors relative ${activeTab === "urdf"10 unmodified lines11121314151617299 unmodified lines31731831932032132232310 unmodified linesimport StatsPanel from "@/components/stats-panel";import OverviewPanel from "@/components/overview-panel";import Loading from "@/components/loading-component";import { hasURDFSupport } from "@/lib/so101-robot";import {getAdjacentEpisodesVideoInfo,computeColumnMinMax,299 unmodified lines<span className="absolute bottom-0 left-0 right-0 h-0.5 bg-orange-500" />)}</button>{hasURDFSupport(datasetInfo.robot_type) && (<buttonclassName={`px-6 py-2.5 text-sm font-medium transition-colors relative ${activeTab === "urdf"
6 unmodified lines789101112131415161718192021222324252619 unmodified lines4647484950515253545556575859606162636465666768697071727374757677787943 unmodified lines1231241251261271281291301311321331341351361371381391401417 unmodified lines1491501511521531541551561571581591601611621631641651663 unmodified lines170171172173174175176113 unmodified lines29029129229329429529660 unmodified lines3573583593603613626 unmodified lines36937037137237337437537637737837938038138238338438538638738838939039139213 unmodified lines40640740840941041141241341441541641759 unmodified lines4774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305316 unmodified linesimport URDFLoader from "urdf-loader";import type { URDFRobot } from "urdf-loader";import { STLLoader } from "three/examples/jsm/loaders/STLLoader.js";import { Line2 } from "three/examples/jsm/lines/Line2.js";import { LineMaterial } from "three/examples/jsm/lines/LineMaterial.js";import { LineGeometry } from "three/examples/jsm/lines/LineGeometry.js";import type { EpisodeData } from "@/app/[org]/[dataset]/[episode]/fetch-data";import { fetchEpisodeChartData } from "@/app/[org]/[dataset]/[episode]/actions";const SERIES_DELIM = " | ";const SCALE = 10;const DEG2RAD = Math.PI / 180;function getUrdfUrl(robotType: string | null): string {const lower = (robotType ?? "").toLowerCase();if (lower.includes("so100") && !lower.includes("so101")) return "/urdf/so101/so100.urdf";return "/urdf/so101/so101_new_calib.urdf";}// Detect unit: servo ticks (0-4096), degrees (>6.28), or radians19 unmodified linesfunction autoMatchJoints(urdfJointNames: string[], columnKeys: string[]): Record<string, string> {const mapping: Record<string, string> = {};for (const jointName of urdfJointNames) {const lower = jointName.toLowerCase();const exactMatch = columnKeys.find((k) => {const suffix = (k.split(SERIES_DELIM).pop()?.trim() ?? k).toLowerCase();return suffix === lower;});if (exactMatch) { mapping[jointName] = exactMatch; continue; }const fuzzy = columnKeys.find((k) => k.toLowerCase().includes(lower));if (fuzzy) mapping[jointName] = fuzzy;}return mapping;}// Tip link names to try (so101 then so100 naming)const TIP_LINK_NAMES = ["gripper_frame_link", "gripperframe", "gripper_link", "gripper"];const TRAIL_DURATION = 1.0; // secondsconst TRAIL_COLOR = new THREE.Color("#ff6600");const MAX_TRAIL_POINTS = 300;// ─── Robot scene (imperative, inside Canvas) ───function RobotScene({urdfUrl, jointValues, onJointsLoaded, trailEnabled, trailResetKey,}: {urdfUrl: string;jointValues: Record<string, number>;onJointsLoaded: (names: string[]) => void;trailEnabled: boolean;trailResetKey: number;}) {const { scene, size } = useThree();const robotRef = useRef<URDFRobot | null>(null);43 unmodified linesuseEffect(() => {setLoading(true);setError(null);const manager = new THREE.LoadingManager();const loader = new URDFLoader(manager);loader.loadMeshCb = (url, mgr, onLoad) => {const stlLoader = new STLLoader(mgr);stlLoader.load(url,(geometry) => {const isMotor = url.includes("sts3215");const material = new THREE.MeshStandardMaterial({color: isMotor ? "#1a1a1a" : "#FFD700",metalness: isMotor ? 0.7 : 0.1,roughness: isMotor ? 0.3 : 0.6,});onLoad(new THREE.Mesh(geometry, material));},undefined,7 unmodified linesrobot.rotateOnAxis(new THREE.Vector3(1, 0, 0), -Math.PI / 2);robot.traverse((c) => { c.castShadow = true; });robot.updateMatrixWorld(true);robot.scale.set(SCALE, SCALE, SCALE);scene.add(robot);// Find the tip link for the trailfor (const name of TIP_LINK_NAMES) {if (robot.frames[name]) { tipLinkRef.current = robot.frames[name]; break; }}const revolute = Object.values(robot.joints).filter((j) => j.jointType === "revolute" || j.jointType === "continuous").map((j) => j.name);onJointsLoaded(revolute);setLoading(false);},undefined,3 unmodified linesif (robotRef.current) { scene.remove(robotRef.current); robotRef.current = null; }tipLinkRef.current = null;};}, [urdfUrl, scene, onJointsLoaded]);const tipWorldPos = useMemo(() => new THREE.Vector3(), []);113 unmodified lines}) {const { datasetInfo, episodes } = data;const fps = datasetInfo.fps || 30;const urdfUrl = useMemo(() => getUrdfUrl(datasetInfo.robot_type), [datasetInfo.robot_type]);// Episode selection & chart dataconst [selectedEpisode, setSelectedEpisode] = useState(data.episodeId);60 unmodified lines// Trailconst [trailEnabled, setTrailEnabled] = useState(true);// Playbackconst [frame, setFrame] = useState(0);6 unmodified linesframeRef.current = f;}, []);// Compute joint values for current frameconst jointValues = useMemo(() => {if (totalFrames === 0 || urdfJointNames.length === 0) return {};const row = chartData[Math.min(frame, totalFrames - 1)];const rawValues: number[] = [];const names: string[] = [];for (const jn of urdfJointNames) {const col = mapping[jn];if (col && typeof row[col] === "number") {rawValues.push(row[col]);names.push(jn);}}const converted = detectAndConvert(rawValues);const values: Record<string, number> = {};names.forEach((n, i) => { values[n] = converted[i]; });return values;}, [chartData, frame, mapping, totalFrames, urdfJointNames]);13 unmodified lines<span className="text-white text-lg animate-pulse">Loading episode {selectedEpisode}…</span></div>)}<Canvas camera={{ position: [0.3 * SCALE, 0.25 * SCALE, 0.3 * SCALE], fov: 45, near: 0.01, far: 100 }}><ambientLight intensity={0.5} /><directionalLight position={[3, 5, 4]} intensity={1.2} /><directionalLight position={[-2, 3, -2]} intensity={0.4} /><hemisphereLight args={["#b1e1ff", "#444444", 0.4]} /><RobotScene urdfUrl={urdfUrl} jointValues={jointValues} onJointsLoaded={onJointsLoaded} trailEnabled={trailEnabled} trailResetKey={selectedEpisode} /><Gridargs={[10, 10]} cellSize={0.2} cellThickness={0.5} cellColor="#334155"sectionSize={1} sectionThickness={1} sectionColor="#475569"59 unmodified lines<span className="text-xs text-slate-500 tabular-nums w-20 text-right shrink-0">F {frame}/{Math.max(totalFrames - 1, 0)}</span></div>{/* Data source + joint mapping */}<div className="flex gap-4 items-start"><div className="space-y-1 shrink-0"><label className="text-xs text-slate-400">Data source</label><div className="flex gap-1 flex-wrap">{groupNames.map((name) => (<button key={name} onClick={() => setSelectedGroup(name)}className={`px-2 py-1 text-xs rounded transition-colors ${selectedGroup === name ? "bg-orange-600 text-white" : "bg-slate-700 text-slate-300 hover:bg-slate-600"}`}>{name}</button>))}</div></div><div className="flex-1 overflow-x-auto"><table className="w-full text-xs"><thead><tr className="text-slate-500"><th className="text-left font-normal px-1">URDF Joint</th><th className="text-left font-normal px-1">→</th><th className="text-left font-normal px-1">Dataset Column</th><th className="text-right font-normal px-1">Value (rad)</th></tr></thead><tbody>{urdfJointNames.map((jointName) => (<tr key={jointName} className="border-t border-slate-700/50"><td className="px-1 py-0.5 text-slate-300 font-mono">{jointName}</td><td className="px-1 text-slate-600">→</td><td className="px-1 py-0.5"><select value={mapping[jointName] ?? ""}onChange={(e) => setMapping((m) => ({ ...m, [jointName]: e.target.value }))}className="bg-slate-900 text-slate-200 text-xs rounded px-1 py-0.5 border border-slate-600 w-full max-w-[200px]"><option value="">-- unmapped --</option>{selectedColumns.map((col) => {const label = col.split(SERIES_DELIM).pop() ?? col;return <option key={col} value={col}>{label}</option>;})}</select></td><td className="px-1 py-0.5 text-right tabular-nums text-slate-400 font-mono">{jointValues[jointName] !== undefined ? jointValues[jointName].toFixed(3) : "—"}</td></tr>))}</tbody></table></div></div></div></div>);6 unmodified lines789101112131415161718192021222324252627282930313219 unmodified lines52535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011143 unmodified lines1551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851867 unmodified lines1941951961971981992002012022032042052062072082092103 unmodified lines214215216217218219220113 unmodified lines33433533633733833934034160 unmodified lines4024034044054064074086 unmodified lines41541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148213 unmodified lines49649749849950050150250350450550650759 unmodified lines5675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326 unmodified linesimport URDFLoader from "urdf-loader";import type { URDFRobot } from "urdf-loader";import { STLLoader } from "three/examples/jsm/loaders/STLLoader.js";import { ColladaLoader } from "three/examples/jsm/loaders/ColladaLoader.js";import { Line2 } from "three/examples/jsm/lines/Line2.js";import { LineMaterial } from "three/examples/jsm/lines/LineMaterial.js";import { LineGeometry } from "three/examples/jsm/lines/LineGeometry.js";import type { EpisodeData } from "@/app/[org]/[dataset]/[episode]/fetch-data";import { fetchEpisodeChartData } from "@/app/[org]/[dataset]/[episode]/actions";import { isOpenArmRobot } from "@/lib/so101-robot";const SERIES_DELIM = " | ";const DEG2RAD = Math.PI / 180;function getRobotConfig(robotType: string | null) {const lower = (robotType ?? "").toLowerCase();if (lower.includes("openarm")) {return { urdfUrl: "/urdf/openarm/openarm_bimanual.urdf", scale: 3, isDualArm: true };}if (lower.includes("so100") && !lower.includes("so101")) {return { urdfUrl: "/urdf/so101/so100.urdf", scale: 10, isDualArm: false };}return { urdfUrl: "/urdf/so101/so101_new_calib.urdf", scale: 10, isDualArm: false };}// Detect unit: servo ticks (0-4096), degrees (>6.28), or radians19 unmodified linesfunction autoMatchJoints(urdfJointNames: string[], columnKeys: string[]): Record<string, string> {const mapping: Record<string, string> = {};const suffixes = columnKeys.map((k) => (k.split(SERIES_DELIM).pop()?.trim() ?? k).toLowerCase());for (const jointName of urdfJointNames) {const lower = jointName.toLowerCase();// Exact match on column suffixconst exactIdx = suffixes.findIndex((s) => s === lower);if (exactIdx >= 0) { mapping[jointName] = columnKeys[exactIdx]; continue; }// OpenArm: openarm_(left|right)_joint(\d+) → (left|right)_joint_(\d+)const armMatch = lower.match(/^openarm_(left|right)_joint(\d+)$/);if (armMatch) {const pattern = `${armMatch[1]}_joint_${armMatch[2]}`;const idx = suffixes.findIndex((s) => s.includes(pattern));if (idx >= 0) { mapping[jointName] = columnKeys[idx]; continue; }}// OpenArm: openarm_(left|right)_finger_joint1 → (left|right)_gripperconst fingerMatch = lower.match(/^openarm_(left|right)_finger_joint1$/);if (fingerMatch) {const pattern = `${fingerMatch[1]}_gripper`;const idx = suffixes.findIndex((s) => s.includes(pattern));if (idx >= 0) { mapping[jointName] = columnKeys[idx]; continue; }}// finger_joint2 is a mimic joint — skipif (lower.includes("finger_joint2")) continue;// Generic fuzzy fallbackconst fuzzy = columnKeys.find((k) => k.toLowerCase().includes(lower));if (fuzzy) mapping[jointName] = fuzzy;}return mapping;}// Tip link names to try (so101, so100, then openarm naming)const TIP_LINK_NAMES = ["gripper_frame_link", "gripperframe", "gripper_link", "gripper","openarm_left_hand_tcp", "openarm_right_hand_tcp",];const TRAIL_DURATION = 1.0; // secondsconst TRAIL_COLOR = new THREE.Color("#ff6600");const MAX_TRAIL_POINTS = 300;// ─── Robot scene (imperative, inside Canvas) ───function RobotScene({urdfUrl, jointValues, onJointsLoaded, trailEnabled, trailResetKey, scale,}: {urdfUrl: string;jointValues: Record<string, number>;onJointsLoaded: (names: string[]) => void;trailEnabled: boolean;trailResetKey: number;scale: number;}) {const { scene, size } = useThree();const robotRef = useRef<URDFRobot | null>(null);43 unmodified linesuseEffect(() => {setLoading(true);setError(null);const isOpenArm = urdfUrl.includes("openarm");const manager = new THREE.LoadingManager();const loader = new URDFLoader(manager);loader.loadMeshCb = (url, mgr, onLoad) => {// DAE (Collada) files — load with embedded materialsif (url.endsWith(".dae")) {const colladaLoader = new ColladaLoader(mgr);colladaLoader.load(url, (collada) => onLoad(collada.scene), undefined,(err) => onLoad(new THREE.Object3D(), err as Error));return;}// STL files — apply custom materialsconst stlLoader = new STLLoader(mgr);stlLoader.load(url,(geometry) => {let color = "#FFD700";let metalness = 0.1;let roughness = 0.6;if (url.includes("sts3215")) {color = "#1a1a1a"; metalness = 0.7; roughness = 0.3;} else if (isOpenArm) {color = url.includes("body_link0") ? "#3a3a4a" : "#f5f5f5";metalness = 0.15; roughness = 0.6;}const material = new THREE.MeshStandardMaterial({ color, metalness, roughness });onLoad(new THREE.Mesh(geometry, material));},undefined,7 unmodified linesrobot.rotateOnAxis(new THREE.Vector3(1, 0, 0), -Math.PI / 2);robot.traverse((c) => { c.castShadow = true; });robot.updateMatrixWorld(true);robot.scale.set(scale, scale, scale);scene.add(robot);for (const name of TIP_LINK_NAMES) {if (robot.frames[name]) { tipLinkRef.current = robot.frames[name]; break; }}const movable = Object.values(robot.joints).filter((j) => j.jointType === "revolute" || j.jointType === "continuous" || j.jointType === "prismatic").map((j) => j.name);onJointsLoaded(movable);setLoading(false);},undefined,3 unmodified linesif (robotRef.current) { scene.remove(robotRef.current); robotRef.current = null; }tipLinkRef.current = null;};}, [urdfUrl, scale, scene, onJointsLoaded]);const tipWorldPos = useMemo(() => new THREE.Vector3(), []);113 unmodified lines}) {const { datasetInfo, episodes } = data;const fps = datasetInfo.fps || 30;const robotConfig = useMemo(() => getRobotConfig(datasetInfo.robot_type), [datasetInfo.robot_type]);const { urdfUrl, scale, isDualArm } = robotConfig;// Episode selection & chart dataconst [selectedEpisode, setSelectedEpisode] = useState(data.episodeId);60 unmodified lines// Trailconst [trailEnabled, setTrailEnabled] = useState(true);const [showMapping, setShowMapping] = useState(false);// Playbackconst [frame, setFrame] = useState(0);6 unmodified linesframeRef.current = f;}, []);// Filter out mimic joints (finger_joint2) from the UI listconst displayJointNames = useMemo(() => urdfJointNames.filter((n) => !n.toLowerCase().includes("finger_joint2")),[urdfJointNames],);// Auto-detect gripper column range for linear mapping to 0-0.044mconst gripperRanges = useMemo(() => {const ranges: Record<string, { min: number; max: number }> = {};for (const jn of urdfJointNames) {if (!jn.toLowerCase().includes("finger_joint1")) continue;const col = mapping[jn];if (!col) continue;let min = Infinity, max = -Infinity;for (const row of chartData) {const v = row[col];if (typeof v === "number") { if (v < min) min = v; if (v > max) max = v; }}if (min < max) ranges[jn] = { min, max };}return ranges;}, [chartData, mapping, urdfJointNames]);// Compute joint values for current frameconst jointValues = useMemo(() => {if (totalFrames === 0 || urdfJointNames.length === 0) return {};const row = chartData[Math.min(frame, totalFrames - 1)];const revoluteValues: number[] = [];const revoluteNames: string[] = [];const values: Record<string, number> = {};for (const jn of urdfJointNames) {if (jn.toLowerCase().includes("finger_joint2")) continue;const col = mapping[jn];if (!col || typeof row[col] !== "number") continue;const raw = row[col];if (jn.toLowerCase().includes("finger_joint1")) {// Map gripper range → 0-0.044m using auto-detected min/maxconst range = gripperRanges[jn];if (range) {const t = (raw - range.min) / (range.max - range.min);values[jn] = t * 0.044;} else {values[jn] = (raw / 100) * 0.044; // fallback: assume 0-100}} else {revoluteValues.push(raw);revoluteNames.push(jn);}}const converted = detectAndConvert(revoluteValues);revoluteNames.forEach((n, i) => { values[n] = converted[i]; });// Copy finger_joint1 → finger_joint2 (mimic joints)for (const jn of urdfJointNames) {if (jn.toLowerCase().includes("finger_joint2")) {const j1 = jn.replace(/finger_joint2/, "finger_joint1");if (values[j1] !== undefined) values[jn] = values[j1];}}return values;}, [chartData, frame, mapping, totalFrames, urdfJointNames]);13 unmodified lines<span className="text-white text-lg animate-pulse">Loading episode {selectedEpisode}…</span></div>)}<Canvas camera={{ position: [0.3 * scale, 0.25 * scale, 0.3 * scale], fov: 45, near: 0.01, far: 100 }}><ambientLight intensity={0.7} /><directionalLight position={[3, 5, 4]} intensity={1.5} /><directionalLight position={[-2, 3, -2]} intensity={0.6} /><hemisphereLight args={["#b1e1ff", "#666666", 0.5]} /><RobotScene urdfUrl={urdfUrl} jointValues={jointValues} onJointsLoaded={onJointsLoaded} trailEnabled={trailEnabled} trailResetKey={selectedEpisode} scale={scale} /><Gridargs={[10, 10]} cellSize={0.2} cellThickness={0.5} cellColor="#334155"sectionSize={1} sectionThickness={1} sectionColor="#475569"59 unmodified lines<span className="text-xs text-slate-500 tabular-nums w-20 text-right shrink-0">F {frame}/{Math.max(totalFrames - 1, 0)}</span></div>{/* Collapsible joint mapping */}<buttononClick={() => setShowMapping((v) => !v)}className="flex items-center gap-1.5 text-xs text-slate-400 hover:text-slate-200 transition-colors"><span className={`transition-transform ${showMapping ? "rotate-90" : ""}`}>▶</span>Joint Mapping<span className="text-slate-600">({Object.keys(mapping).filter((k) => mapping[k]).length}/{displayJointNames.length} mapped)</span></button>{showMapping && (<div className="flex gap-4 items-start"><div className="space-y-1 shrink-0"><label className="text-xs text-slate-400">Data source</label><div className="flex gap-1 flex-wrap">{groupNames.map((name) => (<button key={name} onClick={() => setSelectedGroup(name)}className={`px-2 py-1 text-xs rounded transition-colors ${selectedGroup === name ? "bg-orange-600 text-white" : "bg-slate-700 text-slate-300 hover:bg-slate-600"}`}>{name}</button>))}</div></div><div className="flex-1 overflow-x-auto max-h-48 overflow-y-auto"><table className="w-full text-xs"><thead className="sticky top-0 bg-slate-800"><tr className="text-slate-500"><th className="text-left font-normal px-1">URDF Joint</th><th className="text-left font-normal px-1">→</th><th className="text-left font-normal px-1">Dataset Column</th><th className="text-right font-normal px-1">Value</th></tr></thead><tbody>{displayJointNames.map((jointName) => (<tr key={jointName} className="border-t border-slate-700/50"><td className="px-1 py-0.5 text-slate-300 font-mono">{jointName}</td><td className="px-1 text-slate-600">→</td><td className="px-1 py-0.5"><select value={mapping[jointName] ?? ""}onChange={(e) => setMapping((m) => ({ ...m, [jointName]: e.target.value }))}className="bg-slate-900 text-slate-200 text-xs rounded px-1 py-0.5 border border-slate-600 w-full max-w-[200px]"><option value="">-- unmapped --</option>{selectedColumns.map((col) => {const label = col.split(SERIES_DELIM).pop() ?? col;return <option key={col} value={col}>{label}</option>;})}</select></td><td className="px-1 py-0.5 text-right tabular-nums text-slate-400 font-mono">{jointValues[jointName] !== undefined ? jointValues[jointName].toFixed(3) : "—"}</td></tr>))}</tbody></table></div></div>)}</div></div>);
2 unmodified lines3452 unmodified linesconst lower = robotType.toLowerCase();return lower.includes("so100") || lower.includes("so101") || lower === "so_follower";}2 unmodified lines345678910111213142 unmodified linesconst lower = robotType.toLowerCase();return lower.includes("so100") || lower.includes("so101") || lower === "so_follower";}export function isOpenArmRobot(robotType: string | null): boolean {if (!robotType) return false;return robotType.toLowerCase().includes("openarm");}export function hasURDFSupport(robotType: string | null): boolean {return isSO101Robot(robotType) || isOpenArmRobot(robotType);}
src/app/[org]/[dataset]/[episode]/episode-viewer.tsx+2-2
src/components/urdf-viewer.tsx+185-84
src/lib/so101-robot.ts+9