|
bitsim
Model, simulate, estimate and control robots in C++
|
This example accompanies the fourth lesson of the robotics course and builds on Forward kinematics. We use the same two arms, and ask the opposite question. You need bitsim configured with Chrono support to run it. The solutions of this example, analytic and numerical, are in bitsim too: planar_ik, spatial_ik, numerical_jacobian and numerical_ik in bitsim::kinematics.
The arm of section 6 moves its tip along the line from \(A\) to \(B\) and back, with the joint angles from inverse kinematics at every step. The thin grey arms are the other solutions for the same point.
Forward kinematics gives the end-effector pose from the joint angles, \(X_{WE} = f(q)\). A task, on the other hand, is usually given in the world: pick up the cup at this point, follow this line. Inverse kinematics answers the question
Given where the end-effector should be, what are the joint angles?
that is, it solves \(f(q) = X_{WE}\) for \(q\). In Forward kinematics we saw that several configurations can put the tip at the same point, and that some points cannot be reached at all. So unlike forward kinematics, inverse kinematics can have no solution, one, several or infinitely many.
In this lesson we only ask for the position of the end-effector, not its orientation. Our arms have two and three joints, which is exactly enough to place a point in the plane and in space.
For the planar arm the solution can be found with geometry. Shoulder, elbow and tip form a triangle with sides \(l_1\), \(l_2\) and \(\sqrt{x^2 + y^2}\), and the law of cosines gives the elbow angle:
\[\cos q_2 = \frac{x^2 + y^2 - l_1^2 - l_2^2}{2 \, l_1 l_2} \qquad q_2 = \pm \arccos(\cos q_2) \]
The two signs are the two elbow positions. The shoulder angle is the direction to the tip, minus the angle between the upper arm and that direction:
\[q_1 = \text{atan2}(y, x) - \text{atan2}(l_2 \sin q_2, \; l_1 + l_2 \cos q_2) \]
Always check an inverse kinematics solution with forward kinematics. It is cheap, and it catches most mistakes in the derivation:
The first target is the point of section 3 in Forward kinematics, and we get back both configurations we found there.
The value of \(\cos q_2\) tells us how many solutions there are:
If the two links had the same length, the inner circle would shrink to a point at the shoulder. Every \(q_1\) would put the tip there: infinitely many solutions.
The spatial arm splits into two problems. Joint 1 turns the vertical plane of the arm towards the target, and in that plane the shoulder and the elbow are the planar arm, with the reach \(r = \sqrt{x^2 + y^2}\) and the height \(z - h_0\) above the shoulder:
\[q_1 = \text{atan2}(y, x) \qquad (q_2, q_3) = \text{planar IK}(r, \; z - h_0) \]
The arm can also turn its back to the target, \(q_1 + 180^\circ\), and reach over its head with a negative reach \(-r\). That gives up to four solutions:
The target is the end-effector position of section 4 in Forward kinematics, so one of the solutions is the \((90, 30, -60)\) we started from there. That is the elbow-up solution facing the target, which is usually the one we want. The others may be out of the range of the joints, or make the arm hit the floor or itself. Choosing between solutions is part of the job.
When the target is right above joint 1, \(x = y = 0\), atan2(y, x) is not defined. Every \(q_1\) works, and that is another case with infinitely many solutions.
Closed-form solutions exist only for arms with a simple structure. For the general case we need the Jacobian, the matrix of the partial derivatives of the forward kinematics. It maps the speed of the joints to the speed of the end-effector:
\[\dot{p} = J(q) \, \dot{q} \qquad J = \frac{\partial f}{\partial q} \]
For the planar arm we can differentiate the formula of the previous lesson:
\[J = \begin{bmatrix} -l_1 \sin q_1 - l_2 \sin(q_1 + q_2) & -l_2 \sin(q_1 + q_2) \\ l_1 \cos q_1 + l_2 \cos(q_1 + q_2) & l_2 \cos(q_1 + q_2) \end{bmatrix} \]
Column \(j\) is the velocity of the tip when only joint \(j\) turns, at 1 rad/s. When we do not have a formula we can still compute the Jacobian numerically, by moving each joint a little and differencing the forward kinematics. The two agree:
Going the other way, \(\dot{q} = J^{-1} \dot{p}\) gives the joint speeds for a desired tip speed. The table asks the tip to move away from the shoulder at 0.1 m/s as the arm gets straighter. For this arm \(\det J = l_1 l_2 \sin q_2\), and as the arm stretches it goes to zero and the joint speeds explode.
A configuration where \(\det J = 0\) is a singularity. There the arm cannot move its tip in some direction at all, here straight away from the shoulder, however fast the joints turn. Singularities are found at the boundary of the workspace, but also inside it, as for the spatial arm with the tip above joint 1. Near one, small tip motions need huge joint motions.
With the Jacobian we can solve inverse kinematics iteratively, for any arm whose forward kinematics we can evaluate. Starting from a guess \(q\), the error is \(e = p_{\text{target}} - f(q)\) and the linear approximation \(f(q + \Delta q) \approx f(q) + J \Delta q\) suggests the step
\[\Delta q = J^{-1} e \]
This is Newton's method. Repeat until the error is small. Near a singularity \(J^{-1}\) is huge, so damped least squares adds a damping term \(\lambda\):
\[\Delta q = J^T (J J^T + \lambda^2 I)^{-1} e \]
For \(\lambda = 0\) this is Newton's step again. With \(\lambda > 0\) the step stays bounded even when \(J\) is singular. Both versions also limit the size of each step to 0.5 rad, since the linear approximation is only good close to \(q\).
Both reach the target of section 3 from the start \((0, 45, -45)\). Newton's method converges much faster at the end: the number of correct digits roughly doubles with every iteration. Damping trades that speed for robustness.
The difference shows when the target is out of reach. The best the arm can do is to stretch towards the target, and that is a singular configuration. Newton's method keeps jumping from one side of it to the other and never settles. Damped least squares settles in the stretched pose, \(q_3 = 0\), and its error is the smallest possible. This matters in practice: a target slightly out of reach should not make the arm shake.
Notice also that the numerical methods find one solution, the one their starting point leads to. Start from a different guess and you may get another. In a controller the previous joint angles are a good starting point, and then a single iteration is often enough.
Now we use inverse kinematics to make the Chrono arm of Forward kinematics draw a straight line in space. At every step we compute the next point on the line, solve for the elbow-up joint angles facing it, and give them to the motors through a ChFunctionSetpoint:
The tip goes from \((0.5, -0.2, 0.2)\) to \((0.2, 0.5, 0.5)\) in 2 s, starting and stopping smoothly. Before the clock starts, the arm is moved to the start of the line and DoAssembly puts the bodies there. We then measure how far the simulated tip is from the line:
The joints move in a complicated way, joint 1 alone turns by 90 degrees, but the tip stays on the line. Moving each joint smoothly from its start angle to its end angle would also get the tip from \(A\) to \(B\), but along a curve. To control the path in the world we need inverse kinematics.
The motors here are ideal: they put each joint exactly where we ask, whatever the load. Real motors need a current to make a torque, and they take time to respond. That is the topic of A brushless DC motor.
With --render the arm of section 6 goes from \(A\) to \(B\) along the yellow line, waits, and comes back, again and again. The orange trail is the path of the simulated tip, and it stays on the line. --record DIR writes the window to DIR, ten pictures a second, for one trip there and back, and stops.
The thin grey arms are the other solutions of section 3 for the same target, drawn from shoulder to elbow to tip. The panel says how many there are. Some of them go through the floor or through the base, which is why the elbow-up solution facing the target is the one the arm uses.