ExoWrist_Smolvla / code /scripts /piper_trajectory_viewer.html
Lyndow's picture
Add verified ExoWrist test assets (201-300)
e302b11 verified
Raw History Blame Contribute Delete
13.4 kB
<!doctype html>
<html lang="zh-CN">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Piper · 轨迹回放</title>
<style>
:root{color-scheme:dark;font-family:system-ui,"Noto Sans CJK SC",sans-serif;color:#e7edf7;background:#0b1220}
*{box-sizing:border-box}body{margin:0;padding:24px;max-width:1600px;margin-inline:auto}h1{font-size:25px;margin:0 0 8px}p{margin:0;color:#9aabc2;font-size:13px;line-height:1.8}.tag{float:right;border:1px solid #295852;color:#71ddbd;border-radius:20px;padding:6px 12px;font-size:12px}.metrics{display:flex;gap:12px;margin:20px 0;flex-wrap:wrap}.metric{background:#111e30;border:1px solid #25344b;padding:12px 18px;border-radius:12px;min-width:145px}.metric b{display:block;font-size:23px;margin-bottom:4px}.metric span{color:#a8b8cc;font-size:12px}.grid{display:grid;grid-template-columns:1.05fr 1fr;gap:16px}.panel{background:#101b2c;border:1px solid #25344b;border-radius:14px;padding:16px;min-width:0}h2{font-size:15px;margin:0 0 10px;font-weight:600}canvas{display:block;width:100%;touch-action:none}#scene{height:418px;cursor:grab}#joints{height:448px;cursor:crosshair}#gripper{height:110px;cursor:crosshair}.legend{display:flex;gap:14px;font-size:12px;color:#a8b8cc;flex-wrap:wrap}.legend i{display:inline-block;width:18px;height:3px;vertical-align:middle;margin-right:5px}.controls{display:flex;align-items:center;gap:12px;margin:18px 0 10px;flex-wrap:wrap}button,select{border:1px solid #3a506b;background:#1b2d45;color:#e7edf7;border-radius:7px;padding:9px 14px;cursor:pointer}button:hover{background:#294561}#play{background:#2864d5;border-color:#3d7cee;min-width:86px}input[type=range]{accent-color:#6ee2c3;flex:1;min-width:180px}#time{font-variant-numeric:tabular-nums;min-width:180px;font-size:13px}.values{margin-top:12px;display:grid;grid-template-columns:repeat(3,1fr);gap:8px;font-size:13px;color:#bdd0e5}.values div{padding:8px;background:#16253a;border-radius:6px}.note{margin-top:16px;padding:12px 16px;border-left:3px solid #ccab62;background:#181f2d;color:#c5c6bd;font-size:12px;line-height:1.8}#tooltip{min-height:20px;color:#90a8c4;font-size:12px;margin-top:8px}@media(max-width:950px){.grid{grid-template-columns:1fr}body{padding:14px}.tag{float:none;display:inline-block;margin-bottom:10px}#scene{height:370px}}
</style>
<span class="tag">离线回放 · 无硬件连接</span>
<h1>Piper 机械臂轨迹</h1>
<p>实际关节反馈 → SDK 正运动学 → 末端路径。拖动三维视图旋转,滚轮缩放;拖动时间轴回放。</p>
<div class="metrics">
<div class="metric"><b id="duration"></b><span>记录时长</span></div>
<div class="metric"><b id="samples"></b><span>视频对齐数据行</span></div>
<div class="metric"><b id="valid"></b><span>实际关节有效率</span></div>
<div class="metric"><b id="distance"></b><span>FK 末端累计路程 · 含测量抖动</span></div>
</div>
<div class="grid">
<section class="panel">
<h2>三维末端路径 / 连杆示意 <button id="reset" style="float:right;padding:3px 9px;font-size:11px">重置视角</button></h2>
<canvas id="scene" aria-label="由关节反馈推算的三维机械臂与末端轨迹"></canvas>
<div class="legend"><span><i style="background:#51c8ed"></i>路径起点</span><span><i style="background:#ff8a59"></i>路径终点</span><span><i style="background:#6ee2c3"></i>当前连杆 / 末端</span><span>基座坐标 · mm</span></div>
<div class="values" id="values"></div>
<div id="tooltip"></div>
</section>
<section class="panel">
<h2>六关节角度 <span style="font-size:11px;color:#91a5bd;font-weight:400">实线:实际反馈 虚线:示教目标(缺失断开)</span></h2>
<canvas id="joints" aria-label="六个关节角度随时间变化"></canvas>
<h2 style="margin-top:4px">实际夹爪开口 · mm</h2>
<canvas id="gripper" aria-label="实际夹爪开口随时间变化"></canvas>
</section>
</div>
<div class="controls">
<button id="play">播放</button><button id="back">↶ 回到开始</button>
<select id="speed" aria-label="播放速度"><option value="0.25">0.25×</option><option value="0.5">0.5×</option><option value="1" selected>1×</option><option value="2">2×</option></select>
<input id="seek" type="range" min="0" step="0.001" value="0" aria-label="回放时间轴">
<span id="time"></span>
</div>
<div class="note" id="note"></div>
<script id="data" type="application/json">__TRAJECTORY_DATA__</script>
<script>
"use strict";
const DATA=JSON.parse(document.getElementById('data').textContent), S=DATA.samples, meta=DATA.summary;
const $=id=>document.getElementById(id), first=S[0].t, duration=S.at(-1).t-first;
const colors=['#68b5ff','#ffb65e','#a898ff','#68ddbb','#ff8394','#d2e172'];
let index=0,elapsed=0,playing=false,lastTick=0,animationId=null,yaw=-0.9,pitch=0.65,zoom=1;
$('duration').textContent=duration.toFixed(2)+' s';$('samples').textContent=S.length.toLocaleString();
$('valid').textContent=(100*meta.valid_feedback/S.length).toFixed(1)+'%';
$('distance').textContent=(meta.estimated_tip_path_length_mm/1000).toFixed(2)+' m';
$('seek').max=duration;
$('note').textContent=`仅用于观察轨迹,不是机器人控制界面。三维坐标由 Piper SDK ${meta.sdk_version} 的 FK 模型推算,DH offset=${meta.dh_offset}(默认假设),未标定夹爪 TCP、基座到相机或世界坐标变换。连杆为 DH 坐标原点连接示意,不是精确外形。缺失关节反馈不绘制;时间差超过 100 ms 的段不连接。时间轴采用 CSV 的实际时间,非固定 60 fps;本页不验证曝光同步或机械臂安全。`;
const points=S.filter(s=>s.links).flatMap(s=>s.links);
const lo=[0,1,2].map(k=>Math.min(...points.map(p=>p[k]))),hi=[0,1,2].map(k=>Math.max(...points.map(p=>p[k])));
const center=lo.map((v,k)=>(v+hi[k])/2), span=Math.max(...hi.map((v,k)=>v-lo[k]),300);
function canvas(id){const c=$(id),r=c.getBoundingClientRect(),d=devicePixelRatio||1;
if(c.width!==Math.round(r.width*d)||c.height!==Math.round(r.height*d)){c.width=Math.round(r.width*d);c.height=Math.round(r.height*d);}
const ctx=c.getContext('2d');ctx.setTransform(d,0,0,d,0,0);ctx.clearRect(0,0,r.width,r.height);return [ctx,r.width,r.height];}
function scene(){const [ctx,w,h]=canvas('scene'),scale=Math.min(w,h)*0.72/span*zoom;
const project=p=>{const x=p[0]-center[0],y=p[1]-center[1],z=p[2]-center[2],a=Math.cos(yaw)*x-Math.sin(yaw)*y,b=Math.sin(yaw)*x+Math.cos(yaw)*y;return [w/2+a*scale,h*.53-(Math.cos(pitch)*z-Math.sin(pitch)*b)*scale];};
function line(a,b,color,width=1){a=project(a);b=project(b);ctx.strokeStyle=color;ctx.lineWidth=width;ctx.beginPath();ctx.moveTo(...a);ctx.lineTo(...b);ctx.stroke();}
function dot(p,color,r=4){p=project(p);ctx.fillStyle=color;ctx.beginPath();ctx.arc(...p,r,0,Math.PI*2);ctx.fill();}
for(let a=-600;a<=600;a+=100){line([a,-600,0],[a,600,0],'#203149');line([-600,a,0],[600,a,0],'#203149');}
[['X',[120,0,0],'#f07888'],['Y',[0,120,0],'#65d49c'],['Z',[0,0,120],'#78acff']].forEach(([label,p,color])=>{line([0,0,0],p,color,2);const xy=project(p);ctx.fillStyle=color;ctx.font='12px sans-serif';ctx.fillText(label,...xy);});
for(let i=1;i<S.length;i++){const a=S[i-1],b=S[i];if(!a.links||!b.links||b.t-a.t>.1)continue;
ctx.globalAlpha=i>index?.45:1;line(a.links.at(-1),b.links.at(-1),`hsl(${195-177*i/(S.length-1)} 85% 66%)`,i>index?1.4:2.5);ctx.globalAlpha=1;}
const valid=S.filter(s=>s.links);dot(valid[0].links.at(-1),'#51c8ed',5);dot(valid.at(-1).links.at(-1),'#ff8a59',5);
const current=S[index];if(current.links){for(let i=1;i<current.links.length;i++)line(current.links[i-1],current.links[i],'#6ee2c3',5);
current.links.forEach((p,j)=>dot(p,j===6?'#ffffff':'#9fedd8',j===6?7:4));}
ctx.fillStyle='#7f96b4';ctx.font='11px sans-serif';ctx.fillText('正交投影 · 拖动旋转 / 滚轮缩放',12,h-12);
}
const ranges=Array.from({length:6},(_,j)=>{const a=S.flatMap(s=>[s.q?.[j],s.target?.[j]]).filter(Number.isFinite);let mn=Math.min(...a),mx=Math.max(...a);const pad=Math.max((mx-mn)*.16,2);return [mn-pad,mx+pad];});
function curves(){const [ctx,w,h]=canvas('joints'),left=46,right=w-16,rowH=h/6;
const x=t=>left+(t-first)/duration*(right-left);
for(let j=0;j<6;j++){const top=j*rowH+8,bottom=(j+1)*rowH-16,[mn,mx]=ranges[j],y=v=>bottom-(v-mn)/(mx-mn)*(bottom-top);
ctx.font='10px sans-serif';ctx.fillStyle='#7d93b0';ctx.strokeStyle='#253650';ctx.lineWidth=1;ctx.setLineDash([]);
for(let k=0;k<=4;k++){const xx=left+(right-left)*k/4;ctx.beginPath();ctx.moveTo(xx,top);ctx.lineTo(xx,bottom);ctx.stroke();}
ctx.fillStyle=colors[j];ctx.fillText('J'+(j+1),5,top+10);ctx.fillStyle='#859bb8';ctx.fillText(mx.toFixed(0)+'°',6,top+23);ctx.fillText(mn.toFixed(0)+'°',6,bottom);
for(const key of ['target','q']){ctx.lineWidth=key==='q'?1.7:1;ctx.strokeStyle=key==='q'?colors[j]:'#687b95';ctx.setLineDash(key==='q'?[]:[4,4]);ctx.beginPath();let previous=null;
for(const s of S){if(!s[key]){previous=null;continue;}const xx=x(s.t),yy=y(s[key][j]);if(previous===null||s.t-previous>.1)ctx.moveTo(xx,yy);else ctx.lineTo(xx,yy);previous=s.t;}ctx.stroke();}
ctx.setLineDash([]);ctx.strokeStyle='#d9e6ff';ctx.lineWidth=.8;ctx.beginPath();ctx.moveTo(x(first+elapsed),top);ctx.lineTo(x(first+elapsed),bottom);ctx.stroke();
if(S[index].q){ctx.fillStyle=colors[j];ctx.beginPath();ctx.arc(x(S[index].t),y(S[index].q[j]),3,0,Math.PI*2);ctx.fill();}
ctx.fillStyle='#7d93b0';ctx.fillText('0',left,bottom+12);ctx.fillText(duration.toFixed(1)+' s',right-35,bottom+12);
}
const [g,gw,gh]=canvas('gripper'),gl=46,gr=gw-16,gb=gh-20,gt=10;
const openings=S.map(s=>s.grip).filter(Number.isFinite),maxG=Math.max(70,...openings),gx=t=>gl+(t-first)/duration*(gr-gl),gy=v=>gb-v/maxG*(gb-gt);
g.strokeStyle='#293b53';g.lineWidth=1;for(const v of [0,maxG/2,maxG]){g.beginPath();g.moveTo(gl,gy(v));g.lineTo(gr,gy(v));g.stroke();g.fillStyle='#859bb8';g.font='10px sans-serif';g.fillText(v.toFixed(0),9,gy(v)+4);}
g.strokeStyle='#67dec2';g.lineWidth=2;g.beginPath();let prev=null;
for(const s of S){if(s.grip===null){prev=null;continue;}if(prev===null||s.t-prev>.1)g.moveTo(gx(s.t),gy(s.grip));else g.lineTo(gx(s.t),gy(s.grip));prev=s.t;}g.stroke();
g.strokeStyle='#d9e6ff';g.lineWidth=1;g.beginPath();g.moveTo(gx(first+elapsed),gt);g.lineTo(gx(first+elapsed),gb);g.stroke();
}
function render(){scene();curves();const s=S[index],tip=s.links?.at(-1);$('seek').value=elapsed;
$('time').textContent=`${elapsed.toFixed(2)} / ${duration.toFixed(2)} s · #${s.frame}`;
$('values').replaceChildren();for(let j=0;j<6;j++){const e=document.createElement('div');e.style.color=colors[j];e.textContent=`J${j+1} ${s.q?s.q[j].toFixed(2)+'°':'缺失'}`;$('values').appendChild(e);}
$('tooltip').textContent=tip?`FK 末端 XYZ:${tip.map(v=>v.toFixed(1)).join(' / ')} mm | 夹爪:${s.grip===null?'缺失':s.grip.toFixed(1)+' mm'}`:'当前帧实际关节反馈缺失';}
function seek(t){elapsed=Math.max(0,Math.min(duration,t));let a=0,b=S.length;
while(a<b){const m=(a+b)>>1;if(S[m].t<=first+elapsed)a=m+1;else b=m;}index=Math.max(0,a-1);render();}
function stop(){playing=false;if(animationId!==null)cancelAnimationFrame(animationId);animationId=null;$('play').textContent='播放';}
function tick(now){if(!playing)return;const dt=(now-lastTick)/1000;lastTick=now;seek(elapsed+dt*Number($('speed').value));if(elapsed>=duration)stop();else animationId=requestAnimationFrame(tick);}
$('play').onclick=()=>{if(playing)stop();else{if(elapsed>=duration)seek(0);playing=true;$('play').textContent='暂停';lastTick=performance.now();animationId=requestAnimationFrame(tick);}};
$('back').onclick=()=>{stop();seek(0);};$('seek').oninput=e=>{stop();seek(Number(e.target.value));};
for(const id of ['joints','gripper']){$(id).onclick=e=>{stop();const rect=$(id).getBoundingClientRect();seek((e.clientX-rect.left-46)/(rect.width-62)*duration);};}
let drag=null;$('scene').onpointerdown=e=>{drag=[e.clientX,e.clientY,yaw,pitch];$('scene').setPointerCapture(e.pointerId);};
$('scene').onpointermove=e=>{if(!drag)return;yaw=drag[2]+(e.clientX-drag[0])*.009;pitch=Math.max(-1.4,Math.min(1.4,drag[3]+(e.clientY-drag[1])*.008));scene();};
$('scene').onpointerup=()=>drag=null;$('scene').onpointercancel=()=>drag=null;
$('scene').addEventListener('wheel',e=>{e.preventDefault();zoom=Math.max(.35,Math.min(3.5,zoom*Math.exp(-e.deltaY*.001)));scene();},{passive:false});
$('reset').onclick=()=>{yaw=-.9;pitch=.65;zoom=1;scene();};window.addEventListener('resize',render);render();
window.trajectoryViewer={seek,get index(){return index},get frames(){return S.length}};
if(new URLSearchParams(location.search).has('selftest')){
const assert=(ok,message)=>{if(!ok)throw Error(message);};
try{
seek(duration/2);assert(index>0&&index<S.length-1,'midpoint seek');
$('seek').value=duration/4;$('seek').dispatchEvent(new Event('input'));assert(Math.abs(elapsed-duration/4)<.002,'slider input');
$('play').click();assert(playing&&animationId!==null,'play');$('play').click();assert(!playing&&animationId===null,'pause');
$('back').click();assert(index===0&&elapsed===0,'rewind');seek(duration);assert(index===S.length-1,'last frame');
seek(duration*.65);$('note').textContent='UI SELF-TEST PASS: midpoint / slider / play / pause / rewind / final frame. '+$('note').textContent;
}catch(error){document.title='UI SELF-TEST FAIL';$('note').textContent='UI SELF-TEST FAIL: '+error.message;throw error;}
}
</script>
</html>