I am using an electronic gyroscope and I want to calculate the degrees the device has turned regardless of any angle changes whilst it is turning. So for example with the Z gyroscope I can calculate a 90 degree turn accurately, but of course when I tilt the device, the Z gyroscope readings slow down relative to the angle. The steeper the angle the slower the readings. I guess tilt compensation is possible using trigonometry, but I have tried various things and I have not been able to get it right.

I want to be able to measure the degrees turned around a particular axis regardless of how the device is tilting during that turn.

I have also tried summing all the X, Y and Z values together, which might work but the gyroscope values change during any angle change, and that is added to the equation.

For informed help, please read the "How to use this forum" post and follow the instructions. You need to specify the components you are using, and post the code, using code tags.

Consumer gyros just measure rate of angular change, and they drift, so you can't expect to measure turns very accurately (even short ones), unless you have calibrated the gyro at startup.

If the rotation axis of your model is not aligned with one of the gyro axes, then you need to calculate the total angle change in all three Euler angles, in order to estimate the model turn angle. This is mathematically challenging and will not work well if based only on a gyro, because all three measurements are drifting at different rates. However, if you are interested in the details, here is a good overview.

If you want to measure absolute orientation, you will need an AHRS or absolute orientation sensor. The BNO055 works reasonably well.

I am using a MPU6050, invensense. I don't think my code would be a good idea because I have tried so many different things it would just cloud the area.

I have accurate roll and pitch based on accelerometer and gyroscope fusion. I should be able to use that to help, but like i said, I have tried various ways without success

Lets assume I am using only the Z gryoscope at 2000 DPS , and roll is currently flat, and pitch is at 20 degrees. What is the calculation to arrive at the correct rotation in degrees?

I guess the gyroscope output now needs to be multiplied or divided by something. What would that be?

A 3D IMU like that gives you attitude. Attitude can be either as quarternion, DCM or
Euler angle set. You need the whole 3D attitude, taking a single gyro reading from it is meaningless unless the thing is constrained to be only rotating about the z-axis.

And yes you do need geometry!

Your statement:

I want to be able to measure the degrees turned around a particular axis regardless of how the device is tilting during that turn.

Needs very careful definition to mean anything at all. 3D rotations are not commutative and are
much less intuitive that I suspect you think...

I guess the gyroscope output now needs to be multiplied or divided by something.

Unfortunately, it is not that simple. See reply #5.

Obtaining orientations in terms of angles is difficult and confusing, because there are many different angular systems and the order of rotations makes a huge difference.

Unless you can take the time to define your problem more clearly, all we can do is recommend that you get an absolute orientation sensor.

Assuming a setting of 2000 dps when the MPU is flat (X acceleration = 0, Y acceleration = 0, Z acceleration = 1) when it is tilted on the X axis by 45 degrees, will the Z gyro output now be effectively 1000 dps?

What I would like to know is, what is the mathematical relationship for the change in gyroscope output as the angle changes. Is it a linear change, so at 45 degrees pitch, the rotational change in degrees is half? or is it something else.

You certainly are. How do you define the "X axis"? Presumably X is horizontal, so parallel to the Earth's surface, but in which direction does X point?

You need to learn about the definition of coordinate systems and 3D rotations. Start here or here, among many other places.

Think of it this way:

Suppose I rotate a model about an arbitrary axis, not parallel to any of my chosen X, Y or Z axis.
Is there a single rotation about one of the X, Y or Z axes, that superimposes the initial and final models?

The answer is, in general, NO. Successive rotations about at least two and possibly all three of the chosen coordinate axes will almost always be required.

Assuming it is flat (acceleration x and y = 0, Z = 1) and outputting 200 dps as it moves around the Z axis. It continues to move on this plane and when it is tilted 45 degrees on the Y axis, will there be 100 dps around the Z axis and 100 dps around the Y axis? or is it 0.707 of the dps, or something else?

Assuming the device continues to rotate around the original Z axis, i can use the following formula to get the degrees per second, regardless of the pitch.