Last active
December 16, 2017 12:30
-
-
Save peteroid/60be49c185e6e29fd04fe5254ebb47d0 to your computer and use it in GitHub Desktop.
Get the quaternion from arduino output string with filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // !!! Attention !!! | |
| void initPort (SerialPort port) { | |
| // set this encoding to extend the ascii range | |
| port.Encoding = System.Text.Encoding.GetEncoding(28591); | |
| } | |
| // return: | |
| // quat = Quaternion.identity if packet is invalid | |
| // index = -1 if packet is invalid | |
| void getQuatFromString (string s, out Quaternion quaternion, out int index) { | |
| char[] packet = s.ToCharArray (); | |
| string str = ""; | |
| foreach (char c in packet) { | |
| str += (int)c + " "; | |
| } | |
| //Debug.Log (str); | |
| // Windows user: use (packet.Length != 18 || packet[0] != 36) as condition | |
| if (packet.Length != 19 || packet[0] != 36 || packet[18] != 13) { | |
| quaternion = Quaternion.identity; | |
| index = -1; | |
| } else { | |
| float[] q = new float[4]; | |
| q[0] = ((packet[2] << 8) | packet[3]) / 16384f; | |
| q[1] = ((packet[4] << 8) | packet[5]) / 16384f; | |
| q[2] = ((packet[6] << 8) | packet[7]) / 16384f; | |
| q[3] = ((packet[8] << 8) | packet[9]) / 16384f; | |
| for (int i = 0; i < 4; i++) if (q[i] >= 2) q[i] = -4 + q[i]; | |
| //Debug.Log (quat.x + " " + quat.y + " " + quat.z + " " + quat.w); | |
| quaternion = new Quaternion (q [1], q [2], q [3], q [0]); | |
| index = packet [1] - '0'; | |
| } | |
| } | |
| // sample usage | |
| void readFromArduinoPort (SerialPort serial) { | |
| string serialRead = serial.ReadLine (); | |
| int outIndex; | |
| Quaternion quat; | |
| getQuatFromString (serialRead, out quat, out outIndex); | |
| if (outIndex != -1 && !quat.Equals(Quaternion.identity)) { | |
| objs [outIndex].rotation = quat; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment