special_motion.c 12 KB

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