ShootCheck.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. keyAccList.Enqueue(acc);
  163. keyTimeList.Enqueue(t);
  164. return false;
  165. }
  166. else if (acc < cmd.getAcc() && maxAcc != 0) {
  167. //积分求初速度
  168. shootSpeed = 0;
  169. float lasKeytAcc = 0;
  170. int keyAccIndex = 0;
  171. float timeInterval = 0.002f;
  172. foreach (var keyAcc in keyAccList)
  173. {
  174. if (keyAccIndex > 0)
  175. {
  176. shootSpeed += keyAcc * timeInterval;
  177. shootSpeed -= (keyAcc - lasKeytAcc) * timeInterval / 2;
  178. }
  179. else if (keyAccIndex == 0 && keyAccList.Count == 1)
  180. {
  181. shootSpeed = keyAcc * timeInterval;
  182. }
  183. lasKeytAcc = keyAcc;
  184. keyAccIndex++;
  185. }
  186. //加速度acc的单位是g,最后需要乘上
  187. shootSpeed *= 9.80665f;
  188. //积分出来的值还是太小,需要一个倍率
  189. shootSpeed *= 20;
  190. string strShootSpeed = "初速度: " + shootSpeed + " 帧数: " + keyAccList.Count + "\n";
  191. Debug.LogWarning(strShootSpeed);
  192. string str1 = strShootSpeed + "检测到射出的数据:\n";
  193. for (int i=0;i<keyAccList.Count;i++)
  194. {
  195. float keyAcc = keyAccList.ElementAt(i);
  196. string time = keyTimeList.ElementAt(i);
  197. str1 += "加速度:"+keyAcc+time+"\n";
  198. }
  199. Debug.LogWarning(str1);
  200. if (webSocket != null)
  201. {
  202. webSocket.Send(str1);
  203. }
  204. //本轮计算结束
  205. keyAccList.Clear();
  206. keyTimeList.Clear();
  207. maxAcc = 0;
  208. Dolock();
  209. Invoke("Unlock", 1.8f);
  210. return true;
  211. }
  212. return false;
  213. }
  214. void Dolock()
  215. {
  216. locked = true;
  217. }
  218. void Unlock()
  219. {
  220. locked = false;
  221. }
  222. void Log(string text)
  223. {
  224. if (this.text)
  225. {
  226. this.text.text = text;
  227. } else {
  228. Debug.Log(text);
  229. }
  230. }
  231. }
  232. [Serializable]
  233. class CMD {
  234. // public string ax = "y";
  235. // public int a = 6000;
  236. // public int r = 2;
  237. public string a = "y";
  238. public int a1 = 3;
  239. public int a2 = -3;
  240. public int r = 2;
  241. public float getAcc() {
  242. return a1;
  243. }
  244. }