o09Axis.cs 37 KB

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