We’re back! Another exciting post about robotic control theory, but don’t worry, it’s short and ends with simulation code. The subject of today’s post is handling singularities.
What is a singularity
This came up recently when I had build this beautiful controller for a simple two link arm that would occasionally go nuts. After looking at it for a while it became obvious this was happening whenever the elbow angle reached or got close to 0 or . Here’s an animation:
What’s going on here? Here’s what. The Jacobian has dropped rank and become singular (i.e. non-invertible), and when we try to calculate our mass matrix for operational space
the values explode in the inverse calculation. Dropping rank means that the rows of the Jacobian are no longer linearly independent, which means that the matrix can be rotated such that it gives a matrix with a row of zeros. This row of zeros is the degenerate direction, and the problems come from trying to send forces in that direction.
To determine when the Jacobian becomes singular its determinant can be examined; if the determinant of the matrix is zero, then it is singular. Looking the Jacobian for the end-effector:
When it can be that
, so the Jacobian becomes
which gives a determinant of
Similarly, when , where
and
, the Jacobian is
Calculating the determinant of this we get
Note that while in these cases the Jacobian is a square matrix in the event that it is not a square matrix, the determinant of can be found instead.
Fixing the problem
When a singularity is occurring it can be detected, but now it must be handled such that the controller behaves appropriately. This can be done by identifying the degenerate dimensions and setting the force in those directions to zero.
First the SVD decomposition of is found. To get the inverse of this matrix (i.e. to find
) from the returned
and
matrices is a matter of inverting the matrix
:
where is a diagonal matrix of singular values.
Because is diagonal it is very easy to find its inverse, which is calculated by taking the reciprocal of each of the diagonal elements.
Whenever the system approaches a singularity some of the values of will start to get very small, and when we take the reciprocal of them we start getting huge numbers, which is where the value explosion comes from. Instead of allowing this to happen, a check for approaching the singularity can be implemented, which then sets the singular values entries smaller than the threshold equal to zero, canceling out any forces that would be sent in that direction.
Here’s the code:
Mx_inv = np.dot(JEE, np.dot(np.linalg.inv(Mq), JEE.T)) if abs(np.linalg.det(np.dot(JEE,JEE.T))) > .005**2: # if we're not near a singularity Mx = np.linalg.inv(Mx_inv) else: # in the case that the robot is entering near singularity u,s,v = np.linalg.svd(Mx_inv) for i in range(len(s)): if s[i] < .005: s[i] = 0 else: s[i] = 1.0/float(s[i]) Mx = np.dot(v, np.dot(np.diag(s), u.T))
And here’s an animation of the controlled arm now that we’ve accounted for movement when near singular configurations:
As always, the code for this can be found up on my Github. The default is to run using a two link arm simulator written in Python. To run, simply download everything and run the run_this.py
file.
Everything is also included required to run the MapleSim arm simulator. To do this, go into the TwoLinkArm folder, and run python setup.py build_ext -i
. This should compile the arm simulation to a shared object library that Python can now access on your system. To use it, edit the run_this.py
file to import from TwoLinkArm/arm_python
to TwoLinkArm/arm
and you should be good to go!
More details on getting the MapleSim arm to run can be found in this post.
This is helpful for me QUT robotics course. I still cannot work out how to get singularity in MATLAB or even using Jacobian on paper.
Glad you found it useful! There’s a good example / workthrough of encountering singularity with a 2-link manipulator here: http://bit.ly/1NucGVL Hopefully that helps!
What about the robot is insufficient or redundant? The Jacobin is not invertible at the first place
We still use the same technique! In part 7 I show the same code controlling a 3 link arm. Basically when you break the matrix down using SVD we’re computing a pseudo-inverse, and so it’s robust to these situations. Some more info here: http://bit.ly/1iFCVzE
[…] we are now able to calculate the operational space inertia matrix. This code I’ve explicitly worked through before, and I’ll show it in the full code below, so I won’t go over it again […]
The jacobians for q1=0 and q1=pi and their determinants look confusing to me. for example in the upper left field of the jacobian for q1=0 you would have -L0*sin(q0) – L1*sin(q0) wich is equal to (-L0 – L1)*sin(q0) wich is equal to -(L0 + L1)*sin(q0) but you write -(L0 – L1)*sin(q0) . Also it’s given determinant only equals 0 for q0=0 and q0=pi but it should equal 0 for all q0. Might be a calculation mistake
Hello, thanks for the catch! You’re right it should be
(L0 + L1) * (-sin(q0))
, I’ve fixed it in the post. And you’re right, the determinant should be 0 for all q0, I suspect a calculation mistake.Hi! You mentioned the following for non-square Jacobians.
‘Note that while in these cases the Jacobian is a square matrix in the event that it is not a square matrix, the determinant of \textbf{J}(\textbf{q})\;\textbf{J}^T(\textbf{q}) can be found instead.’
Can you please refer me to a source which talks about this in detail?
Hello! Yup, a quick google search for “determinant of non square matrix” provides a bunch of links that talk about it. The last comment on this page is pretty good: https://math.stackexchange.com/questions/854180/determinant-of-a-non-square-matrix
Cheers,
One thing I’m not getting is that you set the singular values below a certain threshold to zero, but then the inverse would still be Infinity (at least numerically) right?
Ah, almost. I’m checking for values close to zero, and instead of calculating the inverse (s_inv=1/s) just setting s_inv=0.
Oh ok thanks, I figured that should be the case, but it was just a bit unclear from your writing:
‘..which then sets the singular values entries smaller than the threshold equal to zero, canceling out any forces that would be sent in that direction…”
Its written that the singular values are set to zero and not the inverse’s.
Thank you! Nice post!
Great posts. Wondering if you are still actively maintaining the code?
I followed the instructions in the github page and ran setup but keep getting errors. I suppose some packages are supposed to be installed. I did not find any documentations on the requirements.
Hello! I should go through and update these links, this code is not being actively maintained anymore but we have current (and greatly extended) code up at https://github.com/abr/abr_control
Please let me know if you run into any issues with that install, or examples (which are in the /docs/examples folder).