o09Axis.cs 34 KB

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