I have a 9 DOF IMU sensor connected through an Arduino and the values are exposed as public static floats.
So I have float UnitySerialPort.x, y, z, w. Values are between -1 and 1. I want to use these values to transform/rotate an object. All the examples I can find are either in JS or just for euler x,y,z transforms only.
Anyone have an examples?
Thanks!
transform.rotation = new Quaternion(x, y, z, w);
The first thing I do is take an initial reading from my IMU to get the quaternion values before I move it.
X, Y, Z, W = 0.0181, -0.7184, 0.0104, 0.6953. If I apply that transform it’s going to immediately move my object. I only want to move it relative to this position. So I save these values into a Quaternion called initialQ. newQ are my readings that are updating 10 times per second. finalQ is what I then use to do the transform which should be relatively small movements.
If I do a Debug.Log(initialQ) I get back 0, -0.7, 0, 0.7. So clearly it’s rounding them which is going to be a problem because the initialQ won’t look much different than newQ because my movements are small. Even my finalQ comes back as 0, -0.7, 0, 0.7. If my initialQ and newQ are basically the same then shouldn’t finalQ end up as 0?
Even then with a finalQ like that I applied the transform and it didn’t move the object.
void Start() {
headIMUobj = GameObject.FindGameObjectWithTag("headTag");
// connect to Arduino, get initial reading once (initialQ) and then loop to get newQ readings after that.
}
void Update () {
Quaternion newQ = new Quaternion (x, y, z, w);
Quaternion finalQ = new Quaternion ();
Quaternion inverseQ = Quaternion.Inverse (newQ);
finalQ = initialQ * newQ * inverseQ;
Debug.Log ("InitialQ: " + initialQ);
Debug.Log ("NewQ:" + newQ);
Debug.Log ("InverseQ: " + inverseQ);
Debug.Log ("FinalQ: " + finalQ);
//headIMUobj.transform.rotation = new Quaternion (x, y, z, w);
headIMUobj.transform.rotation = finalQ;
}
}