|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +Demonstration of the differential IK feature of OptIK. Loads a model, |
| 5 | +configures the solver, and steps of diff-IK with varying velocity limits. |
| 6 | +
|
| 7 | +Usage: |
| 8 | + Run this script by providing your own URDF and kinematic chain: |
| 9 | +
|
| 10 | + $ python example_diff_ik.py <my_robot.urdf> <base_link> <ee_link> |
| 11 | +""" |
| 12 | + |
| 13 | +import sys |
| 14 | + |
| 15 | +import numpy as np |
| 16 | +from optik import Robot |
| 17 | + |
| 18 | +np.set_printoptions(suppress=True, precision=2) |
| 19 | + |
| 20 | +urdf_path, base_name, ee_name = sys.argv[1:4] |
| 21 | + |
| 22 | +robot = Robot.from_urdf_file(urdf_path, base_name, ee_name) |
| 23 | +n = robot.num_positions() |
| 24 | + |
| 25 | +rng = np.random.default_rng(seed=42) |
| 26 | +x0 = rng.uniform(*robot.joint_limits()) |
| 27 | + |
| 28 | +for v_max in [0.1, 0.5, 1.0, 10.0]: |
| 29 | + V_tgt = np.array([0.0, 0.0, 0.5, 0.0, 0.0, 1.0]) |
| 30 | + |
| 31 | + if (sol := robot.diff_ik(x0, V_tgt, [v_max] * n)) is not None: |
| 32 | + alpha, v_star = sol |
| 33 | + |
| 34 | + J = np.array(robot.joint_jacobian(x0)) |
| 35 | + R_W = np.array(robot.fk(x0))[:3, :3] |
| 36 | + J_W = np.vstack( |
| 37 | + ( |
| 38 | + R_W @ J[:3, :], |
| 39 | + R_W @ J[3:, :], |
| 40 | + ) |
| 41 | + ) |
| 42 | + V_star = np.matmul(J_W, v_star) |
| 43 | + |
| 44 | + print("------") |
| 45 | + print(" x0 =", np.array(x0)) |
| 46 | + print(" v_max =", np.array(v_max)) |
| 47 | + print(" V_tgt =", np.array(V_tgt)) |
| 48 | + print(" alpha =", alpha) |
| 49 | + print(" v* =", np.array(v_star)) |
| 50 | + print(" V* =", np.array(V_star)) |
| 51 | + |
| 52 | + assert 0.0 <= alpha <= 1.0 |
| 53 | + np.testing.assert_allclose(V_tgt, V_star / alpha, atol=1e-6) |
0 commit comments