footPDR.c 20 KB

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