Hi, Roger:
Your data look pretty good. I plotted them on the surface of a unit sphere using Matlab, see attached.
The CHRobotics application note conveniently provides the definition for converting a quaternion to a rotation matrix. I had some free time this morning, so I cobbled together a few definitions from Richard's RTMath.cpp and the one in CHRobotics, to create a rotation matrix that should correspond to the Euler angles output by RTIMULib.
The matrix generated is quite dissimilar to the rotation matrix produced by your original code, and I assume that this is due to the differing order of application or sign convention for the rotations.
I have not yet figured out how to check whether the rotation matrix agrees with the actual orientation, as there is still some ambiguity on whether the rotation defined by the quaternion or its inverse is to be applied. Nevertheless, the code below may provide a starting point and I hope it is helpful.
I will keep thinking about it! And, after all this is working, one could add the appropriate relativistic corrections ![]()
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// globals, current pose
float qt[]={0.,0.,0.,0.}; //current body orientation quaternion
float euler[]={0.,0.,0.}; //current body orientation Euler angles
float R[3][3];
// normalize the quaternion to a unit four-vector
void normalize()
{
float length = sqrt(qt[0] * qt[0] + qt[1] * qt[1] +
qt[2] * qt[2] + qt[3] * qt[3]);
if ((length == 0) || (length == 1))
return;
qt[0] /= length;
qt[1] /= length;
qt[2] /= length;
qt[3] /= length;
}
// generate Euler angles from the current quaternion
void toEuler(void)
{
euler[0] = (atan2(2.0 * (qt[2] * qt[3] + qt[0] * qt[1]),
1 - 2.0 * (qt[1] * qt[1] + qt[2] * qt[2])));
euler[1] = (asin(2.0 * (qt[0] * qt[2] - qt[1] * qt[3])));
euler[2]= (atan2(2.0 * (qt[1] * qt[2] + qt[0] * qt[3]),
1 - 2.0 * (qt[2] * qt[2] + qt[3] * qt[3])));
}
// generate a quaternion from the current Euler angles
void fromEuler(void)
{
float cosX2 = cos(euler[0] / 2.0f);
float sinX2 = sin(euler[0] / 2.0f);
float cosY2 = cos(euler[1] / 2.0f);
float sinY2 = sin(euler[1] / 2.0f);
float cosZ2 = cos(euler[2] / 2.0f);
float sinZ2 = sin(euler[2] / 2.0f);
qt[0] = cosX2 * cosY2 * cosZ2 + sinX2 * sinY2 * sinZ2;
qt[1] = sinX2 * cosY2 * cosZ2 - cosX2 * sinY2 * sinZ2;
qt[2] = cosX2 * sinY2 * cosZ2 + sinX2 * cosY2 * sinZ2;
qt[3] = cosX2 * cosY2 * sinZ2 - sinX2 * sinY2 * cosZ2;
normalize();
}
// generate the rotation matrix corresponding to the current quaternion
// for definition of matrix see https://www.chrobotics.com/docs/AN-1006-UnderstandingQuaternions.pdf
void quaternion_to_R(void)
{
R[0][0] = qt[0]*qt[0] + qt[1]*qt[1] - qt[2]*qt[2] - qt[3]*qt[3];
R[0][1] = 2.0*(qt[1]*qt[2] - qt[0]*qt[3]);
R[0][2] = 2.0*(qt[1]*qt[3] + qt[0]*qt[2]);
R[1][0] = 2.0*(qt[1]*qt[2] + qt[0]*qt[3]);
R[1][1] = qt[0]*qt[0] - qt[1]*qt[1] + qt[2]*qt[2] - qt[3]*qt[3];
R[1][2] = 2.0*(qt[2]*qt[3] - qt[0]*qt[1]);
R[2][0] = 2.0*(qt[1]*qt[3] - qt[0]*qt[2]);
R[2][1] = 2.0*(qt[2]*qt[3] + qt[0]*qt[1]);
R[2][2] = qt[0]*qt[0] - qt[1]*qt[1] - qt[2]*qt[2] + qt[3]*qt[3];
}
int main()
{
float accel[3]={0.1,0,1.0};// data will come from the accelerometers
float gravity[3]={0,0,1.0};// always vertically downwards at g = 1.0
float rG[3],rA[3];
float mA[3];
// Angle of rotation will come from RTImuLib
float alpha= 0.1;// some aribitar values for testing
float beta= 0.2;// some aribitar values for testing
float theta = 0.3;// some aribitar values for testing
euler[0]=alpha;
euler[1]=beta;
euler[2]=theta;
printf("Euler angles: %5.1f %5.1f %5.1f\r\n",euler[0],euler[1],euler[2]);
fromEuler();
quaternion_to_R();
// print out the corresponding rotation matrix
printf ("QR\r\n %8.5f %8.5f %8.5f\r\n %8.5f %8.5f %8.5f\r\n %8.5f %8.5f %8.5f\r\n",
R[0][0],R[0][1],R[0][2],R[1][0],R[1][1],R[1][2],R[2][0],R[2][1],R[2][2]);
float det = R[0][0]*(R[1][1]*R[2][2]-R[1][2]*R[2][1])\
-R[0][1]*(R[1][0]*R[2][2]-R[1][2]*R[2][0])\
+R[0][2]*(R[1][0]*R[2][1]-R[1][1]*R[2][0]);
printf(" det(R) = %f\r\n",det); //for check, should be +1 exactly
rG[0]= gravity[0]*R[0][0] + gravity[1]*R[0][1] + gravity[2]*R[0][2] ;
rG[1]= gravity[0]*R[1][0] + gravity[1]*R[1][1] + gravity[2]*R[1][2] ;
rG[2]= gravity[0]*R[2][0] + gravity[1]*R[2][1] + gravity[2]*R[2][2] ;
rA[0]= accel[0]*R[0][0] + accel[1]*R[0][1] + accel[2]*R[0][2] ;
rA[1]= accel[0]*R[1][0] + accel[1]*R[1][1] + accel[2]*R[1][2] ;
rA[2]= accel[0]*R[2][0] + accel[1]*R[2][1] + accel[2]*R[2][2] ;
mA[0]=rA[0]-rG[0];
mA[1]=rA[1]-rG[1];
mA[2]=rA[2]-rG[2];
printf("\r\nA %8.5f %8.5f %8.5f\r\n",accel[0],accel[1],accel[2]);
printf("G %8.5f %8.5f %8.5f\r\n",gravity[0],gravity[1],gravity[2]);
printf("\r\nrA %8.5f %8.5f %8.5f\r\n",rA[0],rA[1],rA[2]);
printf("rG %8.5f %8.5f %8.5f\r\n",rG[0],rG[1],rG[2]);
printf("\r\nrA-rG in body frame %8.5f %8.5f %8.5f\r\n",mA[0],mA[1],mA[2]);
//rotate modified acceleration back into the Earth frame (use transpose of R)
rA[0]= mA[0]*R[0][0] + mA[1]*R[1][0] + mA[2]*R[2][0] ;
rA[1]= mA[0]*R[0][1] + mA[1]*R[1][1] + mA[2]*R[2][1] ;
rA[2]= mA[0]*R[0][2] + mA[1]*R[1][2] + mA[2]*R[2][2] ;
printf("A-G in Earth frame %8.5f %8.5f %8.5f\r\n",rA[0],rA[1],rA[2]);
return 0;
}
Program output
Euler angles: 0.1 0.2 0.3
QR
0.93629 -0.27510 0.21835
0.28963 0.95643 -0.03696
-0.19867 0.09784 0.97517
det(R) = 1.000000A 0.10000 0.00000 1.00000
G 0.00000 0.00000 1.00000rA 0.31198 -0.00799 0.95530
rG 0.21835 -0.03696 0.97517rA-rG in body frame 0.09363 0.02896 -0.01987
A-G in Earth frame 0.10000 0.00000 -0.00000Process returned 0 (0x0) execution time : -0.000 s



