Spaces:
Running
Running
Publish tested Next AI Lab release
Browse files- README.md +11 -13
- index.html +74 -178
README.md
CHANGED
|
@@ -1,13 +1,11 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: Next Servo
|
| 3 |
-
sdk: static
|
| 4 |
-
app_file: index.html
|
| 5 |
-
colorFrom: green
|
| 6 |
-
colorTo: gray
|
| 7 |
-
models:
|
| 8 |
-
- thelamapi/Next-Servo
|
| 9 |
-
---
|
| 10 |
-
|
| 11 |
-
# Next Servo
|
| 12 |
-
|
| 13 |
-
[Model card and Python source](https://huggingface.co/thelamapi/Next-Servo)
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Next Servo
|
| 3 |
+
sdk: static
|
| 4 |
+
app_file: index.html
|
| 5 |
+
colorFrom: green
|
| 6 |
+
colorTo: gray
|
| 7 |
+
models:
|
| 8 |
+
- thelamapi/Next-Servo
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# Next Servo
|
|
|
|
|
|
index.html
CHANGED
|
@@ -1,178 +1,74 @@
|
|
| 1 |
-
<!doctype html>
|
| 2 |
-
<html lang="en">
|
| 3 |
-
<meta charset="utf-8">
|
| 4 |
-
<meta name="viewport" content="width=device-width,
|
| 5 |
-
<title>Next Servo</title>
|
| 6 |
-
<style>
|
| 7 |
-
*{box-sizing:border-box}
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
input
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
const
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
});
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
ctx.arc(...pixel(p), radius, 0, 2*Math.PI);
|
| 76 |
-
if (fill) { ctx.fillStyle = fill; ctx.fill(); }
|
| 77 |
-
if (stroke) { ctx.lineWidth = 2; ctx.strokeStyle = stroke; ctx.stroke(); }
|
| 78 |
-
}
|
| 79 |
-
|
| 80 |
-
function forward([a,b]) {
|
| 81 |
-
const elbow = [Math.cos(a), Math.sin(a)];
|
| 82 |
-
return [elbow, [elbow[0]+Math.cos(a+b), elbow[1]+Math.sin(a+b)]];
|
| 83 |
-
}
|
| 84 |
-
|
| 85 |
-
function draw() {
|
| 86 |
-
ctx.fillStyle = "#e4ede4";
|
| 87 |
-
ctx.fillRect(0,0,620,460);
|
| 88 |
-
for (let n=-2; n<=2; n+=0.5) {
|
| 89 |
-
line([[n,-2],[n,2]], "#d0ded2", 1);
|
| 90 |
-
line([[-2,n],[2,n]], "#d0ded2", 1);
|
| 91 |
-
}
|
| 92 |
-
if (!scene) return;
|
| 93 |
-
const [start,goal,[x,y,r]] = scene;
|
| 94 |
-
circle([x,y],100*r,"#dfa18f","#a74436");
|
| 95 |
-
circle(start,7,null,"#506961");
|
| 96 |
-
circle(goal,11,null,"#087b64");
|
| 97 |
-
circle([0,0],8,"#142e28");
|
| 98 |
-
if (!route.length) return;
|
| 99 |
-
|
| 100 |
-
const t = Number(get("progress").value)/1000*(route.length-1);
|
| 101 |
-
const i = Math.floor(t);
|
| 102 |
-
const next = route[Math.min(i+1,route.length-1)];
|
| 103 |
-
const q = route[i].map((value,j) => value+(next[j]-value)*(t-i));
|
| 104 |
-
const [elbow,tip] = forward(q);
|
| 105 |
-
line([...route.slice(0,i+1).map(pose => forward(pose)[1]),tip],"#429e86",3);
|
| 106 |
-
line([[0,0],elbow,tip],"#142e28",7);
|
| 107 |
-
circle(elbow,7,"#142e28","white");
|
| 108 |
-
circle(tip,6.5,"#087b64","white");
|
| 109 |
-
}
|
| 110 |
-
|
| 111 |
-
function solve() {
|
| 112 |
-
cancelAnimationFrame(animation);
|
| 113 |
-
const [sx,sy,gx,gy,ox,oy,r] = inputs.map(input => input.valueAsNumber);
|
| 114 |
-
route = [];
|
| 115 |
-
get("progress").disabled = true;
|
| 116 |
-
if (![sx,sy,gx,gy,ox,oy,r].every(Number.isFinite) ||
|
| 117 |
-
Math.hypot(sx,sy)>2 || Math.hypot(gx,gy)>2 || r<0.01 || r>1) {
|
| 118 |
-
get("status").textContent = "Use reachable endpoints and a radius between 0.01 and 1.";
|
| 119 |
-
draw();
|
| 120 |
-
return;
|
| 121 |
-
}
|
| 122 |
-
scene = [[sx,sy],[gx,gy],[ox,oy,r]];
|
| 123 |
-
try {
|
| 124 |
-
py.globals.set("request_json", JSON.stringify(scene));
|
| 125 |
-
const result = JSON.parse(py.runPython("solve_request(request_json)"));
|
| 126 |
-
route = result.route;
|
| 127 |
-
get("progress").value = 0;
|
| 128 |
-
get("progress").disabled = !route.length;
|
| 129 |
-
get("status").textContent = route.length
|
| 130 |
-
? (route.length-1) + " planner edges · Direct move " + (result.direct ? "accepted" : "rejected")
|
| 131 |
-
: "No route certified for this configuration.";
|
| 132 |
-
draw();
|
| 133 |
-
if (!route.length) return;
|
| 134 |
-
const started = performance.now();
|
| 135 |
-
function play(now) {
|
| 136 |
-
get("progress").value = Math.min(1000, Math.round((now-started)/2.2));
|
| 137 |
-
draw();
|
| 138 |
-
if (Number(get("progress").value)<1000) animation = requestAnimationFrame(play);
|
| 139 |
-
}
|
| 140 |
-
animation = requestAnimationFrame(play);
|
| 141 |
-
} catch (error) {
|
| 142 |
-
get("status").textContent = "Planning error: " + error.message;
|
| 143 |
-
}
|
| 144 |
-
}
|
| 145 |
-
|
| 146 |
-
function preset() {
|
| 147 |
-
presets[Number(get("preset").value)].flat().forEach((value,i) => inputs[i].value = value);
|
| 148 |
-
solve();
|
| 149 |
-
}
|
| 150 |
-
|
| 151 |
-
get("form").addEventListener("submit", event => { event.preventDefault(); solve(); });
|
| 152 |
-
get("preset").addEventListener("change", preset);
|
| 153 |
-
get("progress").addEventListener("input", () => { cancelAnimationFrame(animation); draw(); });
|
| 154 |
-
draw();
|
| 155 |
-
|
| 156 |
-
try {
|
| 157 |
-
const indexURL = "https://cdn.jsdelivr.net/pyodide/v0.27.7/full/";
|
| 158 |
-
const {loadPyodide} = await import(indexURL+"pyodide.mjs");
|
| 159 |
-
py = await loadPyodide({indexURL});
|
| 160 |
-
const source = await fetch("./servo.py");
|
| 161 |
-
if (!source.ok) throw new Error("servo.py could not be loaded");
|
| 162 |
-
py.runPython((await source.text()).replace(/^\uFEFF/,""));
|
| 163 |
-
py.runPython([
|
| 164 |
-
"import json",
|
| 165 |
-
"def solve_request(raw):",
|
| 166 |
-
" case = json.loads(raw)",
|
| 167 |
-
" return json.dumps(dict(route=plan(*case), direct=direct(*case)))"
|
| 168 |
-
].join("\n"));
|
| 169 |
-
presets = JSON.parse(py.runPython("json.dumps(DEMOS)"));
|
| 170 |
-
["Across the fixture","High shelf","Between stations","Narrow pocket"]
|
| 171 |
-
.forEach((name,i) => get("preset").add(new Option(name,i)));
|
| 172 |
-
for (const id of ["preset","fields","run"]) get(id).disabled = false;
|
| 173 |
-
preset();
|
| 174 |
-
} catch (error) {
|
| 175 |
-
get("status").textContent = "Live planner could not load: " + error.message;
|
| 176 |
-
}
|
| 177 |
-
</script>
|
| 178 |
-
</html>
|
|
|
|
| 1 |
+
<!doctype html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<meta charset="utf-8">
|
| 4 |
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
| 5 |
+
<title>Next Servo</title>
|
| 6 |
+
<style>
|
| 7 |
+
*{box-sizing:border-box}body{margin:0;background:#f3f6f1;color:#142c27;font:16px system-ui}
|
| 8 |
+
main{max-width:960px;margin:auto;padding:36px 20px}h1{font-size:clamp(42px,8vw,78px);letter-spacing:-.06em;margin:0}
|
| 9 |
+
p{line-height:1.55;color:#415a52}canvas,video{width:100%;display:block;border-radius:16px;background:#e4eee5}
|
| 10 |
+
button,select,input,textarea{font:inherit}button,select,input,textarea{padding:9px;border:1px solid #aabdaf;border-radius:8px;background:white}
|
| 11 |
+
button{background:#08775e;color:white;cursor:pointer;margin:8px 0}button:disabled{opacity:.5}
|
| 12 |
+
textarea{width:100%;font:16px monospace}a{color:#08644f}label{display:inline-block;margin:6px}
|
| 13 |
+
</style>
|
| 14 |
+
<main><h1>Next Servo</h1><p>Plan a collision-aware transfer and inspect each joint movement.</p>
|
| 15 |
+
<canvas id="map" width="720" height="480" aria-label="Arm and payload"></canvas>
|
| 16 |
+
<form id="form"><div id="fields"></div><button id="run" disabled>Plan transfer</button></form>
|
| 17 |
+
<label>Motion <input id="motion" type="range" value="0" min="0" max="1000"></label>
|
| 18 |
+
<p id="status" role="status">Loading planner...</p>
|
| 19 |
+
<video controls playsinline poster="preview.png" src="replay.mp4"></video>
|
| 20 |
+
<script type="module">
|
| 21 |
+
const el=id=>document.getElementById(id), pen=el("map").getContext("2d");
|
| 22 |
+
const inputs=["Pickup X","Pickup Y","Goal X","Goal Y","Obstacle X","Obstacle Y","Radius"].map(name=>{
|
| 23 |
+
const label=document.createElement("label"), input=document.createElement("input");
|
| 24 |
+
input.type="number";input.step="any";input.required=true;input.style.width="85px";
|
| 25 |
+
label.append(name+" ",input);el("fields").append(label);return input;
|
| 26 |
+
});
|
| 27 |
+
let py, route=[], scene, anim;
|
| 28 |
+
function xy([x,y]){return [360+105*x,240-105*y]}
|
| 29 |
+
function dot(p,r,color){pen.beginPath();pen.arc(...xy(p),r,0,Math.PI*2);pen.fillStyle=color;pen.fill()}
|
| 30 |
+
function line(points,color,width){pen.beginPath();points.forEach((p,i)=>i?pen.lineTo(...xy(p)):pen.moveTo(...xy(p)));pen.strokeStyle=color;pen.lineWidth=width;pen.lineCap="round";pen.stroke()}
|
| 31 |
+
function forward([a,b]){const e=[Math.cos(a),Math.sin(a)];return [e,[e[0]+Math.cos(a+b),e[1]+Math.sin(a+b)]]}
|
| 32 |
+
function draw(){
|
| 33 |
+
pen.fillStyle="#e4eee5";pen.fillRect(0,0,720,480);
|
| 34 |
+
for(let n=-2;n<=2;n+=.5){line([[n,-2],[n,2]],"#cadbcc",1);line([[-2,n],[2,n]],"#cadbcc",1)}
|
| 35 |
+
if(!scene)return;
|
| 36 |
+
const [start,target,[x,y,r]]=scene;
|
| 37 |
+
dot(start,6,"#647c74");dot(target,10,"#3376b5");dot([x,y],r*105,"#db9682");
|
| 38 |
+
if(!route.length)return;
|
| 39 |
+
const t=el("motion").value/1000*(route.length-1), i=Math.floor(t);
|
| 40 |
+
const q=route[i].map((a,j)=>a+(route[Math.min(i+1,route.length-1)][j]-a)*(t-i));
|
| 41 |
+
const [e,tip]=forward(q);
|
| 42 |
+
line([...route.slice(0,i+1).map(q=>forward(q)[1]),tip],"#359b83",3);
|
| 43 |
+
line([[0,0],e,tip],"#142c27",7);dot([0,0],8,"#142c27");dot(e,8,"#142c27");dot(tip,6.8,"#08775e");
|
| 44 |
+
}
|
| 45 |
+
function solve(){
|
| 46 |
+
cancelAnimationFrame(anim);
|
| 47 |
+
const [a,b,c,d,x,y,r]=inputs.map(input=>input.valueAsNumber);
|
| 48 |
+
if(Math.hypot(a,b)>=2||Math.hypot(c,d)>=2||r<=0||r>1){
|
| 49 |
+
el("status").textContent="Choose reachable endpoints and a radius between 0 and 1.";return;
|
| 50 |
+
}
|
| 51 |
+
scene=[[a,b],[c,d],[x,y,r]]; route=[];
|
| 52 |
+
try{
|
| 53 |
+
py.globals.set("request_text",JSON.stringify(scene));
|
| 54 |
+
route=JSON.parse(py.runPython("json.dumps(plan(*json.loads(request_text)))"));
|
| 55 |
+
el("status").textContent=route.length ? "Transfer planned · "+(route.length-1)+" edges" : "No route certified.";
|
| 56 |
+
el("motion").value=0;draw();
|
| 57 |
+
if(route.length){const start=performance.now();function play(t){el("motion").value=Math.min(1000,(t-start)/2.4);draw();if(el("motion").value<1000)anim=requestAnimationFrame(play)}anim=requestAnimationFrame(play)}
|
| 58 |
+
}catch(error){el("status").textContent=error.message;draw()}
|
| 59 |
+
}
|
| 60 |
+
el("form").onsubmit=e=>{e.preventDefault();solve()};
|
| 61 |
+
el("motion").oninput=()=>{cancelAnimationFrame(anim);draw()};
|
| 62 |
+
draw();
|
| 63 |
+
try{
|
| 64 |
+
const indexURL="https://cdn.jsdelivr.net/pyodide/v0.27.7/full/";
|
| 65 |
+
py=await (await import(indexURL+"pyodide.mjs")).loadPyodide({indexURL});
|
| 66 |
+
const res=await fetch("servo.py");if(!res.ok)throw Error("Planner source unavailable");
|
| 67 |
+
py.runPython((await res.text()).replace(/^\uFEFF/,"")+"\nimport json");
|
| 68 |
+
JSON.parse(py.runPython("json.dumps(DEMOS[0])")).flat().forEach((n,i)=>inputs[i].value=n);
|
| 69 |
+
el("run").disabled=false;solve();
|
| 70 |
+
}catch(error){el("status").textContent="Could not load planner. "+error.message}
|
| 71 |
+
</script>
|
| 72 |
+
|
| 73 |
+
<p><a href="https://huggingface.co/thelamapi/Next-Servo">Model card and source</a></p>
|
| 74 |
+
</main></html>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|