special_motion.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. left_front_mag = cal_mean_val(front_mag_left_filter_window, 5);
  183. left_back_mag = cal_mean_val(back_mag_left_filter_window, 5);
  184. right_front_mag = cal_mean_val(front_mag_right_filter_window, 5);
  185. right_back_mag = cal_mean_val(back_mag_right_filter_window, 5);
  186. }
  187. front_mag_left_norm = left_front_mag;
  188. back_mag_left_norm = left_back_mag;
  189. front_mag_right_norm = right_front_mag;
  190. back_mag_right_norm = right_back_mag;
  191. time_count ++;
  192. float left_acc_z = left_acc[2]/2048.f;
  193. float right_acc_z = right_acc[2]/2048.f;
  194. /*
  195. 处理蹲的动作
  196. */
  197. int front_down_tag = 0;
  198. int back_down_tag = 0;
  199. press_down_detect_new(time_stamp, front_mag_left_norm, back_mag_left_norm,
  200. front_mag_right_norm, back_mag_right_norm,left_zupt,right_zupt,
  201. 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,
  202. &front_down_tag, &back_down_tag);
  203. // is_down = front_down_tag + back_down_tag * 2;
  204. if (front_down_tag || back_down_tag)
  205. //if (front_down_tag)
  206. {
  207. //连续发送30次防止丢包
  208. down_send_count = 20;
  209. }
  210. if(down_send_count > 0)
  211. {
  212. is_down = 1;
  213. down_send_count --;
  214. }
  215. else
  216. {
  217. is_down = 0;
  218. }
  219. /*
  220. 处理跳的动作
  221. */
  222. if(left_zupt)
  223. {
  224. memcpy(last_left_pos, left_pos, 3 * sizeof(int32_t));
  225. }
  226. if(right_zupt)
  227. {
  228. memcpy(last_right_pos, right_pos, 3 * sizeof(int32_t));
  229. }
  230. for(int i = 0; i < 3; i++)
  231. {
  232. left_pos_offset[i] = left_pos[i] - last_left_pos[i];
  233. right_pos_offset[i] = right_pos[i] - last_right_pos[i];
  234. }
  235. short jump_tag = 0;
  236. // int normal_jump_tag = att_jump_detect(left_att[1] / 10000.f, right_att[1] / 10000.f, left_zupt, right_zupt);
  237. //
  238. 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);
  239. int pos_jump_tag = pos_jump_detect(left_pos_offset, right_pos_offset, left_zupt, right_zupt);
  240. if(pos_jump_tag || unnormal_jump_tag)
  241. {
  242. jump_tag = 1;
  243. }
  244. //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);
  245. if (jump_tag)
  246. {
  247. /*
  248. gpio_mt_run(300);
  249. */
  250. jump_send_count = 10;
  251. }
  252. //连续发20次蹲,终端再处理
  253. if(jump_send_count > 0)
  254. {
  255. is_jump = 1;
  256. jump_send_count --;
  257. }
  258. else
  259. {
  260. is_jump = 0;
  261. }
  262. down_res = is_down;
  263. jump_res = is_jump;
  264. }
  265. //uint8_t buf[64];
  266. //uint8_t L=0;
  267. void imu_package_to_pack(uint8_t* left_buff, uint8_t left_len, uint8_t* right_buff, uint8_t right_len)
  268. {
  269. static uint8_t package_time;
  270. int index = 0;
  271. int left_index = 0;
  272. int right_index = 0;
  273. while(left_index < left_len)
  274. {
  275. buf[index ++] = left_buff[left_index ++];
  276. }
  277. while(right_index < right_len)
  278. {
  279. buf[index ++] = right_buff[right_index ++];
  280. }
  281. buf[index ++] = (uint8_t) is_down;
  282. buf[index ++] = (uint8_t) is_jump;
  283. buf[index ++] = (uint8_t) (package_time ++);
  284. L = index;
  285. }
  286. void integrate_left_right(uint8_t* left_buff,uint8_t left_len, uint8_t* right_buff, uint8_t right_len)
  287. {
  288. static uint16_t time_stamp;
  289. static int16_t left_pos_offset[3];
  290. static int16_t right_pos_offset[3];
  291. static int32_t last_left_pos[3];
  292. static int32_t last_right_pos[3];
  293. //将双脚的数据读取出来
  294. decode_shoes_data_right(right_buff, right_len);
  295. decode_shoes_data_left(left_buff, left_len);
  296. //先处理跳蹲再发过去
  297. int16_t down = 0;
  298. int16_t jump = 0;
  299. //获取位置的偏移量再判断跳
  300. if(left_zupt == 1)
  301. {
  302. memcpy(last_left_pos, left_pos, 3 * sizeof(int32_t));
  303. }
  304. if(right_zupt == 1)
  305. {
  306. memcpy(last_right_pos, right_pos, 3 * sizeof(int32_t));
  307. }
  308. for(int i = 0; i < 3; i++)
  309. {
  310. left_pos_offset[i] = left_pos[i] - last_left_pos[i];
  311. right_pos_offset[i] = right_pos[i] - last_right_pos[i];
  312. }
  313. special_motion(time_stamp, left_front_mag, left_back_mag, right_front_mag, right_back_mag,
  314. left_pos_offset, right_pos_offset , &down, &jump);
  315. // SEGGER_RTT_printf(0,"left_step: %d, right_step: %d, ======= \n", left_step, right_step);
  316. inter_package_data();
  317. }
  318. //获取包裹的接口
  319. void get_game_package(uint8_t* buff_address, uint8_t * buff_len)
  320. {
  321. memcpy(buff_address, buf, L);
  322. // SEGGER_RTT_printf(0,"L: %d, =======+++++++++ \n", L);
  323. *buff_len = L;
  324. }