- Home /

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

# Rotate with raw gyro data.

So I have t$$anonymous$$s 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 t$$anonymous$$s 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.

### People who like this

### Your answer

### Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

### Follow this Question

### Related Questions

ConfigurableJoint - angular positions (rotation) problem 1 Answer

3D camera relatively using gyro a la N.O.V.A. 2 0 Answers

EulerAngles conversion Quaternion problem 2 Answers

Why is this rotation not performed as expected? 1 Answer

Rotation jumping to a different rotation after rotating a certain amount 0 Answers