// Drone twin panel: the open job as a painting job a drone is flying. Set the launch point, upload a flight log (CSV), and see what the // twin believes is painted, what is left, and what the log says about how the drone really flew. The heat map overlay is off by default. // (Separate from the wall twin in twin.js, which fuses photos and video in millimetres.) "use strict"; (() => { const $ = (sel) => document.querySelector(sel); const toggles = $("#layer-toggles"), stage = $("#stage"), anchor = $("#reasons"); if (!toggles || !stage || !anchor) return; const st = { loaded: false, loading: false, info: null, jobId: null }; const esc = (s) => String(s ?? "").replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c])); const num = (v) => { const n = parseFloat(v); return Number.isFinite(n) ? n : null; }; // overlay layer + switch const chip = document.createElement("label"); chip.className = "chip"; chip.innerHTML = `Drone twin`; toggles.appendChild(chip); const box = chip.querySelector("input"); const img = document.createElement("img"); img.className = "layer off dtwin-layer"; img.alt = ""; img.dataset.layer = "dronetwin"; (document.querySelector('img.layer[data-layer="paint"]:not(.edge)') || $("#photo")).after(img); // next sortie: the passes the belief still wants, drawn over the photo (own switch in the panel, off by default) const NS = "http://www.w3.org/2000/svg"; const nextSvg = document.createElementNS(NS, "svg"); nextSvg.setAttribute("class", "layer dtwin-next"); nextSvg.setAttribute("preserveAspectRatio", "none"); nextSvg.style.display = "none"; img.after(nextSvg); // marked pass: one line picked from the audit table, so a bad pass can be pointed at on the photo const passSvg = document.createElementNS(NS, "svg"); passSvg.setAttribute("class", "layer dtwin-marks"); passSvg.setAttribute("preserveAspectRatio", "none"); passSvg.style.display = "none"; nextSvg.after(passSvg); // panel const panel = document.createElement("section"); panel.id = "dtwin-panel"; panel.innerHTML = `

Drone twin

Without a flight log the twin only knows the plan. A log adds what the drone really flew and how well; nothing here claims a wall is finished unless the belief says so and a camera has looked recently.

`; anchor.parentNode.insertBefore(panel, anchor.nextSibling); async function api(path, options = {}) { const r = await fetch(path, options); const body = await r.json().catch(() => ({})); if (!r.ok) throw new Error(body.error || r.statusText); return body; } const j = () => (typeof job !== "undefined" && job ? job : null); const ago = (sec) => (sec == null ? "never" : sec < 90 ? `${Math.round(sec)} s ago` : sec < 5400 ? `${Math.round(sec / 60)} min ago` : `${(sec / 3600).toFixed(1)} h ago`); function render(info) { const s = info.status, c = info.calibration || {}; const words = { complete: "Complete", another_sortie: "Another sortie needed", hand_finish: "Hand finish left", needs_look: "Needs a look" }; const cls = { complete: "ok", another_sortie: "warn", hand_finish: "warn", needs_look: "bad" }[s.verdict] || ""; $("#dt-status").hidden = false; const au = info.spray_audit, arows = au && Array.isArray(au.passes) ? au.passes : []; const starved = au ? (au.starved_passes || arows.filter((p) => (p.flags || []).includes("starved_end")).map((p) => p.id)) : []; const heavy = au ? (au.heavy_passes || arows.filter((p) => (p.flags || []).includes("heavy_end")).map((p) => p.id)) : []; const pct = (v) => Math.round((v || 0) * 100); const action = { complete: "Nothing to fly. Keep the last re-scan with the job and hand it over.", another_sortie: "Fly the next sortie below, then upload a re-scan photo.", needs_look: "Take a re-scan photo: the belief is old, or parts of the wall were never seen.", hand_finish: "What is left is narrower than the fan: hand finish, then re-scan to confirm.", }[s.verdict] || ""; const tol = 0.15 * (Number((info.setup || {}).standoff_mm) || 300); // ±15 % film budget, in mm of standoff const sag = au && au.standoff_sag_mm != null ? Number(au.standoff_sag_mm) : null; const sagPct = sag == null ? null : Math.max(2, Math.min(100, Math.round((sag / tol) * 100))); const spray = sag == null ? "" : `
Spray push on the nozzle${au.reaction_n} N
sag ${sag} mm of ±${Math.round(tol)} mm budget${au.standoff_bias_mm ? ` · setpoint biased ${au.standoff_bias_mm} mm` : ""}
`; const fixes = starved.length + heavy.length; // when nearly every pass shares one fault, the count is useless and the cause is what matters: say it const medSpeed = (() => { const v = arows.map((p) => Number(p.speed_mm_s)).filter((x) => Number.isFinite(x) && x > 0).sort((a, b) => a - b); return v.length ? v[Math.floor(v.length / 2)] : null; })(); const share = (k) => arows.filter((p) => Number(p[k + "_share"] || 0) > 0.2).length; const plan = Number(((au && au.design) || {}).speed_mm_s); let diag = ""; if (arows.length >= 5 && plan > 0 && medSpeed) { if (share("heavy") >= 0.7 * arows.length && plan > medSpeed) { diag = `Almost every pass came out heavy: the log averages ${Math.round(medSpeed)} mm/s against the plan's ${Math.round(plan)} mm/s, so each metre got about ${(plan / medSpeed).toFixed(1)}× the paint it was planned for.`; } else if (share("starved") >= 0.7 * arows.length && medSpeed > plan) { diag = `Almost every pass came out thin: the log averages ${Math.round(medSpeed)} mm/s against the plan's ${Math.round(plan)} mm/s, so each metre got about ${(medSpeed / plan).toFixed(1)}× too much speed for full film.`; } } const diagEl = diag ? `

