ShootCheck.cs 7.7 KB

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