footPDR.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. #include "pdrStatus.h"
  2. #include "footPDR.h"
  3. //当地的重力加速度
  4. float g = 9.8179995f;
  5. float dt = 0.01f;
  6. float P[81], acc_n[3];
  7. float Temporary_array1[9], Temporary_array2[9];
  8. float K[27], P_prev[81], delta_x[9];
  9. float C[9], C_prev[9];
  10. float vel_n[3], pos_n[3];
  11. float last_pos_n[3];
  12. float pos_offset[3];
  13. int stand_num = 0;
  14. float gyr_extreme[6];
  15. float gyr_mean[3];
  16. float num_peak;
  17. float gyrBias[3];
  18. uint32_t frame_index = 0;
  19. //重置磁航向,计算双脚的磁航向,以确定身体的正朝向
  20. float heading_buff[20];
  21. float zupt_heading;
  22. int step_index = 0;
  23. int32_t last_timestamp;
  24. void set_pdr_status()
  25. {
  26. frame_index = 0;
  27. }
  28. void saveStepData(int index, float heading)
  29. {
  30. heading_buff[index % 20] = heading;
  31. }
  32. float calDeltaHeading(int index, float now_heading)
  33. {
  34. //寻找相似的方向
  35. int start_index = index - 20;
  36. if(start_index < 0)
  37. start_index = 0;
  38. float deltaHeading;
  39. for(int i = start_index; i < index; i++)
  40. {
  41. deltaHeading = now_heading - heading_buff[i % 20];
  42. if(deltaHeading < -3.141591f)
  43. {
  44. deltaHeading = (deltaHeading + 6.2831852f);
  45. }
  46. else if(deltaHeading > 3.141591f)
  47. {
  48. deltaHeading = (deltaHeading - 6.2831852f);
  49. }
  50. if(fabsf(deltaHeading) < 0.0873f)//5/180*pi
  51. {
  52. return deltaHeading;
  53. }
  54. }
  55. return 99;
  56. }
  57. void calKafmanGain9x4(float *K, float *P)
  58. {
  59. float m_rever[4][4];
  60. float m[4][4];
  61. m[0][0] = P[20];m[0][1] = P[24];m[0][2] = P[25];m[0][3] = P[26];
  62. m[1][0] = P[56];m[1][1] = P[60];m[1][2] = P[61];m[1][3] = P[62];
  63. m[2][0] = P[65];m[2][1] = P[69];m[2][2] = P[70];m[2][3] = P[71];
  64. m[3][0] = P[74];m[3][1] = P[78];m[3][2] = P[79];m[3][3] = P[80];
  65. for(int i = 0; i <4; i++)
  66. {
  67. m[i][i] += SIGMA_V * SIGMA_V;
  68. }
  69. matrix_inverse(m, m_rever);
  70. K[0] = P[2] * m_rever[0][0] + P[6] * m_rever[1][0] + P[7] * m_rever[2][0] + P[8] * m_rever[3][0];
  71. K[1] = P[2] * m_rever[0][1] + P[6] * m_rever[1][1] + P[7] * m_rever[2][1] + P[8] * m_rever[3][1];
  72. K[2] = P[2] * m_rever[0][2] + P[6] * m_rever[1][2] + P[7] * m_rever[2][2] + P[8] * m_rever[3][2];
  73. K[3] = P[2] * m_rever[0][3] + P[6] * m_rever[1][3] + P[7] * m_rever[2][3] + P[8] * m_rever[3][3];
  74. K[4] = P[11] * m_rever[0][0] + P[15] * m_rever[1][0] + P[16] * m_rever[2][0] + P[17] * m_rever[3][0];
  75. K[5] = P[11] * m_rever[0][1] + P[15] * m_rever[1][1] + P[16] * m_rever[2][1] + P[17] * m_rever[3][1];
  76. K[6] = P[11] * m_rever[0][2] + P[15] * m_rever[1][2] + P[16] * m_rever[2][2] + P[17] * m_rever[3][2];
  77. K[7] = P[11] * m_rever[0][3] + P[15] * m_rever[1][3] + P[16] * m_rever[2][3] + P[17] * m_rever[3][3];
  78. K[8] = P[20] * m_rever[0][0] + P[24] * m_rever[1][0] + P[25] * m_rever[2][0] + P[26] * m_rever[3][0];
  79. K[9] = P[20] * m_rever[0][1] + P[24] * m_rever[1][1] + P[25] * m_rever[2][1] + P[26] * m_rever[3][1];
  80. K[10] = P[20] * m_rever[0][2] + P[24] * m_rever[1][2] + P[25] * m_rever[2][2] + P[26] * m_rever[3][2];
  81. K[11] = P[20] * m_rever[0][3] + P[24] * m_rever[1][3] + P[25] * m_rever[2][3] + P[26] * m_rever[3][3];
  82. K[12] = P[29] * m_rever[0][0] + P[33] * m_rever[1][0] + P[34] * m_rever[2][0] + P[35] * m_rever[3][0];
  83. K[13] = P[29] * m_rever[0][1] + P[33] * m_rever[1][1] + P[34] * m_rever[2][1] + P[35] * m_rever[3][1];
  84. K[14] = P[29] * m_rever[0][2] + P[33] * m_rever[1][2] + P[34] * m_rever[2][2] + P[35] * m_rever[3][2];
  85. K[15] = P[29] * m_rever[0][3] + P[33] * m_rever[1][3] + P[34] * m_rever[2][3] + P[35] * m_rever[3][3];
  86. K[16] = P[38] * m_rever[0][0] + P[42] * m_rever[1][0] + P[43] * m_rever[2][0] + P[44] * m_rever[3][0];
  87. K[17] = P[38] * m_rever[0][1] + P[42] * m_rever[1][1] + P[43] * m_rever[2][1] + P[44] * m_rever[3][1];
  88. K[18] = P[38] * m_rever[0][2] + P[42] * m_rever[1][2] + P[43] * m_rever[2][2] + P[44] * m_rever[3][2];
  89. K[19] = P[38] * m_rever[0][3] + P[42] * m_rever[1][3] + P[43] * m_rever[2][3] + P[44] * m_rever[3][3];
  90. K[20] = P[47] * m_rever[0][0] + P[51] * m_rever[1][0] + P[52] * m_rever[2][0] + P[53] * m_rever[3][0];
  91. K[21] = P[47] * m_rever[0][1] + P[51] * m_rever[1][1] + P[52] * m_rever[2][1] + P[53] * m_rever[3][1];
  92. K[22] = P[47] * m_rever[0][2] + P[51] * m_rever[1][2] + P[52] * m_rever[2][2] + P[53] * m_rever[3][2];
  93. K[23] = P[47] * m_rever[0][3] + P[51] * m_rever[1][3] + P[52] * m_rever[2][3] + P[53] * m_rever[3][3];
  94. K[24] = P[56] * m_rever[0][0] + P[60] * m_rever[1][0] + P[61] * m_rever[2][0] + P[62] * m_rever[3][0];
  95. K[25] = P[56] * m_rever[0][1] + P[60] * m_rever[1][1] + P[61] * m_rever[2][1] + P[62] * m_rever[3][1];
  96. K[26] = P[56] * m_rever[0][2] + P[60] * m_rever[1][2] + P[61] * m_rever[2][2] + P[62] * m_rever[3][2];
  97. K[27] = P[56] * m_rever[0][3] + P[60] * m_rever[1][3] + P[61] * m_rever[2][3] + P[62] * m_rever[3][3];
  98. K[28] = P[65] * m_rever[0][0] + P[69] * m_rever[1][0] + P[70] * m_rever[2][0] + P[71] * m_rever[3][0];
  99. K[29] = P[65] * m_rever[0][1] + P[69] * m_rever[1][1] + P[70] * m_rever[2][1] + P[71] * m_rever[3][1];
  100. K[30] = P[65] * m_rever[0][2] + P[69] * m_rever[1][2] + P[70] * m_rever[2][2] + P[71] * m_rever[3][2];
  101. K[31] = P[65] * m_rever[0][3] + P[69] * m_rever[1][3] + P[70] * m_rever[2][3] + P[71] * m_rever[3][3];
  102. K[32] = P[74] * m_rever[0][0] + P[78] * m_rever[1][0] + P[79] * m_rever[2][0] + P[80] * m_rever[3][0];
  103. K[33] = P[74] * m_rever[0][1] + P[78] * m_rever[1][1] + P[79] * m_rever[2][1] + P[80] * m_rever[3][1];
  104. K[34] = P[74] * m_rever[0][2] + P[78] * m_rever[1][2] + P[79] * m_rever[2][2] + P[80] * m_rever[3][2];
  105. K[35] = P[74] * m_rever[0][3] + P[78] * m_rever[1][3] + P[79] * m_rever[2][3] + P[80] * m_rever[3][3];
  106. }
  107. void calDeltaX9x4(float *K, float *measure, float *delta_x)
  108. {
  109. for(int i = 0; i < 9; i++)
  110. {
  111. delta_x[i] = 0.0f;
  112. for(int j = 0; j < 4; j ++)
  113. {
  114. delta_x[i] += (K[i * 4 + j] *measure[j]);
  115. }
  116. }
  117. }
  118. void calStateCov9x4(float *P, float *K)
  119. {
  120. static float P_copy[81];
  121. for(int i = 0; i < 9; i++)
  122. {
  123. for(int j = 0; j < 9; j++)
  124. {
  125. P_copy[i * 9 + j] = K[i * 4] * P[18 + j] + K[i * 4 + 1] * P[54 + j] + K[i * 4 + 2] * P[63 + j] + K[i * 4 + 3] * P[72 + j];
  126. }
  127. }
  128. for(int i = 0; i < 81 ; i ++)
  129. {
  130. P[i] -= P_copy[i];
  131. }
  132. }
  133. float calHeading(float mag[3], float acc[3])
  134. {
  135. float hSqrt;
  136. float eSqrt;
  137. float h[3]; //东向
  138. h[0] = mag[1] * acc[2] - mag[2] * acc[1];
  139. h[1] = mag[2] * acc[0] - mag[0] * acc[2];
  140. h[2] = mag[0] * acc[1] - mag[1] * acc[0];
  141. hSqrt = 1/sqrt(h[0] * h[0] + h[1] * h[1] + h[2] * h[2]);
  142. for(int i = 0; i < 3; i++)
  143. {
  144. h[i] *= hSqrt;
  145. }
  146. float e[3]; //北向
  147. e[0] = acc[1] * h[2] - acc[2] * h[1];
  148. e[1] = acc[2] * h[0] - acc[0] * h[2];
  149. e[2] = acc[0] * h[1] - acc[1] * h[0];
  150. eSqrt = 1/sqrt(e[0] * e[0] + e[1] * e[1] + e[2] * e[2]);
  151. for(int i = 0; i < 3; i++)
  152. {
  153. e[i] *= eSqrt;
  154. }
  155. return atan2(-h[1], e[1]);
  156. }
  157. void resetAttbyMag(float C[9], float acc[3], float mag[3])
  158. {
  159. float accScale = sqrt(acc[0] * acc[0] + acc[1] * acc[1] + acc[2] * acc[2]);
  160. float pitch = asin(-acc[0]/accScale);
  161. float roll = atan2(acc[1], acc[2]);
  162. float pitch_sin = sin(pitch);
  163. float pitch_cos = cos(pitch);
  164. float roll_sin = sin(roll);
  165. float roll_cos = cos(roll);
  166. float mag_heading;
  167. C[0] = pitch_cos;
  168. C[1] = pitch_sin * roll_sin;
  169. C[2] = pitch_sin * roll_cos;
  170. C[3] = 0.0;
  171. C[4] = roll_cos;
  172. C[5] = -roll_sin;
  173. mag_heading = atan2(-C[4] * mag[1] - C[5] * mag[2], C[0] * mag[0] + C[1] * mag[1] + C[2] * mag[2]);
  174. float yaw_sin = sin(mag_heading);
  175. float yaw_cos = cos(mag_heading);
  176. C[0] = pitch_cos * yaw_cos;
  177. C[1] = pitch_sin * roll_sin * yaw_cos - roll_cos * yaw_sin;
  178. C[2] = pitch_sin * roll_cos * yaw_cos + roll_sin * yaw_sin;
  179. C[3] = pitch_cos * yaw_sin;
  180. C[4] = pitch_sin * roll_sin * yaw_sin + roll_cos * yaw_cos;
  181. C[5] = pitch_sin * roll_cos * yaw_sin - roll_sin * yaw_cos;
  182. C[6] = acc[0];
  183. C[7] = acc[1];
  184. C[8] = acc[2];
  185. }
  186. void Initialize(float *gyr, float *acc)
  187. {
  188. stand_num = 0;
  189. memset(last_pos_n, 0, 3 * sizeof(float));
  190. memset(pos_offset, 0, 3 * sizeof(float));
  191. memset(P, 0, 81 * sizeof(float));
  192. memset(acc_n, 0, 3 * sizeof(float));
  193. memset(vel_n, 0, 3 * sizeof(float));
  194. memset(pos_n, 0, 3 * sizeof(float));
  195. memset(Temporary_array1, 0, 9 * sizeof(float));
  196. memset(Temporary_array2, 0, 9 * sizeof(float));
  197. memset(K, 0, 27 * sizeof(float));
  198. memset(P_prev, 0, 81 * sizeof(float));
  199. memset(delta_x, 0, 9 * sizeof(float));
  200. memset(C, 0, 9 * sizeof(float));
  201. memset(Temporary_array1, 0, 9 * sizeof(float));
  202. memset(Temporary_array2, 0, 9 * sizeof(float));
  203. init_attitude_matrix(C, acc, g);
  204. memcpy(C_prev, C, 9 * sizeof(float));
  205. // memcpy(gyrBias, (uint32_t *)FLASH_ADD, 3 * sizeof(float));
  206. }
  207. void attitude_matrix_update(float *C, float *Temporary_array1, float *Temporary_array2, float *gyr, float dt)
  208. {
  209. Temporary_array1[0] = 2.0f;
  210. Temporary_array1[1] = dt * gyr[2];
  211. Temporary_array1[2] = -dt * gyr[1];
  212. Temporary_array1[3] = -dt * gyr[2];
  213. Temporary_array1[4] = 2.0f;
  214. Temporary_array1[5] = dt * gyr[0];
  215. Temporary_array1[6] = dt * gyr[1];
  216. Temporary_array1[7] = -dt * gyr[0];
  217. Temporary_array1[8] = 2.0f;
  218. invert3x3(Temporary_array1, Temporary_array2);
  219. memset(Temporary_array1, 0, 9 * sizeof(float));
  220. Temporary_array1[0] = 2 * C[0] + C[1] * dt * gyr[2] - C[2] * dt * gyr[1];
  221. Temporary_array1[1] = 2 * C[1] - C[0] * dt * gyr[2] + C[2] * dt * gyr[0];
  222. Temporary_array1[2] = 2 * C[2] + C[0] * dt * gyr[1] - C[1] * dt * gyr[0];
  223. Temporary_array1[3] = 2 * C[3] + C[4] * dt * gyr[2] - C[5] * dt * gyr[1];
  224. Temporary_array1[4] = 2 * C[4] - C[3] * dt * gyr[2] + C[5] * dt * gyr[0];
  225. Temporary_array1[5] = 2 * C[5] + C[3] * dt * gyr[1] - C[4] * dt * gyr[0];
  226. Temporary_array1[6] = 2 * C[6] + C[7] * dt * gyr[2] - C[8] * dt * gyr[1];
  227. Temporary_array1[7] = 2 * C[7] - C[6] * dt * gyr[2] + C[8] * dt * gyr[0];
  228. Temporary_array1[8] = 2 * C[8] + C[6] * dt * gyr[1] - C[7] * dt * gyr[0];
  229. multiply3x3(Temporary_array1, Temporary_array2, C);
  230. }
  231. float max_window_val(float *window, int window_size)
  232. {
  233. float val = window[0];
  234. for (int i = 0; i < window_size; i++)
  235. {
  236. if (window[i] > val)
  237. val = window[i];
  238. }
  239. return val;
  240. }
  241. int max_window_int(int *window, int window_size)
  242. {
  243. int val = window[0];
  244. for (int i = 0; i < window_size; i++)
  245. {
  246. if (window[i] > val)
  247. val = window[i];
  248. }
  249. return val;
  250. }
  251. float min_window_val(float *window, int window_size)
  252. {
  253. float val = window[0];
  254. for (int i = 0; i < window_size; i++)
  255. {
  256. if (window[i] < val)
  257. val = window[i];
  258. }
  259. return val;
  260. }
  261. int min_window_int(int *window, int window_size)
  262. {
  263. int val = window[0];
  264. for (int i = 0; i < window_size; i++)
  265. {
  266. if (window[i] < val)
  267. val = window[i];
  268. }
  269. return val;
  270. }
  271. //press_tren 函数功能:提供走路过程中上升沿,下降沿
  272. //1 为上升 2 为 下降 0为不需要得状态
  273. int press_trend(int index, int *window, int window_size)
  274. {
  275. int i;
  276. int max_val = window[(index - 1) % window_size];
  277. int max_index = index;
  278. int min_val = max_val;
  279. int min_index = max_index;
  280. for (i = 1; i < window_size + 1; i++)
  281. {
  282. if (max_val < window[(index - i) % window_size])
  283. {
  284. max_index = index - i + 1;
  285. max_val = window[(index - i) % window_size];
  286. }
  287. if (min_val > window[(index - i) % window_size])
  288. {
  289. min_index = index - i + 1;
  290. min_val = window[(index - i) % window_size];
  291. }
  292. }
  293. if (max_index > min_index && max_val > min_val + 50000)
  294. {
  295. return 1;
  296. }
  297. if (max_index < min_index && max_val > min_val + 50000)
  298. {
  299. return 2;
  300. }
  301. return 0;
  302. }
  303. void dcm2angle(float *dcm, float *roll, float *pitch, float *yaw)
  304. {
  305. *yaw = atan2(dcm[3], dcm[0]);
  306. *pitch = asin(-dcm[6]);
  307. *roll = atan2(dcm[7], dcm[8]);
  308. }
  309. void quat2angleTest(float qin[4], float *roll, float *pitch, float *yaw)
  310. {
  311. //float r11 = qin[0] * qin[0] + qin[1] * qin[1] - qin[2] * qin[2] - qin[3] * qin[3];
  312. float r11 = 2.0f * (qin[1] * qin[2] + qin[0] * qin[3]);
  313. //float r21 = 2.0f * (qin[1] * qin[2] - qin[0] * qin[3]);
  314. float r12 = qin[0] * qin[0] + qin[1] * qin[1] - qin[2] * qin[2] - qin[3] * qin[3];
  315. float r21 = -2.0f * (qin[1] * qin[3] - qin[0] * qin[2]);
  316. float r31 = 2.0f * (qin[2] * qin[3] + qin[0] * qin[1]);
  317. float r32 = qin[0] * qin[0] - qin[1] * qin[1] - qin[2] * qin[2] + qin[3] * qin[3];
  318. if (r21 < -0.999999999f)
  319. r21 = -1.0f;
  320. else if (r21 > 0.999999999f)
  321. r21 = 1.0f;
  322. *roll = atan2(r11, r12);
  323. *pitch = asin(r21);
  324. *yaw = atan2(r31, r32);
  325. }
  326. void dcm2angleTest(float C[9], short att[3])
  327. {
  328. float yaw, pitch, roll;
  329. pitch = asin(-C[6]);
  330. if(C[6] > 0.999999f || C[6] < -0.999999f)
  331. {
  332. //当俯仰角为90度的时候,则假设翻滚角为0度
  333. yaw = atan2(-C[1], C[4]);
  334. roll = 0.0f;
  335. }
  336. else
  337. {
  338. yaw = atan2(C[3], C[0]);
  339. roll = atan2(C[7], C[8]);
  340. }
  341. att[0] = (short)(yaw * 10000.f); //yaw
  342. att[1] = (short)(pitch * 10000.f); //pitch
  343. att[2] = (short)(roll * 10000.f); //roll
  344. }
  345. void quat2dcm(float *qin, float *dcm)
  346. {
  347. float qin_norm = 1 / sqrt(qin[0] * qin[0] + qin[1] * qin[1] + qin[2] * qin[2] + qin[3] * qin[3]);
  348. for (int i = 0; i < 4; i++)
  349. qin[i] *= qin_norm;
  350. dcm[0] = qin[0] * qin[0] + qin[1] * qin[1] - qin[2] * qin[2] - qin[3] * qin[3];
  351. dcm[1] = 2.0f * (qin[1] * qin[2] + qin[0] * qin[3]);
  352. dcm[2] = 2.0f * (qin[1] * qin[3] - qin[0] * qin[2]);
  353. dcm[3] = 2.0f * (qin[1] * qin[2] - qin[0] * qin[3]);
  354. dcm[4] = qin[0] * qin[0] - qin[1] * qin[1] + qin[2] * qin[2] - qin[3] * qin[3];
  355. dcm[5] = 2.0f * (qin[2] * qin[3] + qin[0] * qin[1]);
  356. dcm[6] = 2.0f * (qin[1] * qin[3] + qin[0] * qin[2]);
  357. dcm[7] = 2.0f * (qin[2] * qin[3] - qin[0] * qin[1]);
  358. dcm[8] = qin[0] * qin[0] - qin[1] * qin[1] - qin[2] * qin[2] + qin[3] * qin[3];
  359. }
  360. void multiply3x3T(float *a, float *b, float* dst)
  361. {
  362. dst[0] = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
  363. dst[1] = a[0] * b[3] + a[1] * b[4] + a[2] * b[5];
  364. dst[2] = a[0] * b[6] + a[1] * b[7] + a[2] * b[8];
  365. dst[3] = a[3] * b[0] + a[4] * b[1] + a[5] * b[2];
  366. dst[4] = a[3] * b[3] + a[4] * b[4] + a[5] * b[5];
  367. dst[5] = a[3] * b[6] + a[4] * b[7] + a[5] * b[8];
  368. dst[6] = a[6] * b[0] + a[7] * b[1] + a[8] * b[2];
  369. dst[7] = a[6] * b[3] + a[7] * b[4] + a[8] * b[5];
  370. dst[8] = a[6] * b[6] + a[7] * b[7] + a[8] * b[8];
  371. }
  372. void deltaAttMatrix(float C_prev[9], float C_now[9], float deltaC[9])
  373. {
  374. //detaC = C_prev'* C;
  375. deltaC[0] = C_now[0] * C_prev[0] + C_now[3] * C_prev[3] + C_now[6] * C_prev[6];
  376. deltaC[1] = C_now[1] * C_prev[0] + C_now[4] * C_prev[3] + C_now[7] * C_prev[6];
  377. deltaC[2] = C_now[2] * C_prev[0] + C_now[5] * C_prev[3] + C_now[8] * C_prev[6];
  378. deltaC[3] = C_now[0] * C_prev[1] + C_now[3] * C_prev[4] + C_now[6] * C_prev[7];
  379. deltaC[4] = C_now[1] * C_prev[1] + C_now[4] * C_prev[4] + C_now[7] * C_prev[7];
  380. deltaC[5] = C_now[2] * C_prev[1] + C_now[5] * C_prev[4] + C_now[8] * C_prev[7];
  381. deltaC[6] = C_now[0] * C_prev[2] + C_now[3] * C_prev[5] + C_now[6] * C_prev[8];
  382. deltaC[7] = C_now[1] * C_prev[2] + C_now[4] * C_prev[5] + C_now[7] * C_prev[8];
  383. deltaC[8] = C_now[2] * C_prev[2] + C_now[5] * C_prev[5] + C_now[8] * C_prev[8];
  384. }
  385. void normVector(float a[3])
  386. {
  387. float norm = 1.0f/sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
  388. a[0] *= norm;
  389. a[1] *= norm;
  390. a[2] *= norm;
  391. }
  392. void resetUKF(float *UKF_Q, float UKF_P[4][4], float *mag_prev, float *mag, float *UKF_C, float *C)
  393. {
  394. memset(UKF_Q, 0, 4 * sizeof(float));
  395. UKF_Q[0] = 1.0f;
  396. memcpy(mag_prev, mag, 3 * sizeof(float));
  397. memcpy(UKF_C, C, 9 * sizeof(float));
  398. for (int i = 0; i < 4; i++)
  399. for (int j = 0; j < 4; j++)
  400. {
  401. UKF_P[i][j] = 0.0f;
  402. }
  403. for (int i = 0; i < 4; i++)
  404. {
  405. UKF_P[i][i] = 0.0000001f;
  406. }
  407. }
  408. //利用陀螺仪的双极端盘判断是否在稳定的范围
  409. int isStandCon(float gyr_extreme[6])
  410. {
  411. SEGGER_RTT_printf(0," left_sh , gyr_extreme[1] - gyr_extreme[0] = %d\n",(int)((gyr_extreme[1] - gyr_extreme[0])*1000.f));
  412. SEGGER_RTT_printf(0," left_sh , gyr_extreme[1] - gyr_extreme[0] = %d\n",(int)((gyr_extreme[3] - gyr_extreme[2])*1000.f));
  413. SEGGER_RTT_printf(0," left_sh , gyr_extreme[1] - gyr_extreme[0] = %d\n",(int)((gyr_extreme[5] - gyr_extreme[4])*1000.f));
  414. if(gyr_extreme[1] - gyr_extreme[0] < 0.015f && gyr_extreme[3] - gyr_extreme[2] < 0.015f && gyr_extreme[5] - gyr_extreme[4] < 0.015f)
  415. {
  416. return 1;
  417. }
  418. return 0;
  419. }
  420. void estimate_gyr_bias(float *gyr)
  421. {
  422. if (num_peak == 0)
  423. {
  424. for (int i = 0; i < 3; i++)
  425. {
  426. gyr_extreme[2 * i] = gyr[i];
  427. gyr_extreme[2 * i + 1] = gyr[i];
  428. }
  429. }
  430. for (int i = 0; i < 3; i++)
  431. {
  432. if (gyr[i] < gyr_extreme[2 * i])
  433. {
  434. gyr_extreme[2 * i] = gyr[i];
  435. }
  436. if (gyr[i] > gyr_extreme[2 * i + 1])
  437. {
  438. gyr_extreme[2 * i + 1] = gyr[i];
  439. }
  440. }
  441. for (int i = 0; i < 3; i++)
  442. {
  443. gyr_mean[i] += gyr[i];
  444. }
  445. num_peak++;
  446. //在线估计陀螺仪的零偏, 6050的零偏偏大
  447. if (num_peak == 300)
  448. {
  449. if (isStandCon(gyr_extreme))
  450. {
  451. //识别每一次游戏模式下,静止状态的陀螺仪令零偏
  452. for(int i = 0; i < 3; i++)
  453. {
  454. gyrBias[i] = gyr_mean[i] * 0.0033f;
  455. }
  456. SEGGER_RTT_printf(0,"gyrBias has cor!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  457. }
  458. num_peak = 0;
  459. memset(gyr_mean, 0, 3 * sizeof(float));
  460. }
  461. }
  462. void estimate_gyr_bias_interface(int16_t *gyr, int sample_count)
  463. {
  464. static float gyr_f[3];
  465. for(int i = 0; i < 3; i++)
  466. {
  467. gyr_f[i] = gyr[i] * 0.0610f;
  468. }
  469. if(sample_count == 0)
  470. {
  471. num_peak = 0;
  472. }
  473. estimate_gyr_bias(gyr_f);
  474. }
  475. unsigned char footPDR(int32_t num, float *gyr, float *acc, int16_t front_zero, int16_t back_zero, int16_t acc_zero, int32_t* pos_res, int16_t* att)
  476. {
  477. unsigned char movement_e = 0;
  478. dt = (float)(num - last_timestamp) * 0.000001f;
  479. if(num - last_timestamp <= 0)
  480. {
  481. dt = 0.01f;
  482. }
  483. last_timestamp = num;
  484. for (int i = 0; i < 3; i++)
  485. {
  486. gyr[i] *= (PI / 180);
  487. acc[i] *= g;
  488. }
  489. //估计零偏
  490. estimate_gyr_bias(gyr);
  491. gyr[0] -= gyrBias[0];
  492. gyr[1] -= gyrBias[1];
  493. gyr[2] -= gyrBias[2];
  494. //下面为惯导解算
  495. if (frame_index == 0)
  496. {
  497. Initialize(gyr, acc);
  498. frame_index = 1;
  499. SEGGER_RTT_printf(0, "PDR INIT");
  500. return movement_e;
  501. }
  502. //惯导解算: 姿态矩阵更新
  503. attitude_matrix_update(C, Temporary_array1, Temporary_array2, gyr, dt);
  504. //惯导解算: 将IMU坐标系的加速度转换到“导航坐标系”下
  505. multiply3x1(C, acc, acc_n);
  506. //惯导解算: 更新IMU速度
  507. vel_n[0] = vel_n[0] + acc_n[0] * dt;
  508. vel_n[1] = vel_n[1] + acc_n[1] * dt;
  509. vel_n[2] = vel_n[2] + (acc_n[2] - g) * dt;
  510. //惯导解算: 更新IMU位置
  511. pos_n[0] = pos_n[0] + vel_n[0] * dt;
  512. pos_n[1] = pos_n[1] + vel_n[1] * dt;
  513. pos_n[2] = pos_n[2] + vel_n[2] * dt;
  514. //ekf步骤: 状态协方差矩阵预测更新
  515. //P = F*P*F' + Q;
  516. State_covariance_matrix_update(P, acc_n, dt);
  517. //zupt
  518. if (front_zero == 1 || back_zero == 1 || acc_zero == 1)
  519. {
  520. //ekf步骤: 计算卡尔曼滤波增益
  521. //K = P*H'/(H*P*H' + R);
  522. calKafmanGain9x4(K, P);
  523. //ekf步骤: 观测误差更新
  524. //delta_x = K * [vel_n(:,i);];
  525. float now_heading = atan2(C[3], C[0]);
  526. float measure[4];
  527. //计算理想的角度
  528. memset(measure, 0, 4 *sizeof(float));
  529. measure[0] = calDeltaHeading(step_index, now_heading);
  530. if(measure[0] > 10.0f)
  531. {
  532. measure[0] = 0;
  533. }
  534. measure[1] = vel_n[0];
  535. measure[2] = vel_n[1];
  536. measure[3] = vel_n[2];
  537. calDeltaX9x4(K, measure, delta_x);
  538. //ekf步骤: 状态协方差矩阵观测更新
  539. calStateCov9x4(P, K);
  540. //这里先从设置 delta_x(3) = atan2(C(2,1),C(1,1));
  541. //意味着每一步的参考方向是IMU X轴方向
  542. // delta_x[2] = atan2(C[3], C[0]);
  543. // theta = -1.7801 + atan2(C[3], C[0]);
  544. // theta = 0.0f;
  545. //修正姿态矩阵
  546. Att_matrix_corr(C, C_prev, Temporary_array1, Temporary_array2, delta_x);
  547. //修正位置
  548. //pos_n_corr(pos_n, delta_x);
  549. //修正速度
  550. vel_n_corr(vel_n, delta_x);
  551. if(back_zero == 1)
  552. {
  553. zupt_heading = now_heading;
  554. stand_num ++;
  555. }
  556. memcpy(last_pos_n, pos_n, 3 * sizeof(float));
  557. }
  558. else
  559. {
  560. //存放一步内记录的mag数据以及heading数据
  561. if(stand_num > 5)
  562. {
  563. saveStepData(step_index, zupt_heading);
  564. step_index ++;
  565. }
  566. stand_num = 0;
  567. }
  568. //状态协方差矩阵保持正交性,以防出现退化
  569. State_covariance_matrix_orthogonalization(P);
  570. pos_offset[0] = pos_offset[0] + pos_n[0] - last_pos_n[0];
  571. pos_offset[1] = pos_offset[1] + pos_n[1] - last_pos_n[1];
  572. pos_offset[2] = pos_offset[2] + pos_n[2] - last_pos_n[2];
  573. memcpy(last_pos_n, pos_n, 3 * sizeof(float));
  574. dcm2angleTest(C, att); //航向角,俯仰角, 翻滚角(z y x)
  575. pos_res[0] = (int32_t) (pos_offset[0] * 100.0f);
  576. pos_res[1] = (int32_t) (pos_offset[1] * 100.0f);
  577. pos_res[2] = (int32_t) (pos_offset[2] * 100.0f);
  578. return movement_e;
  579. }