Axis663Handler.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using Newtonsoft.Json;
  5. using o0;
  6. using System.Reflection;
  7. public class Axis663Handler : AxisBaseHandler
  8. {
  9. private o0.Bow.o0663Axis _663Axis;
  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. /*
  15. public o0.Geometry.Vector<int> GyrByteIndex = new o0.Geometry.Vector<int>(-3, -2, -1);
  16. public o0.Geometry.Vector<int> AccByteIndex = new o0.Geometry.Vector<int>(-3, -2, -1);
  17. public o0.Geometry.Vector<int> MagByteIndex = new o0.Geometry.Vector<int>(3, 2, -1);/**///9轴 usb向上 pcb向右 电池向左
  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 Axis663Handler(AimHandler aimHandler) : base(aimHandler) {}
  22. public override void Init()
  23. {
  24. _663Axis = new o0.Bow.o0663Axis(GyrByteIndex, GyrByteIndex, AccByteIndex, AccByteIndex, MagByteIndex);
  25. _663Axis.Attitude = new o0.IMU._663AxisPreProcessor(GyrByteIndex, GyrByteIndex, AccByteIndex, AccByteIndex, MagByteIndex);
  26. _663Axis.LoadIdentity();
  27. }
  28. public override void Update(byte[] bytes)
  29. {
  30. if (_663Axis.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 (_663Axis)
  41. Qua = _663Axis.Update(new byte[] { bytes[13], bytes[14], bytes[15], bytes[16], bytes[17], bytes[18] },
  42. new byte[] { bytes[25], bytes[26], bytes[27], bytes[28], bytes[29], bytes[30] },
  43. new byte[] { bytes[7], bytes[8], bytes[9], bytes[10], bytes[11], bytes[12] },
  44. new byte[] { bytes[19], bytes[20], bytes[21], bytes[22], bytes[23], bytes[24] },
  45. new byte[] { bytes[31], bytes[32], bytes[33], bytes[34], bytes[35], bytes[36] },
  46. bytes[1], bytes[2], bytes[3], bytes[4]);/**///双陀螺仪
  47. }
  48. catch (Exception e) {
  49. Debug.LogError(e.Message);
  50. Debug.LogError(e.StackTrace);
  51. }
  52. m_aimHandler.newRotation = o0.Bow.Extension.ToUnityQuaternion(Qua);
  53. }
  54. public override void DoIdentity()
  55. {
  56. _663Axis.SetIdentityAndSave();
  57. m_aimHandler.newRotation = o0.Bow.Extension.ToUnityQuaternion(_663Axis.getLastState().Qua);
  58. }
  59. public override void NotifyAxisOnShot()
  60. {
  61. _663Axis.OnShot(100, 100, 100000);
  62. }
  63. public override void CalibrateGyr(bool calibration) {
  64. try {
  65. _663Axis.Attitude.GyrCalibrate = calibration;
  66. } catch (Exception) {}
  67. }
  68. public override void ResetGyr() {
  69. _663Axis.Attitude.GyrCalibrate = true;
  70. _663Axis.Attitude.GyrCalibrate = false;
  71. }
  72. public override void ResetMag() {
  73. _663Axis.Attitude.MagCalibrater = new o0.IMU.MagnetometerAutoCalibrater();
  74. }
  75. public override bool IsGyrCompleted() {
  76. FieldInfo fieldInfo = _663Axis.Attitude.GetType().GetField("GyrCalibrater1", BindingFlags.Instance | BindingFlags.NonPublic);
  77. var mm = fieldInfo.GetValue(_663Axis.Attitude) as MeanMaintainer<o0.Geometry.Vector<double>>;
  78. return mm.Count > 0;
  79. }
  80. public override bool IsMagCompleted() {
  81. return _663Axis.Attitude.MagCalibrater.Complete;
  82. }
  83. public override IEnumerator SaveGyr() {
  84. yield return null;
  85. SaveCalibrateRecord();
  86. }
  87. public override IEnumerator SaveMag() {
  88. yield return null;
  89. SaveCalibrateRecord();
  90. }
  91. private void SaveCalibrateRecord()
  92. {
  93. try
  94. {
  95. string record = JsonConvert.SerializeObject(_663Axis.Attitude);
  96. if (!string.IsNullOrEmpty(record))
  97. {
  98. Debug.Log("663轴数据序列化成功");
  99. UserComp.Instance.saveCalibrateRecord(record);
  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. Json.FromJson<o0.IMU._663AxisPreProcessor>(record, ref _663Axis.Attitude);
  113. Debug.Log("663轴数据恢复成功" + ", MagComplete" + _663Axis.Attitude.MagCalibrater.Complete);
  114. }
  115. catch (Exception e)
  116. {
  117. Debug.LogError(e.Message);
  118. Debug.LogError(e.StackTrace);
  119. }
  120. m_aimHandler.CorrectMagCompleted(_663Axis.Attitude.MagCalibrater.Complete);
  121. }
  122. }