Transformation of gyroscope axis

I'm using the Gy-25 gyroscope sensor for the angular calculation of a rotating link. The link has 2 DOF, one DOF in the plane, and the other one is in the spatial. The problem is that the reference frame of the sensor is global, and when I rotate the sensor in the spatial (first spatial rotation and then rotation in that plane), all the roll, pitch, and yaw angles change. I want to transform the global angles to the local frame of the sensor, so if I rotate the link in the space, only one of the angles change. Can anybody help me with that?
By the way, the sensor output is an angle, not acceleration. Thanks in advance

The GY-25 uses the MPU-6050 IMU but cannot be reprogrammed.

I suggest to buy an MPU-6050 sensor module and use it independently, with any Arduino. The modules are cheap, and there are lots of tutorials and code example on line.

Here is the simplest tilt and roll application that works correctly with standard Euler angles:

// minimal MPU-6050 tilt and roll (sjr)
// works perfectly with GY-521, pitch and roll signs agree with arrows on sensor module 7/2019
// tested with eBay Pro Mini, **no external pullups on SDA and SCL** (works with internal pullups!)
const int MPU_addr1 = 0x68;
float xa, ya, za, roll, pitch;

void setup() {

  Wire.begin();                                      //begin the wire communication
  Wire.beginTransmission(MPU_addr1);                 //begin, send the slave adress (in this case 68)
  Wire.write(0x6B);                                  //make the reset (place a 0 into the 6B register)
  Wire.endTransmission(true);                        //end the transmission

void loop() {

  Wire.requestFrom(MPU_addr1, 6, true); //get six bytes accelerometer data

  xa = << 8 |;
  ya = << 8 |;
  za = << 8 |;

//formula from

  roll = atan2(ya , za) * 180.0 / PI;
  pitch = atan2(-xa , sqrt(ya * ya + za * za)) * 180.0 / PI;

  Serial.print("roll = ");
  Serial.print(", pitch = ");

Let me explain my problem. I attached the schematic of my system. I’m using both gy-25 and gy-521 for calculation of the angle. As shown in the picture, joint 1 has 2 DOF, one for spatial movements, and the second one for planar rotation. The angles of the planar movement of joint 1, measures by an encoder. Each of joint 2 and 3 has 1 DOF, and the angles are measured by gy-521 and gy-25. I want to measure the angular rotation around the first joint’s spatial axis and planar rotation of joint 2 with gy-25. The planar rotation of joint 3 measures by gy-521.
If I rotate the first joint around its spatial axis or rotate the joint 2 in the plane, the values of the roll, pitch, and yaw of both gyroscopes change which means that the rotation is a combination rotation around multiple axes because the gyroscope reference frame is global, but the rotation isn’t around one of that reference frame’s axes. So I want to transform the angles which are in the global frame to be measured in the local frame of the sensor, but I don’t know how to do that.

Your figure implies that this is not a simple transformation, but rather an Inverse Kinematics problem.

For completeness, to transform angles from one reference frame to another, you first need to specify the exact angular systems used. For example, there are 12 different Euler angle systems, distinguished by the order that the rotations are applied and the definition of a positive rotation angle.

If you can represent a transformation as a 3D rotation matrix, then you need only apply the transformation matrix that converts the reference frame into the object frame.

Thanks for your answer.
I'm familiar with the Inverse kinematic problem and transformation matrix.
If I consider the global reference frame in the first joint and rotate around the first joint spatial axis, you mean to calculate the 3*3 rotation matrix around that axis with rotated angle and simply multiply that with a vector consists of the roll, pitch, and yaw angles?

simply multiply that with a vector consists of the roll, pitch, and yaw angles?

No, that is not how transformation of angles is done. The links I provided explain the procedures.

If you have the 3D transformation matrix that represents the combined rotations, then a simple matrix multiplication is all you need to convert between reference and object frames.

Angles are a useful device for humans to think about and understand coordinate system transformations, but not for much of anything else. In actual application, we always use rotation matrices or quaternions.

I know based on the angle system that I can reach the local frame, I should multiply the transform matrices in order to have general transformation matrix between the global and local frame but with this sensors that I use, I don’t have the rotated angles so that I can calculate transformation matrix. Do you have any suggestion?

Without a good diagram I think its just going to be confusion.

Frames are related to each other by a 3D rotation, which can be represented as a DCM or quaternion.

Your link can be viewed as a 3D rotation between standing part and moving part. Thus you can transform frames between standing part and moving part if you know this rotation.

(MEMS) gyro's only measure rates of rotation, so cannot measure angles. You need encoders to do this.

However you can use a complete IMU to measure orientation w.r.t. the earth's gravity and magnetic field, and from this infer a 3D rotation w.r.t a reference frame.

So its pretty clear that gyro by itself cannot be useful to you.

Thanks for your answer. I attached the kinematic chain of my system and explained about that earlier.
How about if I use a gyro attached to the first joint in order to measure spatial rotations of this joint or even attach one gyro on the base and calculate the other gyroscopes angles with respect to this frame?

I can't make much sense of the diagram you posted, together with the written description.

It would probably help if you provide a good diagram, along with the mathematical statements corresponding to the forward kinematic chain. The diagram must show the definitions of all the angles and their respective reference frames.

Example of a useful diagram:

Vague terms like "the gyro", GY-521, etc. are not only completely unhelpful, they are misleading.

I attached the new figure. Z1, Z2, Z3, Z4 are the rotation axes. Joint 1 is for spatial rotation, and on the links 2 and 3, I attached two gyroscopes (gy-25 and gy-521) to measure the theta1 in joint 1, theta 3 in joint 3 and theta 4 in joint 4. Theta 2 is being measured with an encoder.
Forward kinematic:
X = bcos(theta1) + cos(theta1)(L1cos(theta2) +L2cos(theta3)+L3*cos(theta4))

In my real system, you should rotate the figure 90 degree CCW and with this configuration, if you fix joint 2, joint 3, and joint 4 and just rotate around joint 1 (theta1), then all the values of both gyroscopes change. So I need to transform the gyroscope values from their frame into the global frame so that I can measure theta 3, 4, and 1. I know I should use the transformation matrix, but I know how to calculate that based on the gyroscopes values.

Why is there an instance that gyro's give angles?

A rate gyro, as one that is in a GY-25, gives a rate of change. One can get current angle from the accelerometers, but those are prone to noise. So one will want to filter that noise, with a GY 25 a simple low pass complementary filter. works fine.

Thanks for your reply. That's not my problem and the angles that I get with gy-25 are pretty good.
My problem is that I don't know how to calculate transfer matrix