Przeglądaj źródła

地磁校准结果检测方式更换

lvjincheng 3 lat temu
rodzic
commit
145af9f28a

+ 25 - 0
Assets/BowArrow/Scripts/Bluetooth/AimHandler.cs

@@ -135,6 +135,7 @@ public class AimHandler : MonoBehaviour
         } catch (Exception e) { Debug.LogError(e.Message); }
     }
 
+    [NonSerialized] public bool isCalibrateMagPerfect = false;
      public void CalibrateMag(bool calibration) {
         try {
             if (calibration)
@@ -148,6 +149,7 @@ public class AimHandler : MonoBehaviour
             else
             {
                 List<Vector3> list = MagCalibrater.getRecords();
+
                 //停止校准时候,看看数组值
                 float maxDistance = 0f,ratio = 1f;
                 Vector3 maxVector3 = new Vector3(0,0,0);
@@ -194,6 +196,29 @@ public class AimHandler : MonoBehaviour
 
                 MagCalibrationButton.GetComponentInChildren<Text>().text = "开始地磁计校准";
                 PlayerPrefs.SetString("o0MagneticCalibrater", JsonConvert.SerializeObject(MagCalibrater));
+
+                #region jc-看校准是否完美,求Records方差
+                if (list == null || list.Count == 0) isCalibrateMagPerfect = false;
+                else
+                {
+                    float sumV = 0;
+                    float[] listArray = new float[list.Count];
+                    for (int i = 0; i < list.Count; i++)
+                    {
+                        listArray[i] = MagCalibrater.Update(list[i]).magnitude;
+                        sumV += listArray[i];
+                    }
+                    float avarageV = sumV / listArray.Length;
+                    float varianceV = 0;
+                    foreach (float vvv in listArray)
+                    {
+                        varianceV += Mathf.Pow(vvv - avarageV, 2);
+                    }
+                    varianceV /= listArray.Length;
+                    isCalibrateMagPerfect = varianceV < 0.001;
+                    Debug.Log("jc-地磁校准方差=" + varianceV);
+                }
+                #endregion
             }
         } catch (Exception e) { Debug.LogError(e.Message); }
     }

+ 16 - 15
Assets/BowArrow/Scripts/View/DeviceCalibrateView.cs

@@ -171,21 +171,22 @@ public class DeviceCalibrateView : MonoBehaviour
         } else {
             AimHandler.ins.CalibrateMag(false);
             //检测校准完成时的圆是否理想
-            bool circleIsOk = true;
-            if (AimHandler.ins) {
-                Vector3 radius = AimHandler.ins.MagCalibrater._Radius;
-                if (radius.Equals(Vector3.zero)) {
-                    circleIsOk = false;
-                } else if (radius.x == 0 || radius.y == 0 || radius.z == 0) {
-                    circleIsOk = false;
-                } else if (
-                    Mathf.Abs(1f - radius.x / radius.y) > 0.2f ||
-                    Mathf.Abs(1f - radius.x / radius.z) > 0.2f ||
-                    Mathf.Abs(1f - radius.y / radius.z) > 0.2f
-                ) {
-                    circleIsOk = false;
-                }
-            }
+            bool circleIsOk = AimHandler.ins.isCalibrateMagPerfect;
+            //bool circleIsOk = true;
+            // if (AimHandler.ins) {
+            //     Vector3 radius = AimHandler.ins.MagCalibrater._Radius;
+            //     if (radius.Equals(Vector3.zero)) {
+            //         circleIsOk = false;
+            //     } else if (radius.x == 0 || radius.y == 0 || radius.z == 0) {
+            //         circleIsOk = false;
+            //     } else if (
+            //         Mathf.Abs(1f - radius.x / radius.y) > 0.2f ||
+            //         Mathf.Abs(1f - radius.x / radius.z) > 0.2f ||
+            //         Mathf.Abs(1f - radius.y / radius.z) > 0.2f
+            //     ) {
+            //         circleIsOk = false;
+            //     }
+            // }
             //提示校准效果不理想
             if (!circleIsOk) activeMagTipFail(true);
             //调整校准按钮