Axis9Handler.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. SideTipView.ShowTip("开始序列化九轴数据", Color.white);
  93. string record = attitudeJson.Stringify(_9Axis.Attitude);
  94. if (!string.IsNullOrEmpty(record))
  95. {
  96. SideTipView.ShowTip($"九轴数据序列化完成(长度{record.Length})\nGyrMeanLen:{_9Axis.Attitude.GyrCalibrater.Mean.Length}\nMagVariance:{_9Axis.Attitude.MagCalibrater.Variance}", Color.white);
  97. Debug.Log("9轴数据序列化成功");
  98. if (!IsGyrCompleted()) {
  99. SideTipView.ShowTip("陀螺仪未校准,因校准数据不完整无法上传!", Color.yellow);
  100. return;
  101. }
  102. if (!IsMagCompleted()) {
  103. SideTipView.ShowTip("地磁计未校准,因校准数据不完整无法上传!", Color.yellow);
  104. return;
  105. }
  106. UserComp.Instance.saveCalibrateRecord(record);
  107. SideTipView.ShowTip("正在上传九轴数据", Color.white);
  108. }
  109. }
  110. catch (Exception e)
  111. {
  112. Debug.LogError(e.Message);
  113. Debug.LogError(e.StackTrace);
  114. }
  115. }
  116. public override void ResumeCalibrateRecord(string record)
  117. {
  118. try
  119. {
  120. SideTipView.ShowTip($"成功加载服务端的九轴数据(长度{record.Length})", Color.white);
  121. _9Axis.Attitude = attitudeJson.Parse<o0.IMU._9AxisPreProcessor>(record);
  122. Debug.Log("9轴反序列化完成");
  123. SideTipView.ShowTip($"九轴数据恢复成功\nGyrMeanLen:{_9Axis.Attitude.GyrCalibrater.Mean.Length}\nMagVariance:{_9Axis.Attitude.MagCalibrater.Variance}", Color.white);
  124. PrintAxisInfo();
  125. if (!IsAxisRight())
  126. {
  127. _9Axis.Attitude = new o0.IMU._9AxisPreProcessor(GyrByteIndex, AccByteIndex, MagByteIndex);
  128. Debug.Log("跟保存的轴向不相同,重置校准记录!");
  129. SideTipView.ShowTip("跟保存的轴向不相同,重置校准记录!", Color.yellow);
  130. PrintAxisInfo();
  131. }
  132. Debug.Log("9轴数据恢复结果: " + _9Axis.Attitude.MagCalibrater.Complete);
  133. }
  134. catch (Exception e)
  135. {
  136. Debug.LogError(e.Message);
  137. Debug.LogError(e.StackTrace);
  138. }
  139. m_aimHandler.CorrectMagCompleted(_9Axis.Attitude.MagCalibrater.Complete);
  140. }
  141. //判断轴向是否正确
  142. private bool IsAxisRight()
  143. {
  144. object o = _9Axis.Attitude;
  145. Type t = o.GetType();
  146. var _gyr = t.GetField("GyrByteIndex", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(o);
  147. var _acc = t.GetField("AccByteIndex", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(o);
  148. var _mag = t.GetField("MagByteIndex", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(o);
  149. bool gyrEqual = GyrByteIndex.Equals(_gyr);
  150. bool accEqual = AccByteIndex.Equals(_acc);
  151. bool magEqual = MagByteIndex.Equals(_mag);
  152. return gyrEqual && accEqual && magEqual;
  153. }
  154. //打印当前轴向
  155. public void PrintAxisInfo()
  156. {
  157. object o = _9Axis.Attitude;
  158. Type t = o.GetType();
  159. var _gyr = t.GetField("GyrByteIndex", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(o);
  160. var _acc = t.GetField("AccByteIndex", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(o);
  161. var _mag = t.GetField("MagByteIndex", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(o);
  162. Debug.Log($"当前轴向:gyr:{_gyr.ToString()} acc:{_acc.ToString()} mag:{_mag.ToString()}");
  163. Debug.Log("当前轴向是否正确:" + IsAxisRight());
  164. }
  165. #region 视角归位-恢复和保存
  166. private void LoadIdentity()
  167. {
  168. try {
  169. string accStr = PlayerPrefs.GetString("AccIdentity0", "");
  170. if (accStr.Length > 0) {
  171. double[] arr = JsonConvert.DeserializeObject<double[]>(accStr);
  172. o0.Bow.o09AxisAfterXiaMenFromDll.AccIdentity = new o0.Geometry.Vector<double>(arr);
  173. }
  174. string magStr = PlayerPrefs.GetString("MagIdentity0", "");
  175. if (magStr.Length > 0) {
  176. double[] arr = JsonConvert.DeserializeObject<double[]>(magStr);
  177. o0.Bow.o09AxisAfterXiaMenFromDll.MagIdentity = new o0.Geometry.Vector<double>(arr);
  178. }
  179. } catch (System.Exception e) {
  180. Debug.LogError(e.Message);
  181. Debug.LogError(e.StackTrace);
  182. }
  183. }
  184. private void SetIdentityAndSave()
  185. {
  186. _9Axis.SetIdentity();
  187. //save
  188. var a = o0.Bow.o09AxisAfterXiaMenFromDll.AccIdentity;
  189. var m = o0.Bow.o09AxisAfterXiaMenFromDll.MagIdentity;
  190. PlayerPrefs.SetString("AccIdentity0", JsonConvert.SerializeObject(new double[]{
  191. a.x, a.y, a.z
  192. }));
  193. PlayerPrefs.SetString("MagIdentity0",JsonConvert.SerializeObject(new double[]{
  194. m.x, m.y, m.z
  195. }));
  196. }
  197. #endregion
  198. }