Some great options offered. The code now uses the inbuilt functions to provide solutions and the multiplication has been corrected.
The solutions match those offered by James Tursa, David Goodmanson in his function and Bruno Luong.
What I am struggling to understand is why method 1 is not acceptable.
If I was using a single rotation and the target was 50deg pitch and the attempt was 70 deg pitch, the 'error' defined as the difference between the attempt and the target is 20 degrees, isn't it? However this is not the case when using rotation matrices, for example if pitchb = 70 and rollb = 50 and hdgb = 50. It is only the case if using pitchb = 50, rollb = 50 and hdgb = 70. I am afraid I do not understand this.
If I were to have the target orientation pitcha, rolla, hdga all 50 degrees and the attempt orientation to be pitchb= 50, rollb = 70 and hdgb =70, what is the error? According to my code the 'error' for XYZ sequence is -18.9deg pitch, -6.7deg roll, -21.1deg hdg. But the pitch angle was the same between the target and the attempt. Any help gratefully recieved.
clear all
hdga = deg2rad(50);
pitcha = deg2rad(50);
rolla = deg2rad(50);
hdgb = deg2rad(70);
pitchb = deg2rad(70);
rollb = deg2rad(70);
%% using quaternions
Ea = [pitcha rolla hdga];
QaXYZ = eul2quat(Ea,'XYZ');
Eb = [pitchb rollb hdgb];
QbXYZ = eul2quat(Eb,'XYZ');
QResXYZ = quatmultiply(QaXYZ,quatinv(QbXYZ));
if( QResXYZ(1) < 0 )
QResXYZ = -QResXYZ;
end
angle_quatXYZ = 2*acosd(QResXYZ(1))
Ea = [pitcha rolla hdga];
QaZYX = eul2quat(Ea,'ZYX');
Eb = [pitchb rollb hdgb];
QbZYX = eul2quat(Eb,'ZYX');
QResZYX = quatmultiply(QaZYX,quatinv(QbZYX));
if( QResZYX(1) < 0 )
QResZYX = -QResZYX;
end
angle_quatZYX = 2*acosd(QResZYX(1))
Ea = [pitcha rolla hdga];
QaZYZ = eul2quat(Ea,'ZYZ');
Eb = [pitchb rollb hdgb];
QbZYZ = eul2quat(Eb,'ZYZ');
QResZYZ = quatmultiply(QaZYZ,quatinv(QbZYZ));
if( QResZYZ(1) < 0 )
QResZYZ = -QResZYZ;
end
angle_quatZYZ = 2*acosd(QResZYZ(1))
%% Using rotation matrices
ErotmataXYZ = eul2rotm(Ea,'XYZ');
ErotmatbXYZ = eul2rotm(Eb,'XYZ');
ErotmatResXYZ= inv(ErotmatbXYZ) * ErotmataXYZ;
EResRotMatCalcXYZ = rotm2eul(ErotmatResXYZ,'XYZ');
EResRotMatCalcdegXYZ = rad2deg(EResRotMatCalcXYZ)
ErotmataZYX = eul2rotm(Ea,'ZYX');
ErotmatbZYX = eul2rotm(Eb,'ZYX');
ErotmatResZYX= inv(ErotmatbZYX) * ErotmataZYX;
EResRotMatCalcZYX = rotm2eul(ErotmatResZYX,'ZYX');
EResRotMatCalcdegZYX = rad2deg(EResRotMatCalcZYX)
ErotmataZYZ = eul2rotm(Ea,'ZYZ');
ErotmatbZYZ = eul2rotm(Eb,'ZYZ');
ErotmatResZYZ= inv(ErotmatbZYZ) * ErotmataZYZ;
EResRotMatCalcZYZ = rotm2eul(ErotmatResZYZ,'ZYZ');
EResRotMatCalcdegZYZ = rad2deg(EResRotMatCalcZYZ)