o0DistanceToAxis.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using o0.Geometry;
  7. using o0.Num;
  8. using UnityEngine;
  9. namespace o0.Bow
  10. {
  11. public class DistanceToAxis//测试版
  12. {
  13. public DistanceToAxis()
  14. {
  15. DistanceQueue = new SortedQueue<double>();
  16. Distance = 0.6;
  17. }
  18. Vector<double> LastAcc;
  19. Vector<double> LockAcc;//静止状态下加速计向量,仅通过陀螺仪短时校准,长时校准不保证准确性,也无必要性
  20. double LockMS;
  21. double LastGapMS;
  22. Vector<double> Speed;//每秒
  23. SortedQueue<double> DistanceQueue;
  24. double Distance;
  25. /////////////////////////////////////////////////Acc长度以1为标准
  26. public void Update(Vector<double> Gyr, Vector<double> Acc, Vector<double> Mag, double GapMS)//务必仅在射箭瞄准游戏过程中调用
  27. {
  28. if(LastAcc == default)
  29. {
  30. LastAcc = Acc;
  31. return;
  32. }
  33. if (Gyr.Length * 1000 < 1 // 陀螺仪计算的每秒移动小于1度
  34. && (LastAcc - Acc).Length / GapMS * 1000 < 1// 加速计计算的每秒移动小于1度
  35. && Acc.Length < 1.05
  36. && Acc.Length > 0.95
  37. && LastAcc.Length < 1.05
  38. && LastAcc.Length > 0.95)//则当做静止状态,此时记录加速计长度
  39. {
  40. LockAcc = Acc;
  41. LockMS = 0;
  42. LastGapMS = GapMS;
  43. Speed = Vector<double>.Zero;
  44. //Debug.Log(Gyr.Length * 1000 + " | " + Acc.Length + " | " + (LastAcc - Acc).Length / GapMS * 1000);
  45. LastAcc = Acc;
  46. return;
  47. }
  48. if(LockAcc != default)
  49. {
  50. var GyrOperator = -Geometry.Quaternion.Euler((Gyr * GapMS).To<float>());
  51. LockAcc = (GyrOperator * LockAcc.To<float>()).To<double>();
  52. Speed = (GyrOperator * Speed.To<float>()).To<double>();
  53. Speed += (Acc - LockAcc) * 9.8 * (LastGapMS + GapMS) / 1000 / 2;
  54. if (LockMS < 250 && Gyr.Length * 1000 > 5)
  55. {
  56. var distance = Speed.Length / (Gyr.Length * 1000) * 180 / Math.PI;//臂长
  57. if(distance < 1)
  58. {
  59. if (DistanceQueue.Count > 100)
  60. DistanceQueue.Dequeue();
  61. DistanceQueue.Enqueue(distance);
  62. if(DistanceQueue.Count > 30)
  63. {
  64. var mid = DistanceQueue.Count >> 1;
  65. var max = mid + 10;
  66. var list = new List<double>();
  67. for (var i = mid - 9; i < max; ++i)
  68. list.Add(DistanceQueue[i]);
  69. Distance = list.Mean();
  70. }
  71. }
  72. //Debug.Log(Distance);
  73. }
  74. // TestVector.SetAcc((Acc - LockAcc).ToUnityVector(), 1);
  75. // TestVector.SetMag(Speed.ToUnityVector(), 1);
  76. //Debug.Log(Gyr.Length);
  77. LastGapMS = GapMS;
  78. LockMS += GapMS;
  79. }
  80. LastAcc = Acc;
  81. return;
  82. }
  83. //默认轴在后方 z为负
  84. //////// 米/秒 度/毫秒
  85. public Vector<double> GyrToSpeed(Vector<double> Gyr, double GapMS)
  86. {
  87. var GyrOperator = Geometry.Quaternion.Euler((Gyr * GapMS).To<float>()).Inversed;
  88. var axis = Vector<double>.Back * Distance;
  89. axis = (GyrOperator * axis.To<float>() - axis.To<float>()).To<double>() / GapMS * 1000;
  90. // TestVector.SetAcc(axis.ToUnityVector(), 2);
  91. //Debug.Log(axis);
  92. return axis;
  93. }/**/
  94. Vector<double> LastSpeed = default;
  95. public Vector<double> AccCorrection(Vector<double> Gyr, Vector<double> Acc, double GapMS)
  96. {
  97. Vector<double> Speed = GyrToSpeed(Gyr, GapMS);
  98. if (LastSpeed == default)
  99. {
  100. LastSpeed = Speed;
  101. //Debug.Log(Acc.z);
  102. //var Speed =
  103. return Acc;
  104. }
  105. var GyrOperator = Geometry.Quaternion.Euler((Gyr * GapMS).To<float>()).Inversed;
  106. var RotatedLastSpeed = GyrOperator * LastSpeed.To<float>();
  107. var AccCorrection = (Speed - RotatedLastSpeed.To<double>()) / 9.8 / GapMS * 1000;
  108. // TestVector.SetMag(AccCorrection.ToUnityVector(), 2);
  109. // TestVector.SetAcc((Acc + AccCorrection).ToUnityVector(), 3);
  110. LastSpeed = Speed;
  111. return Acc + AccCorrection;//因为Acc是反的
  112. }
  113. }
  114. }