|
|
@@ -101,6 +101,7 @@ public class BluetoothAim : MonoBehaviour
|
|
|
bluetoothHelper.OnCharacteristicChanged += (helper, value, characteristic) =>
|
|
|
{
|
|
|
byte[] bytes = value;
|
|
|
+ Log(String.Join(",", bytes));
|
|
|
BluetoothClient.UploadData(0, bytes);
|
|
|
aimHandler.OnDataReceived(bytes);
|
|
|
};
|
|
|
@@ -157,10 +158,80 @@ class AimHandler
|
|
|
Button CalibrationButton = default;
|
|
|
Text MagScaleText = default;
|
|
|
|
|
|
+ // Vector3 Acc = default;
|
|
|
+ // Vector3 Gyr = default;
|
|
|
+ // Vector3 Mag = default;
|
|
|
+ // long TimeGap = default;
|
|
|
+
|
|
|
+ // static Vector3 AccIdentity = new Vector3(0, -1, 0);
|
|
|
+ // static Vector3 MagIdentity = new Vector3(-1, 2, 0).normalized;
|
|
|
+ // public class State
|
|
|
+ // {
|
|
|
+ // public long TimeGap;
|
|
|
+ // public Vector3 Acc = AccIdentity;
|
|
|
+ // public Vector3 Gyr;
|
|
|
+ // public Vector3 Mag = MagIdentity;
|
|
|
+ // public Quaternion Qua;
|
|
|
+ // public float Variance = 1;
|
|
|
+ // }
|
|
|
+ // List<State> States = new List<State>();
|
|
|
+
|
|
|
+ // Vector3 AccOld;
|
|
|
+ // Vector3 GyrOld;
|
|
|
+ // Vector3 MagOld;
|
|
|
+ // long TimeGapOld;
|
|
|
+
|
|
|
+ // Quaternion o06DOFUpdate(Vector3 AccOld, Vector3 GyrOld, Vector3 MagOld, long TimeGapOld)
|
|
|
+ // {
|
|
|
+ // var Acc = this.AccOld;
|
|
|
+ // var Gyr = this.GyrOld;
|
|
|
+ // var Mag = this.MagOld;
|
|
|
+ // float TimeGap = (TimeGapOld + this.TimeGapOld) / 2;
|
|
|
+ // this.AccOld = AccOld;
|
|
|
+ // this.GyrOld = GyrOld;
|
|
|
+ // this.MagOld = MagOld;
|
|
|
+ // this.TimeGapOld = TimeGapOld;
|
|
|
+
|
|
|
+ // var Last = States.LastOrDefault() ?? new State();
|
|
|
+ // States.Add(new State());
|
|
|
+ // if (States.Count > 10)
|
|
|
+ // States.RemoveAt(0);
|
|
|
+ // var state = States.Last();
|
|
|
+ // state.Acc = Acc;
|
|
|
+ // state.Gyr = Gyr;
|
|
|
+ // state.Mag = Mag;
|
|
|
+
|
|
|
+ // var LastQuaternion = Last.Qua;
|
|
|
+
|
|
|
+ // var newQua = new Quaternion();
|
|
|
+ // newQua.eulerAngles = Gyr * TimeGap;
|
|
|
+ // var quaGyr = LastQuaternion * newQua;
|
|
|
+
|
|
|
+ // float AccLengthToAngle = 90;//1倍引力差相当于多少度方差
|
|
|
+ // float MagLengthToAngle = 90;//1倍磁力差相当于多少度方差
|
|
|
+
|
|
|
+ // float GyrVariance = state.Variance + TimeGap/100 + (Gyr * TimeGap).magnitude * 0.02f;
|
|
|
+ // float AccVariance = TimeGap / 5 + Mathf.Sqrt(Mathf.Pow((Acc.magnitude - 9.8f) / 9.8f * AccLengthToAngle, 2)+ Mathf.Pow(Vector3.Angle(Acc,Last.Acc) * 0.5f, 2));
|
|
|
+ // float MagVariance = TimeGap / 5 + Mathf.Sqrt(Mathf.Pow((Mag.magnitude - 1) / 1 * MagLengthToAngle, 2) + Mathf.Pow(Vector3.Angle(Mag, Last.Mag) * 0.1f, 2));
|
|
|
+ // state.Variance = state.Variance * AccVariance / (state.Variance + AccVariance);
|
|
|
+ // state.Variance = state.Variance * MagVariance / (state.Variance + MagVariance);
|
|
|
+
|
|
|
+ // var quaAccMag = o0Project.o0.FormQuaternion(AccIdentity, MagIdentity, Acc, Mag, AccVariance / (AccVariance + MagVariance));
|
|
|
+ // var quaMinRate = GyrVariance / (GyrVariance + Mathf.Max(AccVariance, MagVariance));
|
|
|
+ // var quaMaxRate = GyrVariance / (GyrVariance + Mathf.Min(AccVariance, MagVariance));
|
|
|
+
|
|
|
+ // Quaternion quaFirst = Quaternion.Slerp(quaGyr, quaAccMag, quaMinRate).normalized;
|
|
|
+
|
|
|
+ // float quaSecondRate = (quaMaxRate - quaMinRate) / (1 - quaMinRate);
|
|
|
+ // state.Qua = AccVariance < MagVariance ? o0Project.o0.FormQuaternion(quaFirst, AccIdentity, Acc, quaSecondRate): o0Project.o0.FormQuaternion(quaFirst, MagIdentity, Mag, quaSecondRate);
|
|
|
+
|
|
|
+ // return state.Qua;
|
|
|
+ // }
|
|
|
+
|
|
|
+ long TimeGap = default;
|
|
|
Vector3 Acc = default;
|
|
|
Vector3 Gyr = default;
|
|
|
Vector3 Mag = default;
|
|
|
- long TimeGap = default;
|
|
|
|
|
|
static Vector3 AccIdentity = new Vector3(0, -1, 0);
|
|
|
static Vector3 MagIdentity = new Vector3(-1, 2, 0).normalized;
|
|
|
@@ -175,10 +246,10 @@ class AimHandler
|
|
|
}
|
|
|
List<State> States = new List<State>();
|
|
|
|
|
|
+ long TimeGapOld;
|
|
|
Vector3 AccOld;
|
|
|
Vector3 GyrOld;
|
|
|
Vector3 MagOld;
|
|
|
- long TimeGapOld;
|
|
|
|
|
|
Quaternion o06DOFUpdate(Vector3 AccOld, Vector3 GyrOld, Vector3 MagOld, long TimeGapOld)
|
|
|
{
|
|
|
@@ -190,7 +261,7 @@ class AimHandler
|
|
|
this.GyrOld = GyrOld;
|
|
|
this.MagOld = MagOld;
|
|
|
this.TimeGapOld = TimeGapOld;
|
|
|
-
|
|
|
+
|
|
|
var Last = States.LastOrDefault() ?? new State();
|
|
|
States.Add(new State());
|
|
|
if (States.Count > 10)
|
|
|
@@ -206,19 +277,20 @@ class AimHandler
|
|
|
newQua.eulerAngles = Gyr * TimeGap;
|
|
|
var quaGyr = LastQuaternion * newQua;
|
|
|
|
|
|
- float AccLengthToAngle = 90;//1倍引力差相当于多少度方差
|
|
|
- float MagLengthToAngle = 90;//1倍磁力差相当于多少度方差
|
|
|
+ float AccLengthToAngle = 180;//1倍引力差相当于多少度方差
|
|
|
+ float MagLengthToAngle = 180;//1倍磁力差相当于多少度方差
|
|
|
+
|
|
|
+ float GyrVariance = state.Variance + TimeGap/1000 + (Gyr * TimeGap).magnitude * 0.05f;
|
|
|
+ float AccVariance = Mathf.Max(TimeGap / 10, Mathf.Sqrt(Mathf.Pow((Acc.magnitude - 9.8f) / 9.8f * AccLengthToAngle, 2)+ Mathf.Pow(Vector3.Angle(Acc,Last.Acc) * 0.8f, 2)));
|
|
|
|
|
|
- float GyrVariance = state.Variance + TimeGap/100 + (Gyr * TimeGap).magnitude * 0.02f;
|
|
|
- float AccVariance = TimeGap / 5 + Mathf.Sqrt(Mathf.Pow((Acc.magnitude - 9.8f) / 9.8f * AccLengthToAngle, 2)+ Mathf.Pow(Vector3.Angle(Acc,Last.Acc) * 0.5f, 2));
|
|
|
- float MagVariance = TimeGap / 5 + Mathf.Sqrt(Mathf.Pow((Mag.magnitude - 1) / 1 * MagLengthToAngle, 2) + Mathf.Pow(Vector3.Angle(Mag, Last.Mag) * 0.1f, 2));
|
|
|
+ float MagVariance = Mathf.Max(TimeGap / 1,Mathf.Sqrt(Mathf.Pow((Mag.magnitude - 1) / 1 * MagLengthToAngle, 2) + Mathf.Pow(Vector3.Angle(Mag, Last.Mag) * 0.05f, 2)));
|
|
|
state.Variance = state.Variance * AccVariance / (state.Variance + AccVariance);
|
|
|
state.Variance = state.Variance * MagVariance / (state.Variance + MagVariance);
|
|
|
|
|
|
var quaAccMag = o0Project.o0.FormQuaternion(AccIdentity, MagIdentity, Acc, Mag, AccVariance / (AccVariance + MagVariance));
|
|
|
+
|
|
|
var quaMinRate = GyrVariance / (GyrVariance + Mathf.Max(AccVariance, MagVariance));
|
|
|
var quaMaxRate = GyrVariance / (GyrVariance + Mathf.Min(AccVariance, MagVariance));
|
|
|
-
|
|
|
Quaternion quaFirst = Quaternion.Slerp(quaGyr, quaAccMag, quaMinRate).normalized;
|
|
|
|
|
|
float quaSecondRate = (quaMaxRate - quaMinRate) / (1 - quaMinRate);
|
|
|
@@ -227,6 +299,7 @@ class AimHandler
|
|
|
return state.Qua;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
//转换读取的数据,无符号->有符号
|
|
|
float TwoByteToFloat(byte b1, byte b2)
|
|
|
{
|
|
|
@@ -235,7 +308,7 @@ class AimHandler
|
|
|
return (float) shortNum;
|
|
|
}
|
|
|
|
|
|
- o0MagneticCalibrater MagCalibrater;
|
|
|
+ o0MagneticCalibraterSimple MagCalibrater;
|
|
|
o0Vector3Filter MagFilter = new o0Vector3Filter();
|
|
|
long msOld = 0;
|
|
|
public AimHandler(Transform controlObj, Button SetIdentity, Button CalibrationButton, Text MagScaleText)
|
|
|
@@ -247,7 +320,7 @@ class AimHandler
|
|
|
|
|
|
SetIdentity.onClick.AddListener(DoIdentity);
|
|
|
|
|
|
- MagCalibrater = new o0MagneticCalibrater();
|
|
|
+ MagCalibrater = new o0MagneticCalibraterSimple();
|
|
|
string caliraterDataStr = PlayerPrefs.GetString("o0MagneticCalibrater");
|
|
|
if (caliraterDataStr.Length > 0)
|
|
|
{
|
|
|
@@ -283,6 +356,7 @@ class AimHandler
|
|
|
|
|
|
public void OnDataReceived(byte[] bytes)
|
|
|
{
|
|
|
+ Debug.Log("瞄准模块数据长度" + bytes.Length);
|
|
|
if (bytes.Length != 26)
|
|
|
{
|
|
|
if (bytes[3] == 125)
|
|
|
@@ -295,7 +369,7 @@ class AimHandler
|
|
|
return;
|
|
|
if (bytes[16] == 0 && bytes[17] == 0 && bytes[18] == 0 && bytes[19] == 0 && bytes[20] == 0 && bytes[21] == 0)
|
|
|
return;
|
|
|
-
|
|
|
+
|
|
|
float ax = -TwoByteToFloat(bytes[4], bytes[5]);
|
|
|
float ay = TwoByteToFloat(bytes[6], bytes[7]);
|
|
|
float az = -TwoByteToFloat(bytes[8], bytes[9]);
|
|
|
@@ -358,18 +432,11 @@ class AimHandler
|
|
|
}
|
|
|
if (doIdentity)
|
|
|
{
|
|
|
- if (Quaternion.Angle(newRotation, baseRotation) < 2)
|
|
|
- {
|
|
|
- if (!hasAutoIdentity)
|
|
|
- {
|
|
|
- controlObj.transform.localRotation = newRotation;
|
|
|
- }
|
|
|
- doIdentity = false;
|
|
|
- hasAutoIdentity = true;
|
|
|
- } else {
|
|
|
- AccIdentity = AccOld;
|
|
|
- MagIdentity = MagOld;
|
|
|
- }
|
|
|
+ AccIdentity = AccOld;
|
|
|
+ MagIdentity = MagOld;
|
|
|
+ States.Last().Qua = controlObj.transform.localRotation = Quaternion.identity;
|
|
|
+ doIdentity = false;
|
|
|
+ hasAutoIdentity = true;
|
|
|
}
|
|
|
}
|
|
|
|