${esc(diag)}

` : ""; const onT = Number(au && au.on_target_pct); const audit = au ? `
= 95 ? "warn" : "bad") : ""}">Paint on target${Number.isFinite(onT) ? onT + "%" : "—"} Waste${Number.isFinite(Number(au.waste_l)) ? Number(au.waste_l).toFixed(2) + " L" : "—"} Passes flagged${fixes || "none"} Passes audited${au.pass_count || arows.length || "—"}
${spray}` : ""; const missing = (s.missing || []).filter(Boolean); const why = missing.length && s.verdict !== "complete" ? `
Why it is not done (${missing.length})
` : ""; $("#dt-status").innerHTML = `
${esc(words[s.verdict] || s.verdict)} ${pct(s.believed_painted)}% believed painted · ${s.gap_m2} m² still bare

Next: ${esc(action)}

= 0.5 ? "warn" : "bad"}">Camera-verified${pct(s.fresh)}%last look ${ago(s.last_observed_s)} 0.001 ? "warn" : "ok"}">Never photographed${pct(s.never_seen)}% 0.001 ? "warn" : "ok"}">Stale${pct(s.stale)}%over ${Math.round((s.max_age_s || 0) / 60)} min Unsure${pct(s.uncertain)}%
${audit}${diagEl}${why}
Facade
${info.facade.width_m} × ${info.facade.height_m} m
Fan / pass spacing
${Math.round(info.spray.fan_width_mm)} / ${Math.round(info.spray.pass_spacing_mm)} mm
Sorties logged
${info.sorties}
${c.source ? `
Flown accuracy
±${(c.positioning_m * 100).toFixed(1)} cm · standoff ±${(c.standoff_error_rms_m * 100).toFixed(1)} cm
` : ""} ${c.tpr ? `
Camera hit / false alarm
${Math.round(c.tpr * 100)}% / ${Math.round(c.fpr * 100)}%
` : ""}
`; st.info = info; st.loaded = true; st.jobId = j().id; renderPasses(arows); const wi = $("#dt-whatif"); wi.hidden = false; if (!wi.dataset.asked) { wi.dataset.asked = "1"; runWhatIf(); } $("#dt-actions").hidden = false; $("#dt-json").href = `/api/jobs/${j().id}/dronetwin.json`; $("#dt-geo").href = `/api/jobs/${j().id}/dronetwin_geojson.json`; const v = `?v=${Date.now()}`; img.src = `/api/jobs/${j().id}/dronetwin_heatmap.png${v}`; const sc = info.setup; if (sc.home) { $("#dt-lat").value = sc.home[0]; $("#dt-lon").value = sc.home[1]; $("#dt-alt").value = sc.home[2] ?? ""; } $("#dt-masked").checked = !!sc.masked_openings; drawHistory(info.history || []); st.info = info; st.loaded = true; st.jobId = j().id; st.next = null; if ($("#dt-next").checked) showNext(); else { $("#dt-next-info").hidden = true; $("#dt-plan").hidden = true; } if (typeof applyLayerToggles === "function") applyLayerToggles(); } // the audit's own rows: the worst passes first, click one to mark where it was flown function renderPasses(rows) { const box = $("#dt-passes"), list = $("#dt-passrows"); list.textContent = ""; const usable = (rows || []).filter((p) => Array.isArray(p.line_mm) && p.line_mm.length === 4); if (!usable.length) { box.hidden = true; passSvg.style.display = "none"; return; } const flagged = (p) => (p.flags || []).length > 0; const worst = usable.slice().sort((a, b) => (flagged(b) - flagged(a)) || (Number(a.film_median || 0) - Number(b.film_median || 0))).slice(0, 12); worst.forEach((p) => { const li = document.createElement("li"); li.className = "dt-passrow" + (flagged(p) ? " flagged" : ""); const b = document.createElement("button"); b.type = "button"; b.dataset.id = String(p.id); b.dataset.line = JSON.stringify(p.line_mm); b.innerHTML = `#${esc(p.id)}${Number(p.film_median || 0).toFixed(2)} film` + `${Math.round((p.starved_share || 0) * 100)}% thin${esc((p.flags || []).join(", ") || "ok")}`; li.append(b); list.append(li); }); box.hidden = false; list.onclick = (e) => { const b = e.target.closest("button[data-line]"); if (!b) return; list.querySelectorAll("button").forEach((x) => x.classList.toggle("on", x === b)); markPass(JSON.parse(b.dataset.line), b.dataset.id); }; } function markPass(line, id) { const info = st.info, jb = j(); if (!info || !jb || !Array.isArray(line) || line.length !== 4) return; const mm = Number(info.facade.mm_per_px), H = Number(info.height); if (!(mm > 0) || !(H > 0)) return; const [x0, z0, x1, z1] = line.map(Number); const a = [x0 / mm, H - z0 / mm], b = [x1 / mm, H - z1 / mm]; passSvg.setAttribute("viewBox", `0 0 ${jb.width} ${jb.height}`); passSvg.innerHTML = `` + `` + ``; passSvg.style.display = ""; say(`Pass ${id} marked on the photo.`); } // what-if: the twin's own prediction under a different position error and wind let wiTimer = 0; function wiLabels() { $("#dt-wi-pos-v").textContent = `${Number($("#dt-wi-pos").value).toFixed(1)} cm`; // the slider is in cm, the API wants m $("#dt-wi-wind-v").textContent = `${Number($("#dt-wi-wind").value).toFixed(1)} m/s`; } async function runWhatIf() { if (!j() || !st.loaded) return; wiLabels(); const out = $("#dt-wi-out"); out.innerHTML = `Computing…`; const p = (Number($("#dt-wi-pos").value) / 100).toFixed(3), w = Number($("#dt-wi-wind").value).toFixed(1); try { const r = await api(`/api/jobs/${j().id}/dronetwinwhatif?positioning=${p}&wind=${w}&trials=3`); const tone = r.good >= 0.9 ? "ok" : r.good >= 0.8 ? "warn" : "bad"; out.innerHTML = `Good film${Math.round(r.good * 100)}%` + `Thin${Math.round(r.thin * 100)}%` + `On openings${Math.round(r.overspray_on_openings * 100)}%` + `Hand finish${Number(r.hand_finish_m2 || 0).toFixed(1)} m²` + `Margin off glass${Math.round(r.margin_mm)} mm` + `Confidence${r.trials} trial${r.trials === 1 ? "" : "s"}`; } catch (e) { out.innerHTML = `What-if unavailable: ${esc(e.message)}`; } } // belief over time: one point per sortie or re-scan, from the event ledger function drawHistory(h) { const el = $("#dt-history"); if (h.length < 2) { el.hidden = true; return; } const W = 240, H = 46, P = 4, tMax = Math.max(...h.map((e) => e[0]), 1); const x = (t) => P + (t / tMax) * (W - 2 * P), y = (v) => H - P - v * (H - 2 * P); const line = h.map((e, i) => `${i ? "L" : "M"}${x(e[0]).toFixed(1)},${y(e[2]).toFixed(1)}`).join(""); const dots = h.slice(1).map((e) => `${esc(e[1] === "observe" ? "Re-scan" : "Sortie")}: ${Math.round(e[2] * 100)}%`).join(""); const last = h[h.length - 1][2]; el.hidden = false; el.innerHTML = `
Believed painted over time${Math.round(last * 100)}%
${dots}
sortiere-scan photo
`; } async function showNext() { if (!j() || !st.loaded) { nextSvg.style.display = "none"; return; } try { if (!st.next) st.next = await api(`/api/jobs/${j().id}/dronetwinnext`); } catch (e) { say(`Next sortie unavailable: ${e.message}`); return; } const n = st.next, jb = j(); nextSvg.setAttribute("viewBox", `0 0 ${jb.width} ${jb.height}`); const defs = ``; nextSvg.innerHTML = defs + (n.strokes || []).map((s, i) => { const d = s.map((p, k) => `${k ? "L" : "M"}${p[0].toFixed(1)},${p[1].toFixed(1)}`).join(""); const [ex, ey] = s[s.length - 1], [sx, sy] = s[0]; return `Pass ${i + 1} of ${n.passes}` + `` + `` + (i === 0 ? `` : ""); }).join(""); nextSvg.style.display = ""; const info = $("#dt-next-info"); info.hidden = false; if (!n.passes) { info.innerHTML = `Nothing left for a sortie. ${esc(n.note || "")}`; $("#dt-plan").hidden = true; return; } const g = n.gate, so = n.sorties || []; const paint = so.reduce((a, s) => a + (s.paint_l || 0), 0), spray = so.reduce((a, s) => a + (s.spray_s || 0), 0); info.innerHTML = `
Next sortie: ${n.passes} pass${n.passes === 1 ? "" : "es"}` + (so.length ? `${so.length} flight${so.length === 1 ? "" : "s"} · ${Math.round(spray)} s spray · ${paint.toFixed(2)} L` : "") + `
` + (n.reaction ? `
push ${n.reaction.reaction_n} N · setpoint ${Math.round(n.reaction.setpoint_mm)} mm + ${n.reaction.sag_mm} mm sag
` : "") + (g ? `
${g.go ? "Go: inside the drone's limits" : "No-go: " + esc((g.reasons || []).join("; "))}
` : `
Set a launch point to get a flight plan.
`) + (n.refused ? `
The passes are shown, but no flight plan is offered for download until the gate passes.
` : ""); const a = $("#dt-plan"); if (n.mission && n.mission.length) { a.hidden = false; const body = n.mission.length === 1 ? n.mission[0] : n.mission; if (a.href.startsWith("blob:")) URL.revokeObjectURL(a.href); a.href = URL.createObjectURL(new Blob([JSON.stringify(body, null, 2)], { type: "application/json" })); a.download = n.mission.length === 1 ? `next-sortie-${jb.id}.plan` : `next-sorties-${jb.id}.json`; a.textContent = n.mission.length === 1 ? "Flight plan (.plan)" : `Flight plans (${n.mission.length})`; } else a.hidden = true; } const say = (m) => { $("#dt-note").textContent = m; }; // collapse.js wraps this panel as an accordion card and starts it folded; switching the twin on should open it, or the // first thing the user sees is a bare header (the folded body is inert, so nothing in it is clickable). function openCard() { const card = $("#dtwin-panel"), head = card && card.querySelector(":scope > .acc-head"); if (head && card.classList.contains("collapsed")) head.click(); // same path the user takes, so the state is remembered } async function ensure(request) { if (st.loading) return; if (!j()) { say("Open a job first: the twin is built from its photo and its scale."); return; } openCard(); st.loading = true; say("Building the twin…"); try { const info = request ? await api(`/api/jobs/${j().id}/dronetwin`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(request) }) : await api(`/api/jobs/${j().id}/dronetwin`); render(info); say("Heat map: green painted, yellow-green barely painted, orange barely bare, red bare. Faded = no camera look inside the evidence window (inferred from the flight log, or old)."); } catch (e) { say(`Twin unavailable: ${e.message}`); } finally { st.loading = false; } } $("#dt-setup").addEventListener("click", () => { const lat = num($("#dt-lat").value), lon = num($("#dt-lon").value), alt = num($("#dt-alt").value); const req = { masked_openings: $("#dt-masked").checked }; if (lat !== null && lon !== null) req.home = alt === null ? [lat, lon] : [lat, lon, alt]; ensure(req); }); $("#dt-log").addEventListener("change", async (e) => { const file = e.target.files[0]; e.target.value = ""; if (!file || !j()) return; say(`Reading ${file.name}…`); try { const out = await api(`/api/jobs/${j().id}/dronetwinlog?sortie=${encodeURIComponent((st.info ? st.info.sorties : 0) + 1)}`, { method: "POST", body: await file.arrayBuffer() }); render(out); const q = out.ingest.quality; say(`Log added: ${out.ingest.strokes} spraying runs, ${q.spray_seconds} s of spray, cross-track ±${(q.cross_track_rms_m * 100).toFixed(1)} cm, standoff ${(q.standoff_mean_m * 100).toFixed(0)} cm.`); } catch (err) { say(err.message); } }); $("#dt-scan").addEventListener("change", async (e) => { const file = e.target.files[0]; e.target.value = ""; if (!file || !j()) return; say(`Lining ${file.name} up with the job photo…`); try { const out = await api(`/api/jobs/${j().id}/dronetwinobserve`, { method: "POST", body: await file.arrayBuffer() }); render(out); const a = out.alignment; say(`Re-scan fused: ${a.inliers} matched points (${Math.round(a.inlier_share * 100)}% agree), it saw ${Math.round(out.observed_share_of_target * 100)}% of the wall. Parts it did not see stay as they were.`); } catch (err) { say(`Re-scan not used: ${err.message}`); } }); $("#dt-next").addEventListener("change", (e) => { if (e.target.checked) showNext(); else { nextSvg.style.display = "none"; $("#dt-next-info").hidden = true; } }); box.addEventListener("change", () => { if (typeof applyLayerToggles === "function") applyLayerToggles(); if (box.checked && !st.loaded) ensure(); }); $("#dt-wi-pos").addEventListener("input", () => { wiLabels(); clearTimeout(wiTimer); wiTimer = setTimeout(runWhatIf, 250); }); $("#dt-wi-wind").addEventListener("input", () => { wiLabels(); clearTimeout(wiTimer); wiTimer = setTimeout(runWhatIf, 250); }); $("#dt-copy").addEventListener("click", async () => { const info = st.info; if (!info) return; const body = JSON.stringify({ job: j().id, status: info.status, spray_audit: info.spray_audit, facade: info.facade, setup: info.setup, calibration: info.calibration }, null, 1); try { await navigator.clipboard.writeText(body); say("Report copied: status, spray audit and setup as JSON."); } catch { say("The browser blocked the clipboard: use Twin JSON instead."); } }); const prev = showJob; showJob = function (job_) { prev(job_); st.loaded = false; st.info = null; st.next = null; img.removeAttribute("src"); nextSvg.innerHTML = ""; nextSvg.style.display = "none"; $("#dt-next").checked = false; passSvg.innerHTML = ""; passSvg.style.display = "none"; $("#dt-passes").hidden = true; $("#dt-whatif").hidden = true; delete $("#dt-whatif").dataset.asked; $("#dt-history").hidden = true; $("#dt-next-info").hidden = true; $("#dt-plan").hidden = true; $("#dt-status").hidden = true; $("#dt-actions").hidden = true; if (box.checked) ensure(); }; })();