ShootCheck.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using ArduinoBluetoothAPI;
  5. using BestHTTP.WebSocket;
  6. public class ShootCheck : MonoBehaviour
  7. {
  8. [SerializeField] Text text;
  9. CMD cmd = new CMD();
  10. bool locked = false;
  11. float maxAcc = 0;
  12. public float shootSpeed;
  13. public static ShootCheck ins;
  14. [SerializeField] InputField ipInputField = default;
  15. WebSocket webSocket;
  16. void Start()
  17. {
  18. ins = this;
  19. BluetoothDispatcher.shoot = OnDataReceived;
  20. }
  21. //用户输入时的变化
  22. public void ChangedValue(string value)
  23. {
  24. ipInputField.ActivateInputField();
  25. Debug.Log("输入了"+value);
  26. }
  27. public void EndValue(string value)
  28. {
  29. Debug.Log("最终内容"+value);
  30. }
  31. //socket
  32. public void StartSocket()
  33. {
  34. //socket
  35. string ipStr = ipInputField.text;//ipInputField.GetComponentInChildren<Text>();
  36. string serverIP = ipStr;
  37. // serverIP = "192.168.1.109";
  38. string address = "ws://" + serverIP + ":8088/Ble/";
  39. webSocket = new WebSocket(new Uri(address));
  40. #if !UNITY_WEBGL
  41. webSocket.StartPingThread = true;
  42. #endif
  43. // Subscribe to the WS events
  44. webSocket.OnOpen += OnOpen;
  45. webSocket.OnMessage += OnMessageRecv;
  46. webSocket.OnBinary += OnBinaryRecv;
  47. webSocket.OnClosed += OnClosed;
  48. webSocket.OnError += OnError;
  49. // Debug.Log("OnOpen: ");
  50. // Start connecting to the server
  51. webSocket.Open();
  52. }
  53. public void Destroy()
  54. {
  55. if (webSocket != null)
  56. {
  57. webSocket.Close();
  58. webSocket = null;
  59. }
  60. }
  61. void OnOpen(WebSocket ws)
  62. {
  63. Debug.Log("OnOpen: ");
  64. webSocket.Send("unity");
  65. }
  66. void OnMessageRecv(WebSocket ws, string message)
  67. {
  68. Debug.LogFormat("OnMessageRecv: msg={0}", message);
  69. }
  70. void OnBinaryRecv(WebSocket ws, byte[] data)
  71. {
  72. Debug.LogFormat("OnBinaryRecv: len={0}", data.Length);
  73. }
  74. void OnClosed(WebSocket ws, UInt16 code, string message)
  75. {
  76. Debug.LogFormat("OnClosed: code={0}, msg={1}", code, message);
  77. webSocket = null;
  78. }
  79. void OnError(WebSocket ws, Exception ex)
  80. {
  81. string errorMsg = string.Empty;
  82. #if !UNITY_WEBGL || UNITY_EDITOR
  83. if (ws.InternalRequest.Response != null)
  84. {
  85. errorMsg = string.Format("Status Code from Server: {0} and Message: {1}", ws.InternalRequest.Response.StatusCode, ws.InternalRequest.Response.Message);
  86. }
  87. #endif
  88. Debug.LogFormat("OnError: error occured: {0}\n", (ex != null ? ex.Message : "Unknown Error " + errorMsg));
  89. webSocket = null;
  90. }
  91. //socket
  92. void OnDestroy()
  93. {
  94. ins = null;
  95. BluetoothDispatcher.shoot = null;
  96. }
  97. public void OnBluetoothReady(BluetoothShoot bluetoothShoot) {
  98. bluetoothShoot.WriteData(JsonUtility.ToJson(cmd).Replace("\"", ""));
  99. }
  100. public void OnDataReceived(byte[] bytes) {
  101. string str = "";
  102. for (int i = 0; i < (bytes.Length-2)/6; i++)
  103. {
  104. // float acc = ToAcceleratedSpeed(bytes[i * 10 + 7], bytes[i * 10 + 8]);
  105. // string t = "(采样时间:"+(int)bytes[i * 10 + 5] + "分"+ (int)bytes[i * 10 + 6]+"秒"+ TwoByteToInt(bytes[i * 10 + 3], bytes[i * 10 + 4])+"毫秒)" ;
  106. float acc = ToAcceleratedSpeed(bytes[i * 6 + 5], bytes[i * 6 + 6]);
  107. string t = "(采样时间:"+(int)bytes[i * 6 + 3] + "分"+ (int)bytes[i * 6 + 4]+"秒"+ TwoByteToInt(bytes[i * 6 + 1], bytes[i * 6 + 2])+"毫秒)" ;
  108. str += "加速度:"+acc+t+"\n";
  109. // ts[3] = "(采样时间:"+(int)bytes[33] + "分"+ (int)bytes[34]+"秒"+ TwoByteToInt(bytes[31], bytes[32])+"毫秒)" ;
  110. if (webSocket != null)
  111. {
  112. webSocket.Send(str);
  113. }
  114. if (ins.check(acc))
  115. {
  116. if (ArmBow.ins)
  117. {
  118. ArmBow.ins.ADS_fire();
  119. // if (webSocket != null)
  120. // {
  121. // webSocket.Send(str);
  122. // }
  123. }
  124. }
  125. }
  126. }
  127. float ToAcceleratedSpeed(byte b1, byte b2)
  128. {
  129. int value = TwoByteToInt(b1, b2);
  130. return (float)value / 32768 * 16;
  131. // return (float)value;
  132. }
  133. int TwoByteToInt(byte b1, byte b2)
  134. {
  135. ushort twoByte = (ushort)(b1 * 256 + b2);
  136. short shortNum = (short)twoByte;
  137. return (int)shortNum;
  138. }
  139. bool check(float acc)
  140. {
  141. DebugLine.show(acc); //这个不需要注释,静态函数内置判断
  142. if (locked)
  143. {
  144. return false;
  145. }
  146. if (acc > cmd.getAcc() && acc > maxAcc)
  147. {
  148. maxAcc = acc;
  149. return false;
  150. }
  151. else if (acc < cmd.getAcc() && maxAcc != 0) {
  152. shootSpeed = maxAcc;
  153. // Log("最大加速度:" + maxAcc);
  154. maxAcc = 0;
  155. Dolock();
  156. Invoke("Unlock", 1.8f);
  157. return true;
  158. }
  159. return false;
  160. }
  161. void Dolock()
  162. {
  163. locked = true;
  164. }
  165. void Unlock()
  166. {
  167. locked = false;
  168. }
  169. void Log(string text)
  170. {
  171. if (this.text)
  172. {
  173. this.text.text = text;
  174. } else {
  175. Debug.Log(text);
  176. }
  177. }
  178. }
  179. [Serializable]
  180. class CMD {
  181. // public string ax = "y";
  182. // public int a = 6000;
  183. // public int r = 2;
  184. public string a = "y";
  185. public int a1 = 3;
  186. public int a2 = -3;
  187. public int r = 2;
  188. public float getAcc() {
  189. return a1;
  190. }
  191. }