- Home /

**Question**by nviverosb · Apr 24, 2018 at 12:18 AM · rotationquaternioneuleranglesaccelerometergyroscope

# Rotate with raw gyro data.

So I have this code that I get from my phone's raw gyro data:

```
public class IMU : MonoBehaviour {
[Range(0,1)]
public float a;
public static Vector3 angularVelocity,accel;
public static double Roll, Pitch, Yaw;
private float LSB = 131;//LEAST SIGNIFICATN BIT
public double aRoll, aPitch, magX, magY, time;
private UDPClient server;
private static Vector3 transition;
// Use this for initialization
void Start () {
server = FindObjectOfType<UDPClient> ();
transition = angularVelocity = accel = Vector3.zero;
Roll = Pitch = Yaw = 1;
aRoll = aPitch = 0;
magX = magY = 0;
time = 0;
}
// Update is called once per frame
void Update () {
double t = Time.time;
if(server.IsActive()){
Vector3 m = Transmitter.magnetometer.normalized;
angularVelocity = Transmitter.gyroscope/131;
accel = Transmitter.accelerometer;
aRoll = Math.Atan2 (accel.x,accel.z);
aPitch = Math.Atan2 (accel.y,accel.z);
// aRoll = Math.Atan(accel.y/(Math.Sqrt(accel.x*accel.x + accel.z*accel.z)));
// aPitch = Math.Atan(-accel.x/(Math.Sqrt(accel.y*accel.y + accel.z*accel.z)));
// print (aRoll + " " + aPitch);
if (double.IsNaN(Roll) || double.IsNaN(Pitch)) {
Roll = 1;
Pitch = 1;
}
else {
Roll = a * (angularVelocity.x * time + Roll) + (1 - a) * aRoll * Mathf.Rad2Deg;
Pitch = a * (angularVelocity.y * time + Pitch) + (1 - a) * aPitch * Mathf.Rad2Deg;
}
magX = ( m.z * Math.Sin(Roll) ) - ( m.y * Math.Cos(Roll) );
magY = ( m.x * Math.Cos (Pitch) ) + ( m.y*Math.Sin(Pitch) * Math.Sin(Roll) ) + ( m.z * Math.Sin(Pitch) * Math.Cos(Roll) );
Yaw = Mathf.Rad2Deg * (Math.Atan ( magX/magY ));
print (Roll + " " + Pitch + " " + Yaw);
// transform.Rotate (new Vector3((float)Roll,(float)Pitch,(float)Yaw));
// transform.rotation.eulerAngles = new Vector3 ((float)Roll, (float)Pitch, (float)Yaw);
// transition = Vector3.Lerp (transition,new Vector3((float)Roll,(float)Pitch,(float)Yaw),Time.deltaTime*1);
// transform.eulerAngles = new Vector3 ((float)Roll, (float)Pitch, (float)Yaw);
// transform.rotation = Quaternion.Euler (new Vector3((float)Roll,(float)Pitch,(float)Yaw));
print(Quaternion.Euler(new Vector3 ((float)Roll, (float)Pitch, (float)Yaw)));
}
time = Time.time - t;
}
}
```

and the Roll/Pitch/Yaw angles make sense, but the problem comes when I try to rotate the object using those angles. I'm familiar with the gimbal lock problem that comes with Euler angles, but not familiar with Quaternions though. My object currently rotates like crazy, just spinning very rapidly. Is there ay way I can rotate my object with those angles?

I can't use gyro.attitude, since I am not actually running the app on the phone, I'm just sending data using an UDP stream.

Any suggestions on how to calculate Roll/Pitch/Yaw from raw gyro/accelerometer data?

Thank you very much in advance.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.