Axis9Handler.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using Newtonsoft.Json;
  5. using o0;
  6. using System.Reflection;
  7. public class Axis9Handler : AxisBaseHandler
  8. {
  9. private o0.Bow.o09AxisAfterXiaMenFromDll _9Axis;
  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. /*
  18. public o0.Geometry.Vector<int> GyrByteIndex = new o0.Geometry.Vector<int>(-3, -2, -1);
  19. public o0.Geometry.Vector<int> AccByteIndex = new o0.Geometry.Vector<int>(-3, -2, -1);
  20. public o0.Geometry.Vector<int> MagByteIndex = new o0.Geometry.Vector<int>(-3, -1, 2);/**///双陀螺仪 usb向上 pcb向右 电池向右
  21. public Axis9Handler(AimHandler aimHandler) : base(aimHandler) {}
  22. public override void Init()
  23. {
  24. _9Axis = new o0.Bow.o09AxisAfterXiaMenFromDll(GyrByteIndex, AccByteIndex, MagByteIndex);
  25. _9Axis.Attitude = new o0.IMU._9AxisPreProcessor(GyrByteIndex, AccByteIndex, MagByteIndex);
  26. _9Axis.LoadIdentity();
  27. }
  28. public override void Update(byte[] bytes)
  29. {
  30. if (_9Axis.Attitude.GyrCalibrate)
  31. {
  32. if (m_aimHandler.gyrCalibrateCompleteCount < m_aimHandler.gyrCalibrateTotalCount)
  33. {
  34. m_aimHandler.gyrCalibrateCompleteCount++;
  35. }
  36. }
  37. o0.Geometry.Quaternion Qua = o0.Geometry.Quaternion.Identity;
  38. try
  39. {
  40. lock (_9Axis)
  41. Qua = _9Axis.Update(new byte[] { bytes[13], bytes[14], bytes[15], bytes[16], bytes[17], bytes[18] },
  42. new byte[] { bytes[7], bytes[8], bytes[9], bytes[10], bytes[11], bytes[12] },
  43. new byte[] { bytes[19], bytes[20], bytes[21], bytes[22], bytes[23], bytes[24] },
  44. bytes[1], bytes[2], bytes[3], bytes[4]);/**///9轴
  45. }
  46. catch (Exception e) {
  47. Debug.LogError(e.Message);
  48. Debug.LogError(e.StackTrace);
  49. }
  50. m_aimHandler.newRotation = o0.Bow.Extension.ToUnityQuaternion(Qua);
  51. }
  52. public override void DoIdentity()
  53. {
  54. _9Axis.SetIdentityAndSave();
  55. m_aimHandler.newRotation = o0.Bow.Extension.ToUnityQuaternion(_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. }
  70. public override void ResetMag() {
  71. _9Axis.Attitude.MagCalibrater = new o0.IMU.MagnetometerAutoCalibrater();
  72. }
  73. public override bool IsGyrCompleted() {
  74. FieldInfo fieldInfo = _9Axis.Attitude.GetType().GetField("GyrCalibrater", BindingFlags.Instance | BindingFlags.NonPublic);
  75. var mm = fieldInfo.GetValue(_9Axis.Attitude) as MeanMaintainer<o0.Geometry.Vector<double>>;
  76. return mm.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. string record = JsonConvert.SerializeObject(_9Axis.Attitude);
  94. if (!string.IsNullOrEmpty(record))
  95. {
  96. Debug.Log("9轴数据序列化成功");
  97. UserComp.Instance.saveCalibrateRecord(record);
  98. }
  99. }
  100. catch (Exception e)
  101. {
  102. Debug.LogError(e.Message);
  103. Debug.LogError(e.StackTrace);
  104. }
  105. }
  106. public override void ResumeCalibrateRecord(string record)
  107. {
  108. try
  109. {
  110. Json.FromJson<o0.IMU._9AxisPreProcessor>(record, ref _9Axis.Attitude);
  111. Debug.Log("9轴数据恢复成功" + ", MagComplete" + _9Axis.Attitude.MagCalibrater.Complete);
  112. }
  113. catch (Exception e)
  114. {
  115. Debug.LogError(e.Message);
  116. Debug.LogError(e.StackTrace);
  117. }
  118. m_aimHandler.CorrectMagCompleted(_9Axis.Attitude.MagCalibrater.Complete);
  119. }
  120. }