Axis9Handler.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. SetIdentityAndSave();
  54. m_aimHandler.SetNewRotation(_9Axis.getLastState().Qua);
  55. }
  56. public override void NotifyAxisOnShot()
  57. {
  58. _9Axis.OnShot(100, 100, 100000);
  59. }
  60. public override void CalibrateGyr(bool calibration) {
  61. try {
  62. _9Axis.Attitude.GyrCalibrate = calibration;
  63. } catch (Exception) {}
  64. }
  65. public override void ResetGyr() {
  66. _9Axis.Attitude.GyrCalibrate = true;
  67. _9Axis.Attitude.GyrCalibrate = false;
  68. Debug.Log("陀螺仪校准结果主动重置!");
  69. }
  70. public override void ResetMag() {
  71. _9Axis.Attitude.MagCalibrater = new o0.IMU.MagnetometerAutoCalibrater(0.001d);
  72. Debug.Log("地磁计校准结果主动重置!");
  73. }
  74. public override bool IsGyrCompleted() {
  75. return _9Axis.Attitude.GyrCalibrater.Count > 0;
  76. }
  77. public override bool IsMagCompleted() {
  78. return _9Axis.Attitude.MagCalibrater.Complete;
  79. }
  80. public override IEnumerator SaveGyr() {
  81. yield return null;
  82. SaveCalibrateRecord();
  83. }
  84. public override IEnumerator SaveMag() {
  85. yield return null;
  86. SaveCalibrateRecord();
  87. }
  88. private void SaveCalibrateRecord()
  89. {
  90. try
  91. {
  92. string record = attitudeJson.Stringify(_9Axis.Attitude);
  93. if (!string.IsNullOrEmpty(record))
  94. {
  95. Debug.Log("9轴数据序列化成功");
  96. UserComp.Instance.saveCalibrateRecord(record);
  97. }
  98. }
  99. catch (Exception e)
  100. {
  101. Debug.LogError(e.Message);
  102. Debug.LogError(e.StackTrace);
  103. }
  104. }
  105. public override void ResumeCalibrateRecord(string record)
  106. {
  107. try
  108. {
  109. _9Axis.Attitude = attitudeJson.Parse<o0.IMU._9AxisPreProcessor>(record);
  110. Debug.Log("9轴反序列化完成");
  111. PrintAxisInfo();
  112. if (!IsAxisRight())
  113. {
  114. _9Axis.Attitude = new o0.IMU._9AxisPreProcessor(GyrByteIndex, AccByteIndex, MagByteIndex);
  115. Debug.Log("跟保存的轴向不相同,重置校准记录!");
  116. PrintAxisInfo();
  117. }
  118. Debug.Log("9轴数据恢复结果: " + _9Axis.Attitude.MagCalibrater.Complete);
  119. }
  120. catch (Exception e)
  121. {
  122. Debug.LogError(e.Message);
  123. Debug.LogError(e.StackTrace);
  124. }
  125. m_aimHandler.CorrectMagCompleted(_9Axis.Attitude.MagCalibrater.Complete);
  126. }
  127. //判断轴向是否正确
  128. private bool IsAxisRight()
  129. {
  130. object o = _9Axis.Attitude;
  131. Type t = o.GetType();
  132. var _gyr = t.GetField("GyrByteIndex", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(o);
  133. var _acc = t.GetField("AccByteIndex", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(o);
  134. var _mag = t.GetField("MagByteIndex", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(o);
  135. bool gyrEqual = GyrByteIndex.Equals(_gyr);
  136. bool accEqual = AccByteIndex.Equals(_acc);
  137. bool magEqual = MagByteIndex.Equals(_mag);
  138. return gyrEqual && accEqual && magEqual;
  139. }
  140. //打印当前轴向
  141. public void PrintAxisInfo()
  142. {
  143. object o = _9Axis.Attitude;
  144. Type t = o.GetType();
  145. var _gyr = t.GetField("GyrByteIndex", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(o);
  146. var _acc = t.GetField("AccByteIndex", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(o);
  147. var _mag = t.GetField("MagByteIndex", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(o);
  148. Debug.Log($"当前轴向:gyr:{_gyr.ToString()} acc:{_acc.ToString()} mag:{_mag.ToString()}");
  149. Debug.Log("当前轴向是否正确:" + IsAxisRight());
  150. }
  151. #region 视角归位-恢复和保存
  152. private void LoadIdentity()
  153. {
  154. try {
  155. string accStr = PlayerPrefs.GetString("AccIdentity0", "");
  156. if (accStr.Length > 0) {
  157. double[] arr = JsonConvert.DeserializeObject<double[]>(accStr);
  158. o0.Bow.o09AxisAfterXiaMenFromDll.AccIdentity = new o0.Geometry.Vector<double>(arr);
  159. }
  160. string magStr = PlayerPrefs.GetString("MagIdentity0", "");
  161. if (magStr.Length > 0) {
  162. double[] arr = JsonConvert.DeserializeObject<double[]>(magStr);
  163. o0.Bow.o09AxisAfterXiaMenFromDll.MagIdentity = new o0.Geometry.Vector<double>(arr);
  164. }
  165. } catch (System.Exception e) {
  166. Debug.LogError(e.Message);
  167. Debug.LogError(e.StackTrace);
  168. }
  169. }
  170. private void SetIdentityAndSave()
  171. {
  172. _9Axis.SetIdentity();
  173. //save
  174. var a = o0.Bow.o09AxisAfterXiaMenFromDll.AccIdentity;
  175. var m = o0.Bow.o09AxisAfterXiaMenFromDll.MagIdentity;
  176. PlayerPrefs.SetString("AccIdentity0", JsonConvert.SerializeObject(new double[]{
  177. a.x, a.y, a.z
  178. }));
  179. PlayerPrefs.SetString("MagIdentity0",JsonConvert.SerializeObject(new double[]{
  180. m.x, m.y, m.z
  181. }));
  182. }
  183. #endregion
  184. }