ShootCheck.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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;//"192.168.0.107";
  37. string address = "ws://" + serverIP + ":8088/Ble/";
  38. webSocket = new WebSocket(new Uri(address));
  39. #if !UNITY_WEBGL
  40. webSocket.StartPingThread = true;
  41. #endif
  42. // Subscribe to the WS events
  43. webSocket.OnOpen += OnOpen;
  44. webSocket.OnMessage += OnMessageRecv;
  45. webSocket.OnBinary += OnBinaryRecv;
  46. webSocket.OnClosed += OnClosed;
  47. webSocket.OnError += OnError;
  48. // Debug.Log("OnOpen: ");
  49. // Start connecting to the server
  50. webSocket.Open();
  51. }
  52. public void Destroy()
  53. {
  54. if (webSocket != null)
  55. {
  56. webSocket.Close();
  57. webSocket = null;
  58. }
  59. }
  60. void OnOpen(WebSocket ws)
  61. {
  62. // Debug.Log("OnOpen: ");
  63. webSocket.Send("unity");
  64. }
  65. void OnMessageRecv(WebSocket ws, string message)
  66. {
  67. Debug.LogFormat("OnMessageRecv: msg={0}", message);
  68. }
  69. void OnBinaryRecv(WebSocket ws, byte[] data)
  70. {
  71. Debug.LogFormat("OnBinaryRecv: len={0}", data.Length);
  72. }
  73. void OnClosed(WebSocket ws, UInt16 code, string message)
  74. {
  75. Debug.LogFormat("OnClosed: code={0}, msg={1}", code, message);
  76. webSocket = null;
  77. }
  78. void OnError(WebSocket ws, Exception ex)
  79. {
  80. string errorMsg = string.Empty;
  81. #if !UNITY_WEBGL || UNITY_EDITOR
  82. if (ws.InternalRequest.Response != null)
  83. {
  84. errorMsg = string.Format("Status Code from Server: {0} and Message: {1}", ws.InternalRequest.Response.StatusCode, ws.InternalRequest.Response.Message);
  85. }
  86. #endif
  87. Debug.LogFormat("OnError: error occured: {0}\n", (ex != null ? ex.Message : "Unknown Error " + errorMsg));
  88. webSocket = null;
  89. }
  90. //socket
  91. void OnDestroy()
  92. {
  93. ins = null;
  94. }
  95. public void OnBluetoothReady(BluetoothShoot bluetoothShoot) {
  96. bluetoothShoot.WriteData(JsonUtility.ToJson(cmd).Replace("\"", ""));
  97. }
  98. public void OnDataReceived(byte[] bytes) {
  99. string str = "";
  100. for (int i = 0; i < (bytes.Length-2)/6; i++)
  101. {
  102. // float acc = ToAcceleratedSpeed(bytes[i * 10 + 7], bytes[i * 10 + 8]);
  103. // string t = "(采样时间:"+(int)bytes[i * 10 + 5] + "分"+ (int)bytes[i * 10 + 6]+"秒"+ TwoByteToInt(bytes[i * 10 + 3], bytes[i * 10 + 4])+"毫秒)" ;
  104. float acc = ToAcceleratedSpeed(bytes[i * 6 + 5], bytes[i * 6 + 6]);
  105. string t = "(采样时间:"+(int)bytes[i * 6 + 3] + "分"+ (int)bytes[i * 6 + 4]+"秒"+ TwoByteToInt(bytes[i * 6 + 1], bytes[i * 6 + 2])+"毫秒)" ;
  106. str += "加速度:"+acc+t+"\n";
  107. // ts[3] = "(采样时间:"+(int)bytes[33] + "分"+ (int)bytes[34]+"秒"+ TwoByteToInt(bytes[31], bytes[32])+"毫秒)" ;
  108. if (ins.check(acc))
  109. {
  110. if (ArmBow.ins != null)
  111. {
  112. ArmBow.ins.ADS_fire();
  113. webSocket.Send(str);
  114. }
  115. }
  116. }
  117. }
  118. float ToAcceleratedSpeed(byte b1, byte b2)
  119. {
  120. int value = TwoByteToInt(b1, b2);
  121. return (float)value / 32768 * 16;
  122. }
  123. int TwoByteToInt(byte b1, byte b2)
  124. {
  125. ushort twoByte = (ushort)(b1 * 256 + b2);
  126. short shortNum = (short)twoByte;
  127. return (int)shortNum;
  128. }
  129. bool check(float acc)
  130. {
  131. DebugLine.show(acc);
  132. if (locked)
  133. {
  134. return false;
  135. }
  136. if (acc > cmd.getAcc() && acc > maxAcc)
  137. {
  138. maxAcc = acc;
  139. return false;
  140. } else if (acc < cmd.getAcc() && maxAcc != 0) {
  141. shootSpeed = maxAcc;
  142. // Log("最大加速度:" + maxAcc);
  143. maxAcc = 0;
  144. Dolock();
  145. Invoke("Unlock", 0.8f);
  146. return true;
  147. }
  148. return false;
  149. }
  150. void Dolock()
  151. {
  152. locked = true;
  153. }
  154. void Unlock()
  155. {
  156. locked = false;
  157. }
  158. void Log(string text)
  159. {
  160. if (this.text != null)
  161. {
  162. this.text.text = text;
  163. } else {
  164. Debug.Log(text);
  165. }
  166. }
  167. }
  168. [Serializable]
  169. class CMD {
  170. public string ax = "y";
  171. public int a = 1000;
  172. public int r = 2;
  173. public float getAcc() {
  174. return a * 0.0005f;
  175. }
  176. }
  177. // public class ShootCheck : MonoBehaviour
  178. // {
  179. // float[] accList = new float[30];
  180. // int dataCount = 0;
  181. // float gravity = 0;
  182. // float maxAcc = 0;
  183. // bool hasReachShootThreshold = false;
  184. // bool locked = false;
  185. // int hitCount = 0;
  186. // float rangeAcc; //最新几帧的平均值
  187. // [SerializeField] Text text;
  188. // public float shootSpeed;
  189. // public static ShootCheck ins;
  190. // void Start()
  191. // {
  192. // ins = this;
  193. // BluetoothDispatcher.shoot = OnDataReceived;
  194. // }
  195. // void OnDestroy()
  196. // {
  197. // ins = null;
  198. // }
  199. // float[] ays = new float[4];
  200. // public void OnDataReceived(byte[] bytes) {
  201. // Debug.Log("射击模块数据长度" + bytes.Length);
  202. // ays[0] = ToAcceleratedSpeed(bytes[7], bytes[8]);
  203. // ays[1] = ToAcceleratedSpeed(bytes[17], bytes[18]);
  204. // ays[2] = ToAcceleratedSpeed(bytes[27], bytes[28]);
  205. // ays[3] = ToAcceleratedSpeed(bytes[37], bytes[38]);
  206. // foreach (float ay in ays)
  207. // {
  208. // try
  209. // {
  210. // if (ins.check(ay))
  211. // {
  212. // if (ArmBow.ins != null)
  213. // {
  214. // ArmBow.ins.ADS_fire();
  215. // }
  216. // }
  217. // }
  218. // catch (Exception e)
  219. // {
  220. // Debug.Log(e.Message);
  221. // }
  222. // }
  223. // }
  224. // float ToAcceleratedSpeed(byte b1, byte b2)
  225. // {
  226. // int value = TwoByteToInt(b1, b2);
  227. // return (float)value / 32768 * 16;
  228. // }
  229. // int TwoByteToInt(byte b1, byte b2)
  230. // {
  231. // ushort twoByte = (ushort)(b1 * 256 + b2);
  232. // short shortNum = (short)twoByte;
  233. // return (int)shortNum;
  234. // }
  235. // int validFrameCount = 0;
  236. // bool check(float acc)
  237. // {
  238. // DebugLine.show(acc);
  239. // DebugLine.showSteady(gravity);
  240. // for (int i = accList.Length - 1; i > 0; i--)
  241. // {
  242. // accList[i] = accList[i - 1];
  243. // }
  244. // accList[0] = acc;
  245. // dataCount++;
  246. // if (locked)
  247. // {
  248. // return false;
  249. // }
  250. // if (hasReachShootThreshold) {
  251. // validFrameCount++;
  252. // if (acc <= gravity) {
  253. // hitCount++;
  254. // shootSpeed = maxAcc - gravity;
  255. // Log("第" + hitCount + "次识别射箭\n过滤正轴重力" + gravity.ToString("#0.000") + "后\n所得最大加速度峰值" + (maxAcc - gravity).ToString("#0.000") + "\n有效帧数" + validFrameCount);
  256. // maxAcc = 0;
  257. // validFrameCount = 0;
  258. // hasReachShootThreshold = false;
  259. // Dolock();
  260. // Invoke("Unlock", 0.8f);
  261. // return true;
  262. // } else if (acc > maxAcc) {
  263. // maxAcc = acc;
  264. // }
  265. // if (validFrameCount > 20)
  266. // {
  267. // maxAcc = 0;
  268. // validFrameCount = 0;
  269. // hasReachShootThreshold = false;
  270. // Log("不符合短时间爆发加速");
  271. // }
  272. // // if (this.isSteady()) {
  273. // // hitCount++;
  274. // // shootSpeed = integral / 16f;
  275. // // Log("第" + hitCount + "次识别射箭\n振幅:" + integral);
  276. // // integral = 0;
  277. // // validFrameCount = 0;
  278. // // hasReachShootThreshold = false;
  279. // // return true;
  280. // // } else {
  281. // // integral += Mathf.Abs(acc - gravity);
  282. // // }
  283. // return false;
  284. // }
  285. // if (dataCount > steadyFrameCount)
  286. // {
  287. // if (isSteady())
  288. // {
  289. // gravity = Mathf.Clamp(rangeAcc, -0.981f, 0.981f);
  290. // }
  291. // if (acc - gravity > 2)
  292. // {
  293. // hasReachShootThreshold = true;
  294. // maxAcc = acc;
  295. // // integral += Mathf.Abs(acc - gravity);
  296. // }
  297. // }
  298. // return false;
  299. // }
  300. // // float integral = 0;
  301. // int steadyFrameCount = 6;
  302. // bool isSteady() {
  303. // float totalAcc = 0;
  304. // for (int i = 0; i < steadyFrameCount; i++)
  305. // {
  306. // totalAcc += accList[i];
  307. // }
  308. // rangeAcc = totalAcc / steadyFrameCount;
  309. // float squareAcc = 0;
  310. // for (int i = 0; i < steadyFrameCount; i++)
  311. // {
  312. // squareAcc += (float) Mathf.Pow(accList[i] - rangeAcc, 2);
  313. // }
  314. // squareAcc /= steadyFrameCount;
  315. // return squareAcc < 0.00012;
  316. // }
  317. // void Dolock()
  318. // {
  319. // this.locked = true;
  320. // }
  321. // void Unlock()
  322. // {
  323. // this.locked = false;
  324. // }
  325. // void Log(string text)
  326. // {
  327. // if (this.text != null)
  328. // {
  329. // this.text.text = text;
  330. // } else {
  331. // Debug.Log(text);
  332. // }
  333. // }
  334. // }