ShootCheck.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. }
  96. public void OnBluetoothReady(BluetoothShoot bluetoothShoot) {
  97. bluetoothShoot.WriteData(JsonUtility.ToJson(cmd).Replace("\"", ""));
  98. }
  99. public void OnDataReceived(byte[] bytes) {
  100. string str = "";
  101. for (int i = 0; i < (bytes.Length-2)/6; i++)
  102. {
  103. // float acc = ToAcceleratedSpeed(bytes[i * 10 + 7], bytes[i * 10 + 8]);
  104. // string t = "(采样时间:"+(int)bytes[i * 10 + 5] + "分"+ (int)bytes[i * 10 + 6]+"秒"+ TwoByteToInt(bytes[i * 10 + 3], bytes[i * 10 + 4])+"毫秒)" ;
  105. float acc = ToAcceleratedSpeed(bytes[i * 6 + 5], bytes[i * 6 + 6]);
  106. string t = "(采样时间:"+(int)bytes[i * 6 + 3] + "分"+ (int)bytes[i * 6 + 4]+"秒"+ TwoByteToInt(bytes[i * 6 + 1], bytes[i * 6 + 2])+"毫秒)" ;
  107. str += "加速度:"+acc+t+"\n";
  108. // ts[3] = "(采样时间:"+(int)bytes[33] + "分"+ (int)bytes[34]+"秒"+ TwoByteToInt(bytes[31], bytes[32])+"毫秒)" ;
  109. if (webSocket != null)
  110. {
  111. webSocket.Send(str);
  112. }
  113. if (ins.check(acc))
  114. {
  115. if (ArmBow.ins)
  116. {
  117. ArmBow.ins.ADS_fire();
  118. // if (webSocket != null)
  119. // {
  120. // webSocket.Send(str);
  121. // }
  122. }
  123. }
  124. }
  125. }
  126. float ToAcceleratedSpeed(byte b1, byte b2)
  127. {
  128. int value = TwoByteToInt(b1, b2);
  129. return (float)value / 32768 * 16;
  130. // return (float)value;
  131. }
  132. int TwoByteToInt(byte b1, byte b2)
  133. {
  134. ushort twoByte = (ushort)(b1 * 256 + b2);
  135. short shortNum = (short)twoByte;
  136. return (int)shortNum;
  137. }
  138. bool check(float acc)
  139. {
  140. DebugLine.show(acc); //这个不需要注释,静态函数内置判断
  141. if (locked)
  142. {
  143. return false;
  144. }
  145. if (acc > cmd.getAcc() && acc > maxAcc)
  146. {
  147. maxAcc = acc;
  148. return false;
  149. }
  150. else if (acc < cmd.getAcc() && maxAcc != 0) {
  151. shootSpeed = maxAcc;
  152. // Log("最大加速度:" + maxAcc);
  153. maxAcc = 0;
  154. Dolock();
  155. Invoke("Unlock", 1.8f);
  156. return true;
  157. }
  158. return false;
  159. }
  160. void Dolock()
  161. {
  162. locked = true;
  163. }
  164. void Unlock()
  165. {
  166. locked = false;
  167. }
  168. void Log(string text)
  169. {
  170. if (this.text)
  171. {
  172. this.text.text = text;
  173. } else {
  174. Debug.Log(text);
  175. }
  176. }
  177. }
  178. [Serializable]
  179. class CMD {
  180. // public string ax = "y";
  181. // public int a = 6000;
  182. // public int r = 2;
  183. public string a = "y";
  184. public int a1 = 3;
  185. public int a2 = -3;
  186. public int r = 2;
  187. public float getAcc() {
  188. return a1;
  189. }
  190. }