process_result.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #include "process_result.h"
  2. #include "hal_mt.h"
  3. #include "app_flash.h"
  4. #include "app_switchimu.h"
  5. #include "math.h"
  6. #include "detect_step_by_mag.h"
  7. float gyr[3];
  8. float acc[3];
  9. float mag[3];
  10. int16_t accel[3];
  11. int16_t gyro[3];
  12. int16_t front_magn[3];
  13. int16_t back_magn[3];
  14. int32_t pos[3];
  15. int16_t att[3];
  16. int16_t zupt;
  17. int8_t slave_rssi;
  18. uint8_t foot_data_buf[64];
  19. uint8_t foot_data_len;
  20. int16_t front_zero;
  21. int16_t back_zero;
  22. int16_t acc_zero;
  23. uint16_t front_mag_norm;
  24. uint16_t back_mag_norm;
  25. uint32_t step_count;
  26. int16_t ShoeTouchFlag2;
  27. int16_t Rcounter2;
  28. int16_t Tcounter=0;
  29. int16_t FKEY2;
  30. int16_t BKEY2;
  31. //int32_t RealFrontLiftPoint=30000;
  32. //int32_t RealBackLiftPoint=30000;
  33. int RealFrontLiftPoint;
  34. int RealBackLiftPoint;
  35. float gyr[3];
  36. float acc[3];
  37. float mag[3];
  38. int low_power_dectect;
  39. int low_power_step;
  40. uint16_t low_power_mag;
  41. int zupt_wait_time;
  42. int nan_flag = 0;
  43. //left
  44. //float acc_bias[3] = {0.091139f, 0.101830f, -0.048524f};
  45. //float acc_scale[3] = {0.980197f, 1.003513f, 0.987654f};
  46. //right
  47. float acc_bias[3] = {0.047714f, 0.026054f, -0.008199f};
  48. float acc_scale[3] = {1.003853f, 1.007566f, 0.986378f};
  49. void foot_data_to_package()
  50. {
  51. static uint16_t package_time = 0;
  52. foot_data_len = 0;
  53. foot_data_buf[foot_data_len++] = (uint8_t)(pos[0]>>24);
  54. foot_data_buf[foot_data_len++] = (uint8_t)(pos[0]>>16);
  55. foot_data_buf[foot_data_len++] = (uint8_t)(pos[0]>>8);
  56. foot_data_buf[foot_data_len++] = (uint8_t)(pos[0]>>0);
  57. //foot_data_buf[foot_data_len++] = (uint8_t)(RealFrontLiftPoint>>24);
  58. //foot_data_buf[foot_data_len++] = (uint8_t)(RealFrontLiftPoint>>16);
  59. //foot_data_buf[foot_data_len++] = (uint8_t)(RealFrontLiftPoint>>8);
  60. //foot_data_buf[foot_data_len++] = (uint8_t)(RealFrontLiftPoint>>0);
  61. foot_data_buf[foot_data_len++] = (uint8_t)(pos[1]>>24);
  62. foot_data_buf[foot_data_len++] = (uint8_t)(pos[1]>>16);
  63. foot_data_buf[foot_data_len++] = (uint8_t)(pos[1]>>8);
  64. foot_data_buf[foot_data_len++] = (uint8_t)(pos[1]>>0);
  65. //foot_data_buf[foot_data_len++] = (uint8_t)(RealBackLiftPoint>>24);
  66. //foot_data_buf[foot_data_len++] = (uint8_t)(RealBackLiftPoint>>16);
  67. //foot_data_buf[foot_data_len++] = (uint8_t)(RealBackLiftPoint>>8);
  68. //foot_data_buf[foot_data_len++] = (uint8_t)(RealBackLiftPoint>>0);
  69. foot_data_buf[foot_data_len++] = (uint8_t)(pos[2]>>24);
  70. foot_data_buf[foot_data_len++] = (uint8_t)(pos[2]>>16);
  71. foot_data_buf[foot_data_len++] = (uint8_t)(pos[2]>>8);
  72. foot_data_buf[foot_data_len++] = (uint8_t)(pos[2]>>0);
  73. //foot_data_buf[foot_data_len++] = (uint8_t)(att[0]>>8);
  74. //foot_data_buf[foot_data_len++] = (uint8_t)(att[0]>>0);//注释:温庭玮
  75. foot_data_buf[foot_data_len++] = (uint8_t)(FKEY2>>8);
  76. foot_data_buf[foot_data_len++] = (uint8_t)(FKEY2>>0);
  77. //foot_data_buf[foot_data_len++] = (uint8_t)(att[1]>>8);
  78. //foot_data_buf[foot_data_len++] = (uint8_t)(att[1]>>0);//注释:温庭玮
  79. foot_data_buf[foot_data_len++] = (uint8_t)(ShoeTouchFlag2>>8);
  80. foot_data_buf[foot_data_len++] = (uint8_t)(ShoeTouchFlag2>>0);
  81. //foot_data_buf[foot_data_len++] = (uint8_t)(att[2]>>8);
  82. //foot_data_buf[foot_data_len++] = (uint8_t)(att[2]>>0);//注释:温庭玮
  83. foot_data_buf[foot_data_len++] = (uint8_t)(BKEY2>>8);
  84. foot_data_buf[foot_data_len++] = (uint8_t)(BKEY2>>0);
  85. foot_data_buf[foot_data_len++] = (uint8_t)(front_mag_norm>>8);
  86. foot_data_buf[foot_data_len++] = (uint8_t)(front_mag_norm>>0);
  87. foot_data_buf[foot_data_len++] = (uint8_t)(back_mag_norm>>8);
  88. foot_data_buf[foot_data_len++] = (uint8_t)(back_mag_norm>>0);
  89. foot_data_buf[foot_data_len++] = (uint8_t)(zupt>>0);
  90. foot_data_buf[foot_data_len++] = (uint8_t) slave_rssi;
  91. foot_data_buf[foot_data_len++] = (uint8_t)(package_time>>8);
  92. foot_data_buf[foot_data_len++] = (uint8_t)(package_time>>0);
  93. foot_data_buf[foot_data_len++] = (uint8_t)(accel[0]>>8);
  94. foot_data_buf[foot_data_len++] = (uint8_t)(accel[0]>>0);
  95. foot_data_buf[foot_data_len++] = (uint8_t)(accel[1]>>8);
  96. foot_data_buf[foot_data_len++] = (uint8_t)(accel[1]>>0);
  97. foot_data_buf[foot_data_len++] = (uint8_t)(accel[2]>>8);
  98. foot_data_buf[foot_data_len++] = (uint8_t)(accel[2]>>0);
  99. foot_data_buf[foot_data_len++] = (uint8_t)(step_count>>24);
  100. foot_data_buf[foot_data_len++] = (uint8_t)(step_count>>16);
  101. foot_data_buf[foot_data_len++] = (uint8_t)(step_count>>8);
  102. foot_data_buf[foot_data_len++] = (uint8_t)(step_count>>0);
  103. // SEGGER_RTT_printf(0,"test footPDR \n");
  104. package_time ++;
  105. }
  106. void imu_data_to_package()
  107. {
  108. static uint16_t package_time = 0;
  109. foot_data_len = 0;
  110. foot_data_buf[foot_data_len++] = (uint8_t)(accel[0]>>8);
  111. foot_data_buf[foot_data_len++] = (uint8_t)(accel[0]>>0);
  112. foot_data_buf[foot_data_len++] = (uint8_t)(accel[1]>>8);
  113. foot_data_buf[foot_data_len++] = (uint8_t)(accel[1]>>0);
  114. foot_data_buf[foot_data_len++] = (uint8_t)(accel[2]>>8);
  115. foot_data_buf[foot_data_len++] = (uint8_t)(accel[2]>>0);
  116. foot_data_buf[foot_data_len++] = (uint8_t)(gyro[0]>>8);
  117. foot_data_buf[foot_data_len++] = (uint8_t)(gyro[0]>>0);
  118. foot_data_buf[foot_data_len++] = (uint8_t)(gyro[1]>>8);
  119. foot_data_buf[foot_data_len++] = (uint8_t)(gyro[1]>>0);
  120. foot_data_buf[foot_data_len++] = (uint8_t)(gyro[2]>>8);
  121. foot_data_buf[foot_data_len++] = (uint8_t)(gyro[2]>>0);
  122. foot_data_buf[foot_data_len++] = (uint8_t)(front_magn[0]>>8);
  123. foot_data_buf[foot_data_len++] = (uint8_t)(front_magn[0]>>0);
  124. foot_data_buf[foot_data_len++] = (uint8_t)(front_magn[1]>>8);
  125. foot_data_buf[foot_data_len++] = (uint8_t)(front_magn[1]>>0);
  126. foot_data_buf[foot_data_len++] = (uint8_t)(front_magn[2]>>8);
  127. foot_data_buf[foot_data_len++] = (uint8_t)(front_magn[2]>>0);
  128. foot_data_buf[foot_data_len++] = (uint8_t)(back_magn[0]>>8);
  129. foot_data_buf[foot_data_len++] = (uint8_t)(back_magn[0]>>0);
  130. foot_data_buf[foot_data_len++] = (uint8_t)(back_magn[1]>>8);
  131. foot_data_buf[foot_data_len++] = (uint8_t)(back_magn[1]>>0);
  132. foot_data_buf[foot_data_len++] = (uint8_t)(back_magn[2]>>8);
  133. foot_data_buf[foot_data_len++] = (uint8_t)(back_magn[2]>>0);
  134. // SEGGER_RTT_printf(0," left_sh , foot_data_len = %d\n", foot_data_len);
  135. package_time ++;
  136. }
  137. void get_foot_data(uint8_t *buf, uint8_t *buff_len)
  138. {
  139. memcpy(buf, foot_data_buf, foot_data_len);
  140. *buff_len = foot_data_len;
  141. }
  142. void process_motion(int32_t time_stamp, int16_t _acc[3], int16_t _gry[3], int16_t front_mag[3], int16_t back_mag[3], uint8_t _rssi)
  143. {
  144. /*
  145. 解析数据
  146. */
  147. //static int16_t
  148. if(isnan(_acc[0]) || isnan(_acc[1]) || isnan(_acc[2]) || isnan(_gry[0]) || isnan(_gry[1]) || isnan(_gry[2]) )
  149. {
  150. nan_flag = 1;
  151. }
  152. if(nan_flag == 1)
  153. {
  154. SEGGER_RTT_printf(0,"read nan from IMU", foot_data_len);
  155. }
  156. memcpy(accel, _acc, 3 * sizeof(int16_t));
  157. memcpy(gyro , _gry, 3 * sizeof(int16_t));
  158. memcpy(front_magn , front_mag, 3 * sizeof(int16_t));
  159. memcpy(back_magn, back_mag, 3 * sizeof(int16_t));
  160. slave_rssi = _rssi - 8;
  161. if(slave_rssi < 0)
  162. {
  163. slave_rssi = 0;
  164. }
  165. for(int i = 0; i < 3; i++)
  166. {
  167. gyr[i] = gyro[i] / GYR_LSB;
  168. }
  169. for(int i = 0; i < 3; i++)
  170. {
  171. acc[i] = accel[i] / ACC_LSB;
  172. // acc[i] = (acc[i] + acc_bias[i]) * acc_scale[i];
  173. }
  174. //// SEGGER_RTT_printf(0,"PDR gyro[0]:%d ;gyro[1] : %d ;gyro[2]: %d \n", gyro[0], gyro[1],gyro[2]);
  175. // //SEGGER_RTT_printf(0,"PDR acc[0]:%d ;acc[1] : %d ;acc[2]: %d \n", (int)(acc[0]*1000), (int)(acc[1]*1000),(int)(acc[2]*1000));
  176. front_mag_norm = (uint16_t)(abs(front_magn[2]));
  177. back_mag_norm = (uint16_t)(abs(back_magn[2]));
  178. //
  179. // //SEGGER_RTT_printf(0,"front_mag_norm : %d; back_mag_norm : %d; front_magn[0] : %d; front_magn[1] : %d; front_magn[2] : %d \r\n", front_mag_norm, back_mag_norm,front_magn[0],front_magn[1],front_magn[2]);
  180. //
  181. // //检测零速
  182. // detect_zero_vel(front_magn, back_magn, accel, &front_zero, &back_zero, &acc_zero);
  183. // //轨迹计算
  184. // if(front_zero > 0 || back_zero > 0 )
  185. // {
  186. // zupt = 1;
  187. // }
  188. // else
  189. // {
  190. // zupt = 0;
  191. // }
  192. //
  193. // footPDR(time_stamp, gyr, acc, 0 , zupt, acc_zero, pos, att);
  194. // //SEGGER_RTT_printf(0,"pos[0] : %d; pos[1] : %d ; pos[2] : %d \r\n", pos[0] , pos[1], pos[2]);
  195. //
  196. // //SEGGER_RTT_printf(0,"gyro[0] : %d; gyro[1] : %d ; gyro[2] : %d \r\n", gyro[0] , gyro[1], gyro[2]);
  197. //
  198. // //35号左鞋安装有误差,软件来补锅
  199. //// float pos_tmp[3];
  200. ////
  201. //// pos_tmp[0] = -pos[1] * 0.01f;
  202. //// pos_tmp[1] = pos[0] * 0.01f;
  203. ////
  204. //// pos[0] = (int32_t)((0.9397 * pos_tmp[0] + 0.3420 * pos_tmp[1])*100.0f);
  205. //// pos[1] = (int32_t)((-0.3420 * pos_tmp[0] + 0.9397 * pos_tmp[1])*100.0f);
  206. //
  207. //
  208. //
  209. // int32_t pos_tmp[3];
  210. // memcpy(pos_tmp, pos, 3 * sizeof(int32_t));
  211. // pos[0] = -pos_tmp[1];
  212. // pos[1] = pos_tmp[0];
  213. // accel[2] = -accel[2];
  214. int16_t result;
  215. if(RealTimeStep(front_mag, back_mag,_acc))
  216. //if(detect_step_by_mag(front_mag, _acc[2]))
  217. {
  218. step_count++;
  219. }
  220. SEGGER_RTT_printf(0,"step_count: %d \r\n", step_count);
  221. ShoeTouchFlag2 = Get_ShoeTouchFlag();
  222. Rcounter = Get_Rcounter();
  223. Tcounter = Get_Tcounter();
  224. RealFrontLiftPoint=Get_RealFrontLiftPoint();
  225. RealBackLiftPoint=Get_RealBackLiftPoint();
  226. FKEY2=Get_FKEY();
  227. BKEY2=Get_BKEY();
  228. foot_data_to_package();
  229. }