o09Axis.cs 34 KB

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