NewBehaviourScript.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using ArduinoBluetoothAPI;
  2. using System;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class NewBehaviourScript : MonoBehaviour
  6. {
  7. BluetoothHelper bluetoothHelper;
  8. private bool btnCanClick = true;
  9. [SerializeField] GameObject controlTarget;
  10. [SerializeField] Text text;
  11. long lastTimestampGyr = 0;
  12. long Timestamp = default;
  13. long TimeGap = default;
  14. Vector3 acc = new Vector3();
  15. Vector3 gyr = new Vector3();
  16. Vector3 mag = new Vector3();
  17. o09DOF cal = new o09DOF();
  18. long ConvertDateTimeToInt(System.DateTime time)
  19. {
  20. System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
  21. long t = (time.Ticks - startTime.Ticks) / 10000;
  22. return t;
  23. }
  24. void Start()
  25. {
  26. try {
  27. BluetoothHelper.BLE = false;
  28. bluetoothHelper = BluetoothHelper.GetNewInstance("HC-06");
  29. bluetoothHelper.setFixedLengthBasedStream(11);
  30. bluetoothHelper.OnConnected += (BluetoothHelper helper) =>
  31. {
  32. Log("连接成功");
  33. bluetoothHelper.StartListening();
  34. Log("开始采集信息\n" + bluetoothHelper.getDeviceName());
  35. };
  36. bluetoothHelper.OnConnectionFailed += (BluetoothHelper helper) =>
  37. {
  38. btnCanClick = true;
  39. Log("连接失败");
  40. };
  41. bluetoothHelper.OnDataReceived += (BluetoothHelper helper) =>
  42. {
  43. byte[] bytes = helper.ReadBytes();
  44. if (bytes[1] == 81) {
  45. if (Timestamp == default)
  46. {
  47. Timestamp = ConvertDateTimeToInt(DateTime.Now);
  48. }
  49. else
  50. {
  51. long temp = Timestamp;
  52. Timestamp = ConvertDateTimeToInt(DateTime.Now);
  53. TimeGap = Timestamp - temp;//ms
  54. }
  55. acc.x = TwoByteToFloat(bytes[3], bytes[2]) / 32768f * 16f;
  56. acc.y = TwoByteToFloat(bytes[5], bytes[4]) / 32768f * 16f;
  57. acc.z = TwoByteToFloat(bytes[7], bytes[6]) / 32768f * 16f;
  58. ClearLog();
  59. Log("acc " + acc.x.ToString("#0.00") + " " + acc.y.ToString("#0.00") + " " + acc.z.ToString("#0.00"));
  60. }
  61. if (bytes[1] == 82) {
  62. float timeGap;
  63. if (lastTimestampGyr == default)
  64. {
  65. lastTimestampGyr = ConvertDateTimeToInt(DateTime.Now);
  66. return;
  67. }
  68. else
  69. {
  70. long temp = lastTimestampGyr;
  71. lastTimestampGyr = ConvertDateTimeToInt(DateTime.Now);
  72. timeGap = (lastTimestampGyr - temp) / 1000f;
  73. }
  74. gyr.x = -TwoByteToFloat(bytes[3], bytes[2]) / 32768f * 2f;
  75. gyr.y = TwoByteToFloat(bytes[5], bytes[4]) / 32768f * 2f;
  76. gyr.z = -TwoByteToFloat(bytes[7], bytes[6]) / 32768f * 2f;
  77. Log("gyr " + gyr.x.ToString("#0.00") + " " + gyr.y.ToString("#0.00") + " " + gyr.z.ToString("#0.00"));
  78. }
  79. if (bytes[1] == 84) {
  80. mag.x = TwoByteToFloat(bytes[3], bytes[2]);
  81. mag.y = TwoByteToFloat(bytes[5], bytes[4]);
  82. mag.z = TwoByteToFloat(bytes[7], bytes[6]);
  83. Log("mag " + mag.x.ToString("#0.00") + " " + mag.y.ToString("#0.00") + " " + mag.z.ToString("#0.00"));
  84. Quaternion qua = cal.o06DOFUpdate(acc * 10f, gyr, mag, TimeGap);
  85. controlTarget.transform.localRotation = qua;
  86. }
  87. };
  88. } catch (Exception e) {
  89. Debug.Log(e.Message);
  90. Log("请打开蓝牙");
  91. }
  92. this.Invoke("Connect", 1);
  93. }
  94. void OnDestroy()
  95. {
  96. if (bluetoothHelper != null) {
  97. bluetoothHelper.Disconnect();
  98. }
  99. }
  100. void Update()
  101. {
  102. if (Input.GetKeyDown(KeyCode.Space))
  103. {
  104. cal.Calibrate();
  105. Debug.Log("校准");
  106. }
  107. }
  108. public void Connect()
  109. {
  110. if (!btnCanClick) {
  111. return;
  112. }
  113. btnCanClick = false;
  114. try {
  115. if (bluetoothHelper != null) {
  116. bluetoothHelper.Connect();
  117. Log("正在扫描设备");
  118. } else {
  119. btnCanClick = true;
  120. Log("蓝牙初始化失败");
  121. }
  122. } catch (Exception e) {
  123. btnCanClick = true;
  124. Debug.Log(e.Message);
  125. Log("请先配对蓝牙");
  126. }
  127. }
  128. float TwoByteToFloat(byte b1, byte b2)
  129. {
  130. ushort twoByte = (ushort) (b1 * 256 + b2);
  131. short shortNum = (short) twoByte;
  132. return (float) shortNum;
  133. }
  134. void Log(string text)
  135. {
  136. if (this.text == null) return;
  137. this.text.text += text + "\n";
  138. }
  139. void ClearLog()
  140. {
  141. if (this.text == null) return;
  142. this.text.text = "";
  143. }
  144. }