ShootCheck.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. void Start()
  18. {
  19. ins = this;
  20. BluetoothDispatcher.shoot = OnDataReceived;
  21. }
  22. public void OnBluetoothReady(BluetoothShoot bluetoothShoot) {
  23. bluetoothShoot.WriteData(JsonUtility.ToJson(cmd).Replace("\"", ""));
  24. }
  25. public void OnDataReceived(byte[] bytes) {
  26. if (bytes.Length == 2)
  27. {
  28. DeviceBatteryView.ins.RenderBattery(2, bytes[0]);
  29. return;
  30. }
  31. for (int i = 0; i < (bytes.Length-2)/6; i++)
  32. {
  33. float acc = ToAcceleratedSpeed(bytes[i * 6 + 5], bytes[i * 6 + 6]);
  34. if (ins.check(acc) && ArmBow.ins)
  35. {
  36. ArmBow.ins.ADS_fire();
  37. }
  38. }
  39. }
  40. float ToAcceleratedSpeed(byte b1, byte b2)
  41. {
  42. int value = TwoByteToInt(b1, b2);
  43. return (float)value / 32768 * LoginMgr.myUserInfo.deviceAccValue;
  44. }
  45. int TwoByteToInt(byte b1, byte b2)
  46. {
  47. ushort twoByte = (ushort)(b1 * 256 + b2);
  48. short shortNum = (short)twoByte;
  49. return (int)shortNum;
  50. }
  51. bool check(float acc)
  52. {
  53. DebugLine.show(acc); //这个不需要注释,静态函数内置判断
  54. if (locked)
  55. {
  56. return false;
  57. }
  58. if (acc > cmd.getAcc())
  59. {
  60. if (acc > maxAcc)
  61. {
  62. maxAcc = acc;
  63. }
  64. keyAccList.Enqueue(acc);
  65. return false;
  66. }
  67. else if (acc < cmd.getAcc() && maxAcc != 0) {
  68. //积分求初速度
  69. shootSpeed = 0;
  70. float lasKeytAcc = 0;
  71. int keyAccIndex = 0;
  72. foreach (var keyAcc in keyAccList)
  73. {
  74. if (keyAccIndex > 0)
  75. {
  76. shootSpeed += keyAcc * 0.002f;
  77. shootSpeed -= (keyAcc - lasKeytAcc) * 0.002f / 2;
  78. }
  79. lasKeytAcc = keyAcc;
  80. keyAccIndex++;
  81. }
  82. //加速度acc的单位是g,最后需要乘上
  83. shootSpeed *= 9.80665f;
  84. //积分出来的值还是太小,需要一个倍率
  85. shootSpeed *= 10;
  86. Debug.LogWarning("初速度: " + shootSpeed + " 帧数: " + keyAccList.Count);
  87. //本轮计算结束
  88. keyAccList.Clear();
  89. maxAcc = 0;
  90. Dolock();
  91. Invoke("Unlock", 1.8f);
  92. return true;
  93. }
  94. return false;
  95. }
  96. void Dolock()
  97. {
  98. locked = true;
  99. }
  100. void Unlock()
  101. {
  102. locked = false;
  103. }
  104. void Log(string text)
  105. {
  106. if (this.text)
  107. {
  108. this.text.text = text;
  109. } else {
  110. Debug.Log(text);
  111. }
  112. }
  113. }
  114. [Serializable]
  115. class CMD {
  116. // public string ax = "y";
  117. // public int a = 6000;
  118. // public int r = 2;
  119. public string a = "y";
  120. public int a1 = 3;
  121. public int a2 = -3;
  122. public int r = 2;
  123. public float getAcc() {
  124. return a1;
  125. }
  126. }