special_motion.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. void decode_shoes_data_right(uint8_t* buff,uint8_t right_len)
  29. {
  30. right_pos[0] = ((int32_t)buff[0] << 24) | ((int32_t)buff[1] << 16) | ((int32_t)buff[2] << 8) | ((int32_t)buff[3] << 0);
  31. right_pos[1] = ((int32_t)buff[4] << 24) | ((int32_t)buff[5] << 16) | ((int32_t)buff[6] << 8) | ((int32_t)buff[7] << 0);
  32. right_pos[2] = ((int32_t)buff[8] << 24) | ((int32_t)buff[9] << 16) | ((int32_t)buff[10] << 8) | ((int32_t)buff[11] << 0);
  33. right_att[0] = ((int16_t)buff[12] << 8) | ((int16_t)buff[13] << 0);
  34. right_att[1] = ((int16_t)buff[14] << 8) | ((int16_t)buff[15] << 0);
  35. right_att[2] = ((int16_t)buff[16] << 8) | ((int16_t)buff[17] << 0);
  36. right_front_mag = ((uint16_t)buff[18] << 8) | ((uint16_t)buff[19] << 0);
  37. right_back_mag = ((uint16_t)buff[20] << 8) | ((uint16_t)buff[21] << 0);
  38. right_zupt = ((uint8_t)buff[22]);
  39. right_rssi = (uint8_t) buff[23];
  40. right_time = ((uint16_t)buff[24] << 8) | ((uint16_t)buff[25] << 0);
  41. right_acc[0] = ((int16_t)buff[26] << 8) | ((int16_t)buff[27] << 0);
  42. right_acc[1] = ((int16_t)buff[28] << 8) | ((int16_t)buff[29] << 0);
  43. right_acc[2] = ((int16_t)buff[30] << 8) | ((int16_t)buff[31] << 0);
  44. }
  45. void decode_shoes_data_left(uint8_t* buff,uint8_t left_len)
  46. {
  47. left_pos[0] = ((int32_t)buff[0] << 24) | ((int32_t)buff[1] << 16) | ((int32_t)buff[2] << 8) | ((int32_t)buff[3] << 0);
  48. left_pos[1] = ((int32_t)buff[4] << 24) | ((int32_t)buff[5] << 16) | ((int32_t)buff[6] << 8) | ((int32_t)buff[7] << 0);
  49. left_pos[2] = ((int32_t)buff[8] << 24) | ((int32_t)buff[9] << 16) | ((int32_t)buff[10] << 8) | ((int32_t)buff[11] << 0);
  50. left_att[0] = ((int16_t)buff[12] << 8) | ((int16_t)buff[13] << 0);
  51. left_att[1] = ((int16_t)buff[14] << 8) | ((int16_t)buff[15] << 0);
  52. left_att[2] = ((int16_t)buff[16] << 8) | ((int16_t)buff[17] << 0);
  53. left_front_mag = ((uint16_t)buff[18] << 8) | ((uint16_t)buff[19] << 0);
  54. left_back_mag = ((uint16_t)buff[20] << 8) | ((uint16_t)buff[21] << 0);
  55. left_zupt = ((uint8_t)buff[22]);
  56. left_rssi = (uint8_t) buff[23];
  57. left_time = ((uint16_t)buff[24] << 8) | ((uint16_t)buff[25] << 0);
  58. left_acc[0] = ((int16_t)buff[26] << 8) | ((int16_t)buff[27] << 0);
  59. left_acc[1] = ((int16_t)buff[28] << 8) | ((int16_t)buff[29] << 0);
  60. left_acc[2] = ((int16_t)buff[30] << 8) | ((int16_t)buff[31] << 0);
  61. }
  62. uint8_t buf[64];
  63. uint8_t L=0;
  64. void inter_package_data()
  65. {
  66. static uint8_t package_time;
  67. L=0;
  68. buf[L++] = (uint8_t)(right_pos[0]>>24);
  69. buf[L++] = (uint8_t)(right_pos[0]>>16);
  70. buf[L++] = (uint8_t)(right_pos[0]>>8);
  71. buf[L++] = (uint8_t)(right_pos[0]>>0);
  72. buf[L++] = (uint8_t)(right_pos[1]>>24);
  73. buf[L++] = (uint8_t)(right_pos[1]>>16);
  74. buf[L++] = (uint8_t)(right_pos[1]>>8);
  75. buf[L++] = (uint8_t)(right_pos[1]>>0);
  76. buf[L++] = (uint8_t)(right_pos[2]>>24);
  77. buf[L++] = (uint8_t)(right_pos[2]>>16);
  78. buf[L++] = (uint8_t)(right_pos[2]>>8);
  79. buf[L++] = (uint8_t)(right_pos[2]>>0);
  80. buf[L++] = (uint8_t)(left_pos[0]>>24);
  81. buf[L++] = (uint8_t)(left_pos[0]>>16);
  82. buf[L++] = (uint8_t)(left_pos[0]>>8);
  83. buf[L++] = (uint8_t)(left_pos[0]>>0);
  84. buf[L++] = (uint8_t)(left_pos[1]>>24);
  85. buf[L++] = (uint8_t)(left_pos[1]>>16);
  86. buf[L++] = (uint8_t)(left_pos[1]>>8);
  87. buf[L++] = (uint8_t)(left_pos[1]>>0);
  88. buf[L++] = (uint8_t)(left_pos[2]>>24);
  89. buf[L++] = (uint8_t)(left_pos[2]>>16);
  90. buf[L++] = (uint8_t)(left_pos[2]>>8);
  91. buf[L++] = (uint8_t)(left_pos[2]>>0);
  92. buf[L++] = (uint8_t)(right_att[0]>>8);
  93. buf[L++] = (uint8_t)(right_att[0]>>0);
  94. buf[L++] = (uint8_t)(right_att[1]>>8);
  95. buf[L++] = (uint8_t)(right_att[1]>>0);
  96. buf[L++] = (uint8_t)(right_att[2]>>8);
  97. buf[L++] = (uint8_t)(right_att[2]>>0);
  98. buf[L++] = (uint8_t)(left_att[0]>>8);
  99. buf[L++] = (uint8_t)(left_att[0]>>0);
  100. buf[L++] = (uint8_t)(left_att[1]>>8);
  101. buf[L++] = (uint8_t)(left_att[1]>>0);
  102. buf[L++] = (uint8_t)(left_att[2]>>8);
  103. buf[L++] = (uint8_t)(left_att[2]>>0);
  104. buf[L++] = (uint8_t)(right_acc[0]>>8);
  105. buf[L++] = (uint8_t)(right_acc[0]>>0);
  106. buf[L++] = (uint8_t)(right_acc[1]>>8);
  107. buf[L++] = (uint8_t)(right_acc[1]>>0);
  108. buf[L++] = (uint8_t)(right_acc[2]>>8);
  109. buf[L++] = (uint8_t)(right_acc[2]>>0);
  110. buf[L++] = (uint8_t)(left_acc[0]>>8);
  111. buf[L++] = (uint8_t)(left_acc[0]>>0);
  112. buf[L++] = (uint8_t)(left_acc[1]>>8);
  113. buf[L++] = (uint8_t)(left_acc[1]>>0);
  114. buf[L++] = (uint8_t)(left_acc[2]>>8);
  115. buf[L++] = (uint8_t)(left_acc[2]>>0);
  116. int is_girl = 0;
  117. if(GIRL_SHOES)
  118. is_girl = 1;
  119. buf[L++] = (uint8_t)(is_girl * 16 + right_zupt * 8 + left_zupt * 4 + down_res * 2 + jump_res * 1);
  120. buf[L++] = (uint8_t)(left_rssi >> 0);
  121. buf[L++] = (uint8_t)(package_time);
  122. buf[L++] = (uint8_t)(right_front_mag>>8);
  123. buf[L++] = (uint8_t)(right_front_mag>>0);
  124. buf[L++] = (uint8_t)(right_back_mag>>8);
  125. buf[L++] = (uint8_t)(right_back_mag>>0);
  126. buf[L++] = (uint8_t)(left_front_mag>>8);
  127. buf[L++] = (uint8_t)(left_front_mag>>0);
  128. buf[L++] = (uint8_t)(left_back_mag>>8);
  129. buf[L++] = (uint8_t)(left_back_mag>>0);
  130. package_time ++;
  131. }
  132. void special_motion(int time_stamp, uint16_t front_mag_left,
  133. uint16_t back_mag_left, uint16_t front_mag_right, uint16_t back_mag_right,
  134. int16_t h_pos[3], int16_t s_pos[3], int16_t *down, int16_t *jump)
  135. {
  136. static int32_t last_left_pos[3];
  137. static int32_t last_right_pos[3];
  138. static int16_t left_pos_offset[3];
  139. static int16_t right_pos_offset[3];
  140. float front_mag_left_norm = front_mag_left;
  141. float back_mag_left_norm = back_mag_left;
  142. float front_mag_right_norm = front_mag_right;
  143. float back_mag_right_norm = back_mag_right;
  144. float left_acc_z = left_acc[2]/2048.f;
  145. float right_acc_z = right_acc[2]/2048.f;
  146. /*
  147. 处理蹲的动作
  148. */
  149. int front_down_tag = 0;
  150. int back_down_tag = 0;
  151. press_down_detect_new(time_stamp, front_mag_left_norm, back_mag_left_norm,
  152. front_mag_right_norm, back_mag_right_norm,
  153. 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,
  154. &front_down_tag, &back_down_tag);
  155. // is_down = front_down_tag + back_down_tag * 2;
  156. if (front_down_tag || back_down_tag)
  157. //if (front_down_tag)
  158. {
  159. /*
  160. gpio_mt_run(300);
  161. */
  162. down_send_count = 20;
  163. }
  164. if(down_send_count > 0)
  165. {
  166. is_down = 1;
  167. down_send_count --;
  168. }
  169. else
  170. {
  171. is_down = 0;
  172. }
  173. /*
  174. 处理跳的动作
  175. */
  176. if(left_zupt)
  177. {
  178. memcpy(last_left_pos, left_pos, 3 * sizeof(int32_t));
  179. }
  180. if(right_zupt)
  181. {
  182. memcpy(last_right_pos, right_pos, 3 * sizeof(int32_t));
  183. }
  184. for(int i = 0; i < 3; i++)
  185. {
  186. left_pos_offset[i] = left_pos[i] - last_left_pos[i];
  187. right_pos_offset[i] = right_pos[i] - last_right_pos[i];
  188. }
  189. short jump_tag = 0;
  190. jump_tag = press_jump_detect(left_acc_z,right_acc_z, left_zupt, right_zupt, front_mag_left, front_mag_right);
  191. if (jump_tag)
  192. {
  193. /*
  194. gpio_mt_run(300);
  195. */
  196. jump_send_count = 20;
  197. }
  198. //连续发20次蹲,终端再处理
  199. if(jump_send_count > 0)
  200. {
  201. is_jump = 1;
  202. jump_send_count --;
  203. }
  204. else
  205. {
  206. is_jump = 0;
  207. }
  208. down_res = is_down;
  209. jump_res = is_jump;
  210. }
  211. //uint8_t buf[64];
  212. //uint8_t L=0;
  213. void imu_package_to_pack(uint8_t* left_buff, uint8_t left_len, uint8_t* right_buff, uint8_t right_len)
  214. {
  215. static uint8_t package_time;
  216. int index = 0;
  217. int left_index = 0;
  218. int right_index = 0;
  219. while(left_index < left_len)
  220. {
  221. buf[index ++] = left_buff[left_index ++];
  222. }
  223. while(right_index < right_len)
  224. {
  225. buf[index ++] = right_buff[right_index ++];
  226. }
  227. buf[index ++] = (uint8_t) is_down;
  228. buf[index ++] = (uint8_t) is_jump;
  229. buf[index ++] = (uint8_t) (package_time ++);
  230. L = index;
  231. }
  232. void integrate_left_right(uint8_t* left_buff,uint8_t left_len, uint8_t* right_buff, uint8_t right_len)
  233. {
  234. static uint16_t time_stamp;
  235. static int16_t left_pos_offset[3];
  236. static int16_t right_pos_offset[3];
  237. static int32_t last_left_pos[3];
  238. static int32_t last_right_pos[3];
  239. //直接发送IMU数据过去
  240. if(CAL_MODE)
  241. {
  242. imu_package_to_pack(left_buff, left_len, right_buff, right_len);
  243. }
  244. else
  245. {
  246. //将双脚的数据读取出来
  247. decode_shoes_data_right(right_buff, right_len);
  248. decode_shoes_data_left(left_buff, left_len);
  249. //先处理跳蹲再发过去
  250. int16_t down = 0;
  251. int16_t jump = 0;
  252. //获取位置的偏移量再判断跳
  253. if(left_zupt == 1)
  254. {
  255. memcpy(last_left_pos, left_pos, 3 * sizeof(int32_t));
  256. }
  257. if(right_zupt == 1)
  258. {
  259. memcpy(last_right_pos, right_pos, 3 * sizeof(int32_t));
  260. }
  261. for(int i = 0; i < 3; i++)
  262. {
  263. left_pos_offset[i] = left_pos[i] - last_left_pos[i];
  264. right_pos_offset[i] = right_pos[i] - last_right_pos[i];
  265. }
  266. special_motion(time_stamp, left_front_mag, left_back_mag, right_front_mag, right_back_mag,
  267. left_pos_offset, right_pos_offset , &down, &jump);
  268. inter_package_data();
  269. }
  270. }
  271. //获取包裹的接口
  272. void get_game_package(uint8_t* buff_address, uint8_t * buff_len)
  273. {
  274. memcpy(buff_address, buf, L);
  275. *buff_len = L;
  276. }