kinematics.js

repository·master·Indexed 42 days ago

https://github.com/glumb/kinematics

A JavaScript library for 6DOF (6 Degrees of Freedom) robot kinematics, providing tools for forward and inverse kinematics calculations. It allows users to define robot geometry via link vectors and calculate joint angles or Tool Center Point (TCP) poses.

Tokens
1.1K
Snippets
6
Records
6
Agent score
4%

What's inside kinematics.js

  1. Handling kinematic coupling

    master

    The library assumes a standard series of joints, but some robots have kinematic coupling (e.g., where moving one joint physically affects the position of another).

    If your robot has coupling, you must manually correct the angles returned by inverse(). For example, if moving $R_1$ also changes the angle at $J_2$, you may need to add the $R_1$ value to $R_2$ after calculating the inverse kinematics.

    // Example correction for coupling
    let angles = RobotKin.inverse(...pose)
    angles[2] += angles[1]
    // set angles, do stuff
  2. How to use kinematics.js for 6DOF robot kinematics

    master

    To perform forward and inverse kinematics, import the default export, define your robot's geometry, and instantiate the Kinematics class.

    Geometry Definition: The geometry is an array of 5 entries describing the links V0-V5. Each entry is a tuple of 3 coordinates [x, y, z] representing the vector from $J_n$ to $J_{n+1}$.

    Constraint: For the kinematics to work correctly, the $y,z$ coordinates of $V_3$ and the $x,z$ coordinates of $V_4$ must be 0.

    const Kinematics = require('kinematics').default
    
    // Geometry array: 5 entries describing links V0-V5
    const geometry = [
          [1,  1,  0], // V0: 1x 1y
          [0, 10,  0], // V1: 10y
          [5,  0,  0], // V2: 5x
          [3,  0,  0], // V3: 3x
          [0, -3,  0], // V4: -3y
        ]
    
    const RobotKin = new Kinematics(geometry)
    
    // Example: Forward Kinematics
    let angles = [1.57, 1.2, 0, 0.3, 2.2, 1.1]
    const pose = RobotKin.forward(...angles)[5]
    
    // Example: Inverse Kinematics
    angles = RobotKin.inverse(...pose)
  3. RobotKin.forward()

    master

    Performs forward kinematics. It takes 6 joint angles as arguments and returns an array of joint positions and the Tool Center Point (TCP) orientation.

    Arguments:

    • R0, R1, R2, R3, R4, R5: The six joint angles.

    Returns: An array where the first 5 elements are the $[x, y, z]$ coordinates of joints $J_0$ through $J_4$, and the 6th element is an array containing the $J_5$ position plus the TCP Euler angles: [x, y, z, A, B, C].

    // Signature
    RobotKin.forward(R0, R1, R2, R3, R4, R5)
    
    // Example Return Shape
    [
      [  0,      0,     0 ], // J0
      [  0.5,    1,  -0.8 ], // J1
      [ -0.2, -8.8,   0.3 ], // J2
      [  1.8, -5.6,  -2.8 ], // J3
      [  3.0, -3.6,  -4.7 ], // J4
      [  4.7, -1.3,  -5.5,  1,  6,  -2.8 ] // J5 + TCP Euler angles
    ]
  4. RobotKin.inverse()

    master

    Performs inverse kinematics. It calculates the required joint angles to reach a specific pose.

    Arguments:

    • X, Y, Z: Target coordinates.
    • A, B, C: Euler angles in 'abc' order.

    Returns: An array of 6 angles. If the target pose is out of reach, the returned array will contain NaN for the unreachable angles.

    // Signature
    RobotKin.inverse(X, Y, Z, A, B, C)
    
    // Example Return Shapes
    [ 2, 1.6, 2.1, -3.5, 1, -1.5 ] // Valid angles
    [ 1, 2.3, 3.1, NaN, NaN, NaN ] // Out of reach
  5. Import the Kinematics module

    master

    The kinematics package provides a default export kin which serves as the primary entry point for performing forward and inverse kinematics calculations. You can import it into your project using standard ES module syntax.

    import kin from 'kinematics';