ShootCheck.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 Text shootAngleText;
  97. public void SetShootBackTime()
  98. {
  99. ArmBow.ins.shootBackTime=int.Parse(ArmBowInputField.text);
  100. }
  101. public void OnBluetoothReady(BluetoothShoot bluetoothShoot) {
  102. bluetoothShoot.WriteData(JsonUtility.ToJson(cmd).Replace("\"", ""));
  103. }
  104. public void OnDataReceived(byte[] bytes) {
  105. if (bytes.Length == 2)
  106. {
  107. DeviceBatteryView.ins.RenderBattery(2, bytes[0]);
  108. return;
  109. }
  110. string str = "";
  111. for (int i = 0; i < (bytes.Length-2)/6; i++)
  112. {
  113. float acc = ToAcceleratedSpeed(bytes[i * 6 + 5], bytes[i * 6 + 6]);
  114. string t = "(采样时间:"+(int)bytes[i * 6 + 3] + "分"+ (int)bytes[i * 6 + 4]+"秒"+ TwoByteToInt(bytes[i * 6 + 1], bytes[i * 6 + 2])+"毫秒)" ;
  115. str += "加速度:"+acc+t+"\n";
  116. if (webSocket != null)
  117. {
  118. webSocket.Send(str);
  119. }
  120. if (ins.check(acc) && ArmBow.ins)
  121. {
  122. ArmBow.ins.ADS_fire();
  123. // if (webSocket != null)
  124. // {
  125. // webSocket.Send(str);
  126. // }
  127. }
  128. }
  129. }
  130. float ToAcceleratedSpeed(byte b1, byte b2)
  131. {
  132. int value = TwoByteToInt(b1, b2);
  133. return (float)value / 32768 * LoginMgr.myUserInfo.deviceAccValue;
  134. }
  135. int TwoByteToInt(byte b1, byte b2)
  136. {
  137. ushort twoByte = (ushort)(b1 * 256 + b2);
  138. short shortNum = (short)twoByte;
  139. return (int)shortNum;
  140. }
  141. bool check(float acc)
  142. {
  143. DebugLine.show(acc); //这个不需要注释,静态函数内置判断
  144. if (locked)
  145. {
  146. return false;
  147. }
  148. if (acc > cmd.getAcc())
  149. {
  150. if (acc > maxAcc)
  151. {
  152. maxAcc = acc;
  153. }
  154. keyAccList.Enqueue(acc);
  155. return false;
  156. }
  157. else if (acc < cmd.getAcc() && maxAcc != 0) {
  158. //积分求初速度
  159. shootSpeed = 0;
  160. float lasKeytAcc = 0;
  161. int keyAccIndex = 0;
  162. foreach (var keyAcc in keyAccList)
  163. {
  164. if (keyAccIndex > 0)
  165. {
  166. shootSpeed += keyAcc * 0.002f;
  167. shootSpeed -= (keyAcc - lasKeytAcc) * 0.002f / 2;
  168. }
  169. lasKeytAcc = keyAcc;
  170. keyAccIndex++;
  171. }
  172. //加速度acc的单位是g,最后需要乘上
  173. shootSpeed *= 9.80665f;
  174. //积分出来的值还是太小,需要一个倍率
  175. shootSpeed *= 10;
  176. Debug.LogWarning("初速度: " + shootSpeed + " 帧数: " + keyAccList.Count);
  177. //本轮计算结束
  178. keyAccList.Clear();
  179. maxAcc = 0;
  180. Dolock();
  181. Invoke("Unlock", 1.8f);
  182. return true;
  183. }
  184. return false;
  185. }
  186. void Dolock()
  187. {
  188. locked = true;
  189. }
  190. void Unlock()
  191. {
  192. locked = false;
  193. }
  194. void Log(string text)
  195. {
  196. if (this.text)
  197. {
  198. this.text.text = text;
  199. } else {
  200. Debug.Log(text);
  201. }
  202. }
  203. }
  204. [Serializable]
  205. class CMD {
  206. // public string ax = "y";
  207. // public int a = 6000;
  208. // public int r = 2;
  209. public string a = "y";
  210. public int a1 = 3;
  211. public int a2 = -3;
  212. public int r = 2;
  213. public float getAcc() {
  214. return a1;
  215. }
  216. }