3D Robotik Kol, Ters Kinematik (IK), Eklem Hiyerarşisi ve Mekanik Sistem Motoru
Tek bir HTML5 Canvas üzerinde İleri Kinematik (Forward Kinematics) ebeveyn-çocuk sahne ağacı, FABRIK / CCD Ters Kinematik çözücüsü, 6-DOF Endüstriyel Robot Kol hedef yakalama algoritması ve Krank-Biyel (Piston/Süspansiyon) mekanik bağlantı matematiği.
1. İleri Kinematik (Forward Kinematics — Ebeveyn-Çocuk Eklem Zinciri Matris Hesabı)
Hiyerarşik bir iskelet veya güneş sistemi ağacında her düğüm (JointNode), kendi yerel açısını ve uzunluğunu ebeveyninin dünya koordinatlarına (worldPos) ve kümülatif açılarına ekleyerek tüm alt uzuvları zincirleme taşır:
function updateForwardKinematics3D(node, parentState = { x: 0, y: 0, z: 0, yaw: 0, pitch: 0 }) {
// 1. Ebeveyn açılarıyla yerel eklem açılarını birleştir
const worldYaw = parentState.yaw + (node.localYaw || 0);
const worldPitch = parentState.pitch + (node.localPitch || 0);
// 2. Küresel koordinatlardan 3B uç nokta (End-Effector / Child Origin) hesabı
const cosP = Math.cos(worldPitch), sinP = Math.sin(worldPitch);
const cosY = Math.cos(worldYaw), sinY = Math.sin(worldYaw);
const endPos = {
x: parentState.x + node.length * cosP * cosY,
y: parentState.y - node.length * sinP,
z: parentState.z + node.length * cosP * sinY,
yaw: worldYaw, pitch: worldPitch
};
node.worldStart = { x: parentState.x, y: parentState.y, z: parentState.z };
node.worldEnd = endPos;
// 3. Tüm alt eklemleri (Children) özyinelemeli (Recursive) güncelle
for (const child of (node.children || [])) updateForwardKinematics3D(child, endPos);
return endPos;
}
2. 25 Satırda FABRIK (Forward And Backward Reaching Inverse Kinematics) Ters Kinematik Çözücü
Karmaşık Jacobian matris tersi almadan, 3B uzaydaki bir eklem zincirini (Omuz ➔ Dirsek ➔ Bilek ➔ Kıskaç) hedef noktaya (target) saniyede 60 kez yakınsatan hızlı FABRIK algoritması:
function solveFABRIK3D(joints, lengths, target, iterations = 8) {
const n = joints.length, root = { ...joints[0] }; // Taban (Base) sabit kalır
for (let iter = 0; iter < iterations; iter++) {
// 1. GERİYE DOĞRU UZANMA (Backward Reaching: Uçtan Tabana)
joints[n - 1] = { ...target };
for (let i = n - 2; i >= 0; i--) {
const dx = joints[i].x - joints[i + 1].x, dy = joints[i].y - joints[i + 1].y, dz = joints[i].z - joints[i + 1].z;
const scale = lengths[i] / (Math.hypot(dx, dy, dz) || 1e-6);
joints[i] = { x: joints[i + 1].x + dx * scale, y: joints[i + 1].y + dy * scale, z: joints[i + 1].z + dz * scale };
}
// 2. İLERİYE DOĞRU UZANMA (Forward Reaching: Tabandan Uca)
joints[0] = { ...root };
for (let i = 0; i < n - 1; i++) {
const dx = joints[i + 1].x - joints[i].x, dy = joints[i + 1].y - joints[i].y, dz = joints[i + 1].z - joints[i].z;
const scale = lengths[i] / (Math.hypot(dx, dy, dz) || 1e-6);
joints[i + 1] = { x: joints[i].x + dx * scale, y: joints[i].y + dy * scale, z: joints[i].z + dz * scale };
}
}
return joints;
}
3. Krank-Biyel (Piston) ve Döner Eklem Açı Kısıtlamaları (clampJointAngle)
Robot eklemlerinin anatomik/mekanik sınırlarını aşmasını önleyen açı sınırlayıcı ve dairesel krank yarıçapını (r) biyel kolu (L) üzerinden doğrusal piston konumuna dönüştüren kinematik denklem:
// Döner Eklem Açı Sınırlaması (Biyomekanik / Robotik Servo Kısıtı)
function clampJointAngle(angleRad, minRad, maxRad) {
const norm = Math.atan2(Math.sin(angleRad), Math.cos(angleRad));
return Math.max(minRad, Math.min(maxRad, norm));
}
// Krank-Biyel (Crank-Slider Piston) Kinematik Denklemi
function solveCrankPiston(crankAngle, crankRadius, rodLength) {
const pinX = crankRadius * Math.cos(crankAngle);
const pinY = crankRadius * Math.sin(crankAngle);
const rodVertical = Math.sqrt(Math.max(1, rodLength * rodLength - pinX * pinX));
const pistonY = pinY - rodVertical;
const rodAngle = Math.atan2(-pinX, rodVertical);
return { pinX, pinY, pistonY, rodAngle };
}
Yorumlar
Yorum Gönder