First, you need to install IKPy (see installations instructions).
You also need a URDF file.
By default, we use the files provided in the resources folder of the IKPy repo.
Import the IKPy module :
In [2]:
import ikpy
import numpy as np
import ikpy.utils.plot as plot_utils
The basic element of IKPy is the kinematic Chain
.
To create a chain from an URDF file :
In [3]:
my_chain = ikpy.chain.Chain.from_urdf_file("../resources/poppy_ergo.URDF")
Note : as mentioned before, here we use a file in the resource folder.
In Inverse Kinematics, you want your kinematic chain to reach a 3D position in space.
To have a more general representation of position, IKPy works with homogeneous coordinates. It is a 4x4 matrix storing both position and orientation. Prepare your desired position as a 4x4 matrix. Here we only consider position, not orientation of the chain.
In [6]:
target_position = [ 0.1, -0.2, 0.1]
In [7]:
print("The angles of each joints are : ", my_chain.inverse_kinematics(target_position))
You can check that the Inverse Kinematics is correct by comparing with the original position vector :
In [9]:
real_frame = my_chain.forward_kinematics(my_chain.inverse_kinematics(target_position))
print("Computed position vector : %s, original position vector : %s" % (real_frame[:3, 3], target_frame[:3, 3]))
(If the code below doesn't work, comment the %maplotlib widget
line, and uncomment the %matplotlib inline
line)
In [12]:
# Optional: support for 3D plotting in the NB
# If there is a matplotlib error, uncomment the next line, and comment the line below it.
# %matplotlib inline
%matplotlib widget
import matplotlib.pyplot as plt
fig, ax = plot_utils.init_3d_figure()
my_chain.plot(my_chain.inverse_kinematics(target_position), ax, target=target_vector)
plt.xlim(-0.1, 0.1)
plt.ylim(-0.1, 0.1)
Out[12]:
You're done! Go to the tutorials to understand the general concepts of the library.
In [ ]: