Axis9NopackHandler.cs 6.9 KB

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