o09Axis.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using MathNet.Numerics.LinearAlgebra;
  7. using UnityEngine.UI;
  8. using MathNet.Numerics;
  9. public class o0Vector3Filter
  10. {
  11. Vector3 state = default;
  12. float Variance = 1;
  13. public Vector3 Update(Vector3 v)
  14. {
  15. if (state == default)
  16. return state = v;
  17. Variance += 10;
  18. float mVariance = 1;
  19. state = Vector3.Lerp(state, v, mVariance/ (Variance + mVariance));
  20. Variance = Variance * mVariance / (Variance + mVariance);
  21. return state;
  22. }
  23. }
  24. public class o0MagneticCalibraterEllipsoidFitting//默认在无磁干扰环境下,有磁干扰则无法保证效果
  25. {
  26. [JsonIgnore]
  27. public Vector3 _Center = Vector3.zero;
  28. [JsonIgnore]
  29. Matrix<double> _CorrectMatrix = null;
  30. public float[] Center
  31. {
  32. get
  33. {
  34. return new float[]{_Center.x, _Center.y, _Center.z};
  35. }
  36. set
  37. {
  38. _Center = new Vector3(value[0], value[1], value[2]);
  39. }
  40. }
  41. public double[] CorrectMatrix
  42. {
  43. get
  44. {
  45. if (_CorrectMatrix == null)
  46. return default;
  47. var m = new double[9];
  48. for (var i = 0; i < 3; ++i)
  49. for (var j = 0; j < 3; ++j)
  50. m[j + i * 3] = _CorrectMatrix[i,j];
  51. return m;
  52. }
  53. set
  54. {
  55. _CorrectMatrix = CreateMatrix.Dense<double>(3,3);
  56. for (var i = 0; i < 3; ++i)
  57. for (var j = 0; j < 3; ++j)
  58. _CorrectMatrix[i, j] = value[j + i * 3];
  59. }
  60. }
  61. public o0MagneticCalibraterEllipsoidFitting()
  62. {
  63. //Calibration = true;
  64. }
  65. // public o0MagneticCalibraterEllipsoidFitting(o0Project.Vector3f Center, double[] CorrectMatrix)
  66. public o0MagneticCalibraterEllipsoidFitting(float[] Center, double[] CorrectMatrix)
  67. {
  68. this.Center = Center;
  69. this.CorrectMatrix = CorrectMatrix;
  70. }
  71. [JsonIgnore]
  72. List<Vector3> records = null;
  73. [JsonIgnore]
  74. public Vector3 _Radius = default;
  75. public float[] Radius
  76. {
  77. get
  78. {
  79. return new float[]{_Radius.x, _Radius.y, _Radius.z};
  80. }
  81. set
  82. {
  83. _Radius = new Vector3(value[0], value[1], value[2]);
  84. }
  85. }
  86. public List<Vector3> getRecords() {
  87. //Debug.LogWarning(records);
  88. return records;
  89. }
  90. [JsonIgnore]
  91. List<Vector3> BadRecords = null;
  92. [JsonIgnore]
  93. public bool Calibration
  94. {
  95. get
  96. {
  97. return records != null;
  98. }
  99. set
  100. {
  101. if (value == true)
  102. {
  103. records = new List<Vector3>();
  104. }
  105. else
  106. {
  107. try
  108. {
  109. int mag_data_counter = records.Count; //mag数据数量
  110. double mag_x, mag_y, mag_z;
  111. var mat_D = CreateMatrix.Dense<double>(mag_data_counter, 9);
  112. //读取mag
  113. for (int i = 0; i < mag_data_counter; i++)
  114. {
  115. //mag_x_y_z赋值
  116. mag_x = records[i].x;
  117. mag_y = records[i].y;
  118. mag_z = records[i].z;
  119. mat_D[i, 0] = mag_x * mag_x;
  120. mat_D[i, 1] = mag_y * mag_y;
  121. mat_D[i, 2] = mag_z * mag_z;
  122. mat_D[i, 3] = 2 * mag_x * mag_y;
  123. mat_D[i, 4] = 2 * mag_x * mag_z;
  124. mat_D[i, 5] = 2 * mag_y * mag_z;
  125. mat_D[i, 6] = 2 * mag_x;
  126. mat_D[i, 7] = 2 * mag_y;
  127. mat_D[i, 8] = 2 * mag_z;
  128. }
  129. var mat_DT = mat_D.Transpose();
  130. var mat_Ones = CreateMatrix.Dense<double>(mag_data_counter, 1, 1.0);
  131. var mat_Result = (mat_DT * mat_D).Inverse() * (mat_DT * mat_Ones);
  132. var mat_A_4x4 = CreateMatrix.Dense<double>(4, 4);
  133. mat_A_4x4[0, 0] = mat_Result[0, 0];
  134. mat_A_4x4[0, 1] = mat_Result[3, 0];
  135. mat_A_4x4[0, 2] = mat_Result[4, 0];
  136. mat_A_4x4[0, 3] = mat_Result[6, 0];
  137. mat_A_4x4[1, 0] = mat_Result[3, 0];
  138. mat_A_4x4[1, 1] = mat_Result[1, 0];
  139. mat_A_4x4[1, 2] = mat_Result[5, 0];
  140. mat_A_4x4[1, 3] = mat_Result[7, 0];
  141. mat_A_4x4[2, 0] = mat_Result[4, 0];
  142. mat_A_4x4[2, 1] = mat_Result[5, 0];
  143. mat_A_4x4[2, 2] = mat_Result[2, 0];
  144. mat_A_4x4[2, 3] = mat_Result[8, 0];
  145. mat_A_4x4[3, 0] = mat_Result[6, 0];
  146. mat_A_4x4[3, 1] = mat_Result[7, 0];
  147. mat_A_4x4[3, 2] = mat_Result[8, 0];
  148. mat_A_4x4[3, 3] = -1.0;
  149. var mat_Center = -((mat_A_4x4.SubMatrix(0, 3, 0, 3)).Inverse() * mat_Result.SubMatrix(6, 3, 0, 1));
  150. //椭球圆心 //分块,从0,0开始的3*3的矩阵
  151. var mat_T_4x4 = CreateMatrix.DenseIdentity<double>(4, 4);
  152. mat_T_4x4.SetSubMatrix(3, 1, 0, 3, mat_Center.Transpose());
  153. var mat_R = mat_T_4x4 * mat_A_4x4 * mat_T_4x4.Transpose();
  154. var evd = mat_R.SubMatrix(0, 3, 0, 3) / -mat_R[3, 3];
  155. var eig = evd.Evd();
  156. var mat_Eigval = CreateVector.Dense<double>(3);
  157. var mat_Evecs = eig.EigenVectors;
  158. mat_Eigval[0] = eig.EigenValues[0].Real; //特征值的实部
  159. mat_Eigval[1] = eig.EigenValues[1].Real;
  160. mat_Eigval[2] = eig.EigenValues[2].Real;
  161. var mat_Radii = mat_Eigval.Map(delegate (double x)
  162. {
  163. return 1.0 / Math.Sqrt(Math.Abs(x));
  164. }); //椭球半径,特征值倒数后开方
  165. var mat_Scale = CreateMatrix.DenseIdentity<double>(3, 3);
  166. mat_Scale[0, 0] = mat_Radii[0];
  167. mat_Scale[1, 1] = mat_Radii[1];
  168. mat_Scale[2, 2] = mat_Radii[2];
  169. //double min_Radii = mat_Radii.Minimum(); //返回最小的元素
  170. mat_Scale = mat_Scale.Inverse();// * min_Radii;
  171. var mat_Correct = mat_Evecs * mat_Scale * mat_Evecs.Transpose();
  172. //_Center = new Vector3((float)mat_Center[0], (float)mat_Center[1], (float)mat_Center[2]);
  173. Debug.Log("The Ellipsoid center is:" + mat_Center.ToString());
  174. Debug.Log("The Ellipsoid radii is:" + mat_Radii.ToString());
  175. Debug.Log("The scale matrix is:" + mat_Scale.ToString());
  176. Debug.Log("The correct matrix is:" + mat_Correct.ToString());
  177. _Center = new Vector3((float)mat_Center[0, 0], (float)mat_Center[1, 0], (float)mat_Center[2, 0]);
  178. _Radius = new Vector3((float)mat_Radii[0], (float)mat_Radii[1], (float)mat_Radii[2]);
  179. this._CorrectMatrix = mat_Correct;
  180. {
  181. BadRecords = new List<Vector3>();
  182. var AverageDistance = 0f;
  183. foreach (var i in records)
  184. {
  185. var v = i - new Vector3((float)mat_Center[0, 0], (float)mat_Center[1, 0], (float)mat_Center[2, 0]);
  186. var MathNetV = CreateVector.Dense<double>(3);
  187. MathNetV[0] = v.x;
  188. MathNetV[1] = v.y;
  189. MathNetV[2] = v.z;
  190. //MathNetV = (MathNetV * mat_Scale) * mat_Correct;
  191. MathNetV = (MathNetV) * mat_Correct;
  192. v = new Vector3((float)MathNetV[0], (float)MathNetV[1], (float)MathNetV[2]);
  193. AverageDistance += v.magnitude;
  194. }
  195. AverageDistance /= records.Count;
  196. foreach (var i in records)
  197. {
  198. var v = i - new Vector3((float)mat_Center[0, 0], (float)mat_Center[1, 0], (float)mat_Center[2, 0]);
  199. var MathNetV = CreateVector.Dense<double>(3);
  200. MathNetV[0] = v.x;
  201. MathNetV[1] = v.y;
  202. MathNetV[2] = v.z;
  203. //MathNetV = (MathNetV * mat_Scale) * mat_Correct;
  204. MathNetV = (MathNetV) * mat_Correct;
  205. v = new Vector3((float)MathNetV[0], (float)MathNetV[1], (float)MathNetV[2]);
  206. if(Math.Abs(v.magnitude - AverageDistance) > 0.1* AverageDistance) {
  207. BadRecords.Add(i);
  208. }
  209. }
  210. Debug.Log("BadRecords: "+ BadRecords.Count);
  211. }
  212. }
  213. catch(NonConvergenceException)
  214. {
  215. Debug.Log("数据错误无法拟合");
  216. }
  217. records = null;
  218. }
  219. }
  220. }
  221. public Vector3 Update(Vector3 v)
  222. {
  223. if (v.magnitude > 30)
  224. Debug.Log(v);
  225. if (Calibration)
  226. {
  227. records.Add(v);
  228. return v;
  229. }
  230. if(_CorrectMatrix != null)
  231. {
  232. v -= _Center;
  233. var MathNetV = CreateVector.Dense<double>(3);
  234. MathNetV[0] = v.x;
  235. MathNetV[1] = v.y;
  236. MathNetV[2] = v.z;
  237. //MathNetV = (MathNetV * mat_Scale) * mat_Correct;
  238. MathNetV = (MathNetV) * _CorrectMatrix;
  239. v = new Vector3((float)MathNetV[0], (float)MathNetV[1], (float)MathNetV[2]);
  240. //Debug.Log(v.magnitude);
  241. return v;
  242. }
  243. return v;
  244. }
  245. public float CalibratCompletionPercentage()
  246. {
  247. return 0;
  248. }
  249. }
  250. public class o0MagneticCalibraterSimple//默认在无磁干扰环境下,有磁干扰则无法保证效果
  251. {
  252. [JsonIgnore]
  253. public Vector3 _Center = Vector3.zero;
  254. //Vector3 Center = new Vector3(0,0,0);
  255. [JsonIgnore]
  256. public Vector3 _Radius = new Vector3(2, 2, 2);
  257. public o0Project.Vector3f Center
  258. {
  259. get
  260. {
  261. return new o0Project.Vector3f(_Center.x, _Center.y, _Center.z);
  262. }
  263. set
  264. {
  265. _Center = new Vector3(value.x, value.y, value.z);
  266. }
  267. }
  268. public o0Project.Vector3f Radius
  269. {
  270. get
  271. {
  272. return new o0Project.Vector3f(_Radius.x, _Radius.y, _Radius.z);
  273. }
  274. set
  275. {
  276. _Radius = new Vector3(value.x, value.y, value.z);
  277. }
  278. }
  279. public o0MagneticCalibraterSimple()
  280. {
  281. //Calibration = true;
  282. }
  283. public o0MagneticCalibraterSimple(o0Project.Vector3f Center, o0Project.Vector3f Radius)
  284. {
  285. this.Center = Center;
  286. this.Radius = Radius;
  287. }
  288. [JsonIgnore]
  289. Vector3 Min = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  290. [JsonIgnore]
  291. Vector3 Max = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  292. [JsonIgnore]
  293. public bool Calibration
  294. {
  295. get
  296. {
  297. return !(Min == new Vector3(float.MinValue, float.MinValue, float.MinValue) && Max == new Vector3(float.MaxValue, float.MaxValue, float.MaxValue));
  298. }
  299. set
  300. {
  301. if (value == true)
  302. {
  303. Min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  304. Max = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  305. }
  306. else
  307. {
  308. Min = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  309. Max = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  310. }
  311. }
  312. }
  313. public Vector3 Update(Vector3 v)
  314. {
  315. if (v.magnitude > 30)
  316. Debug.Log(v);
  317. if (Calibration)
  318. {
  319. if (Min.x > v.x)
  320. Min.x = v.x;
  321. if (Min.y > v.y)
  322. Min.y = v.y;
  323. if (Min.z > v.z)
  324. Min.z = v.z;
  325. if (Max.x < v.x)
  326. Max.x = v.x;
  327. if (Max.y < v.y)
  328. Max.y = v.y;
  329. if (Max.z < v.z)
  330. Max.z = v.z;
  331. _Center = (Max + Min) / 2;
  332. _Radius = (Max - Min) / 2;
  333. return v;
  334. }
  335. v -= _Center;
  336. v = new Vector3(v.x / _Radius.x, v.y / _Radius.y, v.z / _Radius.z);
  337. return v;
  338. }
  339. public float CalibratCompletionPercentage()
  340. {
  341. return 0;
  342. }
  343. }
  344. public class o0MagneticCalibrater//默认在无磁干扰环境下,有磁干扰则无法保证效果
  345. {
  346. [JsonIgnore]
  347. public Vector3 _Center = Vector3.zero;
  348. //Vector3 Center = new Vector3(0,0,0);
  349. [JsonIgnore]
  350. public Vector3 _Radius = new Vector3(2, 2, 2);
  351. public o0Project.Vector3f Center
  352. {
  353. get
  354. {
  355. return new o0Project.Vector3f(_Center.x, _Center.y, _Center.z);
  356. }
  357. set
  358. {
  359. _Center = new Vector3(value.x, value.y, value.z);
  360. }
  361. }
  362. public o0Project.Vector3f Radius
  363. {
  364. get
  365. {
  366. return new o0Project.Vector3f(_Radius.x, _Radius.y, _Radius.z);
  367. }
  368. set
  369. {
  370. _Radius = new Vector3(value.x, value.y, value.z);
  371. }
  372. }
  373. public o0MagneticCalibrater()
  374. {
  375. //Calibration = true;
  376. }
  377. public o0MagneticCalibrater(o0Project.Vector3f Center, o0Project.Vector3f Radius)
  378. {
  379. this.Center = Center;
  380. this.Radius = Radius;
  381. }
  382. [JsonIgnore]
  383. HashSet<Vector3> Point = default;
  384. [JsonIgnore]
  385. int PointMaxCount = 50;
  386. [JsonIgnore]
  387. Dictionary<(Vector3, Vector3), float> Distance = default;
  388. public void AddPoint(Vector3 v)
  389. {
  390. if (Point.Contains(v))
  391. return;
  392. foreach (var i in Point)
  393. Distance.Add((i, v), Vector3.Distance(v, i));
  394. Point.Add(v);
  395. }
  396. public void RemovePoint(Vector3 v)
  397. {
  398. Point.Remove(v);
  399. foreach (var i in Point)
  400. {
  401. Distance.Remove((v, i));
  402. Distance.Remove((i, v));
  403. }
  404. }
  405. public float TotalDistance(Vector3 v)
  406. {
  407. float t = 0;
  408. foreach (var i in Point)
  409. {
  410. if (Distance.ContainsKey((i, v)))
  411. {
  412. t += Distance[(i, v)];
  413. continue;
  414. }
  415. else if (Distance.ContainsKey((v, i)))
  416. {
  417. t += Distance[(v, i)];
  418. continue;
  419. }
  420. }
  421. return t;
  422. }
  423. public Vector3 MinDistancePoint()
  424. {
  425. Vector3 minV = default;
  426. float minD = float.MaxValue;
  427. foreach (var i in Point)
  428. {
  429. float d = TotalDistance(i);
  430. if (minV == default || minD > d)
  431. {
  432. minD = d;
  433. minV = i;
  434. }
  435. }
  436. return minV;
  437. }
  438. public Vector3 RadiusScale()
  439. {
  440. Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  441. Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  442. foreach (var i in Point)
  443. {
  444. if (min.x > i.x)
  445. min.x = i.x;
  446. if (min.y > i.y)
  447. min.y = i.y;
  448. if (min.z > i.z)
  449. min.z = i.z;
  450. if (max.x < i.x)
  451. max.x = i.x;
  452. if (max.y < i.y)
  453. max.y = i.y;
  454. if (max.z < i.z)
  455. max.z = i.z;
  456. }
  457. return (max - min) / 2;
  458. }
  459. [JsonIgnore]
  460. public bool Calibration
  461. {
  462. get
  463. {
  464. return Distance != null;
  465. }
  466. set
  467. {
  468. if (value == true)
  469. {
  470. Point = new HashSet<Vector3>();
  471. Distance = new Dictionary<(Vector3, Vector3), float>();
  472. }
  473. else
  474. {
  475. Distance = null;
  476. }
  477. }
  478. }
  479. [JsonIgnore]
  480. public System.Random r = new System.Random();
  481. public Vector3 Update(Vector3 v)
  482. {
  483. if (v.magnitude > 30)
  484. Debug.Log(v);
  485. if (Calibration)
  486. {
  487. AddPoint(v);
  488. if (Point.Count > PointMaxCount)
  489. {
  490. RemovePoint(MinDistancePoint());
  491. _Radius = RadiusScale();
  492. }
  493. Vector3 randomV = Point.ElementAt(r.Next(Point.Count));
  494. var scaledCenter = new Vector3(_Center.x / _Radius.x, _Center.y / _Radius.y, _Center.z / _Radius.z);
  495. var scaledV = new Vector3(randomV.x / _Radius.x, randomV.y / _Radius.y, randomV.z / _Radius.z);
  496. float diff = Vector3.Distance(scaledCenter, scaledV) - 1;
  497. scaledCenter += (scaledV - scaledCenter).normalized * diff * 0.1f;
  498. _Center = new Vector3(scaledCenter.x * _Radius.x, scaledCenter.y * _Radius.y, scaledCenter.z * _Radius.z);
  499. }
  500. /*
  501. if (diff > 0)
  502. {
  503. Center -= v * diff;
  504. }
  505. else
  506. {
  507. }/**/
  508. //Point.Add(v);
  509. //Debug.Log(v.magnitude);
  510. v -= _Center;
  511. v = new Vector3(v.x / _Radius.x, v.y / _Radius.y, v.z / _Radius.z);
  512. return v;
  513. }
  514. public float CalibratCompletionPercentage()
  515. {
  516. if (Point == null)
  517. return 0;
  518. List<float> ScaleDistance = new List<float>();
  519. foreach (var i in Point)
  520. {
  521. var v = i - _Center;
  522. ScaleDistance.Add(new Vector3(v.x / _Radius.x, v.y / _Radius.y, v.z / _Radius.z).magnitude);
  523. }
  524. while (ScaleDistance.Count < PointMaxCount)
  525. ScaleDistance.Add(0);
  526. float average = 0;
  527. foreach (var i in ScaleDistance)
  528. average += i;
  529. average /= ScaleDistance.Count;
  530. float variance = 0;
  531. foreach (var i in ScaleDistance)
  532. variance += Mathf.Pow(average - i, 2);
  533. variance /= ScaleDistance.Count;
  534. return Mathf.Pow((1 - variance / average), 10) * 100;
  535. //return variance;
  536. }
  537. }
  538. public class o0GyrCalibrater
  539. {
  540. [JsonIgnore]
  541. public Vector3 _Average = Vector3.zero;
  542. [JsonIgnore]
  543. public long Count = -1;
  544. [JsonIgnore]
  545. public bool Calibration
  546. {
  547. get
  548. {
  549. return Count != -1;
  550. }
  551. set
  552. {
  553. if (value)
  554. Count = 0;
  555. else
  556. Count = -1;
  557. }
  558. }
  559. public float[] Average
  560. {
  561. get
  562. {
  563. return new float[]{_Average.x, _Average.y, _Average.z};
  564. }
  565. set
  566. {
  567. _Average = new Vector3(value[0], value[1], value[2]);
  568. }
  569. }
  570. public o0GyrCalibrater()
  571. {
  572. }
  573. //[JsonConstructor, o0.BinarySerialization.Constructor]
  574. // public o0GyrCalibrater(o0Project.Vector3f Average)
  575. public o0GyrCalibrater(float[] Average)
  576. {
  577. this.Average = Average;
  578. }
  579. public Vector3 Update(Vector3 v)
  580. {
  581. if (Calibration)
  582. _Average += (v - _Average) / ++Count;
  583. v -= _Average;
  584. if (v.magnitude < 0.0002)
  585. return Vector3.zero;
  586. return v;
  587. }
  588. }
  589. public class o09Axis
  590. {
  591. // public static List<o0UIRawImageTester> Tester = new List<o0UIRawImageTester>();
  592. // public static List<Text> TextTester = new List<Text>();
  593. public List<o0UIRawImageTester> Tester = new List<o0UIRawImageTester>();
  594. public List<Text> TextTester = new List<Text>();
  595. static Vector3 AccIdentity = new Vector3(0, -1, 0);
  596. static Vector3 MagIdentity = new Vector3(-1, 2, 0).normalized;
  597. public class State
  598. {
  599. public long TimeGap;
  600. public Vector3 Acc = AccIdentity;
  601. public Vector3 Gyr;
  602. public Vector3 Mag = MagIdentity;
  603. public Quaternion Qua;
  604. public double Variance = 1;
  605. }
  606. o0Project.Variance HardwareVarianceGyr = new o0Project.Variance(1000);
  607. o0Project.Variance HardwareVarianceAcc = new o0Project.Variance(1000);
  608. o0Project.Variance HardwareVarianceMag = new o0Project.Variance(1000);
  609. public List<State> States = new List<State>();
  610. public Vector3 AccOld;
  611. public Vector3 GyrOld;
  612. public Vector3 MagOld;
  613. public float x;
  614. public float y;
  615. public float z;
  616. long TimeGapOld;
  617. /////////////////////g degree/ms
  618. public Quaternion Update(Vector3 AccOld, Vector3 GyrOld, Vector3 MagOld, long TimeGapOld)
  619. {
  620. o0UIRawImageTester.UpdateAllOffset();
  621. //Debug.Log(TimeGapOld);
  622. var Acc = this.AccOld;
  623. var Gyr = this.GyrOld;
  624. var Mag = this.MagOld;
  625. float TimeGap = (TimeGapOld + this.TimeGapOld) / 2;
  626. this.AccOld = AccOld;
  627. this.GyrOld = GyrOld;
  628. this.MagOld = MagOld;
  629. this.TimeGapOld = TimeGapOld;
  630. var Last = States.LastOrDefault() ?? new State();
  631. if (this.TimeGapOld <= 0)
  632. return Last.Qua;
  633. States.Add(new State());
  634. if (States.Count > 200)
  635. States.RemoveAt(0);
  636. var state = States.Last();
  637. state.Acc = Acc;
  638. state.Gyr = Gyr;
  639. state.Mag = Mag;
  640. //Debug.Log(TimeGap);
  641. HardwareVarianceGyr.Update((Gyr).magnitude);//每毫秒方差2.331017E-09 度左右 0.00000002331017
  642. HardwareVarianceAcc.Update(Vector3.Angle(state.Acc, Last.Acc));//方差0.0012度左右
  643. HardwareVarianceMag.Update(Vector3.Angle(state.Mag, Last.Mag));//方差3.5度左右
  644. //Tester?[7].DrawLine(HardwareVarianceAcc.Value, new Color(0, 0, 0));//0.0012左右
  645. //Tester?[8].DrawLine(HardwareVarianceMag.Value, new Color(0, 0, 0));//3.5左右
  646. //Debug.Log(HardwareVarianceMag.Value);
  647. // var Accwit = GameObject.Find("Accwit");
  648. // var Gyrwit = GameObject.Find("Gyrwit");
  649. // var Magwit = GameObject.Find("Magwit");
  650. var LastQuaternion = Last.Qua;
  651. //var LastQuaternion = Gyrwit.transform.localRotation;
  652. var newQua = new Quaternion();
  653. newQua.eulerAngles = Gyr * TimeGap;
  654. var quaGyr = LastQuaternion * newQua;
  655. // Accwit.transform.localRotation = o0Project.o0.FormQuaternion(Accwit.transform.localRotation, AccIdentity, Acc, 1);
  656. // Magwit.transform.localRotation = o0Project.o0.FormQuaternion(Magwit.transform.localRotation, MagIdentity, Mag, 1);
  657. //Tester?[3].DrawLine(Vector3.Angle(Acc, Last.Acc) / 1, new Color(1, 0, 0));
  658. //Tester?[4].DrawLine(Quaternion.Angle(LastQuaternion, quaGyr) / 45, new Color(1, 0, 0));
  659. //Tester?[5].DrawLine(Vector3.Angle(Mag, Last.Mag) / 5, new Color(1, 0, 0));
  660. double AccLengthToAngle = 5;//1倍引力差相当于多少度方差
  661. double MagLengthToAngle = 5;//1倍磁力差相当于多少度方差
  662. /*
  663. float GyrVariance = Last.Variance + (Gyr * TimeGap).magnitude * 0.05f;
  664. float AccVariance = Mathf.Max(TimeGap / 1, Mathf.Sqrt(Mathf.Pow((Acc.magnitude - 9.8f) / 9.8f * AccLengthToAngle, 2) + Mathf.Pow(Vector3.Angle(Acc, Last.Acc) * 0.8f, 2)));
  665. //Debug.Log(AccVariance);
  666. float MagVariance = Mathf.Max(TimeGap / 0.2f, Mathf.Sqrt(Mathf.Pow((Mag.magnitude - 1) / 1 * MagLengthToAngle, 2) + Mathf.Pow(Vector3.Angle(Mag, Last.Mag) * 0.05f, 2)));
  667. state.Variance = state.Variance * AccVariance / (state.Variance + AccVariance);
  668. state.Variance = state.Variance * MagVariance / (state.Variance + MagVariance);/**/
  669. //测试效果不错但没迭代的版本
  670. /*
  671. *
  672. float GyrVariance = state.Variance + TimeGap/100 + (Gyr * TimeGap).magnitude * 0.05f;
  673. float AccVariance = TimeGap / 30 + Mathf.Sqrt(Mathf.Pow((Acc.magnitude - 9.8f) / 9.8f * AccLengthToAngle, 2)+ Mathf.Pow(Vector3.Angle(Acc,Last.Acc) * 0.5f, 2));
  674. //Debug.Log(AccVariance);
  675. float MagVariance = TimeGap / 1 + Mathf.Sqrt(Mathf.Pow((Mag.magnitude - 1) / 1 * MagLengthToAngle, 2) + Mathf.Pow(Vector3.Angle(Mag, Last.Mag) * 0.1f, 2));
  676. /**/
  677. //Tester?[1].DrawLine(TimeGap / 100f, new Color(0, 0, 1));
  678. //Tester?[2].DrawLine((int)(Last.Variance / 90), new Color(0, 0, 0));
  679. // double GyrVariance = Last.Variance + 0.00000002331017 * TimeGap + Math.Pow((Gyr * TimeGap).magnitude * 0.03, 2);// 指数4 = 方差2 * 欧拉角旋转误差2 移动导致累计误差
  680. // //Debug.Log(Math.Max(0.00000002331017 * TimeGap, Math.Pow((Gyr * TimeGap).magnitude * 0.001f, 2)));
  681. // //Tester?[6].DrawLine((Gyr * TimeGap).magnitude * 0.05f / 90, new Color(0, 0, 0));
  682. // double AccVariance = Math.Max(0.01, Math.Pow((Acc.magnitude - 9.8) / 9.8 * AccLengthToAngle, 4) + Math.Pow(Math.Max(Gyr.magnitude, Vector3.Angle(Acc, Last.Acc) / TimeGap) * 20, 2));
  683. // //double AccVariance = Math.Max(0.01, Math.Pow((Acc.magnitude - 9.8) / 9.8 * AccLengthToAngle, 4) + Math.Pow(Vector3.Angle(Acc, Last.Acc) * 2, 2));
  684. // //Debug.Log(Vector3.Angle(Mag, Last.Mag));
  685. // double MagVariance = Math.Max(3.5, Math.Pow((Mag.magnitude - 1) / 1 * MagLengthToAngle, 4) + Math.Pow(Vector3.Angle(Mag, Last.Mag) * 0.07, 2));
  686. double GyrVariance = Last.Variance + 0.00000002331017 * TimeGap + Math.Pow((Gyr * TimeGap).magnitude , 2) * 0.04;// 指数4 = 方差2 * 欧拉角旋转误差2 移动导致累计误差
  687. double AccVariance = Math.Max(0.01, Math.Pow((Acc.magnitude - 9.8) / 9.8 * AccLengthToAngle, 4) + Math.Pow(Math.Max(Gyr.magnitude, Vector3.Angle(Acc, Last.Acc) / TimeGap), 2) * 400);
  688. //double AccVariance = Math.Max(0.01, Math.Pow(Math.Max(Gyr.magnitude, Vector3.Angle(Acc, Last.Acc) / TimeGap), 2) * 1000);
  689. double MagVariance = Math.Max(3.5, Math.Pow((Mag.magnitude - 1) / 1 * MagLengthToAngle, 4) + Math.Pow(Vector3.Angle(Mag, Last.Mag), 2) * 0.005);
  690. /*
  691. Tester?[1].DrawLine(TimeGap / 100f, new Color(0, 0, 1));
  692. Tester?[2].DrawLine((int)(Last.Variance / 90), new Color(0, 0, 0));
  693. double GyrVariance = Last.Variance + Math.Max(0.00000002331017 * TimeGap, Math.Pow((Gyr * TimeGap).magnitude * 0.001f, 2));// 指数4 = 方差2 * 欧拉角旋转误差2 移动导致累计误差
  694. //Debug.Log(Math.Max(0.00000002331017 * TimeGap, Math.Pow((Gyr * TimeGap).magnitude * 0.001f, 2)));
  695. Tester?[6].DrawLine((Gyr * TimeGap).magnitude * 0.05f / 90, new Color(0, 0, 0));
  696. double AccVariance = Vector3.Angle(Acc, Last.Acc) < 0.01 ? 0.0012f : 99999;
  697. //double AccVariance = Math.Max(0.0012f, Math.Pow((Acc.magnitude - 9.8) / 9.8 * AccLengthToAngle, 2) + Math.Pow(Vector3.Angle(Acc, Last.Acc) * 0.8, 2));
  698. //Debug.Log(Vector3.Angle(Mag, Last.Mag));
  699. double MagVariance = Vector3.Angle(Mag, Last.Mag) < 5 && Vector3.Angle(Mag, Last.Mag) != 0 ? 3.5 : 99999;/**/
  700. //double MagVariance = Math.Max(3.5f, Math.Pow((Mag.magnitude - 1) / 1 * MagLengthToAngle, 2) + Math.Pow(Vector3.Angle(Mag, Last.Mag) * 0.05, 2));
  701. //Debug.Log(MagVariance);
  702. state.Variance = GyrVariance;
  703. state.Variance = state.Variance * (AccVariance+ MagVariance) / (state.Variance + (AccVariance + MagVariance));
  704. //state.Variance = state.Variance * MagVariance / (state.Variance + MagVariance);
  705. //Debug.Log(state.Variance);
  706. //Debug.Log(TextTester[0]);
  707. // TextTester[0].text = "Variance:" + state.Variance;
  708. TextTester[1].text = "GyrVariance:" + GyrVariance;
  709. TextTester[2].text = "StaticGyrVariance:" + 0.00000002331017 * TimeGap;
  710. TextTester[3].text = "MothonGyrVariance:" + Math.Pow((Gyr * TimeGap).magnitude * 0.07, 2);
  711. TextTester[4].text = "GyrSpeed:" + Gyr.magnitude;
  712. TextTester[5].text = "AccVariance:" + AccVariance;
  713. TextTester[6].text = "AccLengthVariance:" + Math.Pow((Acc.magnitude - 9.8) / 9.8 * AccLengthToAngle, 4);
  714. TextTester[7].text = "AccRotate:" + Math.Pow(Math.Max(Gyr.magnitude, Vector3.Angle(Acc, Last.Acc)/TimeGap)* 20, 2);
  715. TextTester[9].text = "AccLength:" + Acc.magnitude;
  716. TextTester[10].text = "Gyr*1000,000:" + (Gyr * 1000000).ToString();
  717. TextTester[11].text = "AngleBetweenIdentity*1000:" + Quaternion.Angle(Last.Qua, Quaternion.identity) * 1000;
  718. // TextTester[12].text = "Qua.eulerAngles.x:" + Last.Qua.eulerAngles.x;
  719. // TextTester[13].text = "Qua.eulerAngles.y:" + Last.Qua.eulerAngles.y;
  720. // TextTester[14].text = "Qua.eulerAngles.z:" + Last.Qua.eulerAngles.z;
  721. /*if (Gyr != Vector3.zero)
  722. {
  723. Debug.Log(Gyr);
  724. }/**/
  725. var quaAccMag = o0Project.o0.FormQuaternion(AccIdentity, MagIdentity, Acc, Mag, (float)(AccVariance / (AccVariance + MagVariance)));
  726. var quaMinRate = GyrVariance / (GyrVariance + Math.Max(AccVariance, MagVariance));
  727. var quaMaxRate = GyrVariance / (GyrVariance + Math.Min(AccVariance, MagVariance));
  728. Quaternion quaFirst = Quaternion.Slerp(quaGyr, quaAccMag, (float)quaMinRate).normalized;
  729. var quaSecondRate = (quaMaxRate - quaMinRate) / (1 - quaMinRate);
  730. // Gyrwit.transform.localRotation = AccVariance < MagVariance ? o0Project.o0.FormQuaternion(quaFirst, AccIdentity, Acc, (float)quaSecondRate) : o0Project.o0.FormQuaternion(quaFirst, MagIdentity, Mag, (float)quaSecondRate);
  731. // state.Qua = Gyrwit.transform.localRotation;
  732. state.Qua = AccVariance < MagVariance ? o0Project.o0.FormQuaternion(quaFirst, AccIdentity, Acc, (float)quaSecondRate) : o0Project.o0.FormQuaternion(quaFirst, MagIdentity, Mag, (float)quaSecondRate);
  733. //Tester?[0].DrawLine(TimeGap / 200, new Color(1, 0, 0));
  734. //Image1.DrawLine();
  735. //Debug.Log((Gyr * TimeGap).magnitude);
  736. //Debug.Log(Quaternion.Angle(state.Qua, Last.Qua));
  737. //TextTester[8].text = "AngleRotated:" + Quaternion.Angle(state.Qua, Last.Qua);
  738. var frontV = Last.Qua * Vector3.forward;
  739.         var upV = Last.Qua * Vector3.up;
  740. x = (Mathf.Atan(upV.y / upV.z) / Mathf.PI * 180 + (upV.z < 0 ? 90:270));
  741.         y = (Mathf.Atan(frontV.z / frontV.x) / Mathf.PI * 180+(frontV.x < 0 ? 90:270));
  742.         z = (Mathf.Atan(upV.y / upV.x) / Mathf.PI * 180 + (upV.x < 0 ? 90:270));
  743. // TextTester[18].text = "x轴角度:" + (Mathf.Atan(upV.y / upV.z) / Mathf.PI * 180 + (upV.z < 0 ? 90:270));
  744. //         TextTester[19].text = "y轴角度:" + (Mathf.Atan(frontV.z / frontV.x) / Mathf.PI * 180+(frontV.x < 0 ? 90:270));
  745. //         TextTester[20].text = "z轴角度" + (Mathf.Atan(upV.y / upV.x) / Mathf.PI * 180 + (upV.x < 0 ? 90:270));
  746. TextTester[12].text = "x轴角度:\n" + (Mathf.Atan(upV.y / upV.z) / Mathf.PI * 180 + (upV.z < 0 ? 90:270));
  747.         TextTester[13].text = "y轴角度:\n" + (Mathf.Atan(frontV.z / frontV.x) / Mathf.PI * 180+(frontV.x < 0 ? 90:270));
  748.         TextTester[14].text = "z轴角度:\n" + (Mathf.Atan(upV.y / upV.x) / Mathf.PI * 180 + (upV.x < 0 ? 90:270));
  749. return state.Qua;
  750. }
  751. public void SetIdentity()
  752. {
  753. Quaternion qua = default;
  754. AccIdentity = AccOld;
  755. MagIdentity = MagOld;
  756. qua = o0Project.o0.FormQuaternion(Quaternion.identity, Vector3.down,AccIdentity, 1);
  757. AccIdentity=qua*AccIdentity;
  758. MagIdentity = qua*MagIdentity;
  759. TextTester[0].text = "AccIdentity:"+AccIdentity;
  760. States.Last().Qua = Quaternion.identity;
  761. States.Last().Qua = qua*States.Last().Qua;//Quaternion.identity;
  762. States.Last().Variance = 0.0000001;
  763. }
  764. public void SetIdentityAccordingToRecords()
  765. {
  766. AccIdentity = Vector3.zero;
  767. foreach (var i in States)
  768. AccIdentity += i.Acc;
  769. AccIdentity /= States.Count;
  770. MagIdentity = Vector3.zero;
  771. foreach (var i in States)
  772. MagIdentity += i.Mag;
  773. MagIdentity /= States.Count;
  774. States.Last().Qua = Quaternion.identity;
  775. States.Last().Variance = 0.0000001;
  776. Vector3.Angle(Vector3.up, States.Last().Mag);
  777. }
  778. }