special_motion.c 12 KB

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