Axis9NopackHandler.cs 6.9 KB

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