My hardware-sensor outputs data like this:

sensorfused-yaw,sensorfused-pitch,sensorfused-roll,gyro-x,gyro-y,gyro-z,mag-x,mag-y,mag-z

sensorfused data is rubbish, it makes no sense at all

gyro is rad/s => gyro-value * dT = delta-angle-in-rad

mag is m/s² => mag-value * dT² = delta-distance-in-m

I wanted to attach this to my VR-helmet and have it attached to the camera in unity. But all I get is rubbish for the values. Rubbish meaning that the rotation is all over the place but not comparable to the input.

The goal is to have my VR-helmet on and walk around in my room seeing the scenery depending on where I am.

So in general, what is wrong with this code and how could I improve it?

wx etc. means total movement.

gx etc. means total rotation

```
bool erstes = true;
...
if (serialPort1.IsOpen)
{
string ausgabe = serialPort1.ReadLine();
string[] werte = ausgabe.Split(',');
if(werte.Length == 9)
{
if (erstes)
{
a = DateTime.Now;
erstes = false;
}
float.TryParse(werte[6], out magnetometer.x);
float.TryParse(werte[7], out magnetometer.y);
float.TryParse(werte[8], out magnetometer.z);
//subtract gravity
gravity[0] = alpha * gravity[0] + (1 - alpha) * magnetometer.x;
gravity[1] = alpha * gravity[1] + (1 - alpha) * magnetometer.y;
gravity[2] = alpha * gravity[2] + (1 - alpha) * magnetometer.z;
DateTime b = DateTime.Now;
TimeSpan diff = b - a;
a = b;
linear_acceleration[0] = magnetometer.x - gravity[0];
linear_acceleration[1] = magnetometer.y - gravity[1];
linear_acceleration[2] = magnetometer.z - gravity[2];
WegX = linear_acceleration[0] * (float)(diff.TotalSeconds * diff.TotalSeconds);
WegY = linear_acceleration[1] * (float)(diff.TotalSeconds * diff.TotalSeconds);
WegZ = linear_acceleration[2] * (float)(diff.TotalSeconds * diff.TotalSeconds);
wx += WegX;
wy += WegY;
wz += WegZ;
float.TryParse(werte[3], out GradX);
float.TryParse(werte[4], out GradY);
float.TryParse(werte[5], out GradZ);
GradX = GradX * (float)diff.TotalSeconds;
GradY = GradY * (float)diff.TotalSeconds;
GradZ = GradZ * (float)diff.TotalSeconds;
GradX = GradX / 10;
GradY = GradY / 10;
GradZ = GradZ / 10;
gx += GradX;
gy += GradY;
gz += GradZ;
transform.Translate(WegX, WegY, WegZ);
transform.rotation = Quaternion.Euler(gx, gy, gz);
}
}
```