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!
2 Answers
2
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;
}
}
How do I connect it to my mesh object? Sorry, this may be a really basic question. I'm still relatively new to Unity.
– overthrowroboticsEvery object in Unity has a transform: if you put a script on this object you will be able to access the transform with what tanoshimi said and make its rotation equal to what comes from your arduino. If your question is how to send data from arduino to unity, the answer is: use the serial port !
– KdRWaylanderJust put this in the Update() function of a component attached to a gameobject, and it will set that object's position as a quaternion based on the 4 values you supply. If you're not sure about components, gameobjects etc., you might want to click the Learn button at the top of the screen.
– tanoshimi