pada kode UI tambahkan 3D object menggunakan Three.js. di bagian HUD Section di halaman web
Browse filesGunakan metode yang sama seperti implementasi di bawah ini namun untuk scala menyesuaikan ukuran object gambar dengan canvas. nama Model Flying drone_.glb. tujuan menambahkan 3d ini agar sebagai monitor realtime dengan kondisi yaw pitch roll dan lainnya pada drone
<!doctype html>
<html lang="id">
<head>
<meta charset="utf-8" />
<title>Drone Tuner — Full (Flying drone_.glb)</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
:root{
--bg:#071021; --panel:#071625; --accent:#00b4d8; --muted:#94a3b8;
}
html,body{height:100%;margin:0;font-family:Inter,system-ui,Segoe UI,Arial;background:linear-gradient(180deg,var(--bg),#04121a);color:#e6eef6}
#app{display:flex;height:100vh;gap:12px;padding:12px;box-sizing:border-box}
#canvas-wrap{flex:1;border-radius:8px;overflow:hidden;background:#051225;display:flex;flex-direction:column;min-width:0}
#scene-container{flex:1;position:relative}
#controls{width:420px;min-width:320px;background:linear-gradient(180deg,var(--panel),#052033);border-radius:8px;padding:14px;overflow:auto}
h2{margin:4px 0 12px 0;font-size:18px}
.group{margin-bottom:12px;background:rgba(255,255,255,0.02);padding:10px;border-radius:8px}
label{display:block;font-size:13px;color:var(--muted);margin-bottom:6px}
input[type="range"]{width:100%}
.value{min-width:54px;text-align:right;font-variant-numeric:tabular-nums}
button{background:var(--accent);color:#021019;border:none;padding:8px 10px;border-radius:6px;cursor:pointer;font-weight:600}
button.secondary{background:transparent;border:1px solid rgba(255,255,255,0.06);color:var(--muted)}
.small{font-size:12px;color:var(--muted)}
.status{padding:8px 10px;border-radius:6px;background:rgba(255,255,255,0.02);font-size:13px;margin-top:8px}
#dropZone{border:2px dashed rgba(255,255,255,0.03);padding:10px;border-radius:6px;text-align:center;color:var(--muted);cursor:pointer}
pre.small{white-space:pre-wrap;font-size:12px;color:var(--muted);background:rgba(255,255,255,0.01);padding:8px;border-radius:6px}
footer.small{margin-top:10px;color:var(--muted)}
#loadingOverlay{position:absolute;left:8px;top:8px;padding:8px 10px;border-radius:6px;background:rgba(0,0,0,0.4);font-size:13px}
</style>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three@0.158.0/build/three.module.js",
"OrbitControls": "https://unpkg.com/three@0.158.0/examples/jsm/controls/OrbitControls.js",
"GLTFLoader": "https://unpkg.com/three@0.158.0/examples/jsm/loaders/GLTFLoader.js",
"OBJLoader": "https://unpkg.com/three@0.158.0/examples/jsm/loaders/OBJLoader.js"
}
}
</script>
</head>
<body>
<div id="app">
<div id="canvas-wrap">
<div style="padding:10px;display:flex;justify-content:space-between;align-items:center">
<div style="font-weight:700">Drone Tuner — Realtime 3D</div>
<div class="small">Orbit/zoom: mouse • CTRL+drag to pan</div>
</div>
<div id="scene-container">
<div id="loadingOverlay" style="display:none">Memuat model...</div>
</div>
</div>
<div id="controls">
<h2>Kontrol & Model</h2>
<div class="group">
<strong>Model</strong>
<div id="dropZone">Klik / drag & drop file model (.glb .gltf .obj) atau biarkan default (Flying drone_.glb)</div>
<input id="fileInput" type="file" accept=".glb,.gltf,.obj" style="display:none" />
<div style="display:flex;gap:8px;margin-top:8px">
<button id="chooseBtn">Pilih Model</button>
<button id="resetModel" class="secondary">Reset ke default</button>
</div>
<div id="modelInfo" class="small" style="margin-top:8px">Model default: <strong>Flying drone_.glb</strong> (harus ditempatkan di folder yang sama dengan file ini)</div>
</div>
<div class="group">
<strong>Transform Model</strong>
<div style="margin-top:6px">
<label>Scale <span id="scaleVal" class="value">1.00</span></label>
<input id="scale" type="range" min="0.01" max="5" step="0.01" value="1" />
</div>
<div style="margin-top:6px">
<label>Rotate X <span id="rxVal" class="value">0°</span></label>
<input id="rx" type="range" min="-180" max="180" step="1" value="0" />
</div>
<div style="margin-top:6px">
<label>Rotate Y <span id="ryVal" class="value">0°</span></label>
<input id="ry" type="range" min="-180" max="180" step="1" value="0" />
</div>
<div style="margin-top:6px">
<label>Rotate Z <span id="rzVal" class="value">0°</span></label>
<input id="rz" type="range" min="-180" max="180" step="1" value="0" />
</div>
<div style="display:flex;gap:8px;margin-top:8px">
<button id="applyTransform" class="secondary">Apply Transform</button>
</div>
</div>
<div class="group">
<strong>Attitude (yaw / pitch / roll)</strong>
<div style="margin-top:6px">
<label>Yaw <span id="yawVal" class="value">0°</span></label>
<input id="yaw" type="range" min="-180" max="180" step="0.1" value="0" />
</div>
<div style="margin-top:6px">
<label>Pitch <span id="pitchVal" class="value">0°</span></label>
<input id="pitch" type="range" min="-90" max="90" step="0.1" value="0" />
</div>
<div style="margin:6px 0 0 0">
<label>Roll <span id="rollVal" class="value">0°</span></label>
<input id="roll" type="range" min="-180" max="180" step="0.1" value="0" />
</div>
<div style="display:flex;gap:8px;margin-top:8px">
<button id="applyManualBtn">Apply Manual</button>
<button id="resetAttBtn" class="secondary">Reset Attitude</button>
</div>
<div style="margin-top:8px">
<label><input type="checkbox" id="smoothChk" checked /> Smoothing gerakan (visual)</label>
</div>
</div>
<div class="group">
<strong>PID Tuning</strong>
<div style="display:grid;grid-template-columns:1fr 1fr;gap:8px;margin-top:6px">
<div>
<label>P <span id="pVal" class="value">1.00</span></label>
<input id="p" type="range" min="0" max="20" step="0.01" value="1.0" />
</div>
<div>
<label>I <span id="iVal" class="value">0.00</span></label>
<input id="i" type="range" min="0" max="10" step="0.001" value="0.0" />
</div>
<div>
<label>D <span id="dVal" class="value">0.05</span></label>
<input id="d" type="range" min="0" max="5" step="0.001" value="0.05" />
</div>
<div>
<label>Trim <span id="trimVal" class="value">0</span></label>
<input id="trim" type="range" min="-100" max="100" step="1" value="0" />
</div>
</div>
<div style="display:flex;gap:8px;margin-top:8px">
<button id="sendPidBtn">Send PID</button>
<button id="savePidBtn" class="secondary">Save</button>
<button id="loadPidBtn" class="secondary">Load</button>
</div>
<div style="margin-top:8px">
<label><input type="checkbox" id="autoSendPid" /> Auto-send saat slider berubah</label>
</div>
</div>
<div class="group">
<strong>Serial / Telemetry</strong>
<div style="display:flex;gap:8px;align-items:center;justify-content:space-between">
<div>
<div class="small">Baud</div>
<select id="baud"><option>115200</option><option>57600</option><option>38400</option><option>9600</option></select>
</div>
<div style="display:flex;gap:8px">
<button id="connectBtn">Connect</button>
<button id="disconnectBtn" class="secondary">Disconnect</button>
</div>
</div>
<div style="margin-top:8px" class="flex-between">
<div class="small">Status</div>
<div id="portStatus" class="status">Not connected</div>
</div>
<div style="margin-top:8px">
<label><input type="checkbox" id="simulate" /> Simulate telemetry (tanpa hardware)</label>
</div>
<div style="margin-top:8px">
<div class="small">Last received:</div>
<div id="lastReceived" class="status">—</div>
</div>
<div style="margin-top:8px">
<label>Raw send</label>
<div style="display:flex;gap:8px">
<input id="rawText" placeholder='Contoh: PID 1.0 0.0 0.05' style="flex:1" />
<button id="sendRawBtn">Send</button>
</div>
</div>
</div>
<pre class="small">
Format telemetry yang didukung (contoh):
Y:10.2,P:-3.4,R:1.5
{"yaw":10,"pitch":-3.4,"roll":1.5}
10.2,-3.4,1.5
</pre>
<footer class="small">Jalankan halaman ini via HTTPS atau <code>http://localhost</code>. Web Serial hanya bekerja di Chrome/Edge (Chromium)</footer>
</div>
</div>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'OrbitControls';
import { GLTFLoader } from 'GLTFLoader';
import { OBJLoader } from 'OBJLoader';
// ---------- Scene ----------
const container = document.getElementById('scene-container');
const renderer = new THREE.WebGLRenderer({ antialias:true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(container.clientWidth || window.innerWidth, container.clientHeight || window.innerHeight);
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x051225);
const camera = new THREE.PerspectiveCamera(50, (container.clientWidth || window.innerWidth) / (container.clientHeight || window.innerHeight), 0.1, 1000);
camera.position.set(0, 3, 6);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
window.addEventListener('resize', onWindowResize);
function onWindowResize(){
co
- index.html +102 -10
|
@@ -521,12 +521,14 @@
|
|
| 521 |
</div>
|
| 522 |
</div>
|
| 523 |
</div>
|
| 524 |
-
|
| 525 |
<div class="sidebar-section">
|
| 526 |
<div class="sidebar-section-title">
|
| 527 |
<span>HUD DRONE</span>
|
| 528 |
</div>
|
| 529 |
<div class="hud-display">
|
|
|
|
|
|
|
|
|
|
| 530 |
<div class="hud-item">
|
| 531 |
<div class="hud-label">POSISI</div>
|
| 532 |
<div class="hud-value" id="position-value">-,-</div>
|
|
@@ -543,10 +545,13 @@
|
|
| 543 |
<div class="hud-label">PITCH</div>
|
| 544 |
<div class="hud-value" id="pitch-value">-°</div>
|
| 545 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 546 |
</div>
|
| 547 |
</div>
|
| 548 |
-
|
| 549 |
-
<div class="sidebar-section">
|
| 550 |
<div class="sidebar-section-title">
|
| 551 |
<span>TARGET</span>
|
| 552 |
</div>
|
|
@@ -616,9 +621,16 @@
|
|
| 616 |
</aside>
|
| 617 |
</main>
|
| 618 |
</div>
|
| 619 |
-
|
| 620 |
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
|
| 621 |
-
<script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 622 |
// Inisialisasi variabel global
|
| 623 |
let map;
|
| 624 |
let droneMarker;
|
|
@@ -842,7 +854,85 @@
|
|
| 842 |
addLogEntry('error', `Failed to send target: ${error.message}`);
|
| 843 |
}
|
| 844 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 845 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 846 |
// Update posisi drone
|
| 847 |
function updateDronePosition(lat, lng, alt, yaw, pitch, roll) {
|
| 848 |
currentPosition = { lat, lng, alt };
|
|
@@ -859,6 +949,10 @@
|
|
| 859 |
document.getElementById('altitude-value').textContent = `${alt.toFixed(1)} m`;
|
| 860 |
document.getElementById('yaw-value').textContent = `${yaw.toFixed(1)}°`;
|
| 861 |
document.getElementById('pitch-value').textContent = `${pitch.toFixed(1)}°`;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 862 |
|
| 863 |
// Update jarak jika ada target
|
| 864 |
if (targetPosition) {
|
|
@@ -870,8 +964,7 @@
|
|
| 870 |
map.setView([lat, lng], map.getZoom());
|
| 871 |
}
|
| 872 |
}
|
| 873 |
-
|
| 874 |
-
// Hitung jarak menggunakan rumus Haversine
|
| 875 |
function calculateDistance(lat1, lon1, lat2, lon2) {
|
| 876 |
const R = 6371000; // Radius bumi dalam meter
|
| 877 |
const dLat = (lat2 - lat1) * Math.PI / 180;
|
|
@@ -1137,12 +1230,11 @@
|
|
| 1137 |
}
|
| 1138 |
|
| 1139 |
// ==================== INITIALIZATION ====================
|
| 1140 |
-
|
| 1141 |
// Inisialisasi saat halaman dimuat
|
| 1142 |
document.addEventListener('DOMContentLoaded', function() {
|
| 1143 |
initMap();
|
| 1144 |
-
|
| 1145 |
-
|
| 1146 |
document.getElementById('connect-btn').addEventListener('click', connectSerial);
|
| 1147 |
document.getElementById('disconnect-btn').addEventListener('click', disconnectSerial);
|
| 1148 |
document.getElementById('send-target-btn').addEventListener('click', sendTargetToDrone);
|
|
|
|
| 521 |
</div>
|
| 522 |
</div>
|
| 523 |
</div>
|
|
|
|
| 524 |
<div class="sidebar-section">
|
| 525 |
<div class="sidebar-section-title">
|
| 526 |
<span>HUD DRONE</span>
|
| 527 |
</div>
|
| 528 |
<div class="hud-display">
|
| 529 |
+
<div style="grid-column: span 2; height: 200px; background: #0f1720; border-radius: 4px; overflow: hidden; border: 1px solid #334155;">
|
| 530 |
+
<canvas id="drone-canvas" style="width: 100%; height: 100%;"></canvas>
|
| 531 |
+
</div>
|
| 532 |
<div class="hud-item">
|
| 533 |
<div class="hud-label">POSISI</div>
|
| 534 |
<div class="hud-value" id="position-value">-,-</div>
|
|
|
|
| 545 |
<div class="hud-label">PITCH</div>
|
| 546 |
<div class="hud-value" id="pitch-value">-°</div>
|
| 547 |
</div>
|
| 548 |
+
<div class="hud-item">
|
| 549 |
+
<div class="hud-label">ROLL</div>
|
| 550 |
+
<div class="hud-value" id="roll-value">-°</div>
|
| 551 |
+
</div>
|
| 552 |
</div>
|
| 553 |
</div>
|
| 554 |
+
<div class="sidebar-section">
|
|
|
|
| 555 |
<div class="sidebar-section-title">
|
| 556 |
<span>TARGET</span>
|
| 557 |
</div>
|
|
|
|
| 621 |
</aside>
|
| 622 |
</main>
|
| 623 |
</div>
|
|
|
|
| 624 |
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
|
| 625 |
+
<script type="importmap">
|
| 626 |
+
{
|
| 627 |
+
"imports": {
|
| 628 |
+
"three": "https://unpkg.com/three@0.158.0/build/three.module.js",
|
| 629 |
+
"GLTFLoader": "https://unpkg.com/three@0.158.0/examples/jsm/loaders/GLTFLoader.js"
|
| 630 |
+
}
|
| 631 |
+
}
|
| 632 |
+
</script>
|
| 633 |
+
<script>
|
| 634 |
// Inisialisasi variabel global
|
| 635 |
let map;
|
| 636 |
let droneMarker;
|
|
|
|
| 854 |
addLogEntry('error', `Failed to send target: ${error.message}`);
|
| 855 |
}
|
| 856 |
}
|
| 857 |
+
// Three.js variables
|
| 858 |
+
let droneScene, droneCamera, droneRenderer, droneModel;
|
| 859 |
+
let currentAttitude = { yaw: 0, pitch: 0, roll: 0 };
|
| 860 |
+
let targetAttitude = { yaw: 0, pitch: 0, roll: 0 };
|
| 861 |
|
| 862 |
+
// Initialize 3D drone visualization
|
| 863 |
+
function initDroneVisualization() {
|
| 864 |
+
const canvas = document.getElementById('drone-canvas');
|
| 865 |
+
|
| 866 |
+
// Scene setup
|
| 867 |
+
droneScene = new THREE.Scene();
|
| 868 |
+
droneScene.background = new THREE.Color(0x0f1720);
|
| 869 |
+
|
| 870 |
+
// Camera setup
|
| 871 |
+
droneCamera = new THREE.PerspectiveCamera(50, canvas.clientWidth / canvas.clientHeight, 0.1, 1000);
|
| 872 |
+
droneCamera.position.set(0, 3, 6);
|
| 873 |
+
|
| 874 |
+
// Renderer setup
|
| 875 |
+
droneRenderer = new THREE.WebGLRenderer({ canvas, antialias: true });
|
| 876 |
+
droneRenderer.setSize(canvas.clientWidth, canvas.clientHeight);
|
| 877 |
+
|
| 878 |
+
// Lighting
|
| 879 |
+
const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444, 0.6);
|
| 880 |
+
droneScene.add(hemiLight);
|
| 881 |
+
|
| 882 |
+
const dirLight = new THREE.DirectionalLight(0xffffff, 0.9);
|
| 883 |
+
dirLight.position.set(5, 10, 7);
|
| 884 |
+
droneScene.add(dirLight);
|
| 885 |
+
|
| 886 |
+
// Grid helper
|
| 887 |
+
const grid = new THREE.GridHelper(20, 20, 0x334155, 0x1e293b);
|
| 888 |
+
grid.position.y = -1;
|
| 889 |
+
droneScene.add(grid);
|
| 890 |
+
|
| 891 |
+
// Load drone model
|
| 892 |
+
const loader = new THREE.GLTFLoader();
|
| 893 |
+
loader.load('Flying drone_.glb', (gltf) => {
|
| 894 |
+
droneModel = gltf.scene;
|
| 895 |
+
droneModel.scale.set(0.8, 0.8, 0.8);
|
| 896 |
+
droneScene.add(droneModel);
|
| 897 |
+
animateDrone();
|
| 898 |
+
}, undefined, (error) => {
|
| 899 |
+
console.error('Error loading drone model:', error);
|
| 900 |
+
// Fallback simple drone if model fails to load
|
| 901 |
+
const geo = new THREE.BoxGeometry(1, 0.2, 1.5);
|
| 902 |
+
const mat = new THREE.MeshStandardMaterial({ color: 0x1e40af });
|
| 903 |
+
droneModel = new THREE.Mesh(geo, mat);
|
| 904 |
+
droneScene.add(droneModel);
|
| 905 |
+
animateDrone();
|
| 906 |
+
});
|
| 907 |
+
|
| 908 |
+
// Handle window resize
|
| 909 |
+
window.addEventListener('resize', () => {
|
| 910 |
+
droneCamera.aspect = canvas.clientWidth / canvas.clientHeight;
|
| 911 |
+
droneCamera.updateProjectionMatrix();
|
| 912 |
+
droneRenderer.setSize(canvas.clientWidth, canvas.clientHeight);
|
| 913 |
+
});
|
| 914 |
+
}
|
| 915 |
+
|
| 916 |
+
// Animate drone model
|
| 917 |
+
function animateDrone() {
|
| 918 |
+
requestAnimationFrame(animateDrone);
|
| 919 |
+
|
| 920 |
+
// Smooth attitude changes
|
| 921 |
+
currentAttitude.yaw += (targetAttitude.yaw - currentAttitude.yaw) * 0.1;
|
| 922 |
+
currentAttitude.pitch += (targetAttitude.pitch - currentAttitude.pitch) * 0.1;
|
| 923 |
+
currentAttitude.roll += (targetAttitude.roll - currentAttitude.roll) * 0.1;
|
| 924 |
+
|
| 925 |
+
if (droneModel) {
|
| 926 |
+
droneModel.rotation.set(
|
| 927 |
+
THREE.MathUtils.degToRad(currentAttitude.pitch),
|
| 928 |
+
THREE.MathUtils.degToRad(currentAttitude.yaw),
|
| 929 |
+
THREE.MathUtils.degToRad(currentAttitude.roll)
|
| 930 |
+
);
|
| 931 |
+
}
|
| 932 |
+
|
| 933 |
+
droneRenderer.render(droneScene, droneCamera);
|
| 934 |
+
}
|
| 935 |
+
|
| 936 |
// Update posisi drone
|
| 937 |
function updateDronePosition(lat, lng, alt, yaw, pitch, roll) {
|
| 938 |
currentPosition = { lat, lng, alt };
|
|
|
|
| 949 |
document.getElementById('altitude-value').textContent = `${alt.toFixed(1)} m`;
|
| 950 |
document.getElementById('yaw-value').textContent = `${yaw.toFixed(1)}°`;
|
| 951 |
document.getElementById('pitch-value').textContent = `${pitch.toFixed(1)}°`;
|
| 952 |
+
document.getElementById('roll-value').textContent = `${roll.toFixed(1)}°`;
|
| 953 |
+
|
| 954 |
+
// Update 3D model attitude
|
| 955 |
+
targetAttitude = { yaw, pitch, roll };
|
| 956 |
|
| 957 |
// Update jarak jika ada target
|
| 958 |
if (targetPosition) {
|
|
|
|
| 964 |
map.setView([lat, lng], map.getZoom());
|
| 965 |
}
|
| 966 |
}
|
| 967 |
+
// Hitung jarak menggunakan rumus Haversine
|
|
|
|
| 968 |
function calculateDistance(lat1, lon1, lat2, lon2) {
|
| 969 |
const R = 6371000; // Radius bumi dalam meter
|
| 970 |
const dLat = (lat2 - lat1) * Math.PI / 180;
|
|
|
|
| 1230 |
}
|
| 1231 |
|
| 1232 |
// ==================== INITIALIZATION ====================
|
|
|
|
| 1233 |
// Inisialisasi saat halaman dimuat
|
| 1234 |
document.addEventListener('DOMContentLoaded', function() {
|
| 1235 |
initMap();
|
| 1236 |
+
initDroneVisualization();
|
| 1237 |
+
// Event listeners untuk kontrol serial
|
| 1238 |
document.getElementById('connect-btn').addEventListener('click', connectSerial);
|
| 1239 |
document.getElementById('disconnect-btn').addEventListener('click', disconnectSerial);
|
| 1240 |
document.getElementById('send-target-btn').addEventListener('click', sendTargetToDrone);
|