Inverse Kinematics
Inverse Kinematics (IK) is the process of finding the required joint angles or positions of a robot at a specific target position and orientation in space.
Usually multiple solutions are possible to solve this problem; it is possible to adopt different approaches.
If the structure is simple enough as in planar robots or 6-axis antropomorphic arms with spherical wrist (kinematic decoupling), it is possible to derive the joint angles/distances analytically.
If the structure of the robot is complex, it is possible to use some numerical methods such as Jacobian Inverse Method/PseudoInverse
In addition, it’s possible to use Neural Networks and more advanced algorithms.
Two Link Planar Arm
It is possible to start from a simple example, the robot below.
| Link | \(\displaystyle a_i\) | \(\displaystyle \alpha_i\) | \(\displaystyle d_i\) | \(\displaystyle \theta_i\) |
|---|---|---|---|---|
| 1 | \(\displaystyle L_1\) | \(\displaystyle 0\) | \(\displaystyle 0\) | \(\displaystyle q_1\) |
| 2 | \(\displaystyle L_2\) | \(\displaystyle 0\) | \(\displaystyle 0\) | \(\displaystyle q_2\) |
|
|
--------------------
Robot: (2 bodies)
Idx Body Name Joint Name Joint Type Parent Name(Idx) Children Name(s)
--- --------- ---------- ---------- ---------------- ----------------
1 link1 joint1 revolute base_link(0) link2(2)
2 link2 joint2 revolute link1(1)
--------------------
|
|
|
|
T = 4x4
1 0 0 1
0 1 0 1
0 0 1 0
0 0 0 1
Transformation Matrix
|
|
A_1 =
\[ \displaystyle \left(\begin{array}{cccc} \cos \left(\theta_1 \right) & -\sin \left(\theta_1 \right) & 0 & L_1 ,\cos \left(\theta_1 \right)\newline \sin \left(\theta_1 \right) & \cos \left(\theta_1 \right) & 0 & L_1 ,\sin \left(\theta_1 \right)\newline 0 & 0 & 1 & 0\newline 0 & 0 & 0 & 1 \end{array}\right) \]
|
|
A_2 =
\[ \displaystyle \left(\begin{array}{cccc} \cos \left(\theta_2 \right) & -\sin \left(\theta_2 \right) & 0 & L_2 ,\cos \left(\theta_2 \right)\newline \sin \left(\theta_2 \right) & \cos \left(\theta_2 \right) & 0 & L_2 ,\sin \left(\theta_2 \right)\newline 0 & 0 & 1 & 0\newline 0 & 0 & 0 & 1 \end{array}\right) \]
|
|
T_base_ee =
\[ \displaystyle \left(\begin{array}{cccc} \cos \left(\theta_1 +\theta_2 \right) & -\sin \left(\theta_1 +\theta_2 \right) & 0 & L_2 ,\cos \left(\theta_1 +\theta_2 \right)+L_1 ,\cos \left(\theta_1 \right)\newline \sin \left(\theta_1 +\theta_2 \right) & \cos \left(\theta_1 +\theta_2 \right) & 0 & L_2 ,\sin \left(\theta_1 +\theta_2 \right)+L_1 ,\sin \left(\theta_1 \right)\newline 0 & 0 & 1 & 0\newline 0 & 0 & 0 & 1 \end{array}\right) \]
|
|
Solutions to the IK problem (Analytical)
Here the solution is pretty simple.
The reacheable workspace is donut-shaped. The external border is \(L_1 +L_2\), while the internal border is \(|L_1 -L_2 |\).
Given a point inside the workspace, in general, it is possible to reach the point in two configuration.
This two possibilities of configuration are called Elbow Up/Down, depending on how the solution is calculated.
Configuration 1
This configuration is called “Elbow Down” because the mid-joint is below the immaginary line joining the origin with the end-effector.
At first is needed to find \(\theta_2\), function of variables \((x_{ee} ,y_{ee} )\).
Using a generalised version of the Pythagoras’ theorem, the “Law of Cosines”:
\[ c^2 =a^2 +b^2 -2ab\cos (\gamma ) \]
And the Pythagoras formula for the right-angled triangle formed by the end-effector projection on x,y axis and the hypotenuse segment joining the origin with the end-effector:
\[ c=\sqrt{x_{ee}^2 +y_{ee}^2 } \]
It is possible to derive \(\theta_2\):
\[ \begin{array}{l} x_{ee}^2 +y_{ee}^2 ={L_1 }^2 +{L_2 }^2 -2L_1 L_2 \cos (\gamma )\newline x_{ee}^2 +y_{ee}^2 -{L_1 }^2 -{L_2 }^2 =-2L_1 L_2 \cos (\gamma )\newline \frac{x_{ee}^2 +y_{ee}^2 -{L_1 }^2 -{L_2 }^2 }{2L_1 L_2 }=-\cos (\gamma )\newline \textrm{since};\newline \theta_2 +\gamma =\pi \Rightarrow -\cos (\pi -\theta_2 )=\frac{x_{ee}^2 +y_{ee}^2 -{L_1 }^2 -{L_2 }^2 }{2L_1 L_2 }\newline \end{array} \]
\[ \begin{array}{l} \textrm{since};\newline \cos (x)=\cos (-x);\textrm{and}\cos (x+\pi )=-\cos (x)\newline \cos (\theta_2 )=\frac{x_{ee}^2 +y_{ee}^2 -{L_1 }^2 -{L_2 }^2 }{2L_1 L_2 } \end{array} \]
Arriving to the solution for \(\theta_2\)
\[ \theta_2 =\arccos \left.\Big(\frac{x_{ee}^2 +y_{ee}^2 -{L_1 }^2 -{L_2 }^2 }{2L_1 L_2 }\right.\Big) \]
Now it is possible to proceed with the calculation of \(\theta_1\).
At first
\[ \theta_1 =\phi -\alpha \]
Using just a little of trigonometry
\[ \begin{array}{l} a=L_2 \sin (\theta_2 )\newline a=c\sin (\alpha )\newline b=c\cos (\alpha )\newline b^{\prime \prime } =L_2 \cos (\theta_2 )\newline \newline x_{ee} =c\cos (\phi )\newline y_{ee} =c\sin (\phi ) \end{array} \]
Expandings the equations to find \(\alpha\)
\[ \begin{array}{l} \frac{a}{b}=\frac{c\sin (\alpha )}{c\cos (\alpha )}\newline \tan (\alpha )=\frac{L_2 \sin (\theta_2 )}{b^{\prime } +b^{\prime \prime } }\newline \alpha =\arctan \Big(\frac{L_2 \sin (\theta_2 )}{L_1 +L_2 \cos (\theta_2 )}\Big) \end{array} \]
Expandings the equations to find \(\phi\)
\[ \begin{array}{l} x_{ee} =c\cos (\phi )\newline y_{ee} =c\sin (\phi )\newline \tan (\phi )=\frac{y_{ee} }{x_{ee} },~~\phi =\arctan \Big(\frac{y_{ee} }{x_{ee} }\Big) \end{array} \]
The \(\arctan\) function has some limitations, it covers only the first and the fourth quadrant.
It exists a function widely used in computer graphics and games, simulations, called atan2 that can be used to solve this problem. It covers all quadrants \((-\pi ,\pi ]\).
The atan2 function is a two-argument arctangent that returns the angle between the positive x axis and a point in the plane. Here a brief comparison between the two functions.
| P | angle (rad) | atan(y/x) |
atan2(y,x) |
|---|---|---|---|
| (2, 2) | 0.7854 | 0.7854 | 0.7854 |
| (-2, 2) | 2.3562 | -0.7854 | 2.3562 |
| (-2, -2) | 3.9270 | 0.7854 | -2.3562 |
| (2, -2) | 5.4978 | -0.7854 | -0.7854 |
As reported on books and IT specifications, it is suggested to use the atan2 function instead of the traditional \(\arctan\) also because it corrects the limitations of the traditional function, correcting eventual division by zero error and adding angle correction giving the possibility to use the function on all quadrants.
Using the more robust atan2 function, the equation becomes:
\[ \phi =\textrm{atan2}(y_{ee} ,x_{ee} ) \]
\[ \alpha =\textrm{atan2}\Big(\frac{L_2 \sin (\theta_2 )}{L_1 +L_2 \cos (\theta_2 )}\Big) \]
Putting everithing togheter
\[ \theta_1 =\textrm{atan2}\Big(\frac{y_{ee} }{x_{ee} }\Big)-\textrm{atan2}\Big(\frac{L_2 \sin (\theta_2 )}{L_1 +L_2 \cos (\theta_2 )}\Big) \]
Configuration 2
Regarding the configuration Elbow Up, it is specular with respect the previous configuration. It is important to note that the angle in this configuration is negative with respect to the zero position.
\[ \cos (\theta_2 )=-\frac{x_{ee}^2 +y_{ee}^2 -{L_1 }^2 -{L_2 }^2 }{2L_1 L_2 } \]
In this configuration \(\theta_1 =\phi +\alpha\)
Regarding \(\phi\) and \(\alpha\), the math behind is the same of the previous case. It is verificable watching the image.
\[ \begin{array}{l} \theta_2 =-\arccos \left.\Big(\frac{x_{ee}^2 +y_{ee}^2 -{L_1 }^2 -{L_2 }^2 }{2L_1 L_2 }\right.\Big)\newline \theta_1 =\textrm{atan2}\Big(\frac{y_{ee} }{x_{ee} }\Big)+\textrm{atan2}\Big(\frac{L_2 \sin (\theta_2 )}{L_1 +L_2 \cos (\theta_2 )}\Big) \end{array} \]
Code and Validation
|
|
Success, the function works!
|
|
Using MATLAB IK function for 6-dof spherical wrist manipulators
ABB IRB1600
It is possible in MATLAB to import popular and well-supported robots.
|
|
--------------------
Robot: (7 bodies)
Idx Body Name Joint Name Joint Type Parent Name(Idx) Children Name(s)
--- --------- ---------- ---------- ---------------- ----------------
1 link_1 joint_1 revolute base_link(0) link_2(2)
2 link_2 joint_2 revolute link_1(1) link_3(3)
3 link_3 joint_3 revolute link_2(2) link_4(4)
4 link_4 joint_4 revolute link_3(3) link_5(5)
5 link_5 joint_5 revolute link_4(4) link_6(6)
6 link_6 joint_6 revolute link_5(5) tool0(7)
7 tool0 joint_6-tool0 fixed link_6(6)
--------------------
|
|
aIK =
analyticalInverseKinematics with properties:
KinematicGroup: [1x1 struct]
RigidBodyTree: [1x1 rigidBodyTree]
KinematicGroupType: 'RRRSSS'
KinematicGroupConfigIdx: [1 2 3 4 5 6]
IsValidGroupForIK: 1
|
|
--------------------
Robot: (7 bodies)
Index Base Name EE Body Name Type Actions
----- --------- ------------ ---- -------
1 base_link link_6 RRRSSS Use this kinematic group
2 base_link tool0 RRRSSS Use this kinematic group
If the robot is a serial chain, has 6 degrees of freedom and has a spherical wrist, it is possible to use a MATLAB function that simplify the process of controlling the robot using analyticalInverseKinematics. Otherwise it is possible to use the function generalizedInverseKinematics, but it is generally slower.
|
|
Joint type: fixed
|
|
aIK =
analyticalInverseKinematics with properties:
KinematicGroup: [1x1 struct]
RigidBodyTree: [1x1 rigidBodyTree]
KinematicGroupType: 'RRRSSS'
KinematicGroupConfigIdx: [1 2 3 4 5 6]
IsValidGroupForIK: 1
|
|
--------------------
Robot: (7 bodies)
Index Base Name EE Body Name Type Actions
----- --------- ------------ ---- -------
1 base_link link_6 RRRSSS Use this kinematic group
2 base_link tool0 RRRSSS Use this kinematic group
|
|
abbIKFn = function_handle with value:
@ikIRB1600
|
|
target = 4x4
0.0000 0 1.0000 1.0000
0 1.0000 0 0
-1.0000 0 0.0000 0.3000
0 0 0 1.0000
|
|
qsol = 2x6
-0.0000 0.9729 -0.1150 3.1416 0.8579 3.1416
-0.0000 0.9729 -0.1150 -0.0000 -0.8579 0.0000
|
|
T from vector
ans = 1x6
-0.0000 0.9729 -0.1150 3.1416 0.8579 3.1416
0.0000 -0.0000 1.0000 1.0000
0.0000 1.0000 0.0000 -0.0000
-1.0000 0.0000 0.0000 0.3000
0 0 0 1.0000
T from vector
ans = 1x6
-0.0000 0.9729 -0.1150 -0.0000 -0.8579 0.0000
0.0000 -0.0000 1.0000 1.0000
0.0000 1.0000 0.0000 -0.0000
-1.0000 0.0000 0.0000 0.3000
0 0 0 1.0000
|
|
Using MATLAB for general IK solutions
If the manipulator has more (or less or equal) than 6 joints, does not have spherical wrist, and has a more or less complex structure, it is possible to use the inverseKinematics function.
3-Link Arm
| Link | \(\displaystyle a_i\) | \(\displaystyle \alpha_i\) | \(\displaystyle d_i\) | \(\displaystyle \theta_i\) |
|---|---|---|---|---|
| 1 | \(\displaystyle 0\) | \(\displaystyle \frac{\pi }{2}\) | \(\displaystyle L_1\) | \(\displaystyle q_1\) |
| 2 | \(\displaystyle L_2\) | \(\displaystyle 0\) | \(\displaystyle 0\) | \(\displaystyle q_2\) |
| 3 | \(\displaystyle L_3\) | \(\displaystyle 0\) | \(\displaystyle 0\) | \(\displaystyle q_3\) |
|
|
The inverseKinematics function, creates a solver to calculate joint configurations for a desired end-effector pose. It is possible to use three different algorithms.
Due to the reduced number of degrees of freedom, it is obvious that it is not possible to impose a desired attitude to the end effector but it is possible to impose a position inside the workspace.
It is also obvious to put \({\text{initialGuess}}_{\text{k}} ={\text{initialGuess}}_{\text{k-1}}\) for smoothness and simplicity in solving when using this function inside a trajectory planning cycle.
|
|
T_target = 4x4
0.0000 0 1.0000 0.4000
0 1.0000 0 0.3000
-1.0000 0 0.0000 1.0000
0 0 0 1.0000
|
|
qsol = 1x3
0.6435 0.0967 1.6368
solInfo = struct with fields:
Iterations: 18
NumRandomRestarts: 0
PoseErrorNorm: 5.8508e-07
ExitFlag: 1
Status: 'success'
|
|
T_obtained = 4x4
-0.1296 -0.7894 0.6000 0.4000
-0.0972 -0.5921 -0.8000 0.3000
0.9868 -0.1620 0.0000 1.0000
0 0 0 1.0000
Path
Just as a demonstration in this paragraph will be showed how to perform a simple drawing of a triangle in the \(yz\) plane at \(x=0.5\) coordinate.
Path design
|
|
Inverse Kinematics for obtaining angles
|
|
Plotting
|
|