 # Having trouble applying quaternions correctly to a live simulation of a golf club using IMU data

I’ve got an IMU mounted into a golf club with axes as shown below:

I’ve got an STL file as well that has the same coordinate system as in the image.

My IMU code is sending quaternion data for the rotation of the club to the browser and I’m trying to get the club to rotate the correct way with the real club.

I’m having problems where the club isn’t rotating on the proper axes and also doesn’t start out at the position I want it to.

So for example, if I leave the club with the y axis pointing to the ground (club laying flat) I want that to be shown in three.js with the club laying flat. If I lift it and stand it up vertically, I want three.js to rotate it to the correct vertical position. If I start three.js and my IMU from the beginning and now I start it vertically, I want the club to start at the correct position.

To do this, I figured I’d use the accelerometer at the beginning when the club is not being touched or moved. I’d have a vector Ax, Ay, Az which I normalize to the direction of gravity. I then feed that in at every loop to rotate the club to that direction before I apply the rotation quaternion. The rotation quaternion is applied like this because it’s the quaternion that rotates FROM the initial position when the IMU was started to the current position. If I don’t rotate the club back to it’s initial position before rotating my the quaternion, it starts to go crazy and rotate faster and faster.

So my problem is that the axes aren’t lining up properly and I’m not sure where the problem lies.

Here’s the relevant code:

Animation function

``````function animate() {

requestAnimationFrame(animate)

controls.update()

currentTime = Date.now()

const dt = currentTime - prevTime

//console.log(dt)

if (dt > 5) {

const quaternion = new THREE.Quaternion(

currentQuat,

currentQuat,

currentQuat,

currentQuat,

)

club.lookAt(initQuat.x, initQuat.y, initQuat.z)

wireframe.lookAt(initQuat.x, initQuat.y, initQuat.z)

quaternion.normalize()

club.applyQuaternion(quaternion)

wireframe.applyQuaternion(quaternion)

i += 1

prevTime = currentTime

}

renderer.render(scene, camera)

}

animate()
``````

Here is where the quaternion get’s set from my websocket message:

``````   socket.addEventListener('message', function (event) {

const msg = event.data.split(',')

currentAccel = msg

currentAccel = msg

currentAccel = msg

currentQuat = msg

currentQuat = msg

currentQuat = msg

currentQuat = msg

initQuat.x = msg

initQuat.y = msg

initQuat.z = msg

initQuat.normalize()

console.log(initQuat)

})
``````

Here’s some C++ for where I’m converting the gyro readings to q_body. q_body is the quaternion that I send to three.js to rotate the object:

``````omega = gyro * DEG_TO_RAD;

q_body_mag = sqrt(sq(omega) + sq(omega) + sq(omega));
gyro_dt = ((gyro_current_time - gyro_past_time) / 1000000.0);

theta = q_body_mag * gyro_dt;
q_gyro = cos(theta / 2);
q_gyro = -(omega / q_body_mag * sin(theta / 2));
q_gyro = -(omega / q_body_mag * sin(theta / 2));
q_gyro = -(omega / q_body_mag * sin(theta / 2));

q = q_body;
q = q_body;
q = q_body;
q = q_body;

q_body = q_gyro * q - q_gyro * q - q_gyro * q - q_gyro * q;
q_body = q_gyro * q + q_gyro * q + q_gyro * q - q_gyro * q;
q_body = q_gyro * q - q_gyro * q + q_gyro * q + q_gyro * q;
q_body = q_gyro * q + q_gyro * q - q_gyro * q + q_gyro * q;
``````

Here’s some extra code for how I’m getting the acceleration vector initially. I take a bunch of readings and take the average of them. I apply this to q_body and also these are the Ax,Ay,Az values I send as the initial orientation:

``````void normalizeQuat(float *quat) {
float magnitude = sqrt(quat * quat + quat * quat + quat * quat + quat * quat);
quat /= magnitude;
quat /= magnitude;
quat /= magnitude;
}

void getInitialQBody() {
float accelMean = {0, 0, 0};
for (int i = 0; i < 200; i += 1) {
getIMU();
accelMean += accel / 200.0f;
accelMean += accel / 200.0f;
accelMean += accel / 200.0f;
delay(10);
}

gravQuat = 0;
gravQuat = accelMean;
gravQuat = accelMean;
gravQuat = accelMean;
normalizeQuat(gravQuat);
q_body = gravQuat;
q_body = gravQuat;
q_body = gravQuat;
q_body = gravQuat;
}``````

Sounds a bit like you’re trying to apply velocity in place where you should be applying acceleration?

Oh no, I’m not applying velocity or acceleration anywhere. I’m applying a rotation.

Well, angular velocity is still a velocity. But regardless, can you describe the issue a bit shorter / share a video or a demo of it? It’s a bit hard to tell from the long post what exactly the issue is.

I don’t really get how you are getting your quaternion, but it’s possible that your gyro does not use the same coordinate system than three.js. Three.js use a right-handed system, if your gyro use a left-handed system I guess you would get issues similar to what you mentioned.

Also a lot of CAD tools are Z-up, whereas three.js is Z-forward

1 Like