o09Axis.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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. /*
  218. {
  219. var textV = new o0Project.Variance(records.Count);
  220. foreach (var i in records)
  221. {
  222. var v = i - new Vector3((float)mat_Center[0, 0], (float)mat_Center[1, 0], (float)mat_Center[2, 0]);
  223. var MathNetV = CreateVector.Dense<double>(3);
  224. MathNetV[0] = v.x;
  225. MathNetV[1] = v.y;
  226. MathNetV[2] = v.z;
  227. //MathNetV = (MathNetV * mat_Scale) * mat_Correct;
  228. MathNetV = (MathNetV) * mat_Correct;
  229. v = new Vector3((float)MathNetV[0], (float)MathNetV[1], (float)MathNetV[2]);
  230. textV.Update(v.magnitude);
  231. }
  232. Debug.Log(textV.Value);
  233. }
  234. {
  235. var textV = new o0Project.Variance(records.Count);
  236. foreach (var i in records)
  237. {
  238. var v = i;
  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) * mat_Correct;
  245. v = new Vector3((float)MathNetV[0], (float)MathNetV[1], (float)MathNetV[2]) - new Vector3((float)mat_Center[0, 0], (float)mat_Center[1, 0], (float)mat_Center[2, 0]);
  246. textV.Update(v.magnitude);
  247. }
  248. Debug.Log(textV.Value);
  249. }
  250. {
  251. var textV = new o0Project.Variance(records.Count);
  252. foreach (var i in records)
  253. {
  254. var v = i - new Vector3((float)mat_Center[0, 0], (float)mat_Center[1, 0], (float)mat_Center[2, 0]);
  255. var MathNetV = CreateVector.Dense<double>(3);
  256. MathNetV[0] = v.x;
  257. MathNetV[1] = v.y;
  258. MathNetV[2] = v.z;
  259. MathNetV = (MathNetV * mat_Scale) * mat_Correct;
  260. v = new Vector3((float)MathNetV[0], (float)MathNetV[1], (float)MathNetV[2]);
  261. textV.Update(v.magnitude);
  262. }
  263. Debug.Log(textV.Value);
  264. }
  265. {
  266. var textV = new o0Project.Variance(records.Count);
  267. foreach (var i in records)
  268. {
  269. var v = i - new Vector3((float)mat_Center[0, 0], (float)mat_Center[1, 0], (float)mat_Center[2, 0]);
  270. var MathNetV = CreateVector.Dense<double>(3);
  271. MathNetV[0] = v.x;
  272. MathNetV[1] = v.y;
  273. MathNetV[2] = v.z;
  274. MathNetV = (MathNetV * mat_Correct) * mat_Scale;
  275. v = new Vector3((float)MathNetV[0], (float)MathNetV[1], (float)MathNetV[2]);
  276. textV.Update(v.magnitude);
  277. }
  278. Debug.Log(textV.Value);
  279. }
  280. {
  281. var textV = new o0Project.Variance(records.Count);
  282. foreach (var i in records)
  283. {
  284. var v = i - new Vector3((float)mat_Center[0, 0], (float)mat_Center[1, 0], (float)mat_Center[2, 0]);
  285. var MathNetV = CreateVector.Dense<double>(3);
  286. MathNetV[0] = v.x;
  287. MathNetV[1] = v.y;
  288. MathNetV[2] = v.z;
  289. MathNetV = MathNetV * mat_Scale;
  290. v = new Vector3((float)MathNetV[0], (float)MathNetV[1], (float)MathNetV[2]);
  291. textV.Update(v.magnitude);
  292. }
  293. Debug.Log(textV.Value);
  294. }
  295. {
  296. var textV = new o0Project.Variance(records.Count);
  297. foreach (var i in records)
  298. {
  299. var v = i - new Vector3((float)mat_Center[0, 0]/ (float)mat_Radii[0], (float)mat_Center[1, 0]/(float)mat_Radii[1], (float)mat_Center[2, 0]/(float)mat_Radii[2]);
  300. textV.Update(v.magnitude);
  301. }
  302. Debug.Log(textV.Value);
  303. }
  304. {
  305. var textV = new o0Project.Variance(records.Count);
  306. foreach (var i in records)
  307. {
  308. var v = i -new Vector3((float)mat_Center[0, 0], (float)mat_Center[1, 0], (float)mat_Center[2, 0]);
  309. textV.Update(v.magnitude);
  310. }
  311. Debug.Log(textV.Value);
  312. }/**/
  313. records = null;
  314. }
  315. }
  316. }
  317. public Vector3 Update(Vector3 v)
  318. {
  319. if (v.magnitude > 30)
  320. Debug.Log(v);
  321. if (Calibration)
  322. {
  323. records.Add(v);
  324. return v;
  325. }
  326. if(_CorrectMatrix != null)
  327. {
  328. v -= _Center;
  329. var MathNetV = CreateVector.Dense<double>(3);
  330. MathNetV[0] = v.x;
  331. MathNetV[1] = v.y;
  332. MathNetV[2] = v.z;
  333. //MathNetV = (MathNetV * mat_Scale) * mat_Correct;
  334. MathNetV = (MathNetV) * _CorrectMatrix;
  335. v = new Vector3((float)MathNetV[0], (float)MathNetV[1], (float)MathNetV[2]);
  336. //Debug.Log(v.magnitude);
  337. return v;
  338. }
  339. return v;
  340. }
  341. public float CalibratCompletionPercentage()
  342. {
  343. return 0;
  344. }
  345. }
  346. public class o0MagneticCalibraterSimple//默认在无磁干扰环境下,有磁干扰则无法保证效果
  347. {
  348. [JsonIgnore]
  349. public Vector3 _Center = Vector3.zero;
  350. //Vector3 Center = new Vector3(0,0,0);
  351. [JsonIgnore]
  352. public Vector3 _Radius = new Vector3(2, 2, 2);
  353. public o0Project.Vector3f Center
  354. {
  355. get
  356. {
  357. return new o0Project.Vector3f(_Center.x, _Center.y, _Center.z);
  358. }
  359. set
  360. {
  361. _Center = new Vector3(value.x, value.y, value.z);
  362. }
  363. }
  364. public o0Project.Vector3f Radius
  365. {
  366. get
  367. {
  368. return new o0Project.Vector3f(_Radius.x, _Radius.y, _Radius.z);
  369. }
  370. set
  371. {
  372. _Radius = new Vector3(value.x, value.y, value.z);
  373. }
  374. }
  375. public o0MagneticCalibraterSimple()
  376. {
  377. //Calibration = true;
  378. }
  379. public o0MagneticCalibraterSimple(o0Project.Vector3f Center, o0Project.Vector3f Radius)
  380. {
  381. this.Center = Center;
  382. this.Radius = Radius;
  383. }
  384. [JsonIgnore]
  385. Vector3 Min = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  386. [JsonIgnore]
  387. Vector3 Max = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  388. [JsonIgnore]
  389. public bool Calibration
  390. {
  391. get
  392. {
  393. return !(Min == new Vector3(float.MinValue, float.MinValue, float.MinValue) && Max == new Vector3(float.MaxValue, float.MaxValue, float.MaxValue));
  394. }
  395. set
  396. {
  397. if (value == true)
  398. {
  399. Min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  400. Max = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  401. }
  402. else
  403. {
  404. Min = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  405. Max = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  406. }
  407. }
  408. }
  409. public Vector3 Update(Vector3 v)
  410. {
  411. if (v.magnitude > 30)
  412. Debug.Log(v);
  413. if (Calibration)
  414. {
  415. if (Min.x > v.x)
  416. Min.x = v.x;
  417. if (Min.y > v.y)
  418. Min.y = v.y;
  419. if (Min.z > v.z)
  420. Min.z = v.z;
  421. if (Max.x < v.x)
  422. Max.x = v.x;
  423. if (Max.y < v.y)
  424. Max.y = v.y;
  425. if (Max.z < v.z)
  426. Max.z = v.z;
  427. _Center = (Max + Min) / 2;
  428. _Radius = (Max - Min) / 2;
  429. return v;
  430. }
  431. v -= _Center;
  432. v = new Vector3(v.x / _Radius.x, v.y / _Radius.y, v.z / _Radius.z);
  433. return v;
  434. }
  435. public float CalibratCompletionPercentage()
  436. {
  437. return 0;
  438. }
  439. }
  440. public class o0MagneticCalibrater//默认在无磁干扰环境下,有磁干扰则无法保证效果
  441. {
  442. [JsonIgnore]
  443. public Vector3 _Center = Vector3.zero;
  444. //Vector3 Center = new Vector3(0,0,0);
  445. [JsonIgnore]
  446. public Vector3 _Radius = new Vector3(2, 2, 2);
  447. public o0Project.Vector3f Center
  448. {
  449. get
  450. {
  451. return new o0Project.Vector3f(_Center.x, _Center.y, _Center.z);
  452. }
  453. set
  454. {
  455. _Center = new Vector3(value.x, value.y, value.z);
  456. }
  457. }
  458. public o0Project.Vector3f Radius
  459. {
  460. get
  461. {
  462. return new o0Project.Vector3f(_Radius.x, _Radius.y, _Radius.z);
  463. }
  464. set
  465. {
  466. _Radius = new Vector3(value.x, value.y, value.z);
  467. }
  468. }
  469. public o0MagneticCalibrater()
  470. {
  471. //Calibration = true;
  472. }
  473. public o0MagneticCalibrater(o0Project.Vector3f Center, o0Project.Vector3f Radius)
  474. {
  475. this.Center = Center;
  476. this.Radius = Radius;
  477. }
  478. [JsonIgnore]
  479. HashSet<Vector3> Point = default;
  480. [JsonIgnore]
  481. int PointMaxCount = 50;
  482. [JsonIgnore]
  483. Dictionary<(Vector3, Vector3), float> Distance = default;
  484. public void AddPoint(Vector3 v)
  485. {
  486. if (Point.Contains(v))
  487. return;
  488. foreach (var i in Point)
  489. Distance.Add((i, v), Vector3.Distance(v, i));
  490. Point.Add(v);
  491. }
  492. public void RemovePoint(Vector3 v)
  493. {
  494. Point.Remove(v);
  495. foreach (var i in Point)
  496. {
  497. Distance.Remove((v, i));
  498. Distance.Remove((i, v));
  499. }
  500. }
  501. public float TotalDistance(Vector3 v)
  502. {
  503. float t = 0;
  504. foreach (var i in Point)
  505. {
  506. if (Distance.ContainsKey((i, v)))
  507. {
  508. t += Distance[(i, v)];
  509. continue;
  510. }
  511. else if (Distance.ContainsKey((v, i)))
  512. {
  513. t += Distance[(v, i)];
  514. continue;
  515. }
  516. }
  517. return t;
  518. }
  519. public Vector3 MinDistancePoint()
  520. {
  521. Vector3 minV = default;
  522. float minD = float.MaxValue;
  523. foreach (var i in Point)
  524. {
  525. float d = TotalDistance(i);
  526. if (minV == default || minD > d)
  527. {
  528. minD = d;
  529. minV = i;
  530. }
  531. }
  532. return minV;
  533. }
  534. public Vector3 RadiusScale()
  535. {
  536. Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  537. Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  538. foreach (var i in Point)
  539. {
  540. if (min.x > i.x)
  541. min.x = i.x;
  542. if (min.y > i.y)
  543. min.y = i.y;
  544. if (min.z > i.z)
  545. min.z = i.z;
  546. if (max.x < i.x)
  547. max.x = i.x;
  548. if (max.y < i.y)
  549. max.y = i.y;
  550. if (max.z < i.z)
  551. max.z = i.z;
  552. }
  553. return (max - min) / 2;
  554. }
  555. [JsonIgnore]
  556. public bool Calibration
  557. {
  558. get
  559. {
  560. return Distance != null;
  561. }
  562. set
  563. {
  564. if (value == true)
  565. {
  566. Point = new HashSet<Vector3>();
  567. Distance = new Dictionary<(Vector3, Vector3), float>();
  568. }
  569. else
  570. {
  571. Distance = null;
  572. }
  573. }
  574. }
  575. [JsonIgnore]
  576. public System.Random r = new System.Random();
  577. public Vector3 Update(Vector3 v)
  578. {
  579. if (v.magnitude > 30)
  580. Debug.Log(v);
  581. if (Calibration)
  582. {
  583. AddPoint(v);
  584. if (Point.Count > PointMaxCount)
  585. {
  586. RemovePoint(MinDistancePoint());
  587. _Radius = RadiusScale();
  588. }
  589. Vector3 randomV = Point.ElementAt(r.Next(Point.Count));
  590. var scaledCenter = new Vector3(_Center.x / _Radius.x, _Center.y / _Radius.y, _Center.z / _Radius.z);
  591. var scaledV = new Vector3(randomV.x / _Radius.x, randomV.y / _Radius.y, randomV.z / _Radius.z);
  592. float diff = Vector3.Distance(scaledCenter, scaledV) - 1;
  593. scaledCenter += (scaledV - scaledCenter).normalized * diff * 0.1f;
  594. _Center = new Vector3(scaledCenter.x * _Radius.x, scaledCenter.y * _Radius.y, scaledCenter.z * _Radius.z);
  595. }
  596. /*
  597. if (diff > 0)
  598. {
  599. Center -= v * diff;
  600. }
  601. else
  602. {
  603. }/**/
  604. //Point.Add(v);
  605. //Debug.Log(v.magnitude);
  606. v -= _Center;
  607. v = new Vector3(v.x / _Radius.x, v.y / _Radius.y, v.z / _Radius.z);
  608. return v;
  609. }
  610. public float CalibratCompletionPercentage()
  611. {
  612. if (Point == null)
  613. return 0;
  614. List<float> ScaleDistance = new List<float>();
  615. foreach (var i in Point)
  616. {
  617. var v = i - _Center;
  618. ScaleDistance.Add(new Vector3(v.x / _Radius.x, v.y / _Radius.y, v.z / _Radius.z).magnitude);
  619. }
  620. while (ScaleDistance.Count < PointMaxCount)
  621. ScaleDistance.Add(0);
  622. float average = 0;
  623. foreach (var i in ScaleDistance)
  624. average += i;
  625. average /= ScaleDistance.Count;
  626. float variance = 0;
  627. foreach (var i in ScaleDistance)
  628. variance += Mathf.Pow(average - i, 2);
  629. variance /= ScaleDistance.Count;
  630. return Mathf.Pow((1 - variance / average), 10) * 100;
  631. //return variance;
  632. }
  633. }
  634. public class o0GyrCalibrater
  635. {
  636. [JsonIgnore]
  637. public Vector3 _Average = Vector3.zero;
  638. [JsonIgnore]
  639. public long Count = -1;
  640. [JsonIgnore]
  641. public bool Calibration
  642. {
  643. get
  644. {
  645. return Count != -1;
  646. }
  647. set
  648. {
  649. if (value)
  650. Count = 0;
  651. else
  652. Count = -1;
  653. }
  654. }
  655. public float[] Average
  656. {
  657. get
  658. {
  659. return new float[]{_Average.x, _Average.y, _Average.z};
  660. }
  661. set
  662. {
  663. _Average = new Vector3(value[0], value[1], value[2]);
  664. }
  665. }
  666. public o0GyrCalibrater()
  667. {
  668. }
  669. //[JsonConstructor, o0.BinarySerialization.Constructor]
  670. // public o0GyrCalibrater(o0Project.Vector3f Average)
  671. public o0GyrCalibrater(float[] Average)
  672. {
  673. this.Average = Average;
  674. }
  675. public Vector3 Update(Vector3 v)
  676. {
  677. if (Calibration)
  678. _Average += (v - _Average) / ++Count;
  679. v -= _Average;
  680. if (v.magnitude < 0.0002)
  681. return Vector3.zero;
  682. return v;
  683. }
  684. }
  685. public class o09Axis
  686. {
  687. // public static List<o0UIRawImageTester> Tester = new List<o0UIRawImageTester>();
  688. // public static List<Text> TextTester = new List<Text>();
  689. public List<Text> TextTester = new List<Text>();
  690. static Vector3 AccIdentity = new Vector3(0, -1, 0);
  691. static Vector3 MagIdentity = new Vector3(-1, 2, 0).normalized;
  692. public class State
  693. {
  694. public long TimeGap;
  695. public Vector3 Acc = AccIdentity;
  696. public Vector3 Gyr;
  697. public Vector3 Mag = MagIdentity;
  698. public Quaternion Qua;
  699. public double Variance = 1;
  700. }
  701. o0Project.Variance HardwareVarianceGyr = new o0Project.Variance(1000);
  702. o0Project.Variance HardwareVarianceAcc = new o0Project.Variance(1000);
  703. o0Project.Variance HardwareVarianceMag = new o0Project.Variance(1000);
  704. public List<State> States = new List<State>();
  705. public Vector3 AccOld;
  706. public Vector3 GyrOld;
  707. public Vector3 MagOld;
  708. public float x;
  709. public float y;
  710. public float z;
  711. long TimeGapOld;
  712. /////////////////////g degree/ms
  713. public Quaternion Update(Vector3 AccOld, Vector3 GyrOld, Vector3 MagOld, long TimeGapOld)
  714. {
  715. //o0UIRawImageTester.UpdateAllOffset();
  716. //Debug.Log(TimeGapOld);
  717. var Acc = this.AccOld;
  718. var Gyr = this.GyrOld;
  719. var Mag = this.MagOld;
  720. float TimeGap = (TimeGapOld + this.TimeGapOld) / 2;
  721. this.AccOld = AccOld;
  722. this.GyrOld = GyrOld;
  723. this.MagOld = MagOld;
  724. this.TimeGapOld = TimeGapOld;
  725. var Last = States.LastOrDefault() ?? new State();
  726. if (this.TimeGapOld <= 0)
  727. return Last.Qua;
  728. States.Add(new State());
  729. if (States.Count > 200)
  730. States.RemoveAt(0);
  731. var state = States.Last();
  732. state.Acc = Acc;
  733. state.Gyr = Gyr;
  734. state.Mag = Mag;
  735. //Debug.Log(TimeGap);
  736. HardwareVarianceGyr.Update((Gyr).magnitude);//每毫秒方差2.331017E-09 度左右 0.00000002331017
  737. HardwareVarianceAcc.Update(Vector3.Angle(state.Acc, Last.Acc));//方差0.0012度左右
  738. HardwareVarianceMag.Update(Vector3.Angle(state.Mag, Last.Mag));//方差3.5度左右
  739. //Tester?[7].DrawLine(HardwareVarianceAcc.Value, new Color(0, 0, 0));//0.0012左右
  740. //Tester?[8].DrawLine(HardwareVarianceMag.Value, new Color(0, 0, 0));//3.5左右
  741. //Debug.Log(HardwareVarianceMag.Value);
  742. // var Accwit = GameObject.Find("Accwit");
  743. // var Gyrwit = GameObject.Find("Gyrwit");
  744. // var Magwit = GameObject.Find("Magwit");
  745. var LastQuaternion = Last.Qua;
  746. //var LastQuaternion = Gyrwit.transform.localRotation;
  747. var newQua = new Quaternion();
  748. newQua.eulerAngles = Gyr * TimeGap;
  749. var quaGyr = LastQuaternion * newQua;
  750. // Accwit.transform.localRotation = o0Project.o0.FormQuaternion(Accwit.transform.localRotation, AccIdentity, Acc, 1);
  751. // Magwit.transform.localRotation = o0Project.o0.FormQuaternion(Magwit.transform.localRotation, MagIdentity, Mag, 1);
  752. //Tester?[3].DrawLine(Vector3.Angle(Acc, Last.Acc) / 1, new Color(1, 0, 0));
  753. //Tester?[4].DrawLine(Quaternion.Angle(LastQuaternion, quaGyr) / 45, new Color(1, 0, 0));
  754. //Tester?[5].DrawLine(Vector3.Angle(Mag, Last.Mag) / 5, new Color(1, 0, 0));
  755. double AccLengthToAngle = 5;//1倍引力差相当于多少度方差
  756. double MagLengthToAngle = 5;//1倍磁力差相当于多少度方差
  757. /*
  758. float GyrVariance = Last.Variance + (Gyr * TimeGap).magnitude * 0.05f;
  759. 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)));
  760. //Debug.Log(AccVariance);
  761. 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)));
  762. state.Variance = state.Variance * AccVariance / (state.Variance + AccVariance);
  763. state.Variance = state.Variance * MagVariance / (state.Variance + MagVariance);/**/
  764. //测试效果不错但没迭代的版本
  765. /*
  766. *
  767. float GyrVariance = state.Variance + TimeGap/100 + (Gyr * TimeGap).magnitude * 0.05f;
  768. 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));
  769. //Debug.Log(AccVariance);
  770. float MagVariance = TimeGap / 1 + Mathf.Sqrt(Mathf.Pow((Mag.magnitude - 1) / 1 * MagLengthToAngle, 2) + Mathf.Pow(Vector3.Angle(Mag, Last.Mag) * 0.1f, 2));
  771. /**/
  772. //Tester?[1].DrawLine(TimeGap / 100f, new Color(0, 0, 1));
  773. //Tester?[2].DrawLine((int)(Last.Variance / 90), new Color(0, 0, 0));
  774. // double GyrVariance = Last.Variance + 0.00000002331017 * TimeGap + Math.Pow((Gyr * TimeGap).magnitude * 0.03, 2);// 指数4 = 方差2 * 欧拉角旋转误差2 移动导致累计误差
  775. // //Debug.Log(Math.Max(0.00000002331017 * TimeGap, Math.Pow((Gyr * TimeGap).magnitude * 0.001f, 2)));
  776. // //Tester?[6].DrawLine((Gyr * TimeGap).magnitude * 0.05f / 90, new Color(0, 0, 0));
  777. // 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));
  778. // //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));
  779. // //Debug.Log(Vector3.Angle(Mag, Last.Mag));
  780. // double MagVariance = Math.Max(3.5, Math.Pow((Mag.magnitude - 1) / 1 * MagLengthToAngle, 4) + Math.Pow(Vector3.Angle(Mag, Last.Mag) * 0.07, 2));
  781. double GyrVariance = Last.Variance + 0.00000002331017 * TimeGap + Math.Pow((Gyr * TimeGap).magnitude , 2) * 0.04;// 指数4 = 方差2 * 欧拉角旋转误差2 移动导致累计误差
  782. 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);
  783. //double AccVariance = Math.Max(0.01, Math.Pow(Math.Max(Gyr.magnitude, Vector3.Angle(Acc, Last.Acc) / TimeGap), 2) * 1000);
  784. double MagVariance = Math.Max(3.5, Math.Pow((Mag.magnitude - 1) / 1 * MagLengthToAngle, 4) + Math.Pow(Vector3.Angle(Mag, Last.Mag), 2) * 0.005);
  785. /*
  786. Tester?[1].DrawLine(TimeGap / 100f, new Color(0, 0, 1));
  787. Tester?[2].DrawLine((int)(Last.Variance / 90), new Color(0, 0, 0));
  788. double GyrVariance = Last.Variance + Math.Max(0.00000002331017 * TimeGap, Math.Pow((Gyr * TimeGap).magnitude * 0.001f, 2));// 指数4 = 方差2 * 欧拉角旋转误差2 移动导致累计误差
  789. //Debug.Log(Math.Max(0.00000002331017 * TimeGap, Math.Pow((Gyr * TimeGap).magnitude * 0.001f, 2)));
  790. Tester?[6].DrawLine((Gyr * TimeGap).magnitude * 0.05f / 90, new Color(0, 0, 0));
  791. double AccVariance = Vector3.Angle(Acc, Last.Acc) < 0.01 ? 0.0012f : 99999;
  792. //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));
  793. //Debug.Log(Vector3.Angle(Mag, Last.Mag));
  794. double MagVariance = Vector3.Angle(Mag, Last.Mag) < 5 && Vector3.Angle(Mag, Last.Mag) != 0 ? 3.5 : 99999;/**/
  795. //double MagVariance = Math.Max(3.5f, Math.Pow((Mag.magnitude - 1) / 1 * MagLengthToAngle, 2) + Math.Pow(Vector3.Angle(Mag, Last.Mag) * 0.05, 2));
  796. //Debug.Log(MagVariance);
  797. state.Variance = GyrVariance;
  798. state.Variance = state.Variance * (AccVariance+ MagVariance) / (state.Variance + (AccVariance + MagVariance));
  799. //state.Variance = state.Variance * MagVariance / (state.Variance + MagVariance);
  800. //Debug.Log(state.Variance);
  801. //Debug.Log(TextTester[0]);
  802. // TextTester[0].text = "Variance:" + state.Variance;
  803. TextTester[1].text = "GyrVariance:" + GyrVariance;
  804. TextTester[2].text = "StaticGyrVariance:" + 0.00000002331017 * TimeGap;
  805. TextTester[3].text = "MothonGyrVariance:" + Math.Pow((Gyr * TimeGap).magnitude * 0.07, 2);
  806. TextTester[4].text = "GyrSpeed:" + Gyr.magnitude;
  807. TextTester[5].text = "AccVariance:" + AccVariance;
  808. TextTester[6].text = "AccLengthVariance:" + Math.Pow((Acc.magnitude - 9.8) / 9.8 * AccLengthToAngle, 4);
  809. TextTester[7].text = "AccRotate:" + Math.Pow(Math.Max(Gyr.magnitude, Vector3.Angle(Acc, Last.Acc)/TimeGap)* 20, 2);
  810. TextTester[9].text = "AccLength:" + Acc.magnitude;
  811. TextTester[10].text = "Gyr*1000,000:" + (Gyr * 1000000).ToString();
  812. TextTester[11].text = "AngleBetweenIdentity*1000:" + Quaternion.Angle(Last.Qua, Quaternion.identity) * 1000;
  813. // TextTester[12].text = "Qua.eulerAngles.x:" + Last.Qua.eulerAngles.x;
  814. // TextTester[13].text = "Qua.eulerAngles.y:" + Last.Qua.eulerAngles.y;
  815. // TextTester[14].text = "Qua.eulerAngles.z:" + Last.Qua.eulerAngles.z;
  816. /*if (Gyr != Vector3.zero)
  817. {
  818. Debug.Log(Gyr);
  819. }/**/
  820. var quaAccMag = o0Project.o0.FormQuaternion(AccIdentity, MagIdentity, Acc, Mag, (float)(AccVariance / (AccVariance + MagVariance)));
  821. var quaMinRate = GyrVariance / (GyrVariance + Math.Max(AccVariance, MagVariance));
  822. var quaMaxRate = GyrVariance / (GyrVariance + Math.Min(AccVariance, MagVariance));
  823. Quaternion quaFirst = Quaternion.Slerp(quaGyr, quaAccMag, (float)quaMinRate).normalized;
  824. var quaSecondRate = (quaMaxRate - quaMinRate) / (1 - quaMinRate);
  825. // Gyrwit.transform.localRotation = AccVariance < MagVariance ? o0Project.o0.FormQuaternion(quaFirst, AccIdentity, Acc, (float)quaSecondRate) : o0Project.o0.FormQuaternion(quaFirst, MagIdentity, Mag, (float)quaSecondRate);
  826. // state.Qua = Gyrwit.transform.localRotation;
  827. state.Qua = AccVariance < MagVariance ? o0Project.o0.FormQuaternion(quaFirst, AccIdentity, Acc, (float)quaSecondRate) : o0Project.o0.FormQuaternion(quaFirst, MagIdentity, Mag, (float)quaSecondRate);
  828. //Tester?[0].DrawLine(TimeGap / 200, new Color(1, 0, 0));
  829. //Image1.DrawLine();
  830. //Debug.Log((Gyr * TimeGap).magnitude);
  831. //Debug.Log(Quaternion.Angle(state.Qua, Last.Qua));
  832. //TextTester[8].text = "AngleRotated:" + Quaternion.Angle(state.Qua, Last.Qua);
  833. var frontV = Last.Qua * Vector3.forward;
  834.         var upV = Last.Qua * Vector3.up;
  835. x = (Mathf.Atan(upV.y / upV.z) / Mathf.PI * 180 + (upV.z < 0 ? 90:270));
  836.         y = (Mathf.Atan(frontV.z / frontV.x) / Mathf.PI * 180+(frontV.x < 0 ? 90:270));
  837.         z = (Mathf.Atan(upV.y / upV.x) / Mathf.PI * 180 + (upV.x < 0 ? 90:270));
  838. // TextTester[18].text = "x轴角度:" + (Mathf.Atan(upV.y / upV.z) / Mathf.PI * 180 + (upV.z < 0 ? 90:270));
  839. //         TextTester[19].text = "y轴角度:" + (Mathf.Atan(frontV.z / frontV.x) / Mathf.PI * 180+(frontV.x < 0 ? 90:270));
  840. //         TextTester[20].text = "z轴角度" + (Mathf.Atan(upV.y / upV.x) / Mathf.PI * 180 + (upV.x < 0 ? 90:270));
  841. TextTester[12].text = "x轴角度:\n" + (Mathf.Atan(upV.y / upV.z) / Mathf.PI * 180 + (upV.z < 0 ? 90:270));
  842.         TextTester[13].text = "y轴角度:\n" + (Mathf.Atan(frontV.z / frontV.x) / Mathf.PI * 180+(frontV.x < 0 ? 90:270));
  843.         TextTester[14].text = "z轴角度:\n" + (Mathf.Atan(upV.y / upV.x) / Mathf.PI * 180 + (upV.x < 0 ? 90:270));
  844. return state.Qua;
  845. }
  846. public void SetIdentity()
  847. {
  848. Quaternion qua = default;
  849. AccIdentity = AccOld;
  850. MagIdentity = MagOld;
  851. qua = o0Project.o0.FormQuaternion(Quaternion.identity, Vector3.down,AccIdentity, 1);
  852. AccIdentity=qua*AccIdentity;
  853. MagIdentity = qua*MagIdentity;
  854. TextTester[0].text = "AccIdentity:"+AccIdentity;
  855. States.Last().Qua = Quaternion.identity;
  856. States.Last().Qua = qua*States.Last().Qua;//Quaternion.identity;
  857. States.Last().Variance = 0.0000001;
  858. }
  859. public void SetIdentityAccordingToRecords()
  860. {
  861. AccIdentity = Vector3.zero;
  862. foreach (var i in States)
  863. AccIdentity += i.Acc;
  864. AccIdentity /= States.Count;
  865. MagIdentity = Vector3.zero;
  866. foreach (var i in States)
  867. MagIdentity += i.Mag;
  868. MagIdentity /= States.Count;
  869. States.Last().Qua = Quaternion.identity;
  870. States.Last().Variance = 0.0000001;
  871. Vector3.Angle(Vector3.up, States.Last().Mag);
  872. }
  873. }