Axis9NopackHandler.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using o0._9Axis;
  5. using Newtonsoft.Json;
  6. public class Axis9NopackHandler : AxisBaseHandler
  7. {
  8. o09Axis _9Axis = new o09Axis();
  9. MagnetometerAutoCalibrater MagCalibrater;
  10. o0GyrCalibrater GyrCalibrater;
  11. public Axis9NopackHandler(AimHandler aimHandler) : base(aimHandler) {}
  12. public override void Init()
  13. {
  14. _9Axis.LoadIdentity();
  15. InitGyr(null);
  16. InitMag(null);
  17. }
  18. public long msOld = 0;
  19. long TimeGap = default;
  20. Vector3 Acc = default;
  21. Vector3 Gyr = default;
  22. Vector3 Mag = default;
  23. public override void Update(byte[] bytes)
  24. {
  25. if (bytes[7] == 0 && bytes[8] == 0 && bytes[9] == 0 && bytes[10] == 0 && bytes[11] == 0 && bytes[12] == 0)
  26. return;
  27. if (bytes[19] == 0 && bytes[20] == 0 && bytes[21] == 0 && bytes[22] == 0 && bytes[23] == 0 && bytes[24] == 0)
  28. return;
  29. float ax = TwoByteToFloat(bytes[7], bytes[8]);
  30. float ay = TwoByteToFloat(bytes[9], bytes[10]);
  31. float az = TwoByteToFloat(bytes[11], bytes[12]);
  32. float roll = TwoByteToFloat(bytes[13], bytes[14]);
  33. float pitch = TwoByteToFloat(bytes[15], bytes[16]);
  34. float yaw = TwoByteToFloat(bytes[17], bytes[18]);
  35. float x = TwoByteToFloat(bytes[19], bytes[20]);
  36. float y = TwoByteToFloat(bytes[21], bytes[22]);
  37. float z = TwoByteToFloat(bytes[23], bytes[24]);
  38. float mxr = TwoByteToFloat(bytes[20], bytes[19]);
  39. float myr = TwoByteToFloat(bytes[22], bytes[21]);
  40. float mzr = TwoByteToFloat(bytes[24], bytes[23]);
  41. if (CommonConfig.devicePlan == 3) {
  42. Acc = new Vector3(az, ay, ax) / 32768 * 16;
  43. Gyr = new Vector3(-yaw, -pitch, -roll) / 32768 * 2;
  44. Mag = new Vector3(z, y, -x) / 32768 * 256; //最新版
  45. } else if (CommonConfig.devicePlan == 0) {
  46. Acc = new Vector3(-az, ay, -ax) / 32768 * 16;
  47. Gyr = new Vector3(yaw, -pitch, roll) / 32768 * 2;
  48. Mag = new Vector3(-z, y, x) / 32768 * 256; //旧版
  49. } else if (CommonConfig.devicePlan == 1) {
  50. Acc = new Vector3(ax, ay, az) / 32768 * 16;
  51. Gyr = new Vector3(roll, pitch, yaw) / 32768 * 2;
  52. Mag = new Vector3(z, x, -y) / 32768 * 256;//第一个6+3硬件
  53. } else if (CommonConfig.devicePlan == 2) {
  54. Acc = new Vector3(-az, ax, -ay) / 32768 * 16;
  55. Gyr = new Vector3(-yaw, roll, -pitch) / 32768 * 2;
  56. Mag = new Vector3(mzr, mxr, -myr) / 32768 * 256;//第二个6+3硬件
  57. }
  58. Gyr = GyrCalibrater.Update(Gyr);
  59. if (GyrCalibrater.Calibration)
  60. {
  61. if (m_aimHandler.gyrCalibrateCompleteCount < m_aimHandler.gyrCalibrateTotalCount)
  62. {
  63. m_aimHandler.gyrCalibrateCompleteCount++;
  64. }
  65. }
  66. mag0o = UnityVectorTo0o(Mag);
  67. try {
  68. if (!MagCalibrater.Update(mag0o)) return;
  69. } catch(System.Exception) {
  70. ResetMag();
  71. PopupMgr.ins.ShowTipTop("磁场干扰请远离电子设备");
  72. return;
  73. }
  74. mag0o = MagCalibrater.EllipsoidFitting.Map(mag0o);
  75. Mag = o0VectorToUnity(mag0o);
  76. var ms = (((long)bytes[1]) *60 + bytes[2])*1000 + (long)TwoByteToFloat(bytes[3], bytes[4]);
  77. if(msOld == default)
  78. {
  79. msOld = ms;
  80. return;
  81. }
  82. TimeGap = ms - msOld;
  83. msOld = ms;
  84. GapMs = TimeGap;
  85. gyr0o = UnityVectorTo0o(Gyr);
  86. acc0o = UnityVectorTo0o(Acc);
  87. distanceToAxis.Update(gyr0o, acc0o, mag0o, GapMs);
  88. acc0o = distanceToAxis.AccCorrection(gyr0o, acc0o, GapMs);/**///轴心偏离矫正
  89. Acc = o0VectorToUnity(acc0o);
  90. m_aimHandler.newRotation = _9Axis.update(Acc, Gyr, Mag, TimeGap);
  91. }
  92. public override void DoIdentity()
  93. {
  94. _9Axis.SetIdentityAndSave();
  95. m_aimHandler.newRotation = _9Axis.getLastState().Qua;;
  96. }
  97. public override void NotifyAxisOnShot()
  98. {
  99. _9Axis.axisCSBridge.o09AxisCS.OnShot(100, 100, 100000);
  100. }
  101. public override void CalibrateGyr(bool calibration) {
  102. GyrCalibrater.Calibration = calibration;
  103. }
  104. public override void InitGyr(string record) {
  105. try {
  106. if (!string.IsNullOrEmpty(record)) {
  107. var res = JsonConvert.DeserializeObject<o0GyrCalibrater>(record);
  108. if (res != null) GyrCalibrater = res;
  109. } else {
  110. GyrCalibrater = new o0GyrCalibrater();
  111. }
  112. } catch(Exception) {}
  113. }
  114. public override void InitMag(string record) {
  115. try {
  116. if (!string.IsNullOrEmpty(record)) {
  117. MagCalibrater = JsonConvert.DeserializeObject<MagnetometerAutoCalibrater>(record, new MagJsonConverter());
  118. } else {
  119. MagCalibrater = new MagnetometerAutoCalibrater();
  120. }
  121. } catch (System.Exception e) {
  122. Debug.LogError("地磁计反序列化出错");
  123. Debug.LogError(e.Message);
  124. Debug.LogError(e.StackTrace);
  125. }
  126. m_aimHandler.CorrectMagCompleted(MagCalibrater.Complete);
  127. }
  128. public override void ResetGyr() {
  129. GyrCalibrater._Average = Vector3.zero;
  130. }
  131. public override void ResetMag() {
  132. MagCalibrater = new MagnetometerAutoCalibrater();
  133. }
  134. public override bool IsGyrCompleted() {
  135. return !GyrCalibrater._Average.Equals(Vector3.zero);
  136. }
  137. public override bool IsMagCompleted() {
  138. return MagCalibrater.Complete;
  139. }
  140. public override IEnumerator SaveGyr() {
  141. yield return null;
  142. string mac = LoginMgr.myUserInfo.mac;
  143. string record = JsonConvert.SerializeObject(GyrCalibrater);
  144. UserPlayer.ins.call("userComp.saveMacCalibrate", 0, mac, record);
  145. }
  146. public override IEnumerator SaveMag() {
  147. yield return null;
  148. string mac = LoginMgr.myUserInfo.mac;
  149. string record = JsonConvert.SerializeObject(MagCalibrater, new JsonConverter[]{new MagJsonConverter()});
  150. UserPlayer.ins.call("userComp.saveMacCalibrate", 1, mac, record);
  151. }
  152. float TwoByteToFloat(byte b1, byte b2)
  153. {
  154. ushort twoByte = (ushort) (b1 * 256 + b2);
  155. short shortNum = (short) twoByte;
  156. return (float) shortNum;
  157. }
  158. o0.Bow.DistanceToAxis distanceToAxis = new o0.Bow.DistanceToAxis();
  159. double GapMs;
  160. o0.Geometry.Vector<double> gyr0o;
  161. o0.Geometry.Vector<double> acc0o;
  162. o0.Geometry.Vector<double> mag0o;
  163. o0.Geometry.Vector<double> UnityVectorTo0o(Vector3 src)
  164. {
  165. return new o0.Geometry.Vector<double>(double.Parse(src.x.ToString()), double.Parse(src.y.ToString()), double.Parse(src.z.ToString()));
  166. }
  167. Vector3 o0VectorToUnity(o0.Geometry.Vector<double> src)
  168. {
  169. return new Vector3(float.Parse(src.x.ToString()), float.Parse(src.y.ToString()), float.Parse(src.z.ToString()));
  170. }
  171. }