ShootCheck.cs 6.3 KB

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