# Robot arm - inverse kinematics

I have a robot arm type structure made from 5 different STL files.

The end object of the arm is flat palm that needs to move in 3D space using mouse.

On dragging the end object using mouse, 4 other 3D objects should rotate/translate for moving end object to desired XY mouse position. For example:

• on dragging end object left/right, first object needs to Yaw.
• on dragging end object up/down, second object needs to Roll, etc.

Any reference/sample application of inverse kinematics (iterative method) would be very helpful.
Thank you.

there are two inverse kinematics methods that you can use to solve this problem.
have you rotations/translations constraints on the joints of the robot ?

if yes
you can use the CCD algorithm, it is a simple algorithm but you must have a mathematical model of the robot

else
you can use the FABRIK algorithm
http://andreasaristidou.com/publications/papers/FABRIK.pdf

1 Like

Thank you for the information.
I donâ€™t have constraints between STL models (3D models rotate/translate within their min & max.)

Both algorithms, CCD and FABRIK, are promising. However, Iâ€™m a beginner in numerical methods for solving kinematic problems. So, any reference for incremental learning would be a great help.

Thank you.

1 Like

Adding @JohnK as he is a king of maths and teaching !!!

Maybe looking at this PG will inform, https://www.babylonjs-playground.com/#VSURBJ#2. Dragging the cube will move the arm. The sphere acts as a pole mesh.

In my own work, I have changed to the next bone up the chain for the pole, and allow for the pole angle to be changed as required.

I require the wrist / feet to also be pose-able. The red square disks a re bone look controllers, which the wrist / foot bones point to. Not sure you require 3 bones to be pose-able, but it can be very time consuming to set-up.

Saying that you have 5 files sounds like your first problem. How do expect to have a skeleton spread over multiple files? Does STL even support skeletons ?

1 Like

2 Likes

Thank you for the playground link, it helps understand how skeleton/bones IK can be used to move robot arm.

In my application, 3D environment is created from 5 different meshes to allow importing and moving them individually.

Is there any way to create skeleton/bones structure programmatically after importing STL (or other file format)? (It can simplify Inverse Kinematics of robot arm.)

Could you share a PG with your scene or just the STL objects ?

That is a little tough to do in BJS itself, but having separate meshes would make it at least possible.

First, you have to add bone indexes & weights to every vertex of each mesh. If you have 1 mesh per bone, then set the index to the index of the bone to match.

``````var doMesh = function(mesh, index) {
const n = mesh.getTotalVertices();
const indexes = new Float32Array(n * 4);
const weights = new Float32Array(n * 4);
for (let i = 0; i < n; i++) {
indexes[i * 4] = index;
weights[i * 4] = 1;
}
mesh.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, indexes),
mesh.setVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind, weights),
}
``````

The skeleton is going to be a lot trickier. Not a lot of code, but you are going to need â€śmagicâ€ť numbers.

``````const skeleton = new BABYLON.Skeleton("name", "0", scene);
let rootBone = new BABYLON.Bone("K2-Root", skeleton, null, BABYLON.Matrix.FromValues(1,0,.0001,0,0,1,0,0,.0001,0,-1,0,0,0,0,1));
let bone1 = new BABYLON.Bone("K2-SpineLower", skeleton, rootBone, BABYLON.Matrix.FromValues(1,0,.0001,0,0,1,0,0,-.0001,0,1,0,0,.8457,0,1));
let bone2 = new BABYLON.Bone("K2-SpineUpper", skeleton, bone1, BABYLON.Matrix.FromValues(1,0,0,0,0,.9895,-.1448,0,0,.1448,.9895,0,0,.2254,0,1));
``````

This is an abbreviated list of a computer generated section from my Blender source code generator. You would not build your matrices with hardcoded values, but probably starting with a static Matrix method to instance one for either the translation or rotation, then another operation to insert the other. Also child bones need to also be multiplied by the inverted parent matrix.