# quat2angle / atan2 strange quadrant change when calculating angle

8 ビュー (過去 30 日間)
Matteo Musso 2024 年 2 月 22 日
コメント済み: Matteo Musso 2024 年 3 月 5 日
Hi,
I'm working with IMU systems and calculating rotation angles from quaternions using the quat2angle function. I encountered a problem I believe in the atan2 function. As you can see in the first figure below the computed angle becomes negative.
In Fig1, you can see three lines:
• In blu, the angle was computed using the quat2angle function with the rotation sequence 'YZX', (it is the first output).
• In orange, the angle was computed with an Android app (Dart language).
• In dashed yellow, the angle was computed with my Dart algorithm translated into Matlab code.
I think the problem is in the atan2 function because it is used to compute the angle in my algorithm.
This behavior doesn't always happen. I have recordings where the angle computation is fine, like here:
Do you know what the reason is, and do you have a solution for it?
I have attached the files containing the quaternions of the two recordings
##### 2 件のコメントなしを表示なしを非表示
David Goodmanson 2024 年 2 月 23 日
Hi Matteo,
How do you know that your dart calculation in Matlab is the one that's incorrect? It always agrees with quat2angle so if anything the data seems to imply that the dart calculation in red is the one with the problem..
Matteo Musso 2024 年 2 月 23 日

Hi David,
This is because the angle should be positive. I'm using the IMU system to measure the shoulder flexion angle. The movement of the 4 groups of peaks is always the same, with the subject facing a different direction. Repetitions within a group are performed in the same direction.
So, it doesn't make sense that the angle turns to a negative value.
I first found this problem with the quat2angle function and then in atan2. I use the tablet to record the data from the IMU system where I can check in real time the shoulder angles. I then postprocessing the data with Matlab where i recompute the angles with quat2angle. I did the dart calculation in Matlab only after founding the discrepancy between the real-time values and the quat2angle results.

サインインしてコメントする。

### 回答 (1 件)

James Tursa 2024 年 3 月 4 日
You don't show us your code, but in general I would note that angle changes like this are sometimes the result of whole quaternion sign flips. I.e., q and -q represent the same orientation, but depending on the algorithm you use when you extract angles you can get different sign flipped results. In these cases, you can sometimes fix things by ensuring that the largest magnitude element of the quaternion doesn't change sign from one sample to the next. That is, at each step beyond the first step, find the largest magnitude element and compare it to the same element in the previous quaternion. If the sign has changed, then flip the signs of all the elements of the current quaternion. Do this one step at a time to your entire stream.
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Matteo Musso 2024 年 3 月 5 日
Hi James,
I used this formula for the blu line: [flex, abd, rot]=quat2angle(Should_quat,'YZX');
and this is the dart function translated into matlab:
function euler_angles = quat2EulerYZX(q)
% Extract quaternion components
w = q(1);
x = q(2);
y = q(3);
z = q(4);
% Pre-compute repeated values
xx = x * x;
xy = x * y;
xz = x * z;
xw = x * w;
yy = y * y;
yz = y * z;
yw = y * w;
zz = z * z;
zw = z * w;
% Compute rotation matrix elements
r00 = 1 - 2 * (yy + zz);
r01 = 2 * (xy - zw);
r02 = 2 * (xz + yw);
r10 = 2 * (xy + zw);
r11 = 1 - 2 * (xx + zz);
r12 = 2 * (yz - xw);
r20 = 2 * (xz - yw);
r21 = 2 * (yz + xw);
r22 = 1 - 2 * (xx + yy);
% Euler angles extraction
flex = -atan2(-r20, r00);
abd = asin(r10);
rot = atan2(-r12, r11);
end
I tried what you suggested but the results are the same.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeAxes Transformations についてさらに検索

R2023a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by