Axis9Handler.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using Newtonsoft.Json;
  5. using System.Reflection;
  6. public class Axis9Handler : AxisBaseHandler
  7. {
  8. private o0.Bow.o09AxisAfterXiaMenFromDll _9Axis;
  9. private AttitudeJson attitudeJson = new AttitudeJson();
  10. /*
  11. public o0.Geometry.Vector<int> GyrByteIndex = new o0.Geometry.Vector<int>(3, -2, 1);
  12. public o0.Geometry.Vector<int> AccByteIndex = new o0.Geometry.Vector<int>(3, -2, 1);
  13. public o0.Geometry.Vector<int> MagByteIndex = new o0.Geometry.Vector<int>(-3, 2, 1);/**///9轴 usb向上 pcb向左 电池向右
  14. public o0.Geometry.Vector<int> GyrByteIndex = new o0.Geometry.Vector<int>(-3, -2, -1);
  15. public o0.Geometry.Vector<int> AccByteIndex = new o0.Geometry.Vector<int>(-3, -2, -1);
  16. public o0.Geometry.Vector<int> MagByteIndex = new o0.Geometry.Vector<int>(3, 2, -1);/**///9轴 usb向上 pcb向右 电池向左
  17. public Axis9Handler(AimHandler aimHandler) : base(aimHandler) {}
  18. public override void Init()
  19. {
  20. CommonConfig.devicePlan = 3;
  21. _9Axis = new o0.Bow.o09AxisAfterXiaMenFromDll(GyrByteIndex, AccByteIndex, MagByteIndex);
  22. _9Axis.Attitude = new o0.IMU._9AxisPreProcessor(GyrByteIndex, AccByteIndex, MagByteIndex);
  23. LoadIdentity();
  24. Debug.Log("9轴启动成功(首次初始化)");
  25. PrintAxisInfo();
  26. }
  27. public override void Update(byte[] bytes)
  28. {
  29. if (_9Axis.Attitude.GyrCalibrate)
  30. {
  31. if (m_aimHandler.gyrCalibrateCompleteCount < m_aimHandler.gyrCalibrateTotalCount)
  32. {
  33. m_aimHandler.gyrCalibrateCompleteCount++;
  34. }
  35. }
  36. o0.Geometry.Quaternion Qua = o0.Geometry.Quaternion.Identity;
  37. try
  38. {
  39. lock (_9Axis)
  40. Qua = _9Axis.Update(new byte[] { bytes[13], bytes[14], bytes[15], bytes[16], bytes[17], bytes[18] },
  41. new byte[] { bytes[7], bytes[8], bytes[9], bytes[10], bytes[11], bytes[12] },
  42. new byte[] { bytes[19], bytes[20], bytes[21], bytes[22], bytes[23], bytes[24] },
  43. bytes[1], bytes[2], bytes[3], bytes[4]);/**///9轴
  44. }
  45. catch (Exception e) {
  46. Debug.LogError(e.Message);
  47. Debug.LogError(e.StackTrace);
  48. }
  49. m_aimHandler.SetNewRotation(Qua);
  50. }
  51. public override void DoIdentity()
  52. {
  53. if (_9Axis.States.Count == 0) return; //没数据时触发视角归位会产生NAN,从而导致算法崩坏
  54. SetIdentityAndSave();
  55. m_aimHandler.SetNewRotation(_9Axis.getLastState().Qua);
  56. }
  57. public override void NotifyAxisOnShot()
  58. {
  59. _9Axis.OnShot(100, 100, 100000);
  60. }
  61. public override void CalibrateGyr(bool calibration) {
  62. try {
  63. _9Axis.Attitude.GyrCalibrate = calibration;
  64. } catch (Exception) {}
  65. }
  66. public override void ResetGyr() {
  67. _9Axis.Attitude.GyrCalibrate = true;
  68. _9Axis.Attitude.GyrCalibrate = false;
  69. Debug.Log("陀螺仪校准结果主动重置!");
  70. }
  71. public override void ResetMag() {
  72. _9Axis.Attitude.MagCalibrater = new o0.IMU.MagnetometerAutoCalibrater(0.001d);
  73. Debug.Log("地磁计校准结果主动重置!");
  74. }
  75. public override bool IsGyrCompleted() {
  76. return _9Axis.Attitude.GyrCalibrater.Count > 0;
  77. }
  78. public override bool IsMagCompleted() {
  79. return _9Axis.Attitude.MagCalibrater.Complete;
  80. }
  81. public override IEnumerator SaveGyr() {
  82. yield return null;
  83. SaveCalibrateRecord();
  84. }
  85. public override IEnumerator SaveMag() {
  86. yield return null;
  87. SaveCalibrateRecord();
  88. }
  89. private void SaveCalibrateRecord()
  90. {
  91. try
  92. {
  93. SideTipView.ShowTip("开始序列化九轴数据", Color.white);
  94. string record = attitudeJson.Stringify(_9Axis.Attitude);
  95. if (!string.IsNullOrEmpty(record))
  96. {
  97. SideTipView.ShowTip($"九轴数据序列化完成(长度{record.Length})\nGyrMeanLen:{_9Axis.Attitude.GyrCalibrater.Mean.Length}\nMagVariance:{_9Axis.Attitude.MagCalibrater.Variance}", Color.white);
  98. Debug.Log("9轴数据序列化成功");
  99. if (!IsGyrCompleted()) {
  100. SideTipView.ShowTip("陀螺仪未校准,因校准数据不完整无法上传!", Color.yellow);
  101. return;
  102. }
  103. if (!IsMagCompleted()) {
  104. SideTipView.ShowTip("地磁计未校准,因校准数据不完整无法上传!", Color.yellow);
  105. return;
  106. }
  107. UserComp.Instance.saveCalibrateRecord(record);
  108. SideTipView.ShowTip("正在上传九轴数据", Color.white);
  109. }
  110. }
  111. catch (Exception e)
  112. {
  113. Debug.LogError(e.Message);
  114. Debug.LogError(e.StackTrace);
  115. }
  116. }
  117. public override void ResumeCalibrateRecord(string record)
  118. {
  119. try
  120. {
  121. SideTipView.ShowTip($"成功加载服务端的九轴数据(长度{record.Length})", Color.white);
  122. _9Axis.Attitude = attitudeJson.Parse<o0.IMU._9AxisPreProcessor>(record);
  123. Debug.Log("9轴反序列化完成");
  124. SideTipView.ShowTip($"九轴数据恢复成功\nGyrMeanLen:{_9Axis.Attitude.GyrCalibrater.Mean.Length}\nMagVariance:{_9Axis.Attitude.MagCalibrater.Variance}", Color.white);
  125. PrintAxisInfo();
  126. if (!IsAxisRight())
  127. {
  128. _9Axis.Attitude = new o0.IMU._9AxisPreProcessor(GyrByteIndex, AccByteIndex, MagByteIndex);
  129. Debug.Log("跟保存的轴向不相同,重置校准记录!");
  130. SideTipView.ShowTip("跟保存的轴向不相同,重置校准记录!", Color.yellow);
  131. PrintAxisInfo();
  132. }
  133. Debug.Log("9轴数据恢复结果: " + _9Axis.Attitude.MagCalibrater.Complete);
  134. }
  135. catch (Exception e)
  136. {
  137. Debug.LogError(e.Message);
  138. Debug.LogError(e.StackTrace);
  139. }
  140. m_aimHandler.CorrectMagCompleted(_9Axis.Attitude.MagCalibrater.Complete);
  141. }
  142. //判断轴向是否正确
  143. private bool IsAxisRight()
  144. {
  145. object o = _9Axis.Attitude;
  146. Type t = o.GetType();
  147. var _gyr = t.GetField("GyrByteIndex", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(o);
  148. var _acc = t.GetField("AccByteIndex", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(o);
  149. var _mag = t.GetField("MagByteIndex", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(o);
  150. bool gyrEqual = GyrByteIndex.Equals(_gyr);
  151. bool accEqual = AccByteIndex.Equals(_acc);
  152. bool magEqual = MagByteIndex.Equals(_mag);
  153. return gyrEqual && accEqual && magEqual;
  154. }
  155. //打印当前轴向
  156. public void PrintAxisInfo()
  157. {
  158. object o = _9Axis.Attitude;
  159. Type t = o.GetType();
  160. var _gyr = t.GetField("GyrByteIndex", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(o);
  161. var _acc = t.GetField("AccByteIndex", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(o);
  162. var _mag = t.GetField("MagByteIndex", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(o);
  163. Debug.Log($"当前轴向:gyr:{_gyr.ToString()} acc:{_acc.ToString()} mag:{_mag.ToString()}");
  164. Debug.Log("当前轴向是否正确:" + IsAxisRight());
  165. }
  166. #region 视角归位-恢复和保存
  167. private void LoadIdentity()
  168. {
  169. try {
  170. string accStr = PlayerPrefs.GetString("AccIdentity0", "");
  171. if (accStr.Length > 0) {
  172. double[] arr = JsonConvert.DeserializeObject<double[]>(accStr);
  173. var v = new o0.Geometry.Vector<double>(arr);
  174. if (!double.IsNaN(v.x) && !double.IsInfinity(v.x)
  175. && !double.IsNaN(v.y) && !double.IsInfinity(v.y)
  176. && !double.IsNaN(v.z) && !double.IsInfinity(v.z)
  177. ) {
  178. o0.Bow.o09AxisAfterXiaMenFromDll.AccIdentity = v;
  179. }
  180. }
  181. string magStr = PlayerPrefs.GetString("MagIdentity0", "");
  182. if (magStr.Length > 0) {
  183. double[] arr = JsonConvert.DeserializeObject<double[]>(magStr);
  184. var v = new o0.Geometry.Vector<double>(arr);
  185. if (!double.IsNaN(v.x) && !double.IsInfinity(v.x)
  186. && !double.IsNaN(v.y) && !double.IsInfinity(v.y)
  187. && !double.IsNaN(v.z) && !double.IsInfinity(v.z)
  188. ) {
  189. o0.Bow.o09AxisAfterXiaMenFromDll.MagIdentity = v;
  190. }
  191. }
  192. } catch (System.Exception e) {
  193. Debug.LogError(e.Message);
  194. Debug.LogError(e.StackTrace);
  195. }
  196. }
  197. private void SetIdentityAndSave()
  198. {
  199. _9Axis.SetIdentity();
  200. //save
  201. var a = o0.Bow.o09AxisAfterXiaMenFromDll.AccIdentity;
  202. var m = o0.Bow.o09AxisAfterXiaMenFromDll.MagIdentity;
  203. PlayerPrefs.SetString("AccIdentity0", JsonConvert.SerializeObject(new double[]{
  204. a.x, a.y, a.z
  205. }));
  206. PlayerPrefs.SetString("MagIdentity0",JsonConvert.SerializeObject(new double[]{
  207. m.x, m.y, m.z
  208. }));
  209. }
  210. #endregion
  211. }