Beam Bots Logo

BB.IK.DLS

CI License: Apache 2.0 Hex version badge Hexdocs badge REUSE status Ask DeepWiki

A Damped Least Squares (Levenberg-Marquardt) inverse kinematics solver for the Beam Bots robotics framework.

DLS computes the joint angles needed to position an end-effector at a target location and orientation. The damping factor prevents instability near kinematic singularities where standard pseudoinverse methods become ill-conditioned.

Features

Installation

Add bb_ik_dls to your list of dependencies in mix.exs:

def deps do
  [
    {:bb_ik_dls, "~> 0.7.0"}
  ]
end

Usage

Basic Solving

robot = MyRobot.robot()
{:ok, state} = BB.Robot.State.new(robot)

# Solve for end-effector to reach target position
target = Vec3.new(0.4, 0.2, 0.1)

case BB.IK.DLS.solve(robot, state, :base_link, :end_effector, target) do
  {:ok, positions, meta} ->
    BB.Robot.State.set_configurations(state, positions)
    IO.puts("Solved in #{meta.iterations} iterations")

  {:error, %BB.Error.Kinematics.NoSolution{residual: residual}} ->
    IO.puts("Failed to converge, residual: #{residual}m")
end

With Orientation

# Full 6-DOF target via Transform
target = Transform.from_position_quaternion(
  Vec3.new(0.3, 0.2, 0.1),
  Quaternion.from_axis_angle(Vec3.unit_z(), :math.pi() / 4)
)

{:ok, positions, meta} = BB.IK.DLS.solve(robot, state, :base_link, :gripper, target)

# Or with axis constraint (point tool in a direction)
target = {Vec3.new(0.3, 0.2, 0.1), {:axis, Vec3.unit_z()}}

Motion Convenience API

# Move end-effector using BB.Motion integration
target = Vec3.new(0.3, 0.2, 0.1)

case BB.IK.DLS.Motion.move_to(MyRobot, :gripper, target, source_link: :base_link) do
  {:ok, meta} -> IO.puts("Reached in #{meta.iterations} iterations")
  {:error, error} -> IO.puts("Failed: #{Exception.message(error)}")
end

# Coordinated multi-limb motion
targets = %{left_foot: Vec3.new(0.1, 0.0, 0.0), right_foot: Vec3.new(-0.1, 0.0, 0.0)}
BB.IK.DLS.Motion.move_to_multi(MyRobot, targets, source_link: :base_link)

Continuous Tracking

# Start tracking a moving target
{:ok, tracker} = BB.IK.DLS.Tracker.start_link(
  robot: MyRobot,
  target_link: :gripper,
  source_link: :base_link,
  initial_target: Vec3.new(0.3, 0.2, 0.1),
  update_rate: 30
)

# Update target from vision callback
BB.IK.DLS.Tracker.update_target(tracker, Vec3.new(0.35, 0.25, 0.15))

# Stop tracking
{:ok, final_positions} = BB.IK.DLS.Tracker.stop(tracker)

Target Formats

Format Description
Vec3.t() Position-only target
Transform.t() Position + full orientation from transform
{Vec3.t(), {:quaternion, Quaternion.t()}} Position + explicit quaternion
{Vec3.t(), {:axis, Vec3.t()}} Position + tool axis direction constraint

Options

Option Default Description
:max_iterations 100 Maximum solver iterations
:tolerance 1.0e-4 Position convergence tolerance (metres)
:orientation_tolerance 0.01 Orientation convergence (radians)
:lambda 0.5 Damping factor
:adaptive_damping true Adjust lambda based on error reduction
:step_size 0.1 Maximum joint update per iteration (radians)
:respect_limits true Clamp solved values to joint limits
:exclude_joints [] Joint names to exclude from solving (e.g. grippers)
:check_collisions false Verify the solution doesn't cause self-collision
:collision_margin 0.0 Safety margin for collision checking (metres)

Algorithm

DLS uses the damped pseudoinverse to compute joint updates:

Δθ = Jᵀ (J Jᵀ + λ²I)⁻¹ e

where:

For position-only targets, J comes from BB.Robot.Kinematics.position_jacobian/4; for orientation-constrained targets, it comes from BB.Robot.Kinematics.jacobian/4. Both are analytical Jacobians provided by BB core. DLS uses Nx for the damped pseudoinverse update.

The damping factor prevents instability when J Jᵀ is near-singular (at kinematic singularities).

Comparison with FABRIK

Aspect DLS FABRIK
Full pose targets on a 6-DOF arm Reliable Fitted alongside position, but converge less often
Planar and floating joints Solved through Refused; scope past them with source_link
Algorithm Matrix operations on a Jacobian Geometric, no Jacobian
Singularity handling Damping No Jacobian to become singular
Solving many chains at once One solve per chain Vectorises over a batch axis

Documentation

Full documentation is available at HexDocs.