DeviceItem.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Wit.SDK.Modular.Sensor.Device;
  6. using Wit.SDK.Modular.Sensor.Modular.DataProcessor.Constant;
  7. public class DeviceItem : MonoBehaviour
  8. {
  9. public string id;
  10. public BWT901BLE dm;
  11. public bool isX;
  12. public bool isY;
  13. public bool isZ;
  14. public List<float> initV3=new List<float>();
  15. // Start is called before the first frame update
  16. void Start()
  17. {
  18. reset();
  19. dm = new BWT901BLE();
  20. dm.SetDeviceAddress(id);
  21. dm.SetDeviceName("WT901BLE67");
  22. dm.OnUpdate += BWT901BLE_OnUpdate;
  23. }
  24. Queue<BWT901BLE> bw = new Queue<BWT901BLE>();
  25. Quaternion qr = Quaternion.Euler(0, 0, 0);
  26. private void BWT901BLE_OnUpdate(BWT901BLE BWT901BLE)
  27. {
  28. // UnityEngine.Debug.Log("BWT901BLE_OnUpdate==>");
  29. DisplayData(BWT901BLE);
  30. }
  31. Queue<sx> q = new Queue<sx>();
  32. /// <summary>
  33. /// ����չʾ
  34. /// Data display
  35. /// </summary>
  36. /// <param name="bWT901BLE"></param>
  37. private void DisplayData(BWT901BLE bWT901BLE)
  38. {
  39. sx sxx = new sx();
  40. sxx.xx = bWT901BLE.GetDeviceData(WitSensorKey.AsX);
  41. sxx.yy = bWT901BLE.GetDeviceData(WitSensorKey.AsY);
  42. sxx.zz = bWT901BLE.GetDeviceData(WitSensorKey.AsZ);
  43. q.Enqueue(sxx);
  44. }
  45. public void reset()
  46. {
  47. qr = Quaternion.Euler(0,0,0);
  48. }
  49. class sx
  50. {
  51. public string xx;
  52. public string yy;
  53. public string zz;
  54. }
  55. private void OnDestroy()
  56. {
  57. if (dm != null)
  58. dm.Close();
  59. }
  60. public bool isxb;
  61. Vector3 v3;
  62. bool isStart;
  63. float f = 0;
  64. private void Update()
  65. {
  66. if(q.Count>0)
  67. {
  68. int ct = q.Count;
  69. for (int i = 0; i < ct; i++)
  70. {
  71. sx s = q.Dequeue();
  72. UnityEngine.Vector3 av = new UnityEngine.Vector3(-(float.Parse(s.zz)), (float.Parse(s.yy)), (float.Parse(s.xx)));
  73. UnityEngine.Quaternion dr = UnityEngine.Quaternion.Euler(av / 10);
  74. qr = qr * dr;
  75. float xx = 0;
  76. float yy = 0;
  77. float zz = 0;
  78. if (isX)
  79. {
  80. xx = qr.eulerAngles.x;
  81. }
  82. if (isY)
  83. {
  84. yy = qr.eulerAngles.y;
  85. }
  86. if (isZ)
  87. {
  88. zz = qr.eulerAngles.z;
  89. }
  90. if(isxb)
  91. {
  92. this.transform.localEulerAngles = new Vector3(zz, xx, yy) + new Vector3(initV3[0], initV3[1], initV3[2]);
  93. }
  94. else
  95. {
  96. this.transform.localEulerAngles = new Vector3(xx, yy, zz) + new Vector3(initV3[0], initV3[1], initV3[2]);
  97. }
  98. }
  99. }
  100. if (!isStart)
  101. {
  102. Debug.Log(id + "��ʼ����");
  103. dm?.Open();
  104. f = 0;
  105. isStart = true;
  106. v3 = this.transform.localRotation.eulerAngles;
  107. }
  108. f = f + Time.deltaTime;
  109. if (f > 10)
  110. {
  111. if (this.transform.localRotation.eulerAngles == v3)
  112. {
  113. isStart = false;
  114. dm.Close();
  115. Debug.Log(id + "�Ͽ�����");
  116. dm.OnUpdate -= BWT901BLE_OnUpdate;
  117. dm = new BWT901BLE();
  118. dm.SetDeviceAddress(id);
  119. dm.SetDeviceName("WT901BLE67");
  120. dm.OnUpdate += BWT901BLE_OnUpdate;
  121. f = 0;
  122. }
  123. else
  124. {
  125. isStart = true;
  126. v3 = this.transform.localRotation.eulerAngles;
  127. f = 0;
  128. }
  129. }
  130. }
  131. }