special_motion.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. #include "special_motion.h"
  2. #include "motion_config.h"
  3. int down_send_count = 0;
  4. int is_down = 0;
  5. int jump_send_count = 0;
  6. int is_jump = 0;
  7. int down_res = 0;
  8. int jump_res = 0;
  9. int16_t left_time;
  10. uint16_t left_press;
  11. int32_t left_pos[3];
  12. int16_t left_att[3];
  13. int16_t left_acc[3];
  14. int16_t left_zupt;
  15. int16_t right_time;
  16. uint16_t right_press;
  17. int32_t right_pos[3];
  18. int16_t right_att[3];
  19. int16_t right_acc[3];
  20. int16_t right_zupt;
  21. uint8_t rssi;
  22. uint8_t right_rssi;
  23. uint8_t left_rssi;
  24. uint16_t right_front_mag;
  25. uint16_t left_front_mag;
  26. uint16_t right_back_mag;
  27. uint16_t left_back_mag;
  28. uint32_t right_step;
  29. uint32_t left_step;
  30. void decode_shoes_data_right(uint8_t* buff,uint8_t right_len)
  31. {
  32. right_pos[0] = ((int32_t)buff[0] << 24) | ((int32_t)buff[1] << 16) | ((int32_t)buff[2] << 8) | ((int32_t)buff[3] << 0);
  33. right_pos[1] = ((int32_t)buff[4] << 24) | ((int32_t)buff[5] << 16) | ((int32_t)buff[6] << 8) | ((int32_t)buff[7] << 0);
  34. right_pos[2] = ((int32_t)buff[8] << 24) | ((int32_t)buff[9] << 16) | ((int32_t)buff[10] << 8) | ((int32_t)buff[11] << 0);
  35. right_att[0] = ((int16_t)buff[12] << 8) | ((int16_t)buff[13] << 0);
  36. right_att[1] = ((int16_t)buff[14] << 8) | ((int16_t)buff[15] << 0);
  37. right_att[2] = ((int16_t)buff[16] << 8) | ((int16_t)buff[17] << 0);
  38. right_front_mag = ((uint16_t)buff[18] << 8) | ((uint16_t)buff[19] << 0);
  39. right_back_mag = ((uint16_t)buff[20] << 8) | ((uint16_t)buff[21] << 0);
  40. right_zupt = ((uint8_t)buff[22]);
  41. right_rssi = (uint8_t) buff[23];
  42. right_time = ((uint16_t)buff[24] << 8) | ((uint16_t)buff[25] << 0);
  43. right_acc[0] = ((int16_t)buff[26] << 8) | ((int16_t)buff[27] << 0);
  44. right_acc[1] = ((int16_t)buff[28] << 8) | ((int16_t)buff[29] << 0);
  45. right_acc[2] = ((int16_t)buff[30] << 8) | ((int16_t)buff[31] << 0);
  46. right_step = ((uint32_t)buff[32] << 24) | ((uint32_t)buff[33] << 16) | ((uint32_t)buff[34] << 8) | ((uint32_t)buff[35] << 0);
  47. }
  48. void decode_shoes_data_left(uint8_t* buff,uint8_t left_len)
  49. {
  50. left_pos[0] = ((int32_t)buff[0] << 24) | ((int32_t)buff[1] << 16) | ((int32_t)buff[2] << 8) | ((int32_t)buff[3] << 0);
  51. left_pos[1] = ((int32_t)buff[4] << 24) | ((int32_t)buff[5] << 16) | ((int32_t)buff[6] << 8) | ((int32_t)buff[7] << 0);
  52. left_pos[2] = ((int32_t)buff[8] << 24) | ((int32_t)buff[9] << 16) | ((int32_t)buff[10] << 8) | ((int32_t)buff[11] << 0);
  53. left_att[0] = ((int16_t)buff[12] << 8) | ((int16_t)buff[13] << 0);
  54. left_att[1] = ((int16_t)buff[14] << 8) | ((int16_t)buff[15] << 0);
  55. left_att[2] = ((int16_t)buff[16] << 8) | ((int16_t)buff[17] << 0);
  56. left_front_mag = ((uint16_t)buff[18] << 8) | ((uint16_t)buff[19] << 0);
  57. left_back_mag = ((uint16_t)buff[20] << 8) | ((uint16_t)buff[21] << 0);
  58. left_zupt = ((uint8_t)buff[22]);
  59. left_rssi = (uint8_t) buff[23];
  60. left_time = ((uint16_t)buff[24] << 8) | ((uint16_t)buff[25] << 0);
  61. left_acc[0] = ((int16_t)buff[26] << 8) | ((int16_t)buff[27] << 0);
  62. left_acc[1] = ((int16_t)buff[28] << 8) | ((int16_t)buff[29] << 0);
  63. left_acc[2] = ((int16_t)buff[30] << 8) | ((int16_t)buff[31] << 0);
  64. left_step = ((uint32_t)buff[32] << 24) | ((uint32_t)buff[33] << 16) | ((uint32_t)buff[34] << 8) | ((uint32_t)buff[35] << 0);
  65. }
  66. uint8_t buf[82];
  67. uint8_t L=0;
  68. void inter_package_data()
  69. {
  70. static uint8_t package_time;
  71. L=0;
  72. buf[L++] = (uint8_t)(right_pos[0]>>24);
  73. buf[L++] = (uint8_t)(right_pos[0]>>16);
  74. buf[L++] = (uint8_t)(right_pos[0]>>8);
  75. buf[L++] = (uint8_t)(right_pos[0]>>0);
  76. buf[L++] = (uint8_t)(right_pos[1]>>24);
  77. buf[L++] = (uint8_t)(right_pos[1]>>16);
  78. buf[L++] = (uint8_t)(right_pos[1]>>8);
  79. buf[L++] = (uint8_t)(right_pos[1]>>0);
  80. buf[L++] = (uint8_t)(right_pos[2]>>24);
  81. buf[L++] = (uint8_t)(right_pos[2]>>16);
  82. buf[L++] = (uint8_t)(right_pos[2]>>8);
  83. buf[L++] = (uint8_t)(right_pos[2]>>0);
  84. buf[L++] = (uint8_t)(left_pos[0]>>24);
  85. buf[L++] = (uint8_t)(left_pos[0]>>16);
  86. buf[L++] = (uint8_t)(left_pos[0]>>8);
  87. buf[L++] = (uint8_t)(left_pos[0]>>0);
  88. buf[L++] = (uint8_t)(left_pos[1]>>24);
  89. buf[L++] = (uint8_t)(left_pos[1]>>16);
  90. buf[L++] = (uint8_t)(left_pos[1]>>8);
  91. buf[L++] = (uint8_t)(left_pos[1]>>0);
  92. buf[L++] = (uint8_t)(left_pos[2]>>24);
  93. buf[L++] = (uint8_t)(left_pos[2]>>16);
  94. buf[L++] = (uint8_t)(left_pos[2]>>8);
  95. buf[L++] = (uint8_t)(left_pos[2]>>0);
  96. buf[L++] = (uint8_t)(right_att[0]>>8);
  97. buf[L++] = (uint8_t)(right_att[0]>>0);
  98. buf[L++] = (uint8_t)(right_att[1]>>8);
  99. buf[L++] = (uint8_t)(right_att[1]>>0);
  100. buf[L++] = (uint8_t)(right_att[2]>>8);
  101. buf[L++] = (uint8_t)(right_att[2]>>0);
  102. buf[L++] = (uint8_t)(left_att[0]>>8);
  103. buf[L++] = (uint8_t)(left_att[0]>>0);
  104. buf[L++] = (uint8_t)(left_att[1]>>8);
  105. buf[L++] = (uint8_t)(left_att[1]>>0);
  106. buf[L++] = (uint8_t)(left_att[2]>>8);
  107. buf[L++] = (uint8_t)(left_att[2]>>0);
  108. buf[L++] = (uint8_t)(right_acc[0]>>8);
  109. buf[L++] = (uint8_t)(right_acc[0]>>0);
  110. buf[L++] = (uint8_t)(right_acc[1]>>8);
  111. buf[L++] = (uint8_t)(right_acc[1]>>0);
  112. buf[L++] = (uint8_t)(right_acc[2]>>8);
  113. buf[L++] = (uint8_t)(right_acc[2]>>0);
  114. buf[L++] = (uint8_t)(left_acc[0]>>8);
  115. buf[L++] = (uint8_t)(left_acc[0]>>0);
  116. buf[L++] = (uint8_t)(left_acc[1]>>8);
  117. buf[L++] = (uint8_t)(left_acc[1]>>0);
  118. buf[L++] = (uint8_t)(left_acc[2]>>8);
  119. buf[L++] = (uint8_t)(left_acc[2]>>0);
  120. int is_girl = 0;
  121. if(GIRL_SHOES)
  122. is_girl = 1;
  123. buf[L++] = (uint8_t)(is_girl * 16 + right_zupt * 8 + left_zupt * 4 + down_res * 2 + jump_res * 1);
  124. buf[L++] = (uint8_t)(left_rssi >> 0);
  125. buf[L++] = (uint8_t)(package_time);
  126. buf[L++] = (uint8_t)(right_front_mag>>8);
  127. buf[L++] = (uint8_t)(right_front_mag>>0);
  128. buf[L++] = (uint8_t)(right_back_mag>>8);
  129. buf[L++] = (uint8_t)(right_back_mag>>0);
  130. buf[L++] = (uint8_t)(left_front_mag>>8);
  131. buf[L++] = (uint8_t)(left_front_mag>>0);
  132. buf[L++] = (uint8_t)(left_back_mag>>8);
  133. buf[L++] = (uint8_t)(left_back_mag>>0);
  134. buf[L++] = (uint8_t)(right_step>>24);
  135. buf[L++] = (uint8_t)(right_step>>16);
  136. buf[L++] = (uint8_t)(right_step>>8);
  137. buf[L++] = (uint8_t)(right_step>>0);
  138. buf[L++] = (uint8_t)(left_step>>24);
  139. buf[L++] = (uint8_t)(left_step>>16);
  140. buf[L++] = (uint8_t)(left_step>>8);
  141. buf[L++] = (uint8_t)(left_step>>0);
  142. package_time ++;
  143. }
  144. //滑动滤波--均值滤波
  145. uint16_t cal_mean_val(uint16_t *filter_window, int length)
  146. {
  147. uint32_t filter_window_sum = 0;
  148. for(int i = 0; i < length; i++)
  149. {
  150. filter_window_sum += filter_window[i];
  151. }
  152. return filter_window_sum/length;
  153. }
  154. void special_motion(int time_stamp, uint16_t front_mag_left,
  155. uint16_t back_mag_left, uint16_t front_mag_right, uint16_t back_mag_right,
  156. int16_t h_pos[3], int16_t s_pos[3], int16_t *down, int16_t *jump)
  157. {
  158. static int32_t last_left_pos[3];
  159. static int32_t last_right_pos[3];
  160. static int left_pos_offset[3];
  161. static int right_pos_offset[3];
  162. static int time_count = 0;
  163. static uint16_t front_mag_left_filter_window[5];
  164. static uint16_t back_mag_left_filter_window[5];
  165. static uint16_t front_mag_right_filter_window[5];
  166. static uint16_t back_mag_right_filter_window[5];
  167. static float front_mag_left_norm;
  168. static float back_mag_left_norm;
  169. static float front_mag_right_norm;
  170. static float back_mag_right_norm;
  171. //PU鞋垫数据通常都带有小抖动,现在需要滤波, 让数据更加好看
  172. memcpy(front_mag_left_filter_window, front_mag_left_filter_window + 1, 4 * sizeof(uint16_t));
  173. memcpy(back_mag_left_filter_window, back_mag_left_filter_window + 1, 4 * sizeof(uint16_t));
  174. memcpy(front_mag_right_filter_window, front_mag_right_filter_window + 1, 4 * sizeof(uint16_t));
  175. memcpy(back_mag_right_filter_window, back_mag_right_filter_window + 1, 4 * sizeof(uint16_t));
  176. front_mag_left_filter_window[4] = front_mag_left;
  177. back_mag_left_filter_window[4] = back_mag_left;
  178. front_mag_right_filter_window[4] = front_mag_right;
  179. back_mag_right_filter_window[4] = back_mag_right;
  180. if(time_count > 4)
  181. {
  182. front_mag_left_norm = cal_mean_val(front_mag_left_filter_window, 5);
  183. back_mag_left_norm = cal_mean_val(back_mag_left_filter_window, 5);
  184. front_mag_right_norm = cal_mean_val(front_mag_right_filter_window, 5);
  185. back_mag_right_norm = cal_mean_val(back_mag_right_filter_window, 5);
  186. }
  187. else
  188. {
  189. front_mag_left_norm = left_front_mag;
  190. back_mag_left_norm = left_back_mag;
  191. front_mag_right_norm = right_front_mag;
  192. back_mag_right_norm = right_back_mag;
  193. }
  194. time_count ++;
  195. float left_acc_z = left_acc[2]/2048.f;
  196. float right_acc_z = right_acc[2]/2048.f;
  197. /*
  198. 处理蹲的动作
  199. */
  200. int front_down_tag = 0;
  201. int back_down_tag = 0;
  202. press_down_detect_new(time_stamp, front_mag_left_norm, back_mag_left_norm,
  203. front_mag_right_norm, back_mag_right_norm,left_zupt,right_zupt,
  204. left_acc[0]/2048.f, left_acc[1]/2048.f, left_acc[2]/2048.f, right_acc[0]/2048.f, right_acc[1]/2048.f, right_acc[2]/2048.f,
  205. &front_down_tag, &back_down_tag);
  206. // is_down = front_down_tag + back_down_tag * 2;
  207. if (front_down_tag || back_down_tag)
  208. //if (front_down_tag)
  209. {
  210. //连续发送30次防止丢包
  211. down_send_count = 20;
  212. }
  213. if(down_send_count > 0)
  214. {
  215. is_down = 1;
  216. down_send_count --;
  217. }
  218. else
  219. {
  220. is_down = 0;
  221. }
  222. /*
  223. 处理跳的动作
  224. */
  225. if(left_zupt)
  226. {
  227. memcpy(last_left_pos, left_pos, 3 * sizeof(int32_t));
  228. }
  229. if(right_zupt)
  230. {
  231. memcpy(last_right_pos, right_pos, 3 * sizeof(int32_t));
  232. }
  233. for(int i = 0; i < 3; i++)
  234. {
  235. left_pos_offset[i] = left_pos[i] - last_left_pos[i];
  236. right_pos_offset[i] = right_pos[i] - last_right_pos[i];
  237. }
  238. short jump_tag = 0;
  239. // int normal_jump_tag = att_jump_detect(left_att[1] / 10000.f, right_att[1] / 10000.f, left_zupt, right_zupt);
  240. //
  241. int unnormal_jump_tag = press_jump_detect( left_acc_z, right_acc_z, left_zupt, right_zupt, (int) front_mag_left_norm, (int) front_mag_right_norm);
  242. int pos_jump_tag = pos_jump_detect(left_pos_offset, right_pos_offset, left_zupt, right_zupt);
  243. if(pos_jump_tag || unnormal_jump_tag)
  244. {
  245. jump_tag = 1;
  246. }
  247. //jump_tag = press_jump_detect(left_acc_z, right_acc_z, left_zupt, right_zupt, front_mag_left_norm, front_mag_right_norm, back_mag_left_norm, back_mag_right_norm);
  248. if (jump_tag)
  249. {
  250. /*
  251. gpio_mt_run(300);
  252. */
  253. jump_send_count = 10;
  254. }
  255. //连续发20次蹲,终端再处理
  256. if(jump_send_count > 0)
  257. {
  258. is_jump = 1;
  259. jump_send_count --;
  260. }
  261. else
  262. {
  263. is_jump = 0;
  264. }
  265. down_res = is_down;
  266. jump_res = is_jump;
  267. }
  268. //uint8_t buf[64];
  269. //uint8_t L=0;
  270. void imu_package_to_pack(uint8_t* left_buff, uint8_t left_len, uint8_t* right_buff, uint8_t right_len)
  271. {
  272. static uint8_t package_time;
  273. int index = 0;
  274. int left_index = 0;
  275. int right_index = 0;
  276. while(left_index < left_len)
  277. {
  278. buf[index ++] = left_buff[left_index ++];
  279. }
  280. while(right_index < right_len)
  281. {
  282. buf[index ++] = right_buff[right_index ++];
  283. }
  284. buf[index ++] = (uint8_t) is_down;
  285. buf[index ++] = (uint8_t) is_jump;
  286. buf[index ++] = (uint8_t) (package_time ++);
  287. L = index;
  288. }
  289. void integrate_left_right(uint8_t* left_buff,uint8_t left_len, uint8_t* right_buff, uint8_t right_len)
  290. {
  291. static uint16_t time_stamp;
  292. static int16_t left_pos_offset[3];
  293. static int16_t right_pos_offset[3];
  294. static int32_t last_left_pos[3];
  295. static int32_t last_right_pos[3];
  296. //将双脚的数据读取出来
  297. decode_shoes_data_right(right_buff, right_len);
  298. decode_shoes_data_left(left_buff, left_len);
  299. //先处理跳蹲再发过去
  300. int16_t down = 0;
  301. int16_t jump = 0;
  302. //获取位置的偏移量再判断跳
  303. if(left_zupt == 1)
  304. {
  305. memcpy(last_left_pos, left_pos, 3 * sizeof(int32_t));
  306. }
  307. if(right_zupt == 1)
  308. {
  309. memcpy(last_right_pos, right_pos, 3 * sizeof(int32_t));
  310. }
  311. for(int i = 0; i < 3; i++)
  312. {
  313. left_pos_offset[i] = left_pos[i] - last_left_pos[i];
  314. right_pos_offset[i] = right_pos[i] - last_right_pos[i];
  315. }
  316. special_motion(time_stamp, left_front_mag, left_back_mag, right_front_mag, right_back_mag,
  317. left_pos_offset, right_pos_offset , &down, &jump);
  318. // SEGGER_RTT_printf(0,"left_step: %d, right_step: %d, ======= \n", left_step, right_step);
  319. inter_package_data();
  320. }
  321. //获取包裹的接口
  322. void get_game_package(uint8_t* buff_address, uint8_t * buff_len)
  323. {
  324. memcpy(buff_address, buf, L);
  325. // SEGGER_RTT_printf(0,"L: %d, =======+++++++++ \n", L);
  326. *buff_len = L;
  327. }