BluetoothD.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using ArduinoBluetoothAPI;
  2. using System;
  3. using UnityEngine;
  4. public class BluetoothD : MonoBehaviour
  5. {
  6. BluetoothHelper bluetoothHelper;
  7. private bool btnCanClick = true;
  8. private float roll = 0;
  9. private float pitch = 0;
  10. private float yaw = 0;
  11. private float baseX = 0;//EulerAngleX
  12. private float baseY = 0;//EulerAngleY
  13. private float baseZ = 0;//EulerAngleY
  14. private int ReceiveDataCount = 0;
  15. private bool hasSetBasePoint = false;
  16. public static GameObject controlTarget;
  17. public static BluetoothD ins;
  18. void Start()
  19. {
  20. ins = this;
  21. try {
  22. BluetoothHelper.BLE = false;
  23. bluetoothHelper = BluetoothHelper.GetNewInstance("HC-06");
  24. bluetoothHelper.setFixedLengthBasedStream(11);
  25. bluetoothHelper.OnConnected += (BluetoothHelper helper) =>
  26. {
  27. Log("连接成功");
  28. bluetoothHelper.StartListening();
  29. Log("开始采集信息\n" + bluetoothHelper.getDeviceName());
  30. };
  31. bluetoothHelper.OnConnectionFailed += (BluetoothHelper helper) =>
  32. {
  33. btnCanClick = true;
  34. Log("连接失败");
  35. };
  36. bluetoothHelper.OnDataReceived += (BluetoothHelper helper) =>
  37. {
  38. byte[] bytes = helper.ReadBytes();
  39. if (bytes[1] == 89) {
  40. float q1 = TwoByteToFloat(bytes[3], bytes[2]);
  41. float q2 = TwoByteToFloat(bytes[5], bytes[4]);
  42. float q3 = TwoByteToFloat(bytes[7], bytes[6]);
  43. float q4 = TwoByteToFloat(bytes[9], bytes[8]);
  44. q1 = q1 / 32768;
  45. q2 = q2 / 32768;
  46. q3 = q3 / 32768;
  47. q4 = q4 / 32768;
  48. q2 = 0f - q2;
  49. q3 = 0f - q3;
  50. ReceiveDataCount++;
  51. if (controlTarget != null) {
  52. controlTarget.transform.rotation = new Quaternion(q2, q1, q3, q4);
  53. pitch = controlTarget.transform.eulerAngles.x;
  54. yaw = controlTarget.transform.eulerAngles.y;
  55. roll = controlTarget.transform.eulerAngles.z;
  56. controlTarget.transform.eulerAngles = new Vector3(controlTarget.transform.eulerAngles.x + baseX, controlTarget.transform.eulerAngles.y + baseY, controlTarget.transform.eulerAngles.z + baseZ);
  57. }
  58. Log(q1 + ", " + q2 + ", " + q3 + ", " + q4);
  59. }
  60. };
  61. } catch (Exception e) {
  62. Debug.Log(e.Message);
  63. Log("请打开蓝牙");
  64. }
  65. this.Invoke("Connect", 1);
  66. }
  67. void OnDestroy()
  68. {
  69. if (bluetoothHelper != null) {
  70. bluetoothHelper.Disconnect();
  71. }
  72. }
  73. public void SetBasePoint()
  74. {
  75. baseX = -pitch;
  76. baseY = 90 -yaw;
  77. baseZ = -90 -roll ;
  78. if (ReceiveDataCount > 10) {
  79. hasSetBasePoint = true;
  80. }
  81. }
  82. public void AutoSetBasePoint()
  83. {
  84. if (!hasSetBasePoint && ReceiveDataCount > 10 && (roll > 87 && roll < 93) && (pitch < 3 || pitch > 357)) {
  85. SetBasePoint();
  86. }
  87. }
  88. public void Connect()
  89. {
  90. if (!btnCanClick) {
  91. return;
  92. }
  93. btnCanClick = false;
  94. try {
  95. if (bluetoothHelper != null) {
  96. bluetoothHelper.Connect();
  97. Log("正在扫描设备");
  98. } else {
  99. btnCanClick = true;
  100. Log("蓝牙初始化失败");
  101. }
  102. } catch (Exception e) {
  103. btnCanClick = true;
  104. Debug.Log(e.Message);
  105. Log("请先配对蓝牙");
  106. }
  107. }
  108. float TwoByteToFloat(byte b1, byte b2)
  109. {
  110. ushort twoByte = (ushort) (b1 * 256 + b2);
  111. short shortNum = (short) twoByte;
  112. return (float) shortNum;
  113. }
  114. void Log(string text)
  115. {
  116. }
  117. }