ukfPDR.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. #include "ukfPDR.h"
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. static float Psr[4][4];
  7. static float PsrTmp[4][4];
  8. static float ChiX[4][9];
  9. static float ChiXtmp[4][9];
  10. static float ChiY[3][9];
  11. static float ChiYcov[3][3];
  12. static float YXcov[4][3];
  13. static float K[4][3];
  14. void UKF_para(int L, float alpha, float beta, float kappa, float *gamma, float *wm, float *wc)
  15. {
  16. //float lamda = alpha * alpha * (L + kappa) - L;
  17. //float lamda = -3.9996f;
  18. //*gamma = sqrt(L + lamda);
  19. *gamma = 0.0199999996f;
  20. for (int i = 0; i < 2 * L + 1; i++)
  21. {
  22. //wm[i] = 0.5 / (L + lamda);
  23. wm[i] = 1250.0f;
  24. //wc[i] = 0.5 / (L + lamda);
  25. wc[i] = 1250.0f;
  26. }
  27. //wm[0] = lamda / (L + lamda);
  28. wm[0] = -9999.0f;
  29. //wc[0] = lamda / (L + lamda) + 1 - alpha * alpha + beta;
  30. wc[0] = -9996.0f;
  31. }
  32. int matrixSubtract(float **leftMatrix, int leftxDimen, int leftyDimen, float **rightMatrix, int rightxDimen, int rightyDimen, float **dst)
  33. {
  34. if (leftxDimen != rightxDimen || leftyDimen != rightyDimen)
  35. {
  36. printf("Dimension don't match");
  37. return -1;
  38. }
  39. int i = 0;
  40. int j = 0;
  41. for (i = 0; i < leftxDimen; i++)
  42. for (j = 0; j < leftyDimen; j++)
  43. {
  44. dst[i][j] = leftMatrix[i][j] - rightMatrix[i][j];
  45. }
  46. return 0;
  47. }
  48. void multiplyMatrix(float a[4][3], int axDimen, int ayDimen, float b[3][3], int bxDimen, int byDimen, float dst[4][3])
  49. {
  50. int k = 0;
  51. float sum = 0.0f;
  52. for (int i = 0; i < axDimen; i++)
  53. for (int j = 0; j < byDimen; j++)
  54. {
  55. sum = 0.0f;
  56. for (k = 0; k < ayDimen; k++)
  57. {
  58. sum += a[i][k] * b[k][j];
  59. }
  60. dst[i][j] = sum;
  61. }
  62. }
  63. void invertMatrix(float src[3][3], float dst[3][3])
  64. {
  65. float det;
  66. /* Compute adjoint: */
  67. dst[0][0] = +src[1][1] * src[2][2] - src[1][2] * src[2][1];
  68. dst[0][1] = -src[0][1] * src[2][2] + src[0][2] * src[2][1];
  69. dst[0][2] = +src[0][1] * src[1][2] - src[0][2] * src[1][1];
  70. dst[1][0] = -src[1][0] * src[2][2] + src[1][2] * src[2][0];
  71. dst[1][1] = +src[0][0] * src[2][2] - src[0][2] * src[2][0];
  72. dst[1][2] = -src[0][0] * src[1][2] + src[0][2] * src[1][0];
  73. dst[2][0] = +src[1][0] * src[2][1] - src[1][1] * src[2][0];
  74. dst[2][1] = -src[0][0] * src[2][1] + src[0][1] * src[2][0];
  75. dst[2][2] = +src[0][0] * src[1][1] - src[0][1] * src[1][0];
  76. /* Compute determinant: */
  77. det = src[0][0] * dst[0][0] + src[0][1] * dst[1][0] + src[0][2] * dst[2][0];
  78. /* Multiply adjoint with reciprocal of determinant: */
  79. det = 1.0f / det;
  80. dst[0][0] *= det;
  81. dst[0][1] *= det;
  82. dst[0][2] *= det;
  83. dst[1][0] *= det;
  84. dst[1][1] *= det;
  85. dst[1][2] *= det;
  86. dst[2][0] *= det;
  87. dst[2][1] *= det;
  88. dst[2][2] *= det;
  89. }
  90. void MeanSigmaMatrix(float **SigmaMatrix, float *meanSigma, float *weight, int stateDimen, int sigmaDimen)
  91. {
  92. int i = 0;
  93. int j = 0;
  94. memset(meanSigma, 0, stateDimen * sizeof(float));
  95. for (i = 0; i < stateDimen; i++)
  96. for (j = 0; j < sigmaDimen; j++)
  97. {
  98. meanSigma[i] += SigmaMatrix[i][j] * weight[j];
  99. }
  100. }
  101. void StateMeasureCovariance(float covarianceMatrix[4][3], float state[4][9], int stateDimen, float measure[3][9], int mearsureDimen, float *wc, int length)
  102. {
  103. int i = 0;
  104. int j = 0;
  105. int k = 0;
  106. float sum;
  107. for (i = 0; i < stateDimen; i++)
  108. for (j = 0; j < mearsureDimen; j++)
  109. {
  110. sum = 0.0;
  111. for (k = 0; k < length; k++)
  112. {
  113. sum += state[i][k] * wc[k] * measure[j][k];
  114. }
  115. covarianceMatrix[i][j] = sum;
  116. }
  117. }
  118. void updateSigmPoint(float **SigmaMatrix, float *meanSigma, int stateDimen, int SigmaDimen)
  119. {
  120. int i = 0;
  121. int j = 0;
  122. for (i = 0; i < stateDimen; i++)
  123. for (j = 0; j < SigmaDimen; j++)
  124. {
  125. SigmaMatrix[i][j] -= meanSigma[i];
  126. }
  127. }
  128. void StateCovarianceMatrix(float stateCovarianceMatrix[4][4], float quatPredictMatrix[4][9], float *statePredict, float *wc, int length)
  129. {
  130. int j = 0;
  131. int i = 0;
  132. int k = 0;
  133. float sum = 0.0f;
  134. for (j = 0; j < length; j++)
  135. {
  136. for (i = 0; i < 4; i++)
  137. {
  138. quatPredictMatrix[i][j] -= statePredict[i];
  139. }
  140. }
  141. for (i = 0; i < 4; i++)
  142. for (j = 0; j < 4; j++)
  143. {
  144. sum = 0.0f;
  145. for (k = 0; k < length; k++)
  146. {
  147. sum += quatPredictMatrix[i][k] * wc[k] * quatPredictMatrix[j][k];
  148. }
  149. stateCovarianceMatrix[i][j] = sum;
  150. }
  151. for (i = 0; i < 4; i++)
  152. {
  153. stateCovarianceMatrix[i][i] += 0.00001f;
  154. }
  155. }
  156. void QuatPredict(float quatPredictMatrix[4][9], float *quatPredict, float *wm, int length)
  157. {
  158. int i = 0;
  159. memset(quatPredict, 0, 4 * sizeof(float));
  160. float sum = 0.0f;
  161. for (i = 0; i < 4; i++)
  162. {
  163. /*
  164. float sum = 0.0f;
  165. for (j = 0; j < length; j++)
  166. {
  167. sum += (quatPredictMatrix[i][j] * wm[j]);
  168. }
  169. */
  170. quatPredict[i] = (quatPredictMatrix[i][0] * wm[0] + quatPredictMatrix[i][1] * wm[1] + quatPredictMatrix[i][2] * wm[2]
  171. + quatPredictMatrix[i][3] * wm[3] + quatPredictMatrix[i][4] * wm[4] + quatPredictMatrix[i][5] * wm[5]
  172. + quatPredictMatrix[i][6] * wm[6] + quatPredictMatrix[i][7] * wm[7] + quatPredictMatrix[i][8] * wm[8]);
  173. sum += quatPredict[i] * quatPredict[i];
  174. }
  175. sum = 1 / sqrt(sum);
  176. for (int i = 0; i < 4; i++)
  177. {
  178. quatPredict[i] *= sum;
  179. }
  180. }
  181. void MagPredict(float magPredictMatrix[3][9], float mag[3], float *wm, int length)
  182. {
  183. int i = 0;
  184. int j = 0;
  185. memset(mag, 0, 3 * sizeof(float));
  186. for (i = 0; i < 3; i++)
  187. {
  188. for (j = 0; j < length; j++)
  189. {
  190. mag[i] += (magPredictMatrix[i][j] * wm[j]);
  191. }
  192. }
  193. }
  194. void UpdateQuat(float *gyr, float dt, float *q)
  195. {
  196. float w1 = dt * gyr[0];
  197. float w2 = dt * gyr[1];
  198. float w3 = dt * gyr[2];
  199. float quatTmp[4];
  200. memcpy(quatTmp, q, 4 * sizeof(float));
  201. quatTmp[0] = q[0] - (q[1] * w1) * 0.5f - (q[2] * w2) * 0.5f - (q[3] * w3) * 0.5f;
  202. quatTmp[1] = q[1] + (q[0] * w1) * 0.5f + (q[2] * w3) * 0.5f - (q[3] * w2) * 0.5f;
  203. quatTmp[2] = q[2] + (q[0] * w2) * 0.5f - (q[1] * w3) * 0.5f + (q[3] * w1) * 0.5f;
  204. quatTmp[3] = q[3] + (q[0] * w3) * 0.5f + (q[1] * w2) * 0.5f - (q[2] * w1) * 0.5f;
  205. memcpy(q, quatTmp, 4 * sizeof(float));
  206. }
  207. void QuatNormalize(float *quat)
  208. {
  209. int i = 0;
  210. float quatNorm = 1 / sqrt(quat[0] * quat[0] + quat[1] * quat[1] + quat[2] * quat[2] + quat[3] * quat[3]);
  211. if (isnan(quatNorm))
  212. {
  213. printf("四元数出现nan \n");
  214. }
  215. for (i = 0; i < 4; i++)
  216. {
  217. quat[i] *= quatNorm;
  218. }
  219. }
  220. void chol(float a_matrix[4][4], float b_matrix[4][4], float c_matrix[4][4], int ndimen)
  221. // 输入参数:
  222. // b_matrix: 对称正定方阵 ndimen: 矩阵维数
  223. // 返回值:
  224. // a_matrix: 下三角矩阵
  225. {
  226. int i, j, r;
  227. float m = 0;
  228. for (i = 0; i < ndimen; i++)
  229. {
  230. for (j = 0; j < ndimen; j++)
  231. c_matrix[i][j] = 0;
  232. }
  233. c_matrix[0][0] = sqrt(b_matrix[0][0]);
  234. for (i = 1; i < ndimen; i++)
  235. {
  236. if (c_matrix[0][0] != 0)
  237. c_matrix[i][0] = b_matrix[i][0] / c_matrix[0][0];
  238. }
  239. for (i = 1; i < ndimen; i++)
  240. {
  241. for (r = 0; r < i; r++) m = m + c_matrix[i][r] * c_matrix[i][r];
  242. c_matrix[i][i] = sqrt(b_matrix[i][i] - m);
  243. m = 0.0;
  244. for (j = i + 1; j < ndimen; j++)
  245. {
  246. for (r = 0; r < i; r++) m = m + c_matrix[i][r] * c_matrix[j][r];
  247. c_matrix[j][i] = (b_matrix[i][j] - m) / c_matrix[i][i];
  248. m = 0;
  249. }
  250. }
  251. for (i = 0; i < ndimen; i++)
  252. {
  253. for (j = 0; j < ndimen; j++)
  254. a_matrix[i][j] = c_matrix[i][j];
  255. }
  256. }
  257. void quatXmag(float ChiX[4][9], float* mag, float ChiY[3][9])
  258. {
  259. float dcm[3][3];
  260. float qin[4];
  261. for (int j = 0; j < 9; j++)
  262. {
  263. for (int k = 0; k < 4; k++)
  264. {
  265. qin[k] = ChiX[k][j];
  266. }
  267. dcm[0][0] = qin[0] * qin[0] + qin[1] * qin[1] - qin[2] * qin[2] - qin[3] * qin[3];
  268. dcm[0][1] = 2.0f * (qin[1] * qin[2] + qin[0] * qin[3]);
  269. dcm[0][2] = 2.0f * (qin[1] * qin[3] - qin[0] * qin[2]);
  270. dcm[1][0] = 2.0f * (qin[1] * qin[2] - qin[0] * qin[3]);
  271. dcm[1][1] = qin[0] * qin[0] - qin[1] * qin[1] + qin[2] * qin[2] - qin[3] * qin[3];
  272. dcm[1][2] = 2.0f * (qin[2] * qin[3] + qin[0] * qin[1]);
  273. dcm[2][0] = 2.0f * (qin[1] * qin[3] + qin[0] * qin[2]);
  274. dcm[2][1] = 2.0f * (qin[2] * qin[3] - qin[0] * qin[1]);
  275. dcm[2][2] = qin[0] * qin[0] - qin[1] * qin[1] - qin[2] * qin[2] + qin[3] * qin[3];
  276. for (int i = 0; i < 3; i++)
  277. {
  278. ChiY[i][j] = dcm[i][0] * mag[0] + dcm[i][1] * mag[1] + dcm[i][2] * mag[2];
  279. }
  280. }
  281. }
  282. void MatrixSubVector(float ChiY[][9], float *magPredict, int nRows)
  283. {
  284. for (int i = 0; i < nRows; i++)
  285. {
  286. for (int j = 0; j < 9; j++)
  287. {
  288. ChiY[i][j] -= magPredict[i];
  289. }
  290. }
  291. }
  292. void MeasureCovMatrix(float measureMatrix[][9], float covMatrix[][3], float *wc, int nRows)
  293. {
  294. float sum;
  295. for (int i = 0; i < nRows; i++)
  296. for (int j = 0; j < nRows; j++)
  297. {
  298. sum = 0;
  299. for (int k = 0; k < 9; k++)
  300. {
  301. sum += measureMatrix[i][k] * wc[k] * measureMatrix[j][k];
  302. }
  303. covMatrix[i][j] = sum;
  304. if (i == j)
  305. {
  306. covMatrix[i][j] += 0.00001f;
  307. }
  308. }
  309. }
  310. void GainUKF(float YXcov[4][3], float Ycov[3][3], float K[4][3])
  311. {
  312. float YcovTmp[3][3];
  313. invertMatrix(Ycov, YcovTmp);
  314. multiplyMatrix(YXcov, 4, 3, YcovTmp, 3, 3, K);
  315. }
  316. void updateState(float q[4], float K[4][3], float mag_now[3], float mag_predict[3])
  317. {
  318. float magSub[3];
  319. for (int i = 0; i < 3; i++)
  320. {
  321. magSub[i] = mag_now[i] - mag_predict[i];
  322. }
  323. float qTmp[4];
  324. memset(qTmp, 0, 4 * sizeof(float));
  325. for (int i = 0; i < 4; i++)
  326. for (int j = 0; j < 3; j++)
  327. {
  328. qTmp[i] += K[i][j] * magSub[j];
  329. }
  330. for (int i = 0; i < 4; i++)
  331. {
  332. q[i] += qTmp[i];
  333. }
  334. }
  335. void updataStateCov(float P[4][4], float Ycov[3][3], float K[4][3])
  336. {
  337. float K_tmp[4][3];
  338. for (int i = 0; i < 4; i++)
  339. {
  340. for (int j = 0; j < 3; j++)
  341. {
  342. K_tmp[i][j] = K[i][0] * Ycov[0][j] + K[i][1] * Ycov[1][j] + K[i][2] * Ycov[2][j];
  343. }
  344. }
  345. for (int i = 0; i < 4; i++)
  346. {
  347. for (int j = 0; j < 4; j++)
  348. {
  349. P[i][j] -= K_tmp[i][0] * K[j][0] + K_tmp[i][1] * K[j][1] + K_tmp[i][2] * K[j][2];
  350. }
  351. }
  352. }
  353. void UpdateThetaMatrix(float theta[4][4], float *gyr, float dt)
  354. {
  355. theta[0][0] = 1.0f;
  356. theta[1][1] = 1.0f;
  357. theta[2][2] = 1.0f;
  358. theta[3][3] = 1.0f;
  359. dt *= 0.5f;
  360. theta[0][1] = -gyr[0] * dt;
  361. theta[0][2] = -gyr[1] * dt;
  362. theta[0][3] = -gyr[2] * dt;
  363. theta[1][0] = gyr[0] * dt;
  364. theta[1][2] = gyr[2] * dt;
  365. theta[1][3] = -gyr[1] * dt;
  366. theta[2][0] = gyr[1] * dt;
  367. theta[2][1] = -gyr[2] * dt;
  368. theta[2][3] = gyr[0] * dt;
  369. theta[3][0] = gyr[2] * dt;
  370. theta[3][1] = gyr[1] * dt;
  371. theta[3][2] = -gyr[0] * dt;
  372. }
  373. /*
  374. void UKF_para(int L, float alpha, float beta, float kappa, float *gamma, float *wm, float *wc)
  375. {
  376. float lamda = alpha * alpha * (L + kappa) - L;
  377. *gamma = sqrt(L + lamda);
  378. for (int i = 0; i < 2 * L + 1; i++)
  379. {
  380. wm[i] = 0.5 / (L + lamda);
  381. wc[i] = 0.5 / (L + lamda);
  382. }
  383. wm[0] = lamda / (L + lamda);
  384. wc[0] = lamda / (L + lamda) + 1 - alpha * alpha + beta;
  385. }
  386. */
  387. void UKF_quat(float *quat, float P[4][4], float *gyr, float *mag_prev, float *mag_now, float gamma, float *wm, float *wc, int L, float dt)
  388. {
  389. float mag[3];
  390. //为四元数更新做准备,减少运算量
  391. //UpdateThetaMatrix(theta, gyr, dt);
  392. //四元数归一
  393. //X_quat = X_quat / norm(X_quat);
  394. QuatNormalize(quat);
  395. //状态协方差矩阵分解,为sigma粒子产生作准备
  396. //Psr = gamma*chol(P)';
  397. chol(Psr, P, PsrTmp, L);
  398. for (int i = 0; i < 4; i++)
  399. for (int j = 0; j < 4; j++)
  400. {
  401. Psr[i][j] *= gamma;
  402. }
  403. //产生sigma粒子矩阵
  404. // ChiX = [X_quat, X_quat*ones(1,L)+Psr, X_quat*ones(1,L)-Psr];
  405. for (int i = 0; i < 4; i++)
  406. {
  407. ChiXtmp[i][0] = quat[i];
  408. }
  409. for (int j = 0; j < 4; j++)
  410. {
  411. for (int i = 0; i < 4; i++)
  412. {
  413. ChiXtmp[i][j + 1] = quat[i] + Psr[i][j];
  414. ChiXtmp[i][j + 5] = quat[i] - Psr[i][j];
  415. }
  416. }
  417. //sigma粒子预测更新
  418. for (int j = 0; j < 2 * L + 1; j++)
  419. {
  420. float sum = 0.0f;
  421. for (int i = 0; i < 4; i++)
  422. {
  423. //ChiX[i][j] = theta[i][0] * ChiXtmp[0][j] + theta[i][1] * ChiXtmp[1][j] + theta[i][2] * ChiXtmp[2][j] + theta[i][3] * ChiXtmp[3][j];
  424. ChiX[i][j] = ChiXtmp[i][j];
  425. sum += ChiX[i][j] * ChiX[i][j];
  426. }
  427. sum = 1 / sqrt(sum);
  428. for (int i = 0; i < 4; i++)
  429. {
  430. ChiX[i][j] *= sum;
  431. }
  432. };
  433. //复制Chix
  434. for (int i = 0; i < 4; i++)
  435. for (int j = 0; j < 9; j++)
  436. {
  437. ChiXtmp[i][j] = ChiX[i][j];
  438. }
  439. QuatPredict(ChiX, quat, wm, 9);
  440. StateCovarianceMatrix(P, ChiX, quat, wc, 2 * L + 1);
  441. quatXmag(ChiXtmp, mag_now, ChiY);
  442. MagPredict(ChiY, mag, wm, 2 * L + 1);
  443. MatrixSubVector(ChiY, mag, 3);
  444. StateMeasureCovariance(YXcov, ChiX, 4, ChiY, 3, wc, 9);
  445. MeasureCovMatrix(ChiY, ChiYcov, wc, 3);
  446. GainUKF(YXcov, ChiYcov, K);
  447. updateState(quat, K, mag_prev, mag);
  448. updataStateCov(P, ChiYcov, K);
  449. }
  450. /*
  451. void UKF_quat(float *quat, float P[4][4], float *gyr, float *mag_prev, float *mag_now, float gamma, float *wm, float *wc, int L, float dt)
  452. {
  453. float theta[4][4];
  454. float mag[3];
  455. //为四元数更新做准备,减少运算量
  456. UpdateThetaMatrix(theta, gyr, dt);
  457. //四元数归一
  458. //X_quat = X_quat / norm(X_quat);
  459. QuatNormalize(quat);
  460. //状态协方差矩阵分解,为sigma粒子产生作准备
  461. //Psr = gamma*chol(P)';
  462. chol(Psr, P, PsrTmp, L);
  463. for (int i = 0; i < 4; i++)
  464. for (int j = 0; j < 4; j++)
  465. {
  466. Psr[i][j] *= gamma;
  467. }
  468. //产生sigma粒子矩阵
  469. // ChiX = [X_quat, X_quat*ones(1,L)+Psr, X_quat*ones(1,L)-Psr];
  470. for (int i = 0; i < 4; i++)
  471. {
  472. ChiXtmp[i][0] = quat[i];
  473. }
  474. for (int j = 0; j < 4; j++)
  475. {
  476. for (int i = 0; i < 4; i++)
  477. {
  478. ChiXtmp[i][j + 1] = quat[i] + Psr[i][j];
  479. ChiXtmp[i][j + 5] = quat[i] - Psr[i][j];
  480. }
  481. }
  482. //sigma粒子预测更新
  483. for (int j = 0; j < 2 * L + 1; j++)
  484. {
  485. float sum = 0.0f;
  486. for (int i = 0; i < 4; i++)
  487. {
  488. ChiX[i][j] = theta[i][0] * ChiXtmp[0][j] + theta[i][1] * ChiXtmp[1][j] + theta[i][2] * ChiXtmp[2][j] + theta[i][3] * ChiXtmp[3][j];
  489. sum += ChiX[i][j] * ChiX[i][j];
  490. }
  491. sum = 1 / sqrt(sum);
  492. for (int i = 0; i < 4; i++)
  493. {
  494. ChiX[i][j] *= sum;
  495. }
  496. };
  497. //复制Chix
  498. for (int i = 0; i < 4; i++)
  499. for (int j = 0; j < 9; j++)
  500. {
  501. ChiXtmp[i][j] = ChiX[i][j];
  502. }
  503. QuatPredict(ChiX, quat, wm, 9);
  504. StateCovarianceMatrix(P, ChiX, quat, wc, 2 * L + 1);
  505. quatXmag(ChiXtmp, mag_prev, ChiY);
  506. MagPredict(ChiY, mag, wm, 2 * L + 1);
  507. MatrixSubVector(ChiY, mag, 3);
  508. StateMeasureCovariance(YXcov, ChiX, 4, ChiY, 3, wc, 9);
  509. MeasureCovMatrix(ChiY, ChiYcov, wc, 3);
  510. GainUKF(YXcov, ChiYcov, K);
  511. updateState(quat, K, mag_now, mag);
  512. updataStateCov(P, ChiYcov, K);
  513. }
  514. */
  515. /*
  516. int main()
  517. {
  518. int L = 4;
  519. float alpha = 0.01f;
  520. float beta = 2.0f;
  521. float kappa = 0.0f;
  522. float gamma ;
  523. float wm[9];
  524. float wc[9];
  525. UKF_para(L, alpha, beta, kappa, &gamma, wm, wc);
  526. std::cout << gamma << std::endl;
  527. for (int i = 0; i < 9; i++)
  528. {
  529. printf("%.20f ", wm[i]);
  530. }
  531. std::cout << std::endl;
  532. std::cout << std::endl;
  533. for (int i = 0; i < 9; i++)
  534. {
  535. printf("%.20f ", wc[i]);
  536. }
  537. std::cout << std::endl;
  538. }
  539. */
  540. /*
  541. void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
  542. {
  543. double *q;
  544. double *P;
  545. double *gyr;
  546. double *mag_prev;
  547. double *mag_now;
  548. double *gama;
  549. double *wm;
  550. double *wc;
  551. double *length;
  552. double *X_out;
  553. double *P_out;
  554. int Prows, Pcols;
  555. int Qrows, Qcols;
  556. int Mrows, Mcols;
  557. int Grows, Gcols;
  558. float P_quat[4][4];
  559. float quat[4];
  560. float Mag_prev[3];
  561. float Mag_now[3];
  562. float Gyr[3];
  563. float Gama;
  564. float Wm[9];
  565. float Wc[9];
  566. int L;
  567. q = mxGetPr(prhs[0]);
  568. Qrows = mxGetM(prhs[0]);
  569. Qcols = mxGetN(prhs[0]);
  570. for (int j = 0; j < Qrows; j++)
  571. {
  572. for (int i = 0; i < Qcols; i++)
  573. {
  574. quat[i*Qrows + j] = q[i*Qrows + j];
  575. }
  576. }
  577. P = mxGetPr(prhs[1]);
  578. Prows = mxGetM(prhs[1]);
  579. Pcols = mxGetN(prhs[1]);
  580. for (int j = 0; j < Prows; j++)
  581. {
  582. for (int i = 0; i < Pcols; i++)
  583. {
  584. P_quat[j][i] = P[i*Prows + j];
  585. }
  586. }
  587. gyr = mxGetPr(prhs[2]);
  588. Grows = mxGetM(prhs[2]);
  589. Gcols = mxGetN(prhs[2]);
  590. for (int j = 0; j < Grows; j++)
  591. {
  592. for (int i = 0; i < Gcols; i++)
  593. {
  594. Gyr[i*Grows + j] = gyr[i*Grows + j];
  595. }
  596. }
  597. mag_prev = mxGetPr(prhs[3]);
  598. Mrows = mxGetM(prhs[3]);
  599. Mcols = mxGetN(prhs[3]);
  600. for (int j = 0; j < Mrows; j++)
  601. {
  602. for (int i = 0; i < Mcols; i++)
  603. {
  604. Mag_prev[i*Mrows + j] = mag_prev[i*Mrows + j];
  605. }
  606. }
  607. mag_now = mxGetPr(prhs[4]);
  608. Mrows = mxGetM(prhs[4]);
  609. Mcols = mxGetN(prhs[4]);
  610. for (int j = 0; j < Mrows; j++)
  611. {
  612. for (int i = 0; i < Mcols; i++)
  613. {
  614. Mag_now[i*Mrows + j] = mag_now[i*Mrows + j];
  615. }
  616. }
  617. gama = mxGetPr(prhs[5]);
  618. Gama = *gama;
  619. wm = mxGetPr(prhs[6]);
  620. Mrows = mxGetM(prhs[6]);
  621. Mcols = mxGetN(prhs[6]);
  622. for (int j = 0; j < Mrows; j++)
  623. {
  624. for (int i = 0; i < Mcols; i++)
  625. {
  626. Wm[i*Mrows + j] = wm[i*Mrows + j];
  627. }
  628. }
  629. wc = mxGetPr(prhs[7]);
  630. Mrows = mxGetM(prhs[7]);
  631. Mcols = mxGetN(prhs[7]);
  632. for (int j = 0; j < Mrows; j++)
  633. {
  634. for (int i = 0; i < Mcols; i++)
  635. {
  636. Wc[i*Mrows + j] = wc[i*Mrows + j];
  637. }
  638. }
  639. length = mxGetPr(prhs[8]);
  640. L = (int)(*length);
  641. UKF_quat(quat, P_quat, Gyr, Mag_prev, Mag_now, Gama, Wm, Wc, L);
  642. plhs[0] = mxCreateDoubleMatrix(4, 1, mxREAL);
  643. X_out = mxGetPr(plhs[0]);
  644. plhs[1] = mxCreateDoubleMatrix(4, 4, mxREAL);
  645. P_out = mxGetPr(plhs[1]);
  646. for (int i = 0; i < 4; i++)
  647. for (int j = 0; j < 1; j++)
  648. {
  649. X_out[j * 4 + i] = quat[j * 4 + i];
  650. }
  651. for (int i = 0; i < 4; i++)
  652. for (int j = 0; j < 4; j++)
  653. {
  654. P_out[j * 4 + i] = P_quat[i][j];
  655. }
  656. }*/