Download simulation.js from kyoungsim/mujoco-cartpole-simulator: direct link, hf CLI and curl.
- Browser
- Download file 26.3 kB
-
https://huggingface.co/spaces/kyoungsim/mujoco-cartpole-simulator/resolve/main/simulation.js
- Command line
-
hf download hf://spaces/kyoungsim/mujoco-cartpole-simulator/simulation.js
-
curl -L -o simulation.js https://huggingface.co/spaces/kyoungsim/mujoco-cartpole-simulator/resolve/main/simulation.js
26.3 kB
| /** | |
| * MuJoCo Cartpole Inverted Pendulum Physics Simulator | |
| * Nonlinear Rigid Body Dynamics with Runge-Kutta 4th Order (RK4) Integration | |
| * & Multi-Mode State Feedback (PD / LQR) Control | |
| */ | |
| // ========================================== | |
| // 1. ๋ฌผ๋ฆฌ ํ๋ผ๋ฏธํฐ (MuJoCo XML ์ผ์น ๋ชจ๋ธ) | |
| // ========================================== | |
| const physicsConfig = { | |
| M: 1.0, // ์๋ ์ง๋ (kg) | |
| m: 0.1, // ๋ง๋ ์ง๋ (kg) | |
| L: 1.0, // ๋ง๋ ์ ์ฒด ๊ธธ์ด (m) | |
| l: 0.5, // ํ์ ์ถ์์ ์ง๋์ค์ฌ๊น์ง ๊ฑฐ๋ฆฌ (L/2 = 0.5m) | |
| I: (1/12) * 0.1 * (1.0 * 1.0), // ์ง๋์ค์ฌ ๊ธฐ์ค ๋ง๋ ๊ด์ฑ๋ชจ๋ฉํธ (kgยทmยฒ) | |
| g: 9.81, // ์ค๋ ฅ ๊ฐ์๋ (m/sยฒ) | |
| bc: 0.1, // ์๋ ์ฌ๋ผ์ด๋ ๊ฐ์ ๊ณ์ (damping) (Ns/m) | |
| bp: 0.01, // ํ์ง ํ์ ๊ฐ์ ๊ณ์ (damping) (Nms/rad) | |
| gear: 10.0, // ๋ชจํฐ ๊ธฐ์ด๋น (u in [-1, 1] -> Force = gear * u) | |
| dt: 0.01, // ์ ๋ถ ์คํ (s) - 100Hz | |
| substeps: 4 // ํ๋ ์๋น ์๋ธ์คํ ์ | |
| }; | |
| // ์์คํ ์ํ: [x, v, theta, omega, u, time] | |
| // x: ์๋ ์์น (m), v: ์๋ ์๋ (m/s) | |
| // theta: ๋ง๋ ๊ฐ๋ (rad) [์์ง ์ง๋ฆฝ=0, ์ค๋ฅธ์ชฝ ๊ธฐ์ธ์ด์ง > 0], omega: ๊ฐ์๋ (rad/s) | |
| let state = { | |
| x: 0.0, | |
| v: 0.0, | |
| theta: 0.15, // MuJoCo ์ฝ๋์ ๋์ผํ๊ฒ ์ด๊ธฐ 0.15 rad (์ฝ 8.6๋) ๊ธฐ์ธ์ | |
| omega: 0.0, | |
| u: 0.0, | |
| time: 0.0 | |
| }; | |
| // ์ ์ด ์ค์ | |
| let controlSettings = { | |
| mode: 'pd', // 'pd', 'lqr', 'none' | |
| k_theta: 8.0, | |
| k_omega: 1.6, | |
| k_x: 0.3, | |
| k_v: 0.6 | |
| }; | |
| let isRunning = true; | |
| let isGraphVisible = true; | |
| // ========================================== | |
| // 2. ๋น์ ํ ์ด๋๋ฐฉ์ ์ (Nonlinear ODE) | |
| // ========================================== | |
| function getDerivatives(s, uForce) { | |
| const { M, m, l, I, g, bc, bp } = physicsConfig; | |
| const x = s.x || 0; | |
| const v = s.v || 0; | |
| const th = s.theta || 0; | |
| const w = s.omega || 0; | |
| const sinTh = Math.sin(th); | |
| const cosTh = Math.cos(th); | |
| // ์ผ๋ฐํ ์ง๋ ํ๋ ฌ ์ฑ๋ถ: | |
| // [ m11 m12 ] [ x_ddot ] = [ f1 ] | |
| // [ m21 m22 ] [ th_ddot ] [ f2 ] | |
| const m11 = M + m; | |
| const m12 = m * l * cosTh; | |
| const m21 = m * l * cosTh; | |
| const m22 = I + m * l * l; | |
| // ์ฐ๋ณ ์ผ๋ฐํ ํ ๋ฒกํฐ: | |
| const f1 = uForce - bc * v + m * l * (w * w) * sinTh; | |
| const f2 = m * g * l * sinTh - bp * w; | |
| // ํ๋ณ์ (ํ๋ ฌ์) | |
| const det = m11 * m22 - m12 * m21; | |
| if (Math.abs(det) < 1e-7) { | |
| return { dx: v, dv: 0, dtheta: w, domega: 0 }; | |
| } | |
| // 2x2 ์ญํ๋ ฌ ๊ณฑ: | |
| const x_ddot = (m22 * f1 - m12 * f2) / det; | |
| const th_ddot = (-m21 * f1 + m11 * f2) / det; | |
| return { | |
| dx: v, | |
| dv: x_ddot, | |
| dtheta: w, | |
| domega: th_ddot | |
| }; | |
| } | |
| // 4์ฐจ ๋ฃฝ๊ฒ-์ฟ ํ (RK4) ์์น ์ ๋ถ๊ธฐ | |
| function rk4Step(s, uForce, dt) { | |
| const k1 = getDerivatives(s, uForce); | |
| const s2 = { | |
| x: s.x + 0.5 * dt * k1.dx, | |
| v: s.v + 0.5 * dt * k1.dv, | |
| theta: s.theta + 0.5 * dt * k1.dtheta, | |
| omega: s.omega + 0.5 * dt * k1.domega | |
| }; | |
| const k2 = getDerivatives(s2, uForce); | |
| const s3 = { | |
| x: s.x + 0.5 * dt * k2.dx, | |
| v: s.v + 0.5 * dt * k2.dv, | |
| theta: s.theta + 0.5 * dt * k2.dtheta, | |
| omega: s.omega + 0.5 * dt * k2.domega | |
| }; | |
| const k3 = getDerivatives(s3, uForce); | |
| const s4 = { | |
| x: s.x + dt * k3.dx, | |
| v: s.v + dt * k3.dv, | |
| theta: s.theta + dt * k3.dtheta, | |
| omega: s.omega + dt * k3.domega | |
| }; | |
| const k4 = getDerivatives(s4, uForce); | |
| return { | |
| x: s.x + (dt / 6) * (k1.dx + 2 * k2.dx + 2 * k3.dx + k4.dx), | |
| v: s.v + (dt / 6) * (k1.dv + 2 * k2.dv + 2 * k3.dv + k4.dv), | |
| theta: s.theta + (dt / 6) * (k1.dtheta + 2 * k2.dtheta + 2 * k3.dtheta + k4.dtheta), | |
| omega: s.omega + (dt / 6) * (k1.domega + 2 * k2.domega + 2 * k3.domega + k4.domega), | |
| u: s.u || 0.0, | |
| time: (s.time || 0.0) + dt | |
| }; | |
| } | |
| // ์ ์ด ์ ๋ ฅ ๊ณ์ฐ | |
| function computeControl(s) { | |
| if (controlSettings.mode === 'none') { | |
| return 0.0; | |
| } | |
| const th = s.theta || 0; | |
| const w = s.omega || 0; | |
| const x = s.x || 0; | |
| const v = s.v || 0; | |
| let u = 0.0; | |
| if (controlSettings.mode === 'pd') { | |
| // ์ฌ์ฉ์์ MuJoCo ์๋ณธ ์ ์ด ์์: | |
| // np.clip(8 * th + 1.6 * w + 0.3 * x + 0.6 * v, -1, 1) | |
| u = controlSettings.k_theta * th + | |
| controlSettings.k_omega * w + | |
| controlSettings.k_x * x + | |
| controlSettings.k_v * v; | |
| } else if (controlSettings.mode === 'lqr') { | |
| // LQR ์ํ ํผ๋๋ฐฑ ๊ฒ์ธ | |
| const K_lqr = [1.73, 2.85, 24.8, 6.42]; | |
| u = (K_lqr[2] * th + K_lqr[3] * w + K_lqr[0] * x + K_lqr[1] * v) / physicsConfig.gear; | |
| } | |
| // ์ก์ถ์์ดํฐ ํฌํ (Saturation: [-1, 1]) | |
| if (isNaN(u)) u = 0.0; | |
| return Math.max(-1.0, Math.min(1.0, u)); | |
| } | |
| // ์ ์ฒด ์ญํ์ ์๋์ง ๊ณ์ฐ (E = T + V) | |
| function computeTotalEnergy(s) { | |
| const { M, m, l, I, g } = physicsConfig; | |
| const cosTh = Math.cos(s.theta || 0); | |
| const v = s.v || 0; | |
| const w = s.omega || 0; | |
| // ์ด๋์๋์ง T | |
| const T = 0.5 * (M + m) * v * v + | |
| m * l * v * w * cosTh + | |
| 0.5 * (I + m * l * l) * w * w; | |
| // ์์น์๋์ง V (๊ธฐ์ค์ : ํ์ง ๋์ด y=0 ์ผ ๋ ์์ง ๋งจ ์ l cos(theta)) | |
| const V = m * g * l * cosTh; | |
| return T + V; | |
| } | |
| // ========================================== | |
| // 3. Three.js 3D ๋ ๋๋ง ์ฌ ๊ตฌ์ฑ (ํ์ค Y-up ์ขํ๊ณ) | |
| // ========================================== | |
| let scene, camera, renderer, controls; | |
| let cartMesh, poleMesh, trackMesh, forceArrowMesh; | |
| const railY = 1.35; // ์์ ๋ํ ์ ๋ง๋๊ฐ ์๋๋ก ์ฒ์ ธ๋ ๋ฐ๋ฅ ๋ฐ ํ๋จ ํจ๋์ ๊ฐ๋ฆฌ์ง ์๋ ๋๋ํ ๋์ด | |
| const railZ = -0.55; // ํ๋ฉด ๋ค์ชฝ์ผ๋ก ์ด๋ํ์ฌ ์ ๋ฉด UI ํจ๋๊ณผ์ ๊ฐ๊ฒฉ ํ๋ณด | |
| function initThree() { | |
| const container = document.getElementById('canvas-container'); | |
| const width = container.clientWidth || 800; | |
| const height = container.clientHeight || 600; | |
| // 1. Scene | |
| scene = new THREE.Scene(); | |
| scene.background = new THREE.Color(0x0a0e17); | |
| scene.fog = new THREE.FogExp2(0x0a0e17, 0.035); | |
| // 2. Camera (Y-up ํ์ค ์ขํ๊ณ: ์นดํธํด ์ ์ฒด๊ฐ ํ๋์ ๋ค์ด์ค๋ ๋๋ํ ์์ผ๊ฐ) | |
| camera = new THREE.PerspectiveCamera(46, width / height, 0.1, 100); | |
| camera.position.set(0, 2.3, 4.6); | |
| // 3. Renderer | |
| renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); | |
| renderer.setSize(width, height); | |
| renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)); | |
| renderer.shadowMap.enabled = true; | |
| renderer.shadowMap.type = THREE.PCFSoftShadowMap; | |
| container.innerHTML = ''; // ๊ธฐ์กด ์บ๋ฒ์ค ์ ๊ฑฐ | |
| container.appendChild(renderer.domElement); | |
| // 4. OrbitControls | |
| controls = new THREE.OrbitControls(camera, renderer.domElement); | |
| controls.enableDamping = true; | |
| controls.dampingFactor = 0.05; | |
| controls.target.set(0, 1.35, railZ); // ์นดํธํด ์ค์ ์์ | |
| controls.maxPolarAngle = Math.PI / 2 - 0.02; // ๋ฐ๋ฅ ๊ดํต ๋ฐฉ์ง | |
| controls.update(); | |
| // 5. ์กฐ๋ช | |
| const ambientLight = new THREE.AmbientLight(0xffffff, 0.65); | |
| scene.add(ambientLight); | |
| const mainLight = new THREE.DirectionalLight(0xffffff, 0.9); | |
| mainLight.position.set(3, 6, 4); | |
| mainLight.castShadow = true; | |
| mainLight.shadow.mapSize.width = 2048; | |
| mainLight.shadow.mapSize.height = 2048; | |
| mainLight.shadow.camera.near = 0.5; | |
| mainLight.shadow.camera.far = 15; | |
| mainLight.shadow.camera.left = -4; | |
| mainLight.shadow.camera.right = 4; | |
| mainLight.shadow.camera.top = 4; | |
| mainLight.shadow.camera.bottom = -4; | |
| scene.add(mainLight); | |
| const fillLight = new THREE.PointLight(0x00f0ff, 0.5, 10); | |
| fillLight.position.set(-3, 2, 2); | |
| scene.add(fillLight); | |
| // 6. MuJoCo ์คํ์ผ ๋ฐ๋ฅ ๊ทธ๋ฆฌ๋ ๋ฐ ํ๋ฉด (XZ ํ๋ฉด) | |
| const gridHelper = new THREE.GridHelper(12, 24, 0x00f0ff, 0x1e293b); | |
| gridHelper.position.set(0, 0, 0); | |
| scene.add(gridHelper); | |
| const floorGeo = new THREE.PlaneGeometry(16, 16); | |
| const floorMat = new THREE.MeshStandardMaterial({ | |
| color: 0x0d121d, | |
| roughness: 0.9, | |
| metalness: 0.1 | |
| }); | |
| const floor = new THREE.Mesh(floorGeo, floorMat); | |
| floor.rotation.x = -Math.PI / 2; | |
| floor.receiveShadow = true; | |
| scene.add(floor); | |
| // 7. ๊ฐ์ด๋ ๋ ์ผ ๋ฐ ๋ฉํ ์คํ ๋ ๊ธฐ๋ฅ | |
| const trackGeo = new THREE.BoxGeometry(5.2, 0.04, 0.04); | |
| const trackMat = new THREE.MeshStandardMaterial({ | |
| color: 0x64748b, | |
| metalness: 0.7, | |
| roughness: 0.3 | |
| }); | |
| trackMesh = new THREE.Mesh(trackGeo, trackMat); | |
| trackMesh.position.set(0, railY, railZ); | |
| trackMesh.castShadow = true; | |
| scene.add(trackMesh); | |
| // ๋ ์ผ ๋ฐ์นจ๋ ๋ค๋ฆฌ ๊ธฐ๋ฅ 3๊ฐ (์ข, ์ฐ, ์ค์) | |
| const legGeo = new THREE.CylinderGeometry(0.035, 0.045, railY, 16); | |
| const legMat = new THREE.MeshStandardMaterial({ color: 0x334155, metalness: 0.8, roughness: 0.4 }); | |
| [-2.5, 0, 2.5].forEach(posX => { | |
| const leg = new THREE.Mesh(legGeo, legMat); | |
| leg.position.set(posX, railY / 2, railZ); | |
| leg.castShadow = true; | |
| scene.add(leg); | |
| // ๋ฒ ์ด์ค ํ๋ ์ดํธ | |
| const baseGeo = new THREE.CylinderGeometry(0.12, 0.14, 0.02, 16); | |
| const base = new THREE.Mesh(baseGeo, legMat); | |
| base.position.set(posX, 0.01, railZ); | |
| base.receiveShadow = true; | |
| scene.add(base); | |
| }); | |
| // ๋ ์ผ ์๋ ์คํ ํผ | |
| const stopperGeo = new THREE.CylinderGeometry(0.06, 0.06, 0.12, 16); | |
| const stopperMat = new THREE.MeshStandardMaterial({ color: 0xef4444 }); | |
| const stopperL = new THREE.Mesh(stopperGeo, stopperMat); | |
| stopperL.position.set(-2.6, railY, railZ); | |
| scene.add(stopperL); | |
| const stopperR = stopperL.clone(); | |
| stopperR.position.set(2.6, railY, railZ); | |
| scene.add(stopperR); | |
| // 8. ์๋ (Cart: ๋๋น 0.4, ๋์ด 0.16, ๊น์ด 0.24) | |
| const cartGeo = new THREE.BoxGeometry(0.4, 0.16, 0.24); | |
| const cartMat = new THREE.MeshStandardMaterial({ | |
| color: 0xf1f5f9, | |
| metalness: 0.4, | |
| roughness: 0.25 | |
| }); | |
| cartMesh = new THREE.Mesh(cartGeo, cartMat); | |
| cartMesh.position.set(0, railY, railZ); | |
| cartMesh.castShadow = true; | |
| cartMesh.receiveShadow = true; | |
| scene.add(cartMesh); | |
| // ์๋ ์ ๋ฉด ๋ค์จ ๋ฑ์ง | |
| const badgeGeo = new THREE.BoxGeometry(0.42, 0.03, 0.02); | |
| const badgeMat = new THREE.MeshBasicMaterial({ color: 0x00f0ff }); | |
| const badge = new THREE.Mesh(badgeGeo, badgeMat); | |
| badge.position.set(0, 0, 0.125); | |
| cartMesh.add(badge); | |
| // 9. ๋ง๋ (Pole: ๊ธธ์ด 1.0m, ์ง๋ฆ 0.06m) | |
| // ์๋ ํ์ง ์์น: ์๋ ์ค์ฌ ์๋จ (Y = 0.08) | |
| const poleGroup = new THREE.Group(); | |
| poleGroup.position.set(0, 0.08, 0); | |
| const poleRadius = 0.03; | |
| const poleLength = physicsConfig.L; // 1.0m | |
| const cylinderGeo = new THREE.CylinderGeometry(poleRadius, poleRadius, poleLength, 24); | |
| const sphereGeo = new THREE.SphereGeometry(poleRadius, 24, 16); | |
| const poleMat = new THREE.MeshStandardMaterial({ | |
| color: 0xf59e0b, | |
| metalness: 0.2, | |
| roughness: 0.2, | |
| emissive: 0xd97706, | |
| emissiveIntensity: 0.2 | |
| }); | |
| const cylinder = new THREE.Mesh(cylinderGeo, poleMat); | |
| cylinder.castShadow = true; | |
| cylinder.position.set(0, poleLength / 2, 0); // Y์ถ ์๋ก ์ธ์ฐ๊ธฐ | |
| const sphereBottom = new THREE.Mesh(sphereGeo, poleMat); | |
| sphereBottom.castShadow = true; | |
| const sphereTop = new THREE.Mesh(sphereGeo, poleMat); | |
| sphereTop.position.set(0, poleLength, 0); | |
| sphereTop.castShadow = true; | |
| poleGroup.add(cylinder); | |
| poleGroup.add(sphereBottom); | |
| poleGroup.add(sphereTop); | |
| // ํ์ง ํ์ ๋ฒ ์ด๋ง ์ถ (Z์ถ ๋ฐฉํฅ) | |
| const hingePivotGeo = new THREE.CylinderGeometry(0.04, 0.04, 0.32, 16); | |
| const hingePivotMat = new THREE.MeshStandardMaterial({ color: 0x334155, metalness: 0.8 }); | |
| const hingePivot = new THREE.Mesh(hingePivotGeo, hingePivotMat); | |
| hingePivot.rotation.x = Math.PI / 2; | |
| poleGroup.add(hingePivot); | |
| cartMesh.add(poleGroup); | |
| poleMesh = poleGroup; | |
| // 10. ์ ์ด๋ ฅ / ์ธ๋ ๊ฐ์ํ ํ์ดํ | |
| const arrowDir = new THREE.Vector3(1, 0, 0); | |
| forceArrowMesh = new THREE.ArrowHelper(arrowDir, new THREE.Vector3(0, railY, railZ), 0.5, 0x00f0ff, 0.14, 0.09); | |
| scene.add(forceArrowMesh); | |
| // ์๋์ฐ ๋ฆฌ์ฌ์ด์ฆ ๋ฆฌ์ค๋ | |
| window.addEventListener('resize', onWindowResize); | |
| } | |
| function onWindowResize() { | |
| const container = document.getElementById('canvas-container'); | |
| if (!container || !renderer || !camera) return; | |
| const width = container.clientWidth; | |
| const height = container.clientHeight; | |
| if (width === 0 || height === 0) return; | |
| camera.aspect = width / height; | |
| camera.updateProjectionMatrix(); | |
| renderer.setSize(width, height); | |
| } | |
| // ========================================== | |
| // 4. ์ค์๊ฐ ์ค์ค๋ก์ค์ฝํ ๊ทธ๋ํ (Canvas) | |
| // ========================================== | |
| const historyLength = 240; | |
| const historyData = { | |
| theta: new Array(historyLength).fill(0), | |
| x: new Array(historyLength).fill(0), | |
| force: new Array(historyLength).fill(0) | |
| }; | |
| function updateOscilloscope(theta, x, force) { | |
| historyData.theta.shift(); | |
| historyData.theta.push(theta || 0); | |
| historyData.x.shift(); | |
| historyData.x.push(x || 0); | |
| historyData.force.shift(); | |
| historyData.force.push(force || 0); | |
| if (!isGraphVisible) return; | |
| const canvas = document.getElementById('oscilloscopeCanvas'); | |
| if (!canvas) return; | |
| const ctx = canvas.getContext('2d'); | |
| const w = canvas.width; | |
| const h = canvas.height; | |
| ctx.clearRect(0, 0, w, h); | |
| // ๋ฐฐ๊ฒฝ ๊ฒฉ์์ | |
| ctx.strokeStyle = 'rgba(255, 255, 255, 0.06)'; | |
| ctx.lineWidth = 1; | |
| ctx.beginPath(); | |
| for (let y = 20; y < h; y += 25) { | |
| ctx.moveTo(0, y); | |
| ctx.lineTo(w, y); | |
| } | |
| for (let gx = 0; gx < w; gx += 50) { | |
| ctx.moveTo(gx, 0); | |
| ctx.lineTo(gx, h); | |
| } | |
| ctx.stroke(); | |
| // ์ค์ 0 ๊ธฐ์ค์ | |
| const midY = h / 2; | |
| ctx.strokeStyle = 'rgba(255, 255, 255, 0.18)'; | |
| ctx.setLineDash([4, 4]); | |
| ctx.beginPath(); | |
| ctx.moveTo(0, midY); | |
| ctx.lineTo(w, midY); | |
| ctx.stroke(); | |
| ctx.setLineDash([]); | |
| // ์ ๊ทธ๋ฆฌ๊ธฐ ํจ์ | |
| function drawLine(arr, color, scale) { | |
| ctx.strokeStyle = color; | |
| ctx.lineWidth = 2; | |
| ctx.beginPath(); | |
| for (let i = 0; i < arr.length; i++) { | |
| const px = (i / (historyLength - 1)) * w; | |
| const py = midY - arr[i] * scale; | |
| if (i === 0) ctx.moveTo(px, py); | |
| else ctx.lineTo(px, py); | |
| } | |
| ctx.stroke(); | |
| } | |
| // theta (rad): ยฑ1.0 rad ๊ธฐ์ค ์ค์ผ์ผ | |
| drawLine(historyData.theta, '#00f0ff', 70); | |
| // x (m): ยฑ2.0 m ๊ธฐ์ค ์ค์ผ์ผ | |
| drawLine(historyData.x, '#f43f5e', 35); | |
| // force (N): ยฑ10 N ๊ธฐ์ค ์ค์ผ์ผ | |
| drawLine(historyData.force, '#fbbf24', 5); | |
| } | |
| // ========================================== | |
| // 5. ์๋ฎฌ๋ ์ด์ ๋ฃจํ | |
| // ========================================== | |
| function animate() { | |
| requestAnimationFrame(animate); | |
| if (isRunning) { | |
| const subDt = physicsConfig.dt / physicsConfig.substeps; | |
| for (let s = 0; s < physicsConfig.substeps; s++) { | |
| const u = computeControl(state); | |
| state.u = u; | |
| const force = u * physicsConfig.gear; | |
| state = rk4Step(state, force, subDt); | |
| // ์๋ ๋ ์ผ ํ๊ณ ์ํํธ ๋ฐ์ด์ค (ยฑ2.3m) | |
| if (Math.abs(state.x) > 2.3) { | |
| state.x = Math.sign(state.x) * 2.3; | |
| state.v = -state.v * 0.3; // ๋ฐ๋ฐ๊ณ์ 0.3 | |
| } | |
| } | |
| } | |
| // Three.js 3D ๋ชจ๋ธ ์ ๋ฐ์ดํธ | |
| if (cartMesh) { | |
| // 1. ์๋ ์์น (X์ถ ์ด๋, ๋์ด railY, ๊น์ด railZ) | |
| cartMesh.position.set(state.x, railY, railZ); | |
| // 2. ๋ง๋ ํ์ ๊ฐ (Z์ถ ์ค์ฌ ํ์ ) | |
| // theta = 0 ์ผ ๋ ์์ง(+Y), ์ค๋ฅธ์ชฝ ๊ธฐ์ธ์ด์ง(theta > 0)์ผ ๋ ์๊ณ๋ฐฉํฅ(-Z ํ์ ) | |
| if (poleMesh) { | |
| poleMesh.rotation.z = -state.theta; | |
| } | |
| // 3. ํ ํ์ดํ | |
| const totalForce = (state.u || 0) * physicsConfig.gear; | |
| if (forceArrowMesh) { | |
| forceArrowMesh.position.set(state.x, railY, railZ); | |
| if (Math.abs(totalForce) > 0.05) { | |
| forceArrowMesh.visible = true; | |
| forceArrowMesh.setDirection(new THREE.Vector3(Math.sign(totalForce), 0, 0)); | |
| forceArrowMesh.setLength(Math.min(1.2, Math.abs(totalForce) * 0.1), 0.1, 0.06); | |
| } else { | |
| forceArrowMesh.visible = false; | |
| } | |
| } | |
| } | |
| // 4. UI ํ ๋ ๋ฉํธ๋ฆฌ ์ ๋ฐ์ดํธ (์์ ํ ๊ธฐ๋ณธ๊ฐ ์ฒ๋ฆฌ) | |
| const deg = ((state.theta || 0) * 180 / Math.PI).toFixed(2); | |
| const xVal = (state.x || 0).toFixed(2); | |
| const omegaVal = (state.omega || 0).toFixed(2); | |
| const uVal = (state.u !== undefined ? state.u : 0).toFixed(2); | |
| const energyVal = (computeTotalEnergy(state) || 0).toFixed(2); | |
| const elTheta = document.getElementById('valTheta'); | |
| const elX = document.getElementById('valX'); | |
| const elOmega = document.getElementById('valOmega'); | |
| const elCtrl = document.getElementById('valCtrl'); | |
| const elEnergy = document.getElementById('valEnergy'); | |
| if (elTheta) elTheta.textContent = `${deg}ยฐ`; | |
| if (elX) elX.textContent = `${xVal} m`; | |
| if (elOmega) elOmega.textContent = `${omegaVal} rad/s`; | |
| if (elCtrl) elCtrl.textContent = `${uVal}`; | |
| if (elEnergy) elEnergy.textContent = `${energyVal} J`; | |
| // ํ๋ก๊ทธ๋ ์ค ๋ฐ (-45๋ ~ +45๋ ๋ฒ์) | |
| const barTheta = document.getElementById('barTheta'); | |
| const barX = document.getElementById('barX'); | |
| if (barTheta) { | |
| const thetaPct = Math.max(0, Math.min(100, 50 + (state.theta / (Math.PI / 4)) * 50)); | |
| barTheta.style.width = `${thetaPct}%`; | |
| } | |
| if (barX) { | |
| const xPct = Math.max(0, Math.min(100, 50 + (state.x / 2.3) * 50)); | |
| barX.style.width = `${xPct}%`; | |
| } | |
| // ์ค์ค๋ก์ค์ฝํ ๊ทธ๋ํ ๊ฐฑ์ | |
| const totalForce = (state.u || 0) * physicsConfig.gear; | |
| updateOscilloscope(state.theta, state.x, totalForce); | |
| if (controls) controls.update(); | |
| if (renderer && scene && camera) { | |
| renderer.render(scene, camera); | |
| } | |
| } | |
| // ========================================== | |
| // 6. UI ์ด๋ฒคํธ ๋ฆฌ์ค๋ ๋ฐ์ธ๋ฉ | |
| // ========================================== | |
| function setupEvents() { | |
| // ์ฌ์ / ์ผ์์ ์ง | |
| const btnPlayPause = document.getElementById('btnPlayPause'); | |
| if (btnPlayPause) { | |
| btnPlayPause.addEventListener('click', () => { | |
| isRunning = !isRunning; | |
| const playIcon = document.getElementById('playIcon'); | |
| if (playIcon) playIcon.textContent = isRunning ? 'โธ' : 'โถ'; | |
| }); | |
| } | |
| // ๋ฆฌ์ | |
| const btnReset = document.getElementById('btnReset'); | |
| if (btnReset) { | |
| btnReset.addEventListener('click', () => { | |
| state.x = 0.0; | |
| state.v = 0.0; | |
| state.theta = 0.15; // 0.15 rad | |
| state.omega = 0.0; | |
| state.u = 0.0; | |
| state.time = 0.0; | |
| }); | |
| } | |
| // ์ธ๋ ์ธ๊ฐ (Impulse) | |
| const btnPushLeft = document.getElementById('btnPushLeft'); | |
| if (btnPushLeft) { | |
| btnPushLeft.addEventListener('click', () => { | |
| state.v = (state.v || 0) - 1.8; // ์๋ ์ ์ผ์ชฝ ์๋ ๊ธ๊ฒฉ ์ธ๊ฐ | |
| }); | |
| } | |
| const btnPushRight = document.getElementById('btnPushRight'); | |
| if (btnPushRight) { | |
| btnPushRight.addEventListener('click', () => { | |
| state.v = (state.v || 0) + 1.8; // ์๋ ์ ์ค๋ฅธ์ชฝ ์๋ ๊ธ๊ฒฉ ์ธ๊ฐ | |
| }); | |
| } | |
| const btnTiltPole = document.getElementById('btnTiltPole'); | |
| if (btnTiltPole) { | |
| btnTiltPole.addEventListener('click', () => { | |
| state.theta = (state.theta || 0) + (Math.random() > 0.5 ? 0.35 : -0.35); // ๋ง๋ ๊ฐ๋ 20๋ ์ธ๋ | |
| }); | |
| } | |
| // ๊ทธ๋ํ ํจ๋ ํ ๊ธ | |
| const btnToggleGraph = document.getElementById('btnToggleGraph'); | |
| const graphPanel = document.getElementById('graphPanel'); | |
| if (btnToggleGraph && graphPanel) { | |
| btnToggleGraph.addEventListener('click', () => { | |
| isGraphVisible = !isGraphVisible; | |
| if (isGraphVisible) { | |
| graphPanel.classList.remove('hidden'); | |
| } else { | |
| graphPanel.classList.add('hidden'); | |
| } | |
| }); | |
| } | |
| // ์ ์ด ๋ชจ๋ ํญ | |
| const modeChips = document.querySelectorAll('.mode-chip'); | |
| const formulaDisplay = document.getElementById('formulaDisplay'); | |
| modeChips.forEach(chip => { | |
| chip.addEventListener('click', () => { | |
| modeChips.forEach(c => c.classList.remove('active')); | |
| chip.classList.add('active'); | |
| controlSettings.mode = chip.dataset.mode; | |
| if (!formulaDisplay) return; | |
| if (controlSettings.mode === 'pd') { | |
| formulaDisplay.innerHTML = `<code>u = clip(8.0ยทฮธ + 1.6ยทฯ + 0.3ยทx + 0.6ยทv, -1, 1)</code><small>์ฌ๋์ด ์ง ๊ท์น: ๊ธฐ์ธ์ด์ง ์ชฝ์ผ๋ก ์๋ ๋ฅผ ๋ฐ์ด ๋ณต์ ํ ํฌ ์ ๋</small>`; | |
| } else if (controlSettings.mode === 'lqr') { | |
| formulaDisplay.innerHTML = `<code>u = -Kยทx = -(1.73ยทx + 2.85ยทv + 24.8ยทฮธ + 6.42ยทฯ) / gear</code><small>LQR ์ต์ ์ ์ด: Riccati ํด ๊ธฐ๋ฐ ์ํ ํผ๋๋ฐฑ</small>`; | |
| } else { | |
| formulaDisplay.innerHTML = `<code>u = 0 (์ ์ด ๋๊ธฐ)</code><small>์์ฐ ์ค๋ ฅ ๋ฐ ๊ด์ฑ์ ์ํ ๋น์ ํ ์ญ์ง์ ์์ ๋ํ</small>`; | |
| } | |
| }); | |
| }); | |
| // ์ฌ๋ผ์ด๋ ๋ฐ์ธ๋ฉ | |
| const setupSlider = (id, displayId, objKey) => { | |
| const slider = document.getElementById(id); | |
| const display = document.getElementById(displayId); | |
| if (!slider || !display) return; | |
| slider.addEventListener('input', (e) => { | |
| const val = parseFloat(e.target.value); | |
| controlSettings[objKey] = val; | |
| display.textContent = val.toFixed(1); | |
| }); | |
| }; | |
| setupSlider('gainTheta', 'dispGainTheta', 'k_theta'); | |
| setupSlider('gainOmega', 'dispGainOmega', 'k_omega'); | |
| setupSlider('gainX', 'dispGainX', 'k_x'); | |
| setupSlider('gainV', 'dispGainV', 'k_v'); | |
| const gearSlider = document.getElementById('actuatorGear'); | |
| const dispGear = document.getElementById('dispGear'); | |
| if (gearSlider && dispGear) { | |
| gearSlider.addEventListener('input', (e) => { | |
| const val = parseFloat(e.target.value); | |
| physicsConfig.gear = val; | |
| dispGear.textContent = val.toFixed(1); | |
| }); | |
| } | |
| // ๋ชจ๋ฌ ์ฐฝ ์ด๊ธฐ/๋ซ๊ธฐ | |
| const btnOpenReport = document.getElementById('btnOpenReport'); | |
| const btnCloseModal = document.getElementById('btnCloseModal'); | |
| const reportModal = document.getElementById('reportModal'); | |
| if (btnOpenReport && reportModal) { | |
| btnOpenReport.addEventListener('click', () => { | |
| loadReportContent(); | |
| reportModal.classList.add('open'); | |
| }); | |
| } | |
| if (btnCloseModal && reportModal) { | |
| btnCloseModal.addEventListener('click', () => { | |
| reportModal.classList.remove('open'); | |
| }); | |
| } | |
| if (reportModal) { | |
| reportModal.addEventListener('click', (e) => { | |
| if (e.target === reportModal) { | |
| reportModal.classList.remove('open'); | |
| } | |
| }); | |
| } | |
| } | |
| // ๋ชจ๋ฌ ๋ณด๊ณ ์ ๋ด์ฉ ๋ ๋๋ง | |
| function loadReportContent() { | |
| const contentEl = document.getElementById('modalReportContent'); | |
| if (!contentEl) return; | |
| contentEl.innerHTML = ` | |
| <h2>1. ๊ฐ์ ๋ฐ ์ฐ๊ตฌ ๋ชฉ์ </h2> | |
| <p> | |
| ์นดํธํด(Cart-Pole, Inverted Pendulum on a Cart) ์์คํ ์ ๋ํ์ ์ธ <strong>๋น์ ํ(Nonlinear), ์ ๊ตฌ๋(Underactuated), ๋ณธ์ง์ ๋ถ์์ (Inherently Unstable)</strong> ๋์ญํ ์์คํ ์ ๋๋ค. | |
| ๋ชจํฐ๋ ์๋ (Cart)์ ์ํ ๋ณ์ง ์ด๋๋ง์ ์ง์ ๊ตฌ๋ํ ์ ์์ผ๋ฉฐ, ํ์ ์กฐ์ธํธ๋ก ์ฐ๊ฒฐ๋ ๋ง๋(Pole)๋ ์ก์ถ์์ดํฐ๊ฐ ์ง์ ํ ํฌ๋ฅผ ์ฃผ์ง ์๋ ์ ๊ตฌ๋ ์์ ๋๋ฅผ ๊ฐ์ง๋๋ค. | |
| ๋ณธ ์๋ฎฌ๋ ์ดํฐ๋ MuJoCo ๋ฌผ๋ฆฌ ์์ง์ XML ๊ท๊ฒฉ๊ณผ ๋์ผํ ์ญํ ํ๋ผ๋ฏธํฐ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๊ณ ์ ๋ฐ RK4 ์ ๋ถ๊ธฐ๋ฅผ ํตํด ๊ตฌ์ถ๋์์ผ๋ฉฐ, ์ฌ๋์ด ์ง๊ด์ผ๋ก ์ค๊ณํ PD ํผ๋๋ฐฑ ์ ์ด ๋ฒ์น์ ๋ฌผ๋ฆฌ์ ์ ํจ์ฑ์ ๋ถ์ํฉ๋๋ค. | |
| </p> | |
| <h2>2. ๋ผ๊ทธ๋์ฃผ ์ญํ(Lagrangian Mechanics)์ ํตํ ์ด๋๋ฐฉ์ ์ ์ ๋</h2> | |
| <p>์ผ๋ฐํ ์ขํ๊ณ๋ก ์๋ ์์น $x$์ ๋ง๋์ ์ฐ์ง ๊ธฐ์ค ํ์ ๊ฐ $\theta$๋ฅผ ์ค์ ํฉ๋๋ค.</p> | |
| <pre> | |
| [์์คํ ์ด๋๋ฐฉ์ ์] | |
| (M + m)แบ + (ml cosฮธ)ฮธฬ = F - b_cแบ + ml ฮธฬยฒ sinฮธ | |
| (ml cosฮธ)แบ + (I + mlยฒ)ฮธฬ = mgl sinฮธ - b_p ฮธฬ | |
| </pre> | |
| <p> | |
| ์ฌ๊ธฐ์ $M=1.0\\text{kg}$์ ์๋ ์ง๋, $m=0.1\\text{kg}$์ ๋ง๋ ์ง๋, $l=0.5\\text{m}$์ ๋ฌด๊ฒ์ค์ฌ๊น์ง ๊ฑฐ๋ฆฌ, $b_c, b_p$๋ ๊ฐ๊ฐ ์๋ ์ฌ๋ผ์ด๋ ๋ฐ ํ์ง ๋ง์ฐฐ ๊ฐ์ ๊ณ์์ ๋๋ค. | |
| </p> | |
| <h2>3. "๊ธฐ์ธ์ด์ง ์ชฝ์ผ๋ก ๋ฏผ๋ค"์ ๋ฌผ๋ฆฌ์ ์ง๊ด๊ณผ ๊ด์ฑ๋ ฅ ์ญํ</h2> | |
| <p> | |
| ๋ง๋๊ฐ ์ค๋ฅธ์ชฝ์ผ๋ก $\theta > 0$ ๊ธฐ์ธ์ด์ก์ ๋, ์ค๋ ฅ $mg$์ ์ํ ํ ํฌ $\\tau_g = mgl\\sin\\theta$๋ ๋ง๋๋ฅผ ์๊ณ ๋ฐฉํฅ(๋ ๋์ด์ง๋ ๋ฐฉํฅ)์ผ๋ก ๊ฐ์์ํต๋๋ค. | |
| ์ด๋ฅผ ์์ํ๊ธฐ ์ํด ์๋ ๋ฅผ ์ค๋ฅธ์ชฝ($\\ddot{x} > 0$)์ผ๋ก ๊ธ๊ฐ์ํ๋ฉด, ๊ฐ์ ์ขํ๊ณ(๋น๊ด์ฑ๊ณ)์์ ๋ง๋์ ์ง๋์ค์ฌ์ ์ผ์ชฝ ๋ฐฉํฅ์ผ๋ก ๊ด์ฑ๋ ฅ(Fictitious Force) $F_{inertial} = -m\\ddot{x}$๋ฅผ ๋ฐ๊ฒ ๋ฉ๋๋ค. | |
| ์ด ๊ด์ฑ๋ ฅ์ด ํ์ง ์ถ์ ๋ํด ํ์ฑํ๋ ์ญํ ํฌ $\\tau_{inertial} = -m\\ddot{x} l \\cos\\theta$๊ฐ ์ค๋ ฅ ํ ํฌ๋ฅผ ์ ์ํ์ฌ ๋ง๋๋ฅผ ์ฐ์ง ๋ฐฉํฅ($\\theta \\to 0$)์ผ๋ก ์ผ์ผ์ผ ์ธ์๋๋ค. | |
| </p> | |
| <h2>4. ์ํ ํผ๋๋ฐฑ ์ ์ด ๊ท์น ๋ถ์</h2> | |
| <p>์ฝ๋์ ์ ์๋ ์ ์ด ๋ฒ์น์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:</p> | |
| <pre>u = clip(8ยทฮธ + 1.6ยทฮธฬ + 0.3ยทx + 0.6ยทแบ, -1, 1), Force F = u ร gear (gear=10)</pre> | |
| <ul> | |
| <li><strong>๋ง๋ ๋น๋ก ๊ฒ์ธ ($k_\\theta = 8.0$):</strong> ๊ฐ๋ ์ค์ฐจ์ ๋น๋กํ์ฌ ๊ฐ๋ ฅํ ๋ณต์๋ ฅ์ ๋ถ์ฌํ๋ '๊ฐ์ ๋นํ๋ฆผ ์คํ๋ง(Virtual Torsional Spring)' ์ญํ .</li> | |
| <li><strong>๋ง๋ ๋ฏธ๋ถ ๊ฒ์ธ ($k_\\omega = 1.6$):</strong> ๊ฐ์๋์ ์ ํญํ์ฌ ์ง๋์ ๊ธ๊ฒฉํ ๊ฐ์ ์ํค๋ '๊ฐ์ ๋ํผ(Virtual Damper)' ์ญํ . ์ค๋ฒ์ํธ ๋ฐฉ์ง.</li> | |
| <li><strong>์๋ ์์น ๊ฒ์ธ ($k_x = 0.3$):</strong> ๋ง๋๊ฐ ์ ์ฑ๋ก ์๋ ๊ฐ ๋ฌดํ์ ๋ฐ๊นฅ์ผ๋ก ๋ฐ๋ ค๋์ง ์๋๋ก ์์ ($x=0$)์ผ๋ก ๋์ด๋น๊ธฐ๋ ๊ตฌ์ฌ๋ ฅ ๋ณต์ ์ญํ .</li> | |
| <li><strong>์๋ ์๋ ๊ฒ์ธ ($k_v = 0.6$):</strong> ์๋ ์ ๋ณ์ง ์ด๋ ์ง๋ ๋ฐ ๋ฐ์ฐ(Drift)์ ์ ๋ํ๋ ๋ํ ์ญํ .</li> | |
| </ul> | |
| <h2>5. ์ ํํ ๋ฐ LQR ์ต์ ์ ์ด๊ธฐ์์ ๋น๊ต</h2> | |
| <p> | |
| ์์ง ํํ์ $(\\theta \\approx 0, \\dot{\\theta} \\approx 0)$ ๊ทผ๋ฐฉ์์ ํ ์ผ๋ฌ ์ ๊ฐ ์๊ฐ ๊ทผ์ฌ($\\sin\\theta \\approx \\theta, \\cos\\theta \\approx 1$)๋ฅผ ์ทจํ๋ฉด ์ํ๊ณต๊ฐ ์ ํ ๋ชจ๋ธ $\\dot{\\mathbf{x}} = A\\mathbf{x} + B u$๋ฅผ ์ป์ต๋๋ค. | |
| ์ธ๊ฐ์ด ๊ฒฝํ์ ์ผ๋ก ์กฐ์จํ ๊ท์น ๊ธฐ๋ฐ ๊ฒ์ธ์ ์ด ์ ํ ์์คํ ์ ๋ชจ๋ ํ๋ฃจํ ๊ทน์ (Closed-Loop Poles)์ ์ข๋ฐํ๋ฉด(LHP, Stable Region)์ ์์ ์ ์ผ๋ก ๋ฐฐ์นํ๊ณ ์์์ ์ํ์ ์ผ๋ก ์ ์ฆํ ์ ์์ต๋๋ค. | |
| </p> | |
| `; | |
| } | |
| // ========================================== | |
| // 7. ์ง์ ์ ์ด๊ธฐํ | |
| // ========================================== | |
| window.addEventListener('DOMContentLoaded', () => { | |
| initThree(); | |
| setupEvents(); | |
| animate(); | |
| }); | |