|
|
@@ -7,6 +7,7 @@ using UnityEngine.UI;
|
|
|
using Newtonsoft.Json;
|
|
|
using o0._9Axis;
|
|
|
using o0;
|
|
|
+using System.Reflection;
|
|
|
|
|
|
/* 瞄准处理器 */
|
|
|
public class AimHandler : MonoBehaviour
|
|
|
@@ -19,43 +20,11 @@ public class AimHandler : MonoBehaviour
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
- [SerializeField] Button SetIdentityButton;
|
|
|
- [SerializeField] Button MagCalibrationButton;
|
|
|
- [SerializeField] Button GyrCalibrationButton;
|
|
|
- [SerializeField] Text MagScaleText;
|
|
|
- [SerializeField] Text GyrScaleText;
|
|
|
-
|
|
|
- //椭圆对象
|
|
|
- [SerializeField] Ellipse ellipseScript;
|
|
|
- [SerializeField] GameObject AccObj;
|
|
|
- [SerializeField] GameObject MagObj;
|
|
|
-
|
|
|
- [SerializeField] GameObject AccMesh;
|
|
|
- [SerializeField] GameObject GryMesh;
|
|
|
- [SerializeField] GameObject MagMesh;
|
|
|
- [SerializeField] GameObject AMesh;
|
|
|
- [SerializeField] Transform DebugTexts;
|
|
|
- [SerializeField] Transform DrawImage;
|
|
|
-
|
|
|
- long TimeGap = default;
|
|
|
- Vector3 Acc = default;
|
|
|
- Vector3 Gyr = default;
|
|
|
- Vector3 Mag = default;
|
|
|
- public o09Axis _9Axis = new o09Axis();
|
|
|
-
|
|
|
- Vector3 cMaxVector = new Vector3(0,0,0);
|
|
|
- Vector3 cMinVector = new Vector3(0, 0, 0);
|
|
|
-
|
|
|
- [o0.Serialize]
|
|
|
- MagnetometerAutoCalibrater MagCalibrater;
|
|
|
- o0GyrCalibrater GyrCalibrater;
|
|
|
|
|
|
//陀螺仪校准进度记录
|
|
|
[NonSerialized] public int gyrCalibrateCompleteCount = 0;
|
|
|
[NonSerialized] public int gyrCalibrateTotalCount = 2000;
|
|
|
|
|
|
- [NonSerialized] public long msOld = 0;
|
|
|
-
|
|
|
public static AimHandler ins;
|
|
|
|
|
|
void Start()
|
|
|
@@ -64,198 +33,42 @@ public class AimHandler : MonoBehaviour
|
|
|
BluetoothDispatcher.aim = OnDataReceived;
|
|
|
|
|
|
//初始化
|
|
|
- _9Axis.LoadIdentity();
|
|
|
-
|
|
|
- _9Axis.AccMesh = AccMesh;
|
|
|
- _9Axis.GryMesh = GryMesh;
|
|
|
- _9Axis.MagMesh = MagMesh;
|
|
|
-
|
|
|
- for (var i = 0; i < 9; ++i)
|
|
|
- {
|
|
|
- _9Axis.Tester.Add(DrawImage.Find(i.ToString()).gameObject.AddComponent<o0UIRawImageTester>());
|
|
|
- }
|
|
|
-
|
|
|
- for (var i = 0; i < 15; ++i)
|
|
|
- {
|
|
|
- _9Axis.TextTester.Add(DebugTexts.transform.Find("Text" + i.ToString()).gameObject.GetComponent<Text>());
|
|
|
- }
|
|
|
-
|
|
|
- if (SetIdentityButton)
|
|
|
- {
|
|
|
- SetIdentityButton.onClick.AddListener(DoIdentity);
|
|
|
- }
|
|
|
-
|
|
|
- InitGyr(null);
|
|
|
- InitMag(null);
|
|
|
- }
|
|
|
-
|
|
|
- public void InitGyr(string record) {
|
|
|
- try {
|
|
|
- if (!string.IsNullOrEmpty(record)) {
|
|
|
- var res = JsonConvert.DeserializeObject<o0GyrCalibrater>(record);
|
|
|
- if (res != null) GyrCalibrater = res;
|
|
|
- } else {
|
|
|
- GyrCalibrater = new o0GyrCalibrater();
|
|
|
- }
|
|
|
- } catch(Exception) {}
|
|
|
- }
|
|
|
-
|
|
|
- public void InitMag(string record) {
|
|
|
- try {
|
|
|
- if (!string.IsNullOrEmpty(record)) {
|
|
|
- MagCalibrater = JsonConvert.DeserializeObject<MagnetometerAutoCalibrater>(record, new MagJsonConverter());
|
|
|
- } else {
|
|
|
- MagCalibrater = new MagnetometerAutoCalibrater();
|
|
|
- }
|
|
|
- } catch (System.Exception e) {
|
|
|
- Debug.LogError("地磁计反序列化出错");
|
|
|
- Debug.LogError(e.Message);
|
|
|
- Debug.LogError(e.StackTrace);
|
|
|
- }
|
|
|
- magComplete = MagCalibrater.Complete;
|
|
|
+ Init();
|
|
|
}
|
|
|
|
|
|
public void ResetGyr() {
|
|
|
- GyrCalibrater._Average = Vector3.zero;
|
|
|
+ _663Axis.Attitude.GyrCalibrate = true;
|
|
|
+ _663Axis.Attitude.GyrCalibrate = false;
|
|
|
}
|
|
|
|
|
|
public void ResetMag() {
|
|
|
- MagCalibrater = new MagnetometerAutoCalibrater();
|
|
|
+ _663Axis.Attitude.MagCalibrater = new o0.IMU.MagnetometerAutoCalibrater();
|
|
|
}
|
|
|
|
|
|
public bool IsGyrCompleted() {
|
|
|
- return !GyrCalibrater._Average.Equals(Vector3.zero);
|
|
|
+ FieldInfo fieldInfo = _663Axis.Attitude.GetType().GetField("GyrCalibrater1", BindingFlags.Instance | BindingFlags.NonPublic);
|
|
|
+ var mm = fieldInfo.GetValue(_663Axis.Attitude) as MeanMaintainer<o0.Geometry.Vector<double>>;
|
|
|
+ return mm.Count > 0;
|
|
|
}
|
|
|
|
|
|
public bool IsMagCompleted() {
|
|
|
- return MagCalibrater.Complete;
|
|
|
+ return _663Axis.Attitude.MagCalibrater.Complete;
|
|
|
}
|
|
|
|
|
|
public IEnumerator SaveGyr() {
|
|
|
yield return null;
|
|
|
- string mac = LoginMgr.myUserInfo.mac;
|
|
|
- string record = JsonConvert.SerializeObject(GyrCalibrater);
|
|
|
- UserPlayer.ins.call("userComp.saveMacCalibrate", 0, mac, record);
|
|
|
+ SaveCalibrateRecord();
|
|
|
}
|
|
|
|
|
|
public IEnumerator SaveMag() {
|
|
|
yield return null;
|
|
|
- string mac = LoginMgr.myUserInfo.mac;
|
|
|
- string record = JsonConvert.SerializeObject(MagCalibrater, new JsonConverter[]{new MagJsonConverter()});
|
|
|
- UserPlayer.ins.call("userComp.saveMacCalibrate", 1, mac, record);
|
|
|
+ SaveCalibrateRecord();
|
|
|
}
|
|
|
|
|
|
public void CalibrateGyr(bool calibration) {
|
|
|
try {
|
|
|
- GyrCalibrater.Calibration = calibration;
|
|
|
- if (calibration)
|
|
|
- {
|
|
|
- GyrCalibrationButton.GetComponentInChildren<Text>().text = "停止陀螺仪校准";
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- GyrCalibrationButton.GetComponentInChildren<Text>().text = "开始陀螺仪校准";
|
|
|
- }
|
|
|
- } catch (Exception e) { Debug.LogError(e.Message); }
|
|
|
- }
|
|
|
-
|
|
|
- [NonSerialized] public bool isCalibrateMagPerfect = false;
|
|
|
- public void CalibrateMag(bool calibration) {
|
|
|
- // try {
|
|
|
- // if (calibration)
|
|
|
- // {
|
|
|
- // PointCorrector.ins = new PointCorrector(this.ellipseScript, this.MagCalibrater);
|
|
|
- // MagCalibrater.Calibration = calibration;
|
|
|
- // MagCalibrationButton.GetComponentInChildren<Text>().text = "停止地磁计校准";
|
|
|
- // this.cMaxVector = new Vector3(float.MinValue, float.MinValue, float.MinValue);
|
|
|
- // this.cMinVector = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
|
|
|
- // this.ellipseScript.ellipseTran.gameObject.SetActive(false);
|
|
|
- // }
|
|
|
- // else
|
|
|
- // {
|
|
|
- // PointCorrector.ins = null;
|
|
|
- // List<Vector3> list = MagCalibrater.getRecords();
|
|
|
-
|
|
|
- // //停止校准时候,看看数组值
|
|
|
- // float maxDistance = 0f,ratio = 1f;
|
|
|
- // Vector3 maxVector3 = new Vector3(0,0,0);
|
|
|
- // List<Vector3> endRecords = new List<Vector3>();
|
|
|
- // foreach (Vector3 i in list)
|
|
|
- // {
|
|
|
- // Vector3 v = i - MagCalibrater._Center;
|
|
|
- // if (Math.Abs(v.magnitude) > maxDistance)
|
|
|
- // {
|
|
|
- // maxVector3 = v;
|
|
|
- // maxDistance = Math.Abs(v.magnitude);
|
|
|
- // if(Math.Abs(v.magnitude) < Math.Abs(MagCalibrater._Radius.magnitude))
|
|
|
- // ratio = Math.Abs(v.magnitude) / Math.Abs(MagCalibrater._Radius.magnitude);
|
|
|
- // else
|
|
|
- // ratio = Math.Abs(MagCalibrater._Radius.magnitude) / Math.Abs(v.magnitude);
|
|
|
- // }
|
|
|
- // }
|
|
|
- // Debug.LogWarning(maxDistance + " == " + Math.Abs(MagCalibrater._Radius.magnitude) + " == " + MagCalibrater._Radius + " = " + maxVector3);
|
|
|
- // //如果比例效果不理想。可以设置为ratio=0.5f
|
|
|
- // foreach (Vector3 i in list)
|
|
|
- // {
|
|
|
- // //- MagCalibrater._Center
|
|
|
- // Vector3 v = i ;
|
|
|
- // v *= ratio;
|
|
|
- // if(endRecords.Count>3000)
|
|
|
- // {
|
|
|
- // endRecords.RemoveAt(0);
|
|
|
- // }
|
|
|
- // endRecords.Add(v);
|
|
|
- // }
|
|
|
- // this.ellipseScript.ClearAndUpdatePointArray();
|
|
|
- // // this.ellipseScript.DrawPointCloud(endRecords);
|
|
|
-
|
|
|
- // MagCalibrater.Calibration = calibration;
|
|
|
-
|
|
|
- // this.ellipseScript.ellipseTran.gameObject.SetActive(true);
|
|
|
- // //绘制椭圆形
|
|
|
- // if (MagCalibrater._Radius != this.ellipseScript.ellipseTran.localScale)
|
|
|
- // {
|
|
|
- // this.ellipseScript.setEllipseLocalScaleAndCenter(MagCalibrater._Radius, MagCalibrater._Center * ratio);
|
|
|
- // //设置绘制图像相机的对应位置
|
|
|
- // this.ellipseScript.setCameraPos(MagCalibrater._Center * ratio);
|
|
|
- // this.ellipseScript.setCameraSize(MagCalibrater._Radius);
|
|
|
- // }
|
|
|
-
|
|
|
- // MagCalibrationButton.GetComponentInChildren<Text>().text = "开始地磁计校准";
|
|
|
- // PlayerPrefs.SetString("o0MagneticCalibrater", JsonConvert.SerializeObject(MagCalibrater));
|
|
|
-
|
|
|
- // #region 看校准是否完美,求Records方差
|
|
|
- // if (list == null || list.Count == 0) isCalibrateMagPerfect = false;
|
|
|
- // else
|
|
|
- // {
|
|
|
- // double sumV = 0;
|
|
|
- // double[] listArray = new double[list.Count];
|
|
|
- // for (int i = 0; i < list.Count; i++)
|
|
|
- // {
|
|
|
- // listArray[i] = MagCalibrater.Update(list[i]).magnitude;
|
|
|
- // sumV += listArray[i];
|
|
|
- // }
|
|
|
- // double avarageV = sumV / listArray.Length;
|
|
|
- // double varianceV = 0;
|
|
|
- // foreach (double vvv in listArray)
|
|
|
- // {
|
|
|
- // varianceV += Math.Pow(vvv - avarageV, 2);
|
|
|
- // }
|
|
|
- // varianceV /= listArray.Length;
|
|
|
- // isCalibrateMagPerfect = varianceV < 0.001;
|
|
|
- // PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("tip_mag-calibrate-variance-equal") + varianceV);
|
|
|
- // }
|
|
|
- // #endregion
|
|
|
- // }
|
|
|
- // } catch (Exception e) { Debug.LogError(e.Message); }
|
|
|
- }
|
|
|
-
|
|
|
- //转换读取的数据,无符号->有符号
|
|
|
- float TwoByteToFloat(byte b1, byte b2)
|
|
|
- {
|
|
|
- ushort twoByte = (ushort) (b1 * 256 + b2);
|
|
|
- short shortNum = (short) twoByte;
|
|
|
- return (float) shortNum;
|
|
|
+ _663Axis.Attitude.GyrCalibrate = calibration;
|
|
|
+ } catch (Exception) {}
|
|
|
}
|
|
|
|
|
|
public void OnDataReceived(byte[] bytes)
|
|
|
@@ -288,143 +101,31 @@ public class AimHandler : MonoBehaviour
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
|
- // if (DeviceBatteryView.ins) {
|
|
|
- // DeviceBatteryView.ins.labelTemperature.text = (TwoByteToFloat(bytes[5], bytes[6]) / 100).ToString("#0.00") + "℃";
|
|
|
- // }
|
|
|
- if (bytes[7] == 0 && bytes[8] == 0 && bytes[9] == 0 && bytes[10] == 0 && bytes[11] == 0 && bytes[12] == 0)
|
|
|
- return;
|
|
|
- if (bytes[19] == 0 && bytes[20] == 0 && bytes[21] == 0 && bytes[22] == 0 && bytes[23] == 0 && bytes[24] == 0)
|
|
|
- return;
|
|
|
|
|
|
- //if (ban9AxisCalculate) //如果箭射出后禁止九轴计算,就缓存最新几帧九轴数据
|
|
|
- //{
|
|
|
- //
|
|
|
- // if (cached9AxisFrames.Count < 2)
|
|
|
- // {
|
|
|
- // cached9AxisFrames.Enqueue(bytes);
|
|
|
- // }
|
|
|
- // else
|
|
|
- // {
|
|
|
- // cached9AxisFrames.Dequeue();
|
|
|
- // cached9AxisFrames.Enqueue(bytes);
|
|
|
- // }
|
|
|
- // return;
|
|
|
- //}
|
|
|
-
|
|
|
- float ax = TwoByteToFloat(bytes[7], bytes[8]);
|
|
|
- float ay = TwoByteToFloat(bytes[9], bytes[10]);
|
|
|
- float az = TwoByteToFloat(bytes[11], bytes[12]);
|
|
|
-
|
|
|
- float roll = TwoByteToFloat(bytes[13], bytes[14]);
|
|
|
- float pitch = TwoByteToFloat(bytes[15], bytes[16]);
|
|
|
- float yaw = TwoByteToFloat(bytes[17], bytes[18]);
|
|
|
-
|
|
|
- float x = TwoByteToFloat(bytes[19], bytes[20]);
|
|
|
- float y = TwoByteToFloat(bytes[21], bytes[22]);
|
|
|
- float z = TwoByteToFloat(bytes[23], bytes[24]);
|
|
|
-
|
|
|
- float mxr = TwoByteToFloat(bytes[20], bytes[19]);
|
|
|
- float myr = TwoByteToFloat(bytes[22], bytes[21]);
|
|
|
- float mzr = TwoByteToFloat(bytes[24], bytes[23]);
|
|
|
-
|
|
|
- if (CommonConfig.devicePlan == 3) {
|
|
|
- Acc = new Vector3(az, ay, ax) / 32768 * 16;
|
|
|
- Gyr = new Vector3(-yaw, -pitch, -roll) / 32768 * 2;
|
|
|
- Mag = new Vector3(z, y, -x) / 32768 * 256; //最新版
|
|
|
- } else if (CommonConfig.devicePlan == 0) {
|
|
|
- Acc = new Vector3(-az, ay, -ax) / 32768 * 16;
|
|
|
- Gyr = new Vector3(yaw, -pitch, roll) / 32768 * 2;
|
|
|
- Mag = new Vector3(-z, y, x) / 32768 * 256; //旧版
|
|
|
- } else if (CommonConfig.devicePlan == 1) {
|
|
|
- Acc = new Vector3(ax, ay, az) / 32768 * 16;
|
|
|
- Gyr = new Vector3(roll, pitch, yaw) / 32768 * 2;
|
|
|
- Mag = new Vector3(z, x, -y) / 32768 * 256;//第一个6+3硬件
|
|
|
- } else if (CommonConfig.devicePlan == 2) {
|
|
|
- Acc = new Vector3(-az, ax, -ay) / 32768 * 16;
|
|
|
- Gyr = new Vector3(-yaw, roll, -pitch) / 32768 * 2;
|
|
|
- Mag = new Vector3(mzr, mxr, -myr) / 32768 * 256;//第二个6+3硬件
|
|
|
- }
|
|
|
-
|
|
|
- AccObj.transform.GetChild(0).localPosition = Acc;
|
|
|
-
|
|
|
- Gyr = GyrCalibrater.Update(Gyr);
|
|
|
-
|
|
|
- if (GyrCalibrater.Calibration)
|
|
|
+ if (_663Axis.Attitude.GyrCalibrate)
|
|
|
{
|
|
|
- if (gyrCalibrateCompleteCount < gyrCalibrateTotalCount) {
|
|
|
+ if (gyrCalibrateCompleteCount < gyrCalibrateTotalCount)
|
|
|
+ {
|
|
|
gyrCalibrateCompleteCount++;
|
|
|
}
|
|
|
}
|
|
|
- // if (GyrScaleText && GyrCalibrater.Calibration)
|
|
|
- // {
|
|
|
- // // _9Axis.getGyrOld(),states缓存列表没数据时,会报错
|
|
|
- // GyrScaleText.text = "" + (_9Axis.getGyrOld() * 1000000).ToString();
|
|
|
- // }
|
|
|
-
|
|
|
- // if(Mag.x > -128 && Mag.y > -128 && Mag.z > -128 && Mag.x < 128 && Mag.y < 128 && Mag.z < 128)
|
|
|
- // {
|
|
|
- // //绘制地磁计点
|
|
|
- // if (MagCalibrater.Calibration)
|
|
|
- // {
|
|
|
- // this.ellipseScript.AddAndUpdatePointArray(Mag);
|
|
|
-
|
|
|
- // if (Mag.x > cMaxVector.x) cMaxVector.x = Mag.x;
|
|
|
- // if (Mag.y > cMaxVector.y) cMaxVector.y = Mag.y;
|
|
|
- // if (Mag.z > cMaxVector.z) cMaxVector.z = Mag.z;
|
|
|
- // if (Mag.x < cMinVector.x) cMinVector.x = Mag.x;
|
|
|
- // if (Mag.y < cMinVector.y) cMinVector.y = Mag.y;
|
|
|
- // if (Mag.z < cMinVector.z) cMinVector.z = Mag.z;
|
|
|
-
|
|
|
- // Vector3 centerPoint = (this.cMaxVector + this.cMinVector) / 2;
|
|
|
-
|
|
|
- // //设置绘制图像相机的对应位置
|
|
|
- // this.ellipseScript.setCameraPos(centerPoint);
|
|
|
- // this.ellipseScript.setCameraSize(this.cMaxVector - this.cMinVector);
|
|
|
-
|
|
|
- // PointCorrector.ins?.Update(centerPoint);
|
|
|
- // }
|
|
|
- // Mag = MagCalibrater.Update(Mag);
|
|
|
-
|
|
|
- // if (MagScaleText)
|
|
|
- // {
|
|
|
- // MagScaleText.text = MagCalibrater._Radius.ToString();
|
|
|
- // }
|
|
|
-
|
|
|
- // }
|
|
|
- mag0o = UnityVectorTo0o(Mag);
|
|
|
- try {
|
|
|
- if (!MagCalibrater.Update(mag0o)) return;
|
|
|
- } catch(System.Exception) {
|
|
|
- ResetMag();
|
|
|
- PopupMgr.ins.ShowTipTop("磁场干扰请远离电子设备");
|
|
|
- return;
|
|
|
- }
|
|
|
- mag0o = MagCalibrater.EllipsoidFitting.Map(mag0o);
|
|
|
- Mag = o0VectorToUnity(mag0o);
|
|
|
- MagObj.transform.GetChild(0).localPosition = Mag;
|
|
|
|
|
|
- var ms = (((long)bytes[1]) *60 + bytes[2])*1000 + (long)TwoByteToFloat(bytes[3], bytes[4]);
|
|
|
- if(msOld == default)
|
|
|
+ o0.Geometry.Quaternion Qua = o0.Geometry.Quaternion.Identity;
|
|
|
+ try
|
|
|
{
|
|
|
- msOld = ms;
|
|
|
- return;
|
|
|
+ lock (_663Axis)
|
|
|
+ Qua = _663Axis.Update(new byte[] { bytes[13], bytes[14], bytes[15], bytes[16], bytes[17], bytes[18] },
|
|
|
+ new byte[] { bytes[25], bytes[26], bytes[27], bytes[28], bytes[29], bytes[30] },
|
|
|
+ new byte[] { bytes[7], bytes[8], bytes[9], bytes[10], bytes[11], bytes[12] },
|
|
|
+ new byte[] { bytes[19], bytes[20], bytes[21], bytes[22], bytes[23], bytes[24] },
|
|
|
+ new byte[] { bytes[31], bytes[32], bytes[33], bytes[34], bytes[35], bytes[36] },
|
|
|
+ bytes[1], bytes[2], bytes[3], bytes[4]);/**///双陀螺仪
|
|
|
}
|
|
|
- TimeGap = ms - msOld;
|
|
|
- msOld = ms;
|
|
|
-
|
|
|
- GapMs = TimeGap;
|
|
|
- gyr0o = UnityVectorTo0o(Gyr);
|
|
|
- acc0o = UnityVectorTo0o(Acc);
|
|
|
-
|
|
|
- distanceToAxis.Update(gyr0o, acc0o, mag0o, GapMs);
|
|
|
- acc0o = distanceToAxis.AccCorrection(gyr0o, acc0o, GapMs);/**///轴心偏离矫正
|
|
|
- Acc = o0VectorToUnity(acc0o);
|
|
|
-
|
|
|
- var GyrOperator = new Quaternion();
|
|
|
- GyrOperator.eulerAngles = Gyr * TimeGap;
|
|
|
- GryMesh.transform.localRotation *= GyrOperator;
|
|
|
-
|
|
|
- AMesh.transform.localRotation = newRotation = _9Axis.update(Acc, Gyr, Mag, TimeGap);
|
|
|
+ catch (Exception e) {
|
|
|
+ Debug.LogError(e.Message);
|
|
|
+ Debug.LogError(e.StackTrace);
|
|
|
+ }
|
|
|
+ newRotation = o0.Bow.Extension.ToUnityQuaternion(Qua);
|
|
|
|
|
|
if (BowQuatDebug.ins) BowQuatDebug.ins.ShowModuleQuat(newRotation.eulerAngles);
|
|
|
|
|
|
@@ -433,21 +134,9 @@ public class AimHandler : MonoBehaviour
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- o0.Bow.DistanceToAxis distanceToAxis = new o0.Bow.DistanceToAxis();
|
|
|
- double GapMs;
|
|
|
- o0.Geometry.Vector<double> gyr0o;
|
|
|
- o0.Geometry.Vector<double> acc0o;
|
|
|
- o0.Geometry.Vector<double> mag0o;
|
|
|
- o0.Geometry.Vector<double> UnityVectorTo0o(Vector3 src) {
|
|
|
- return new o0.Geometry.Vector<double>(double.Parse(src.x.ToString()), double.Parse(src.y.ToString()), double.Parse(src.z.ToString()));
|
|
|
- }
|
|
|
- Vector3 o0VectorToUnity(o0.Geometry.Vector<double> src) {
|
|
|
- return new Vector3(float.Parse(src.x.ToString()), float.Parse(src.y.ToString()), float.Parse(src.z.ToString()));
|
|
|
- }
|
|
|
-
|
|
|
[NonSerialized] public bool lerpForRotation = true;
|
|
|
[NonSerialized] public float lerpTimeRate = 7;
|
|
|
- public void Update()
|
|
|
+ void Update()
|
|
|
{
|
|
|
if (controlObj && !ban9AxisCalculate)
|
|
|
{
|
|
|
@@ -472,43 +161,316 @@ public class AimHandler : MonoBehaviour
|
|
|
bool magComplete;
|
|
|
|
|
|
private bool ban9AxisCalculate = false;
|
|
|
- private Queue<byte[]> cached9AxisFrames = new Queue<byte[]>();
|
|
|
public void Ban9AxisCalculate(bool ban) {
|
|
|
ban9AxisCalculate = ban;
|
|
|
- if (!ban) {
|
|
|
- msOld = default;
|
|
|
- // try
|
|
|
- // {
|
|
|
- // if (StatesBackDebug.ins) _9Axis.DeleteStatesFromTail(StatesBackDebug.ins.val);
|
|
|
- // }
|
|
|
- // catch (Exception) { }
|
|
|
- //恢复九轴计算时,把缓存的最新几帧计算了
|
|
|
- // bool isFirstFrame = true;
|
|
|
- while (cached9AxisFrames.Count > 0)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- OnDataReceived(cached9AxisFrames.Dequeue());
|
|
|
- //最初的一帧是用来顶掉msOld的,不会进state数组,因此不需要设置方差
|
|
|
- // if (!isFirstFrame) {
|
|
|
- // _9Axis.SetAccMagVariance(10000);
|
|
|
- // }
|
|
|
- // isFirstFrame = false;
|
|
|
- }
|
|
|
- catch (Exception) { }
|
|
|
- }
|
|
|
- //立马应用到控制物体中
|
|
|
- if (controlObj) controlObj.localRotation = newRotation;
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
[NonSerialized] public Quaternion newRotation = Quaternion.identity;
|
|
|
|
|
|
public void DoIdentity()
|
|
|
{
|
|
|
- _9Axis.SetIdentityAndSave();
|
|
|
- Quaternion qua = _9Axis.getLastState().Qua;
|
|
|
+ _663Axis.SetIdentityAndSave();
|
|
|
+ Quaternion qua = o0.Bow.Extension.ToUnityQuaternion(_663Axis.getLastState().Qua);
|
|
|
newRotation = qua;
|
|
|
if (controlObj) controlObj.localRotation = qua;
|
|
|
}
|
|
|
-}
|
|
|
+
|
|
|
+ public void OnShot()
|
|
|
+ {
|
|
|
+ _663Axis.OnShot(100, 100, 100000);
|
|
|
+ }
|
|
|
+
|
|
|
+ private o0.Bow.o0663Axis _663Axis;
|
|
|
+ /*
|
|
|
+ public o0.Geometry.Vector<int> GyrByteIndex = new o0.Geometry.Vector<int>(3, -2, 1);
|
|
|
+ public o0.Geometry.Vector<int> AccByteIndex = new o0.Geometry.Vector<int>(3, -2, 1);
|
|
|
+ public o0.Geometry.Vector<int> MagByteIndex = new o0.Geometry.Vector<int>(-3, 2, 1);/**///9轴 usb向上 pcb向左 电池向右
|
|
|
+ /*
|
|
|
+ public o0.Geometry.Vector<int> GyrByteIndex = new o0.Geometry.Vector<int>(-3, -2, -1);
|
|
|
+ public o0.Geometry.Vector<int> AccByteIndex = new o0.Geometry.Vector<int>(-3, -2, -1);
|
|
|
+ public o0.Geometry.Vector<int> MagByteIndex = new o0.Geometry.Vector<int>(3, 2, -1);/**///9轴 usb向上 pcb向右 电池向左
|
|
|
+ public o0.Geometry.Vector<int> GyrByteIndex = new o0.Geometry.Vector<int>(-3, -2, -1);
|
|
|
+ public o0.Geometry.Vector<int> AccByteIndex = new o0.Geometry.Vector<int>(-3, -2, -1);
|
|
|
+ public o0.Geometry.Vector<int> MagByteIndex = new o0.Geometry.Vector<int>(-3, -1, 2);/**///双陀螺仪 usb向上 pcb向右 电池向右
|
|
|
+
|
|
|
+ private void Init()
|
|
|
+ {
|
|
|
+ _663Axis = new o0.Bow.o0663Axis(GyrByteIndex, GyrByteIndex, AccByteIndex, AccByteIndex, MagByteIndex);
|
|
|
+ _663Axis.Attitude = new o0.IMU._663AxisPreProcessor(GyrByteIndex, GyrByteIndex, AccByteIndex, AccByteIndex, MagByteIndex);
|
|
|
+ _663Axis.LoadIdentity();
|
|
|
+ // StartCoroutine(Test());
|
|
|
+ }
|
|
|
+
|
|
|
+ IEnumerator Test() {
|
|
|
+ while (LoginMgr.myUserInfo.id == 0) yield return null;
|
|
|
+ UserComp.Instance.saveMac2();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void ResumeCalibrateRecord(string record)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ Json.FromJson<o0.IMU._663AxisPreProcessor>(record, ref _663Axis.Attitude);
|
|
|
+ Debug.Log("663轴数据恢复成功" + ", MagComplete" + _663Axis.Attitude.MagCalibrater.Complete);
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ Debug.LogError(e.Message);
|
|
|
+ Debug.LogError(e.StackTrace);
|
|
|
+ }
|
|
|
+ magComplete = _663Axis.Attitude.MagCalibrater.Complete;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void SaveCalibrateRecord()
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ string record = JsonConvert.SerializeObject(_663Axis.Attitude);
|
|
|
+ if (!string.IsNullOrEmpty(record))
|
|
|
+ {
|
|
|
+ Debug.Log("663轴数据序列化成功");
|
|
|
+ UserComp.Instance.saveCalibrateRecord(record);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ Debug.LogError(e.Message);
|
|
|
+ Debug.LogError(e.StackTrace);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public const int DeviceType = 663;
|
|
|
+}
|
|
|
+
|
|
|
+// /* 瞄准处理器 */
|
|
|
+// public class AimHandler : MonoBehaviour
|
|
|
+// {
|
|
|
+// Transform controlObj {
|
|
|
+// get {
|
|
|
+// if (CameraToLook.ins) {
|
|
|
+// return CameraToLook.ins.transform;
|
|
|
+// }
|
|
|
+// return null;
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+// //陀螺仪校准进度记录
|
|
|
+// [NonSerialized] public int gyrCalibrateCompleteCount = 0;
|
|
|
+// [NonSerialized] public int gyrCalibrateTotalCount = 2000;
|
|
|
+
|
|
|
+// public static AimHandler ins;
|
|
|
+
|
|
|
+// void Start()
|
|
|
+// {
|
|
|
+// ins = this;
|
|
|
+// BluetoothDispatcher.aim = OnDataReceived;
|
|
|
+
|
|
|
+// //初始化
|
|
|
+// Init();
|
|
|
+// }
|
|
|
+
|
|
|
+// public void ResetGyr() {
|
|
|
+// _9Axis.Attitude.GyrCalibrate = true;
|
|
|
+// _9Axis.Attitude.GyrCalibrate = false;
|
|
|
+// }
|
|
|
+
|
|
|
+// public void ResetMag() {
|
|
|
+// _9Axis.Attitude.MagCalibrater = new o0.IMU.MagnetometerAutoCalibrater();
|
|
|
+// }
|
|
|
+
|
|
|
+// public bool IsGyrCompleted() {
|
|
|
+// FieldInfo fieldInfo = _9Axis.Attitude.GetType().GetField("GyrCalibrater", BindingFlags.Instance | BindingFlags.NonPublic);
|
|
|
+// var mm = fieldInfo.GetValue(_9Axis.Attitude) as MeanMaintainer<o0.Geometry.Vector<double>>;
|
|
|
+// return mm.Count > 0;
|
|
|
+// }
|
|
|
+
|
|
|
+// public bool IsMagCompleted() {
|
|
|
+// return _9Axis.Attitude.MagCalibrater.Complete;
|
|
|
+// }
|
|
|
+
|
|
|
+// public IEnumerator SaveGyr() {
|
|
|
+// yield return null;
|
|
|
+// SaveCalibrateRecord();
|
|
|
+// }
|
|
|
+
|
|
|
+// public IEnumerator SaveMag() {
|
|
|
+// yield return null;
|
|
|
+// SaveCalibrateRecord();
|
|
|
+// }
|
|
|
+
|
|
|
+// public void CalibrateGyr(bool calibration) {
|
|
|
+// try {
|
|
|
+// _9Axis.Attitude.GyrCalibrate = calibration;
|
|
|
+// } catch (Exception) {}
|
|
|
+// }
|
|
|
+
|
|
|
+// public void OnDataReceived(byte[] bytes)
|
|
|
+// {
|
|
|
+// // Debug.Log("瞄准模块数据长度" + bytes.Length);
|
|
|
+// if (bytes.Length != 27)
|
|
|
+// {
|
|
|
+// if (bytes.Length == 2) {
|
|
|
+// if (bytes[0] == 0x66 && bytes[1] == 0x31) {
|
|
|
+// if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) {
|
|
|
+// //视角回正
|
|
|
+// DoIdentity();
|
|
|
+// //鼠标居中自然会居中
|
|
|
+// } else {
|
|
|
+// AutoResetView.DoIdentity();
|
|
|
+// }
|
|
|
+// } else if (bytes[0] == 0x66 && bytes[1] == 0x32) {
|
|
|
+// if (SB_EventSystem.ins) {
|
|
|
+// // if (SB_EventSystem.ins && !CommonConfig.SpecialVersion1) {
|
|
|
+// //唤起/隐藏虚拟鼠标
|
|
|
+// SB_EventSystem.ins.AwakenSimulateMouse();
|
|
|
+// }
|
|
|
+// } else if (bytes[1] == 10) {
|
|
|
+// //显示电量
|
|
|
+// DeviceBatteryView.ins.RenderBattery(1, bytes[0]);
|
|
|
+// }
|
|
|
+// } else if (bytes[0] == 0x5b) {
|
|
|
+// //红外射击检测
|
|
|
+// ShootCheck.ins.ShootByInfrared(bytes);
|
|
|
+// }
|
|
|
+// return;
|
|
|
+// }
|
|
|
+
|
|
|
+// if (_9Axis.Attitude.GyrCalibrate)
|
|
|
+// {
|
|
|
+// if (gyrCalibrateCompleteCount < gyrCalibrateTotalCount)
|
|
|
+// {
|
|
|
+// gyrCalibrateCompleteCount++;
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+// o0.Geometry.Quaternion Qua = o0.Geometry.Quaternion.Identity;
|
|
|
+// try
|
|
|
+// {
|
|
|
+// lock (_9Axis)
|
|
|
+// Qua = _9Axis.Update(new byte[] { bytes[13], bytes[14], bytes[15], bytes[16], bytes[17], bytes[18] },
|
|
|
+// new byte[] { bytes[7], bytes[8], bytes[9], bytes[10], bytes[11], bytes[12] },
|
|
|
+// new byte[] { bytes[19], bytes[20], bytes[21], bytes[22], bytes[23], bytes[24] },
|
|
|
+// bytes[1], bytes[2], bytes[3], bytes[4]);/**///9轴
|
|
|
+// }
|
|
|
+// catch (Exception e) {
|
|
|
+// Debug.LogError(e.Message);
|
|
|
+// Debug.LogError(e.StackTrace);
|
|
|
+// }
|
|
|
+// newRotation = o0.Bow.Extension.ToUnityQuaternion(Qua);
|
|
|
+
|
|
|
+// if (BowQuatDebug.ins) BowQuatDebug.ins.ShowModuleQuat(newRotation.eulerAngles);
|
|
|
+
|
|
|
+// if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) {
|
|
|
+// SB_EventSystem.ins.MoveSimulateMouse(newRotation);
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+// [NonSerialized] public bool lerpForRotation = true;
|
|
|
+// [NonSerialized] public float lerpTimeRate = 7;
|
|
|
+// void Update()
|
|
|
+// {
|
|
|
+// if (controlObj && !ban9AxisCalculate)
|
|
|
+// {
|
|
|
+// if (lerpForRotation)
|
|
|
+// {
|
|
|
+// controlObj.localRotation = Quaternion.Lerp(controlObj.localRotation, newRotation, Time.deltaTime * lerpTimeRate);
|
|
|
+// }
|
|
|
+// else
|
|
|
+// {
|
|
|
+// controlObj.localRotation = newRotation;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// _magCompleteTemp = IsMagCompleted();
|
|
|
+// if (!magComplete && _magCompleteTemp) {
|
|
|
+// StartCoroutine(SaveMag());
|
|
|
+// PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("tip_mag-calibrate_success"));
|
|
|
+// }
|
|
|
+// magComplete = _magCompleteTemp;
|
|
|
+// }
|
|
|
+
|
|
|
+// bool _magCompleteTemp;
|
|
|
+// bool magComplete;
|
|
|
+
|
|
|
+// private bool ban9AxisCalculate = false;
|
|
|
+// public void Ban9AxisCalculate(bool ban) {
|
|
|
+// ban9AxisCalculate = ban;
|
|
|
+// }
|
|
|
+
|
|
|
+// [NonSerialized] public Quaternion newRotation = Quaternion.identity;
|
|
|
+
|
|
|
+// public void DoIdentity()
|
|
|
+// {
|
|
|
+// _9Axis.SetIdentityAndSave();
|
|
|
+// Quaternion qua = o0.Bow.Extension.ToUnityQuaternion(_9Axis.getLastState().Qua);
|
|
|
+// newRotation = qua;
|
|
|
+// if (controlObj) controlObj.localRotation = qua;
|
|
|
+// }
|
|
|
+
|
|
|
+// public void OnShot()
|
|
|
+// {
|
|
|
+// _9Axis.OnShot(100, 100, 100000);
|
|
|
+// }
|
|
|
+
|
|
|
+// private o0.Bow.o09AxisAfterXiaMenFromDll _9Axis;
|
|
|
+// /*
|
|
|
+// public o0.Geometry.Vector<int> GyrByteIndex = new o0.Geometry.Vector<int>(3, -2, 1);
|
|
|
+// public o0.Geometry.Vector<int> AccByteIndex = new o0.Geometry.Vector<int>(3, -2, 1);
|
|
|
+// public o0.Geometry.Vector<int> MagByteIndex = new o0.Geometry.Vector<int>(-3, 2, 1);/**///9轴 usb向上 pcb向左 电池向右
|
|
|
+// public o0.Geometry.Vector<int> GyrByteIndex = new o0.Geometry.Vector<int>(-3, -2, -1);
|
|
|
+// public o0.Geometry.Vector<int> AccByteIndex = new o0.Geometry.Vector<int>(-3, -2, -1);
|
|
|
+// public o0.Geometry.Vector<int> MagByteIndex = new o0.Geometry.Vector<int>(3, 2, -1);/**///9轴 usb向上 pcb向右 电池向左
|
|
|
+// /*
|
|
|
+// public o0.Geometry.Vector<int> GyrByteIndex = new o0.Geometry.Vector<int>(-3, -2, -1);
|
|
|
+// public o0.Geometry.Vector<int> AccByteIndex = new o0.Geometry.Vector<int>(-3, -2, -1);
|
|
|
+// public o0.Geometry.Vector<int> MagByteIndex = new o0.Geometry.Vector<int>(-3, -1, 2);/**///双陀螺仪 usb向上 pcb向右 电池向右
|
|
|
+// //public static o0.Bow.o0663Axis _663Axis;
|
|
|
+// //public static o0.Bow.o09AxisFromDll _9Axis;
|
|
|
+
|
|
|
+// private void Init()
|
|
|
+// {
|
|
|
+// _9Axis = new o0.Bow.o09AxisAfterXiaMenFromDll(GyrByteIndex, AccByteIndex, MagByteIndex);
|
|
|
+// _9Axis.Attitude = new o0.IMU._9AxisPreProcessor(GyrByteIndex, AccByteIndex, MagByteIndex);
|
|
|
+// _9Axis.LoadIdentity();
|
|
|
+// // StartCoroutine(Test());
|
|
|
+// }
|
|
|
+
|
|
|
+// IEnumerator Test() {
|
|
|
+// while (LoginMgr.myUserInfo.id == 0) yield return null;
|
|
|
+// UserComp.Instance.saveMac2();
|
|
|
+// }
|
|
|
+
|
|
|
+// public void ResumeCalibrateRecord(string record)
|
|
|
+// {
|
|
|
+// try
|
|
|
+// {
|
|
|
+// Json.FromJson<o0.IMU._9AxisPreProcessor>(record, ref _9Axis.Attitude);
|
|
|
+// Debug.Log("9轴数据恢复成功" + ", MagComplete" + _9Axis.Attitude.MagCalibrater.Complete);
|
|
|
+// }
|
|
|
+// catch (Exception e)
|
|
|
+// {
|
|
|
+// Debug.LogError(e.Message);
|
|
|
+// Debug.LogError(e.StackTrace);
|
|
|
+// }
|
|
|
+// magComplete = _9Axis.Attitude.MagCalibrater.Complete;
|
|
|
+// }
|
|
|
+
|
|
|
+// public void SaveCalibrateRecord()
|
|
|
+// {
|
|
|
+// try
|
|
|
+// {
|
|
|
+// string record = JsonConvert.SerializeObject(_9Axis.Attitude);
|
|
|
+// if (!string.IsNullOrEmpty(record))
|
|
|
+// {
|
|
|
+// Debug.Log("9轴数据序列化成功");
|
|
|
+// UserComp.Instance.saveCalibrateRecord(record);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// catch (Exception e)
|
|
|
+// {
|
|
|
+// Debug.LogError(e.Message);
|
|
|
+// Debug.LogError(e.StackTrace);
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+// public const int DeviceType = 9;
|
|
|
+// }
|