dtalige.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include "ble_comm.h"
  2. #include "vl53l1.h"
  3. #include "tool.h"
  4. typedef struct {
  5. unsigned char imubuff[72];
  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. extern uint16_t lose_pack_all;
  24. void IMU_dtalige(IMU_Hal_RL_t *outp, queue_t *ph, queue_t *ps, process_motion_t process_p)
  25. {
  26. static char sta = 0;
  27. static unsigned short R_timestamp_re = 0;
  28. switch (sta)
  29. {
  30. case 0:
  31. if (ps->element_count > 0)
  32. {
  33. BLE_PRINT( "IMU_dtalige init.\r\n");
  34. sta = 1;
  35. queue_reset(ps);
  36. queue_reset(ph);
  37. }
  38. break;
  39. case 1:
  40. while (ps->element_count > ph->element_count)
  41. {
  42. queue_out(ps, &outp->s);
  43. R_timestamp_re = outp->s.ts;
  44. }
  45. if (ps->element_count > 0)
  46. {
  47. queue_out(ps, &outp->s);
  48. R_timestamp_re++;
  49. if (R_timestamp_re != outp->s.ts)
  50. {
  51. do
  52. {
  53. BLE_PRINT( "lose packet %d\r\n", R_timestamp_re);
  54. #if LOSSPACK_ENANBLE
  55. extern uint16_t lose_pack_all;
  56. // static uint32_t last_R_timestamp_re = 0;
  57. // static uint32_t lost_sum = 0;
  58. //
  59. // if((R_timestamp_re - last_R_timestamp_re) == 1){
  60. // lost_sum++;
  61. // }else{
  62. // if(lose_pack_all < lost_sum)lose_pack_all = lost_sum;
  63. // lost_sum = 0;
  64. // }
  65. // last_R_timestamp_re = R_timestamp_re;
  66. lose_pack_all++;
  67. #endif
  68. R_timestamp_re++;
  69. queue_out(ph, &outp->h);
  70. if (R_timestamp_re == outp->h.ts)
  71. {
  72. queue_out(ph, &outp->h);
  73. process_p(outp->h.imubuff, outp->h.imubuff_len, outp->s.imubuff, outp->s.imubuff_len);
  74. get_game_package(rev_io.imubuff, &rev_io.imubuff_len);
  75. rev_io.ts++;
  76. rev_io.imubuff[rev_io.imubuff_len + 1] = rev_io.ts;
  77. rev_io.imubuff[rev_io.imubuff_len] = rev_io.ts >> 8;
  78. if(rev_io.ts%3==0)
  79. send_protocol(0, 4, rev_io.imubuff, rev_io.imubuff_len);
  80. break;
  81. }
  82. if (ph->element_count <= 0)
  83. {
  84. sta = 0;
  85. break;
  86. }
  87. } while (1);
  88. }
  89. else
  90. {
  91. queue_out(ph, &outp->h);
  92. process_p(outp->h.imubuff, outp->h.imubuff_len, outp->s.imubuff, outp->s.imubuff_len);
  93. get_game_package(rev_io.imubuff, &rev_io.imubuff_len);
  94. rev_io.ts++;
  95. rev_io.imubuff[rev_io.imubuff_len + 1] = rev_io.ts;
  96. rev_io.imubuff[rev_io.imubuff_len] = rev_io.ts >> 8;
  97. if(rev_io.ts%3==0)
  98. send_protocol(0, 4, rev_io.imubuff, rev_io.imubuff_len);
  99. }
  100. }
  101. break;
  102. }
  103. }
  104. void IMU_Dtalige(void)
  105. {
  106. IMU_dtalige(&temp_IMU_DAT, &IMU_DAT_H_queue, &IMU_DAT_S_queue, integrate_left_right);
  107. }
  108. void IMU_Rec_data(uint8_t* pdat,uint8_t len)
  109. {
  110. if(pdat[3]==4)
  111. {
  112. memcpy(rev_s.imubuff,&pdat[4],pdat[1]);
  113. rev_s.imubuff_len=pdat[1]-7;
  114. rev_s.ts=((uint16_t)pdat[pdat[1]-3]<<8) |((uint16_t)pdat[pdat[1]-2]<<0);;
  115. queue_in(&IMU_DAT_S_queue,&rev_s);
  116. }
  117. }
  118. void IMU_Process_motion_queue(uint8_t IS_HOST, int32_t time_stamp, int16_t* _acc,int16_t* _gry, int16_t* front_mag, int16_t* back_mag, uint8_t _rssi)
  119. {
  120. process_motion(time_stamp, _acc, _gry, front_mag, back_mag, _rssi);
  121. get_foot_data(rev.imubuff,&rev.imubuff_len);
  122. rev.ts++;
  123. if(IS_HOST)
  124. {
  125. queue_in(&IMU_DAT_H_queue,&rev);
  126. }
  127. else
  128. {
  129. rev.imubuff[rev.imubuff_len+1]=rev.ts;
  130. rev.imubuff[rev.imubuff_len]=rev.ts>>8;
  131. send_protocol(0,4,rev.imubuff,rev.imubuff_len+2);
  132. }
  133. }