special_motion.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. void special_motion(int time_stamp, uint16_t front_mag_left,
  145. uint16_t back_mag_left, uint16_t front_mag_right, uint16_t back_mag_right,
  146. int16_t h_pos[3], int16_t s_pos[3], int16_t *down, int16_t *jump)
  147. {
  148. static int32_t last_left_pos[3];
  149. static int32_t last_right_pos[3];
  150. static int16_t left_pos_offset[3];
  151. static int16_t right_pos_offset[3];
  152. float front_mag_left_norm = front_mag_left;
  153. float back_mag_left_norm = back_mag_left;
  154. float front_mag_right_norm = front_mag_right;
  155. float back_mag_right_norm = back_mag_right;
  156. float left_acc_z = left_acc[2]/2048.f;
  157. float right_acc_z = right_acc[2]/2048.f;
  158. /*
  159. 处理蹲的动作
  160. */
  161. int front_down_tag = 0;
  162. int back_down_tag = 0;
  163. press_down_detect_new(time_stamp, front_mag_left_norm, back_mag_left_norm,
  164. front_mag_right_norm, back_mag_right_norm,left_zupt,right_zupt,
  165. 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,
  166. &front_down_tag, &back_down_tag);
  167. // is_down = front_down_tag + back_down_tag * 2;
  168. if (front_down_tag || back_down_tag)
  169. //if (front_down_tag)
  170. {
  171. /*
  172. gpio_mt_run(300);
  173. */
  174. down_send_count = 30;
  175. }
  176. if(down_send_count > 0)
  177. {
  178. is_down = 1;
  179. down_send_count --;
  180. }
  181. else
  182. {
  183. is_down = 0;
  184. }
  185. /*
  186. 处理跳的动作
  187. */
  188. if(left_zupt)
  189. {
  190. memcpy(last_left_pos, left_pos, 3 * sizeof(int32_t));
  191. }
  192. if(right_zupt)
  193. {
  194. memcpy(last_right_pos, right_pos, 3 * sizeof(int32_t));
  195. }
  196. for(int i = 0; i < 3; i++)
  197. {
  198. left_pos_offset[i] = left_pos[i] - last_left_pos[i];
  199. right_pos_offset[i] = right_pos[i] - last_right_pos[i];
  200. }
  201. short jump_tag = 0;
  202. jump_tag = press_jump_detect(left_acc_z, right_acc_z, left_zupt, right_zupt, front_mag_left, front_mag_right, back_mag_left, back_mag_right);
  203. if (jump_tag)
  204. {
  205. /*
  206. gpio_mt_run(300);
  207. */
  208. jump_send_count = 30;
  209. }
  210. //连续发20次蹲,终端再处理
  211. if(jump_send_count > 0)
  212. {
  213. is_jump = 1;
  214. jump_send_count --;
  215. }
  216. else
  217. {
  218. is_jump = 0;
  219. }
  220. down_res = is_down;
  221. jump_res = is_jump;
  222. }
  223. //uint8_t buf[64];
  224. //uint8_t L=0;
  225. void imu_package_to_pack(uint8_t* left_buff, uint8_t left_len, uint8_t* right_buff, uint8_t right_len)
  226. {
  227. static uint8_t package_time;
  228. int index = 0;
  229. int left_index = 0;
  230. int right_index = 0;
  231. while(left_index < left_len)
  232. {
  233. buf[index ++] = left_buff[left_index ++];
  234. }
  235. while(right_index < right_len)
  236. {
  237. buf[index ++] = right_buff[right_index ++];
  238. }
  239. buf[index ++] = (uint8_t) is_down;
  240. buf[index ++] = (uint8_t) is_jump;
  241. buf[index ++] = (uint8_t) (package_time ++);
  242. L = index;
  243. }
  244. void integrate_left_right(uint8_t* left_buff,uint8_t left_len, uint8_t* right_buff, uint8_t right_len)
  245. {
  246. static uint16_t time_stamp;
  247. static int16_t left_pos_offset[3];
  248. static int16_t right_pos_offset[3];
  249. static int32_t last_left_pos[3];
  250. static int32_t last_right_pos[3];
  251. //将双脚的数据读取出来
  252. decode_shoes_data_right(right_buff, right_len);
  253. decode_shoes_data_left(left_buff, left_len);
  254. //先处理跳蹲再发过去
  255. int16_t down = 0;
  256. int16_t jump = 0;
  257. //获取位置的偏移量再判断跳
  258. if(left_zupt == 1)
  259. {
  260. memcpy(last_left_pos, left_pos, 3 * sizeof(int32_t));
  261. }
  262. if(right_zupt == 1)
  263. {
  264. memcpy(last_right_pos, right_pos, 3 * sizeof(int32_t));
  265. }
  266. for(int i = 0; i < 3; i++)
  267. {
  268. left_pos_offset[i] = left_pos[i] - last_left_pos[i];
  269. right_pos_offset[i] = right_pos[i] - last_right_pos[i];
  270. }
  271. special_motion(time_stamp, left_front_mag, left_back_mag, right_front_mag, right_back_mag,
  272. left_pos_offset, right_pos_offset , &down, &jump);
  273. // SEGGER_RTT_printf(0,"left_step: %d, right_step: %d, ======= \n", left_step, right_step);
  274. inter_package_data();
  275. }
  276. //获取包裹的接口
  277. void get_game_package(uint8_t* buff_address, uint8_t * buff_len)
  278. {
  279. memcpy(buff_address, buf, L);
  280. // SEGGER_RTT_printf(0,"L: %d, =======+++++++++ \n", L);
  281. *buff_len = L;
  282. }