Files
appRobotControlScara/web/index.html
2026-04-22 22:06:45 +02:00

247 lines
6.8 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8" />
<title>FluidNC Robot Control</title>
<style>
body {
background: #234;
color: #e0e0e0;
font-family: Arial, sans-serif;
text-align: center;
}
h1 {
margin-top: 20px;
}
h2{
margin-top: 40px;
}
#posBox {
margin: 20px auto;
width: 406px;
padding: 15px;
background: #356;
border-radius: 10px;
font-size: 20px;
}
.btnGrid {
display: grid;
grid-template-columns: repeat(5, 80px);
gap: 10px;
justify-content: center;
margin-top: 30px;
}
button {
background: #478;
border: none;
padding: 15px;
font-size: 18px;
color: white;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background: #666;
}
.noMouseHover:hover{
background: #478;
}
.gcodeRow {
display: inline-flex;
gap: 8px;
align-items: stretch;
margin-top: 16px;
}
.controlInput {
height: 48px;
font-size: 18px;
padding: 0 10px;
box-sizing: border-box;
border-radius: 8px;
border: 1px solid #555;
background: #fff;
color: #000;
min-width: 260px;
}
.controlButton {
height: 48px;
padding: 0 18px;
display: inline-flex;
align-items: center;
justify-content: center;
}
</style>
</head>
<body>
<h1>2D Robot Control</h1>
<div id="posBox">
<div>X: <span id="posX">0.000</span></div>
<div>Y: <span id="posY">0.000</span></div>
<div>Z: <span id="posZ">0.000</span></div>
</div>
<h2>Jog Controls</h2>
<div class="btnGrid">
<button onclick="jog('X', -10)">X -10</button>
<button onclick="jog('X', -1)">X -1</button>
<button class="noMouseHover"></button>
<button onclick="jog('X', 1)">X +1</button>
<button onclick="jog('X', 10)">X +10</button>
<button onclick="jog('Y', -10)">Y -10</button>
<button onclick="jog('Y', -1)">Y -1</button>
<button class="noMouseHover"></button>
<button onclick="jog('Y', 1)">Y +1</button>
<button onclick="jog('Y', 10)">Y +10</button>
<button onclick="jog('Z', -10)">Z -10</button>
<button onclick="jog('Z', -1)">Z -1</button>
<button class="noMouseHover"></button>
<button onclick="jog('Z', 1)">Z +1</button>
<button onclick="jog('Z', 10)">Z +10</button>
</div>
<h2>GCode Eingabe</h2>
<div class="gcodeRow">
<input id="gcodeInput" class="controlInput" type="text"
placeholder="G0 X10 Z-5"
onkeydown="if (event.key === 'Enter') { sendGcodeFeld(); this.select(); }"
autofocus>
<button class="controlButton" onclick="sendGcodeFeld()">Senden</button>
</div>
<br/><br/>
<div class="gcodeRow">
<button class="controlButton" onclick="setZero()">Zurücksetzen auf 0</button>
</div>
<script>
let ws;
function connectWS() {
console.log("Try to connect to " + "wss://" + window.location.hostname + ":" + window.location.port);
ws = new WebSocket("wss://" + window.location.hostname + ":" + window.location.port);
ws.onopen = () => {
console.log("WS connected");
};
ws.onclose = () => {
console.warn("WS disconnected → reconnecting...");
setTimeout(connectWS, 1000);
};
ws.onmessage = (ev) => {
const text = ev.data;
// GRBL/FluidNC status line?
if (text.startsWith("<")) {
handleStatus(text);
return;
}
// Try JSON only if valid JSON
try {
const data = JSON.parse(text);
console.log("JSON:", data);
} catch (e) {
// non JSON → ignore
}
};
ws.onerror = (e) => console.error("WS error:", e);
}
connectWS();
// --- Jog-Funktion ---
function jog(axis, value) {
ws.send(JSON.stringify({
type: "jog",
axis: axis,
relative: true,
value: value
}));
}
// --- GRBL Status Parser ---
function handleStatus(rawLine) {
const line = String(rawLine).trim();
// 1) Versuche WPos direkt zu lesen
const wposMatch = line.match(/WPos:([^|>]+)/);
if (wposMatch) {
const [x, y, z] = wposMatch[1].split(",").map(Number);
updateXZ(x, y, z);
return;
}
// 2) Kein WPos vorhanden → MPos und WCO lesen und WPos = MPos - WCO berechnen
const mposMatch = line.match(/MPos:([^|>]+)/);
const wcoMatch = line.match(/WCO:([^|>]+)/);
if (mposMatch && wcoMatch) {
const mpos = mposMatch[1].split(",").map(Number);
const wco = wcoMatch[1].split(",").map(Number);
const x = (mpos[0] ?? 0) - (wco[0] ?? 0);
const y = (mpos[1] ?? 0) - (wco[1] ?? 0);
const z = (mpos[2] ?? 0) - (wco[2] ?? 0);
updateXZ(x, y, z);
}
}
function updateXZ(x, y, z) {
const elX = document.getElementById("posX");
const elY = document.getElementById("posY");
const elZ = document.getElementById("posZ");
if (elX) elX.textContent = Number.isFinite(x) ? x.toFixed(3) : "—";
if (elY) elY.textContent = Number.isFinite(y) ? y.toFixed(3) : "—";
if (elZ) elZ.textContent = Number.isFinite(z) ? z.toFixed(3) : "—";
}
function sendGcodeFeld() {
const el = document.getElementById("gcodeInput");
const cmd = el.value.trim();
sendGcode("G90");
sendGcode(cmd);
el.value = "";
el.focus();
}
function sendGcode(cmd) {
if (!cmd) return;
ws.send(JSON.stringify({ type: "gcode", cmd }));
}
function setZero() {
sendGcode("G92 X0 Y0 Z0");
}
</script>
</body>
</html>