Axis9Handler.cs 8.1 KB

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