How to change a Quaternion Coordinate system

Hello all,

I’m trying to make a data logger for a motorcycle, currently I’m only interested in the longitudinal acceleration (Acceleration/Brake) and lean angle (Right/Left)

I’m using an Arduino 101 board for this.

Using the example “Arduino/Genuino 101 CurieIMU Orientation Visualiser” as base and from there I can easily obtain the lean angle (Pitch in the code) and adding some lines, the acceleration.

The issue is the following.

I can not assure a location in the motorcycle that allows me to place the board in such a way that the X axis is aligned with the motorcycle, and thus, the readings are not correct.

I have a code that “calibrates” the board position, once activated this code takes one reading of the gravity direction (I will call this G’ onwards) with the motorcycle completely vertical and one with the motorcycle at a certain angle to the left side ( I will call this L’ onwards").

With these two readings it creates a “rotation axis” (I shall call this X’ onwards) making the cross product of the readings and normalizing it.

X’ = G’ x L’ and then X’ = X’ / |X’|

Making another cross product between X’ and G’ I complete the coordinate system with Y’

Y’ = G’ x X’ and then Y’ = Y’ / | Y’|

With these vectors I can create the transformation matrix between the board axis and the motorcycle axis, the rows of this matrix are X’ , Y’ and G’ (normalized vectors all).

The data I am looking for to obtain are the acceleration and rotation along X’ axis.

What I’m stuck with is how to transform the readings from the board axis to the motorcycle axis.

I have been reading about quaternions, and come with the following.

If I have the IMU readings on board axis (Q0) and I have the transformation matrix as quaternion (QT), if I make the quaternion product QT x Q0 I will be applying the coordinate transformation to the rotation vector and thus, I can use Madgwick filter to obtain the rotation in X’.

This is build up in my mind, but I would like to confirm the quaternion maths, since it is a field I’m not acquainted with.

Thanks, Manuel.

As a general point of physics, I hope you do realize that the lean angle will not be directly measurable with an accelerometer unless the bike is stationary? Perhaps that will save you from worrying about quaternions.


Thanks for your reply, I'm counting on that, I'm using both the acceleration and gyro readings from the board IMU, using the longitudinal axis for accel/brake value and gyro readings for lean angle.

Madgwick filter uses accel/gyro readings of the board IMU to obtain the rotation quaternion and transforms it to the board axis rotations.

I'm just using the accelerometer reading of local gravity to create the motorcycle coordinate system.

Manuel1976: I'm just using the accelerometer reading of local gravity to create the motorcycle coordinate system.

That won't work. When a cycle is leaning into a turn, the effective center of gravity is still vertical with reference to the bike frame. So you can't use gravity to detect tilt.

I'm afraid I'm not explaining myself well.

With the motorcycle stationary, I fit the board to the vehicle wherever I can.

Then, with the motorcycle stationary I calibrate taking two readings:

1.- Motorcycle stationary and vertical.

2.- Motorcycle stationary and lean to the left.

Then I compute to obtain the transformation matrix and store it.

Since the board will not move relatively to the motorcycle, I have a coordinate system with the X' axis along the motorcycle longitudinal axis. The motorcycle acceleration/brake will be along this direction, and the motorcycle lean angle will be around this direction.

Now, once the motorcycle is in motion, the readings from the gyro on the IMU will provide me lean angles, although these will be in the board coordinate system.

I need to convert the readings to the coordinate system I created with the calibration to obtain the proper values.

The readings from the gyro will not provide you with lean angles. They will all read exactly vertical, as if the bike has no tilt. You are overlooking centripetal force. Trust me. I understand your approach perfectly. It will not work.

I see your point,

So, What array of sensors do you recommend for this?

And anyway, with any other sensor the issue will reappear, I can not assure a placement of them in such a manner that they will have one axis aligned with the motorcycle and I will end up with the need to perform the coordinate change.

This has been discussed on this forum before. The best answer, I think, was some kind of rangefinder to find the road surface on both sides. To my recollection, nobody thought of a compass, but the bike frame is going to distort the magnetic field.

I don't know anything about quaternions. Didn't Leibnitz and Newton disagree about them, or something? :)

Leibnitz and Newton independently invented the calculus.

Aarg is correct that accelerometers cannot be used to detect motorcycle lean angle, but gyros do read out the rotation [u]rate[/u], independent of any forces. I don't know whether consumer grade gyros are sensitive enough to detect the [u]lean rate[/u] of a motorcycle.

In any case it is necessary to combine sensor measurements (Kalman filters and the like) so that the [u]rate[/u] of rotation can be converted into an angle relative to some reference. Fortunately, software and hardware to make functional, high quality navigational sensors have recently become available to experimenters.

Rather than roll your own, you might experiment with the self-contained BNO055 absolute orientation sensor. It is good to about +/- 1 degree in absolute orientation when held still, and does allow one to subtract acceleration due to gravity to get an approximation to the so-called linear acceleration.

Another possibility is to use a state of the art AHRS, like RTImulib, together with one of the supported 9DOF sensors. I wrote about an inexpensive implementation of that package here.

If either method works in your case, let us know. It is simple to use either matrix operations, or quaternion math to correct for misalignment of the sensor orientation, so that it matches the motorcycle frame of reference.


Thanks for your suggestion, actually I have a working assembly of an Arduino mini pro 5V with a BNO055, but this is small enough to fit the sensor in an appropriate position (X axis along motorcycle longitudinal axis)

The lean values are compatible with the real world (aka the feeling I have riding the motorcycle) so "it works".

I was intending to use Arduino 101 because it makes the assembly simpler.

The current assembly consists of a 12V to 5V converter (to feed the Arduino mini from the motorcycle power), the BNO055 sensor, the Arduino itself and the data logger.

With the Arduino 101 converter, board and sensor are all in one, but it will be slightly larger and I have problems fitting it in the appropriate position, hence my question.

This might apply too the current assembly, easing the placement of the assembly.


I wonder about the gyro implementation. How is it done in the sensors, so that the angles depend on linear accelerations?

actually I have a working assembly of an Arduino mini pro 5V with a BNO055

In that case, you can get the quaternion describing your absolute orientation, or the Euler angles directly from the BNO055, and rotate it into the bike frame of reference. All you need to do is determine the appropriate transformation.

Rotation by quaternion math is described in this excellent overview.

Good Morning to all,

Sorry for the delay answering.

DrDiettrich, franky I'm not quite sure about the implementation, with the BNO055 system, the sensor is already fusioned as it comes and using library Naxismotion.h it provides the Roll, Pitch and Yaw angles along the sensor coordinate system. With the Arduino 101 I am using as base the Arduino/Genuino 101 CurieIMU Orientation Visualiser code, I have not get into the proper working of the Madgwick filter, the previous link provides this Report source.

travis_farmer, thanks for the suggestion, the only problem I see with that (at least with the tank bags I own) is that the system might move in the bag. For attachment I have been thinking of printing a 3D box and use GoPro style adhesive mounts in the frame for a strong placement in the motorcycle.

jremington, thanks, I'll take a long reading to the link you sent, I am looking forward to change from the mini-pro/BNO055 assembly to the Arduino 101 assembly to reduce parts, but it might be a nice try, the only thing is that I shall include a button to calibrate the current assembly, something I am counting to do with the in-board BLE of the Arduino 101.