dtalige.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "ble_comm.h"
  2. #include "vl53l1.h"
  3. #include "tool.h"
  4. typedef struct {
  5. unsigned char imubuff[64];
  6. unsigned char imubuff_len;
  7. uint16_t ts;
  8. }IMU_Hal_t;
  9. typedef struct {
  10. IMU_Hal_t h;
  11. IMU_Hal_t s;
  12. }IMU_Hal_RL_t;
  13. typedef void (*process_motion_t)(uint8_t* left_buff,uint8_t left_len, uint8_t* right_buff, uint8_t right_len);
  14. extern void get_game_package(uint8_t* buff_address, uint8_t * buff_len);
  15. extern void integrate_left_right(uint8_t* left_buff,uint8_t left_len, uint8_t* right_buff, uint8_t right_len);
  16. extern void get_foot_data(uint8_t *buf, uint8_t *buff_len);
  17. IMU_Hal_t rev_s={.ts=0};
  18. IMU_Hal_t rev={.ts=0};
  19. IMU_Hal_t rev_io;
  20. IMU_Hal_RL_t temp_IMU_DAT;
  21. QUEUE_DEF(IMU_Hal_t, IMU_DAT_H_queue, size_8, QUEUE_MODE_OVERFLOW);
  22. QUEUE_DEF(IMU_Hal_t, IMU_DAT_S_queue, size_8, QUEUE_MODE_OVERFLOW);
  23. void IMU_dtalige(IMU_Hal_RL_t *outp, queue_t *ph, queue_t *ps, process_motion_t process_p)
  24. {
  25. static char sta = 0;
  26. static unsigned short R_timestamp_re = 0;
  27. switch (sta)
  28. {
  29. case 0:
  30. if (ps->element_count > 0)
  31. {
  32. BLE_PRINT( "IMU_dtalige init.\r\n");
  33. sta = 1;
  34. queue_reset(ps);
  35. queue_reset(ph);
  36. }
  37. break;
  38. case 1:
  39. while (ps->element_count > ph->element_count)
  40. {
  41. queue_out(ps, &outp->s);
  42. R_timestamp_re = outp->s.ts;
  43. }
  44. if (ps->element_count > 0)
  45. {
  46. queue_out(ps, &outp->s);
  47. R_timestamp_re++;
  48. if (R_timestamp_re != outp->s.ts)
  49. {
  50. do
  51. {
  52. BLE_PRINT( "lose packet %d\r\n", R_timestamp_re);
  53. extern uint8_t lost_packs_num;
  54. lost_packs_num++;
  55. R_timestamp_re++;
  56. queue_out(ph, &outp->h);
  57. if (R_timestamp_re == outp->h.ts)
  58. {
  59. queue_out(ph, &outp->h);
  60. process_p(outp->h.imubuff, outp->h.imubuff_len, outp->s.imubuff, outp->s.imubuff_len);
  61. get_game_package(rev_io.imubuff, &rev_io.imubuff_len);
  62. rev_io.ts++;
  63. rev_io.imubuff[rev_io.imubuff_len + 1] = rev_io.ts;
  64. rev_io.imubuff[rev_io.imubuff_len] = rev_io.ts >> 8;
  65. send_protocol(0, 4, rev_io.imubuff, rev_io.imubuff_len);
  66. break;
  67. }
  68. if (ph->element_count <= 0)
  69. {
  70. sta = 0;
  71. break;
  72. }
  73. } while (1);
  74. }
  75. else
  76. {
  77. queue_out(ph, &outp->h);
  78. process_p(outp->h.imubuff, outp->h.imubuff_len, outp->s.imubuff, outp->s.imubuff_len);
  79. get_game_package(rev_io.imubuff, &rev_io.imubuff_len);
  80. rev_io.ts++;
  81. rev_io.imubuff[rev_io.imubuff_len + 1] = rev_io.ts;
  82. rev_io.imubuff[rev_io.imubuff_len] = rev_io.ts >> 8;
  83. send_protocol(0, 4, rev_io.imubuff, rev_io.imubuff_len);
  84. }
  85. }
  86. break;
  87. }
  88. }
  89. void IMU_Dtalige(void)
  90. {
  91. IMU_dtalige(&temp_IMU_DAT, &IMU_DAT_H_queue, &IMU_DAT_S_queue, integrate_left_right);
  92. }
  93. void IMU_Rec_data(uint8_t* pdat,uint8_t len)
  94. {
  95. if(pdat[3]==4)
  96. {
  97. memcpy(rev_s.imubuff,&pdat[4],pdat[1]);
  98. rev_s.imubuff_len=pdat[1]-7;
  99. rev_s.ts=((uint16_t)pdat[pdat[1]-3]<<8) |((uint16_t)pdat[pdat[1]-2]<<0);;
  100. queue_in(&IMU_DAT_S_queue,&rev_s);
  101. }
  102. }
  103. void IMU_Process_motion_queue(uint8_t IS_HOST,int16_t* _acc,int16_t* _gry,int16_t* _mag,uint16_t _press,uint8_t _rssi)
  104. {
  105. static uint8_t times = 0;
  106. times++;
  107. if(times == 10)
  108. {
  109. // uint16_t distand = VL53L1_MEASURE();
  110. // if(distand > 4000)distand = 0;//如果times采集时间太短,出现溢出值。
  111. // SEGGER_RTT_printf(0,"distand=%d\r\n",distand);
  112. // JS_RTT_Print(distand,0,0);
  113. times = 0;
  114. }
  115. process_motion(_acc,_gry,_mag,_press,_rssi);
  116. get_foot_data(rev.imubuff,&rev.imubuff_len);
  117. rev.ts++;
  118. if(IS_HOST)
  119. {
  120. queue_in(&IMU_DAT_H_queue,&rev);
  121. }
  122. else
  123. {
  124. rev.imubuff[rev.imubuff_len+1]=rev.ts;
  125. rev.imubuff[rev.imubuff_len]=rev.ts>>8;
  126. send_protocol(0,4,rev.imubuff,rev.imubuff_len+2);
  127. }
  128. }