News for hackers. Stuff that matters.
Re:Rotations with quaternions by neonological on Tuesday June 01, 2021 @01:47PM (#27358456)
Guys I work at a company that uses Quaternions for rotations of physical objects. PTUs we call them (Pan Tilt Units).

I am telling you Quaternions have HUGE issues. These issues become much more apparent when you deal with physical objects.

Here's the thing Quaternions don't exist in reality. It represents an orientation of rotation but it completely masks the path took to achieve that orientation.

For every gimbal in reality there is an actual YawPitchRoll (YPR) that was executed to achieve that orientation. AS soon as you convert that real YPR into a Quaternion you lose the YPR that was needed to achieve that orienation.

So let's say I need to have one gimbal imitate the position of another gimbal. I take the YPR given to me by gimbal "A" convert the YPR to a Quat, send that Quat over the wire to Gimbal "B" and convert that Quat back to YPR to feed to the gimbal so it can rotate itself to imitate the orientation of gimbal A.

The quat is a higher entropy form of information. Now when converting back to YPR there are MULTIPLE YPRs that yield the same orientation. You can derive a YPR that is out of bounds of the physical gimbal.

Literally you can get a YPR that tells your gimbal to Yaw 190 and pitch all the way back past 90 to 170 degrees and roll 180 degrees until it's right side up. This YPR is identical to a yaw of 10, a pitch of 20 and 0 roll. Quaternions hide the original YPR, you lose information so when you receive a Quaternion it's hard to translate it into a physical realization of the orientation.

The company I work for doesn't realize this. They used Quaternions from day one and we have all kinds of headaches like this when we try to extract the YPR and use these orientations in the real world. Actually I should say only I have these headaches. A lot of people haven't figured out this problem yet.

The only time you should use Quats are if you need to transform an orientation or you're dealing with virtual objects that have no rotational limits. Everybody thinks quats are magic and better. They are not. They have huge downsides. Huge.

Re:Rotations with quaternions by xaedes on Wednesday June 02, 2021 @05:10PM (#27374064)
If you have actual, possibly motorized, physical rotation axes to keep track of, then of course you need to keep track of the actual angles of each separate rotation, somewhere.

Compressing it all into one orientation may work for some use cases, but generally one should not expect that. Similarily one wouldn't try to represent all of the axes of a KuKa arm robot with just one orientation. You need info about the individual axes when you want to control it.

Your company can still use quaternions to represent the rotations of each axis, tho. Might help in convincing them going forward, as they don't have to let go of them (they are good fellas actually).

Re:Rotations with quaternions by neonological on Wednesday June 02, 2021 @06:03PM (#27374583)
>Your company can still use quaternions to represent the rotations of each axis

I don't understand this can you elaborate? My company does indeed use motorized pan tilt units.

Re:Rotations with quaternions by xaedes on Thursday June 03, 2021 @08:47AM (#27380442)
I meant when you want to compute the final orientation (lets ignore position): Each axis results in some orientation change which can be expressed as a quaternion. The final orientation can then be computed by combining the quaternions into one quaternion. This is very similar to the computation of forward kinematics (https://en.wikipedia.org/wiki/Forward_kinematics) for robots, which just have additional translations for the transformations.

As said before you still want to retain the actual angles somewhere.

Depending on your physical configuration one of the Euler angle variants (Tait–Bryan angles is another term to search for) could perfectly describe your case and you could just use these to store the angles. Euler angles can also be converted to quaternion. But you can't recover the original used angles from the quaternion alone because of the 2pi wrapping of angles.

For others still wondering why quaternion or orientation alone won't suffice, here is a different example: Imagine an axis which can rotate more than only one revolution, i.e. can have angles like 4 pi, for example motorized volume knobs. An orientation alone can't represent that as it wraps the angles to 2pi. User manually turns the knob to two full revolutions (4pi) and then increases the value by remote control, which results in motorized knob to rotate. Should the knob turn back to nearly zero revolutions (rotate back 4pi) plus the increase? No, it should rotate to 4pi+increase.

By the way, when you have internally stored the "multi revolution" angle and have a sensor reading in range [0..2pi[ you can recover an multi revolution angle equivalent to the sensor reading with wrapToPiSeq( angle in radians before, sensor reading).

  # to equivalent angle in [0,2pi[ range
  def wrapTo2Pi(rad): 
    return rad % (2*pi)

  # to equivalent angle in ]-pi,pi] range
  def wrapToPi(rad):
    return wrapTo2Pi(rad + pi) - pi
  
  # angle difference between rad0 and rad1, in range ]-pi,pi]
  def angleDiff(rad0, rad1):
    r0 = wrapToPi(rad0)
    r1 = wrapToPi(rad1)
    return wrapToPi(r1-r0)
  
  # rad1 to equivalent angle so the jump from rad0won't be greater than |PI|
  def wrapToPiSeq(rad0, rad1):
    r0 = wrapToPi(rad0)
    r1 = wrapToPi(rad1)
    diff = wrapToPi(r1-r0)
    return rad0+diff
Re:Rotations with quaternions by neonological on Thursday June 03, 2021 @11:37AM (#27382946)
I get what what your saying here. So use 3 quaternions to represent the orientation rather then 1. Right?

But doesn't that magnify the problem by 3x? Extracting the angle from the quat again can yield multiple possibilities. If you have 3 quats you now have 3x more possibilities. This can only be done if you assume certain restrictions for each quat such that they yield a singular axis angle when you do a back conversion.

Additionally doesn't that technique yield more possibility to encode axises that are incorrect? A quaternion representing the x axis can be accidentally encoded with rotations along other axises. It's better to keep the type of your domain restricted to be able to encode only possible answers.

Still this can be done as a valid-ish workaround. I give you credit for that, I wouldn't of thought of this so thanks for ur explanation. Although I might use this idea it is far from ideal imo because of the problems I mentioned above. That is if I interpreted what you're saying correctly?

Also addressing your example, the bigger problem in my mind is that there are still issues that arise even if the physical gimbal is restricted on all axises to (0, 2pi). Any gimbal that can go 4pi likely can go infinite pi and that knob will likely be the same so losing rotational information greater than 2pi is ok for most cases in my mind. (If the system yawed 790pi, users are usually only interested in some value under 2pi). The insidious thing imo, is that information is lost even in (0, 2pi) and a lot of people don't realize this.

Re:Rotations with quaternions by xaedes on Thursday June 03, 2021 @01:07PM (#27384252)
Noooo, I didn't clarify enough.

Represent your axes with the actual angles. These probably correspond to motor position or revolutions.

Use the quaternions only when you want an orientation for these angles.

Maybe you want to know in which direction the PTU is pointing for a particular set of motor positions, or axis angles. Compute the kinematic chain by multiplying the quaternions corresponding to each axis in the order they are physically applied, but multiply from right to left. Your final result is one quaternion representing the direction the PTU is pointing at. (You could also use 3x3 matrices or other representations) Depending on your physical configuration one of the Euler angle variants (Tait–Bryan angles is another term to search for) could perfectly describe your case and you could just use these to store the angles and compute the orientation.

If you don't actually need the final orientation, then you can omit the quaternions altogether.

If you have orientations as input and need to control the motors so the PTUs are pointing in the required direction: I would compute the current orientation of the PTU. Then compute a trajectory of quaternions interpolating from the current orientation to the target orientation (use quaternion slerp for interpolation). Then at each timestep you compute the required motor positions using inverse kinematics [1].

It is a common problem that multiple motor positions are possible and actually an unfinished research problem. Now that I wrote this, I think this might be the problem you are encountering.

For PTU I think it would be ok to try to recover the current target angles from the target quaternions of the trajectory using one of the Euler configurations. There are papers [2] listing all together, so one can try which is correct. Having a configuration chosen there is still the problem of multiple solutions. In this case use the one closest to the actual current angles. For cases when all angles are possible for an axis, use the current angle as target (i.e. motor doesn't change). When you then have an target angle use `wrapToPiSeq` to get an equivalent angle close the current actual angle as input for the motor controller.

[1] https://en.wikipedia.org/wiki/Inverse_kinematics In computer animation and robotics, inverse kinematics is the mathematical process of calculating the variable joint parameters needed to place the end of a kinematic chain, such as a robot manipulator or animation character's skeleton, in a given position and orientation relative to the start of the chain.

[2] In the past I used this (but be careful in which direction they apply the transformations, I stumbled over this): Diebel, J. (2006). Representing attitude: Euler angles, unit quaternions, and rotation vectors. Matrix, 58(15-16), 1-35. https://www.astro.rug.nl/software/kapteyn/_downloads/fa29752...

Re:Rotations with quaternions by xaedes on Thursday June 03, 2021 @01:23PM (#27384500)
When switching target orientation while already approaching one I would use quadratic bezier splines to get a smooth switch (https://ibiblio.org/e-notes/Splines/bezier.html). These just use linear interpolation, multiple times. In the case of quaternions replace the linear interpolations with quaternion slerps and you get quadratic bezier splines over orientations.
Re:Rotations with quaternions by neonological on Thursday June 03, 2021 @01:52PM (#27384942)
I'm not sure how we do it in this case. I know we already follow a trapezoidal velocity profile when approaching a target, but mid switch to another target I'm not sure what we're using. Thanks for this, I will look into it.
Re:Rotations with quaternions by neonological on Thursday June 03, 2021 @01:50PM (#27384908)
> In this case use the one closest to the actual current angles. For cases when all angles are possible for an axis, use the current angle as target (i.e. motor doesn't change). When you then have an target angle use `wrapToPiSeq` to get an equivalent angle close the current actual angle as input for the motor controller.

Yeah that's how I solved this issue. But still if we avoided quaternions we wouldn't have this problem all together, which is my point.

Specifically what's going on is that we're sending quaternion values over the network and the person on the receiving end needs YPR so we're basically like wtf, there's no transformations being performed on the quat, the source of info is a YPR and the output is needed is the same exact YPR so we're only converting to a company wide Quat Protobuf type to send over the wire. I submitted a request to make a new protobuf type that included YPR but I was met with huge company resistance from other engineers saying that a YPR was redundant to a Quat (It's not).

>In computer animation and robotics, inverse kinematics is the mathematical process of calculating the variable joint parameters needed to place the end of a kinematic chain, such as a robot manipulator or animation character's skeleton, in a given position and orientation relative to the start of the chain.

Interesting, but yeah the application in my company is just a single gimbal so there's no chain of "joints" here. I don't think this would apply to our my specific case.

>https://www.astro.rug.nl/software/kapteyn/_downloads/fa29752...

Thanks for all your input. I'll take a look at the link above, it looks interesting.

Re:Rotations with quaternions by phkahler on Tuesday June 01, 2021 @05:03PM (#27361255)
>> For every gimbal in reality there is an actual YawPitchRoll (YPR) that was executed to achieve that orientation. AS soon as you convert that real YPR into a Quaternion you lose the YPR that was needed to achieve that orienation.

I would say you obscure it. You can certainly calculate it from the 4 quaternion parameters.

SolveSpace (Free CAD software) can be used to design assemblies and mechanisms from a set of parts with constraints. You can certainly build a gimbal with it by constraining the pieces. If you do it correctly, it will be possible to re-orient the final 3DoF part and the constraint solver will solve for the angles (assuming you built it that way).

Internally we treat all object orientations as quaternions, so this would just be using the algebraic constraint solver to find the angles. In practice there will be closed form solutions - with problems at gimbal lock.

Re:Rotations with quaternions by neonological on Tuesday June 01, 2021 @06:57PM (#27362533)
>I would say you obscure it. You can certainly calculate it from the 4 quaternion parameters.

No it is an actual information loss.

There are multiple valid YPR solutions like my example illustrated.

There is NO way to determine which YPR out of the multiple possibilities was the original solution.

Something like solve space can only determine the original YPR with additional assumptions or the original YPR still referenced in memory.

Re:Rotations with quaternions by phkahler on Wednesday June 02, 2021 @09:53AM (#27368688)
Nope. The only thing you need to specify is the order the YPR angles are applied (that more a convention than an assumption). In SolveSpace the assembly constraints would effectively encode the order of application.

A quick Google search even turns up a Wikipedia entry on this problem: https://en.wikipedia.org/wiki/Conversion_between_quaternions...

If you really need this problem solve I might know someone willing to do paid consulting on it.

Re:Rotations with quaternions by neonological on Wednesday June 02, 2021 @10:36AM (#27369182)
>If you really need this problem solve I might know someone willing to do paid consulting on it.

If you really need education in math and how to properly do trigonometry I will help you solve this problem for free. Just ask me questions. I'm super nice and won't go around fraudulently espousing an expertise in math and demanding people pay me to solve trivial math problems.

Frankly you are not even qualified to solve the problem yourself or give me a recommendation.

Especially when this problem is basically impossible to solve. I'll give you 10 thousand dollars if you can give me a quaternion that doesn't have multiple yprs here on HN. Literally, give me your venmo.

>Nope. The only thing you need to specify is the order the YPR angles are applied (that more a convention than an assumption). In SolveSpace the assembly constraints would effectively encode the order of application.

The term "yaw pitch roll" ALREADY has the order of the Euler angles applied. Let me tell you that order, it's: yaw, pitch and then roll.

Ypr is different from straight up Euler angles in that ypr has order fixed; hence the term "ypr"

With the order of angles fixed you still get multiple answers for a single quaternion. Why don't you try it in 3D space in your own head. Given A rotational orientation in 3D, find at least two yprs needed to arrive there.

Here maybe this will help you: a ypr of (0,0,0) is the same as a ypr of (180, 180, 180). These two yprs can only be represented by a single quat. Think about it. Given only a quat you cannot determine which ypr was used by the physical gimbal to realize this orientation. For solve space to know it must be making assumptions or holding onto information outside of the quat.

There free education for you and I didn't even ask you for a dime.

Re:Rotations with quaternions by neonological on Wednesday June 02, 2021 @10:17AM (#27368965)
No, no, no. Wrong again. Please study basic trigonometry and read your own sources. Your own link proves you wrong.

The formula for conversion from quat to ypr involves arctan and arcsin. These functions yield multiple answers.

Additionally your own Wikipedia link explicitly states the existence of multiple answers, and that traditionally atan and asin in programming languages yield only one answer. I quote:

"Note, however, that the arctan and arcsin functions implemented in computer languages only produce results between −π/2 and π/2, and for three rotations between −π/2 and π/2 one does not obtain all possible orientations. To generate all the orientations one needs to replace the arctan functions in computer code by atan2"

Either way you misunderstand the math behind quaternions and you lack a basic grasp of trigonometry. Assuming you read your own Wikipedia link, what I said is categorically true.

Re:Rotations with quaternions by edflsafoiewq on Tuesday June 01, 2021 @06:21PM (#27362204)
Interesting to read about experience with a physical gimble, thanks. In this case the "problems" of Euler angles are actually an accurate model of the problem space.
Re:Rotations with quaternions by neonological on Wednesday June 02, 2021 @11:07AM (#27369580)
You can design a hardware ptu that when given a quat it figures out its own ypr to arrive at that orientation. That would make quats feasible for hardware.

However, how the Ypr was picked by the hardware must be explicitly encoded as an assumption that must be part of every conversion from quat to ypr that happens downstream.

Re:Rotations with quaternions by dakr on Tuesday June 01, 2021 @04:01PM (#27360478)
This real hardware handles quaternions just fine: https://en.wikipedia.org/wiki/Stratospheric_Observatory_for_...
Re:Rotations with quaternions by neonological on Tuesday June 01, 2021 @04:49PM (#27361073)
I'm sure it does, I'll give you the benefit of the doubt even though the article makes no mention of Quaternions. My point is, using Quaternions for physical devices is using a hammer on a screw. Huge mistake, but it can be done by people who don't know any better. I'm guessing you worked on this and bought in to the whole Quaternion BS?

I'm in the defense industry as well and guess what? Basically most people don't know any better.