app_detectIsHost.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*********************************************************************
  2. * INCLUDES
  3. */
  4. #include "ble_comm.h"
  5. #include "app_detectIsHost.h"
  6. #include "hal_ble_client.h"
  7. #include "app_flash.h"
  8. #include "hal_qma.h"
  9. #include "hal_mt.h"
  10. #include "tool.h"
  11. #include "bll_imu.h"
  12. #include "hal_charge.h"
  13. #include "bsp_time.h"
  14. #include "hal_mahonyAHRS.h"
  15. #include "hal_ano.h"
  16. #include "hal_led.h"
  17. #include "app_wireless_pair.h"
  18. //全功率模式
  19. static const bll_imu_one_way_param_t all_front_param={
  20. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //前脚 - 加速度正常模式
  21. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //前脚 - 陀螺仪正常模式
  22. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //前脚 - 时间戳25US精度
  23. .timestamp_switch = FML_IMU_TIMESTAMP_ON, //前脚 - 时间戳开启
  24. .acc_fs = FML_IMU_ACC_FS_16G, //前脚 - 加速度量程 - 16G
  25. .gry_fs = FML_IMU_GRY_FS_2000DPS, //前脚 - 陀螺仪量程 - 2000DPS
  26. .mag_fs = FML_IMU_MAG_FS_30GS, //前脚 - 地磁计量程 - 30GS
  27. .acc_odr = FML_IMU_ACC_ODR_104HZ, //前脚 - 加速度采样频率 - 104HZ
  28. .gry_odr = FML_IMU_GRY_ODR_OFF, //前脚 - 陀螺仪采样频率 - 104HZ
  29. .mag_odr = FML_IMU_MAG_ODR_OFF, //前脚 - 地磁计采样频率 - 200HZ
  30. .fifo_odr = FML_IMU_FIFO_ODR_104HZ,
  31. };
  32. static const bll_imu_one_way_param_t all_back_param={
  33. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //后脚 - 加速度正常模式
  34. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //后脚 - 陀螺仪正常模式
  35. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //后脚 - 时间戳25US精度
  36. .timestamp_switch = FML_IMU_TIMESTAMP_OFF, //后脚 - 时间戳关闭
  37. .acc_fs = FML_IMU_ACC_FS_16G, //后脚 - 加速度量程 - 16G
  38. .gry_fs = FML_IMU_GRY_FS_2000DPS, //后脚 - 陀螺仪量程 - 2000DPS
  39. .mag_fs = FML_IMU_MAG_FS_30GS, //后脚 - 地磁计量程 - 30GS
  40. .acc_odr = FML_IMU_ACC_ODR_OFF, //后脚 - 加速度采样频率 - 关闭
  41. .gry_odr = FML_IMU_GRY_ODR_OFF, //后脚 - 陀螺仪采样频率 - 关闭
  42. .mag_odr = FML_IMU_MAG_ODR_OFF, //后脚 - 地磁计采样频率 - 200HZ
  43. .fifo_odr = FML_IMU_FIFO_ODR_OFF,
  44. };
  45. static const bll_imu_param_t all_bll_imu_param_t={
  46. .config_param[FML_IMU_DIR_FRONT] = &all_front_param,
  47. .config_param[FML_IMU_DIR_BACK] = &all_back_param,
  48. };
  49. static uint8_t app_get_Front_Number(int *number){
  50. uint8_t front_CS =0,back_CS =0;
  51. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&all_bll_imu_param_t);
  52. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&all_bll_imu_param_t);
  53. // DEBUG_LOG("front_CS:%d back_CS:%d\r\n",front_CS,back_CS);
  54. if(BLL_IMU_CONFIG_FINISH == front_CS && BLL_IMU_CONFIG_FINISH == back_CS){
  55. *number = bll_imu_get_data_num(BLL_IMU_DIR_FRONT);
  56. if(*number >= 1)return 1;
  57. else return 0;
  58. }
  59. else return 0;
  60. }
  61. /**
  62. @brief 返回主机标志位
  63. @param 无
  64. @return 主机标志位
  65. */
  66. uint8_t app_Get_isHost(void)
  67. {
  68. return mFlash.isHost;
  69. }
  70. static MahonyAHRS_t Front_Mahony={0};
  71. static MahonyAHRS_t Mind_Mahony={0};
  72. float Mahony_M_GetRoll(void) {return Mind_Mahony.roll;}
  73. float Mahony_M_GetPitch(void) {return Mind_Mahony.pitch;}
  74. float Mahony_M_GetYaw(void) {return Mind_Mahony.yaw;}
  75. //void My_Send_Senser(int16_t ax, int16_t ay, int16_t az, int16_t gx, int16_t gy, int16_t gz, int16_t mx, int16_t my, int16_t mz)
  76. //{
  77. // uint8_t buf[32];
  78. // uint8_t L=0;
  79. //
  80. // buf[L++] = (uint8_t)(ax>>8);
  81. // buf[L++] = (uint8_t)(ax>>0);
  82. // buf[L++] = (uint8_t)(ay>>8);
  83. // buf[L++] = (uint8_t)(ay>>0);
  84. // buf[L++] = (uint8_t)(az>>8);
  85. // buf[L++] = (uint8_t)(az>>0);
  86. //
  87. // buf[L++] = (uint8_t)(gx>>8);
  88. // buf[L++] = (uint8_t)(gx>>0);
  89. // buf[L++] = (uint8_t)(gy>>8);
  90. // buf[L++] = (uint8_t)(gy>>0);
  91. // buf[L++] = (uint8_t)(gz>>8);
  92. // buf[L++] = (uint8_t)(gz>>0);
  93. //
  94. // buf[L++] = (uint8_t)(mx>>8);
  95. // buf[L++] = (uint8_t)(mx>>0);
  96. // buf[L++] = (uint8_t)(my>>8);
  97. // buf[L++] = (uint8_t)(my>>0);
  98. // buf[L++] = (uint8_t)(mz>>8);
  99. // buf[L++] = (uint8_t)(mz>>0);
  100. // BLE_Client_Tx_Send(0,BLE_HEART,buf,L);
  101. //}
  102. static volatile uint8_t LRFlagReady =0;
  103. uint8_t get_LR_readly(void)
  104. {
  105. return LRFlagReady;
  106. }
  107. static int8_t isLeftOrRight(int16_t* imuAcc, int16_t* midAcc)
  108. { //10ms读一次imu和中间加速度数据,调用一次
  109. static int16_t LR = 0;
  110. static int16_t imuAcc_update[3];
  111. static int16_t midAcc_update[3];
  112. imuAcc_update[0] = imuAcc[0];
  113. imuAcc_update[1] = imuAcc[1];
  114. imuAcc_update[2] = imuAcc[2];
  115. midAcc_update[0] = midAcc[1];
  116. midAcc_update[1] = midAcc[0];
  117. midAcc_update[2] = -midAcc[2];
  118. Mahony_update(&Front_Mahony,0,0,0,imuAcc_update[0],imuAcc_update[1],imuAcc_update[2],0,0,0);
  119. Mahony_update(&Mind_Mahony, 0,0,0,midAcc_update[0],midAcc_update[1],midAcc_update[2],0,0,0);
  120. int16_t d_roll = abs((int16_t)(Front_Mahony.roll*1) - (int16_t)(Mind_Mahony.roll*1));
  121. if(d_roll>300) d_roll = 360 - d_roll;
  122. int16_t pit = abs((int16_t)(Mahony_M_GetPitch()*1));
  123. // DEBUG_LOG("pit:%d,LR:%d,LRFlagReady:%d,d_roll:%d\n",pit,LR,LRFlagReady,d_roll);
  124. if(pit<60){
  125. if(d_roll>150){ //反着放,右鞋
  126. if(LR<50){
  127. LR++;
  128. }
  129. // if((LR == 50) && LRFlagReady) LED_Start(LED_LRCHECK,COLOR_CYAN);
  130. }else{ //正着放,左鞋
  131. if(LR>-50 ){
  132. LR--;
  133. }
  134. // if((LR == -50 ) && LRFlagReady) LED_Start(LED_LRCHECK,COLOR_PURPLE);
  135. }
  136. }
  137. // ANO_Send_Status(Mind_Mahony.roll,Mind_Mahony.pitch,Front_Mahony.roll,0,0,0,0);
  138. // ANO_Send_Senser(midAcc[0],midAcc[1],midAcc[2],imuAcc[0],imuAcc[1],imuAcc[2],d_roll,LR,0);
  139. if(LR == 50) return 0; //右鞋
  140. if(LR == -50) return 1; //左鞋
  141. return -1; //左鞋
  142. }
  143. static void DetectLr_notify_cb(uint32_t dir_bit){
  144. int number =0;
  145. int ret =0;
  146. bll_imu_data_t f_data={0};
  147. qma_data_t qma_data={0};
  148. int8_t result =0;
  149. if(!((dir_bit >> BLL_IMU_DIR_FRONT) & 0x01))return;
  150. //中间加速度没有设置成功
  151. if(QMA_104HZ != hal_get_QmaFrequency()){
  152. return ;
  153. }
  154. //获取前脚角速度值的值
  155. if(!app_get_Front_Number(&number)){
  156. return;
  157. }
  158. ret = drv_qma_get_acc_data(&qma_data);
  159. if(-1 == ret || (0 == qma_data.acc[0] && 0 == qma_data.acc[1] && 0== qma_data.acc[2])){
  160. return;
  161. }
  162. if(-1 != bll_imu_get_data(BLL_IMU_DIR_FRONT, number-1, &f_data)){
  163. result = isLeftOrRight(f_data.acc,qma_data.acc);
  164. if(result >=0){
  165. if(0 == LRFlagReady){ LRFlagReady =1; }
  166. mFlash.isHost = result;
  167. }
  168. }
  169. }
  170. static void app_detect_LR_Porcess(void){
  171. uint8_t front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&all_bll_imu_param_t);
  172. uint8_t back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&all_bll_imu_param_t);
  173. if(BLL_IMU_CONFIG_DOING != front_CS || BLL_IMU_CONFIG_DOING != back_CS){
  174. bll_imu_Resume_config_param(&all_bll_imu_param_t);
  175. }
  176. }
  177. void app_detect_LR_process_stop(void){
  178. bll_imu_Resume_unregister_config_param(&all_bll_imu_param_t);
  179. Process_Stop(app_detect_LR_Porcess);
  180. hal_qma_setFrequency(QMA_OFF);
  181. }
  182. void app_detect_Init(void)
  183. {
  184. char buf[16];
  185. memset(buf,0,16);
  186. if(mFlash.mClient.isConfig=='C'){
  187. if(host_isconnect()){host_disconnect();}
  188. if(slave_isconnect()) slave_disconnect();
  189. advertising_stop();
  190. ST_scan_stop();
  191. if(app_Get_isHost()){
  192. #if BleNameHoldOn_ENANBLE
  193. slave_set_adv_name((char *) LEFT_NAME,sizeof(LEFT_NAME));
  194. DEBUG_LOG("AdvName(%d):%s\n",sizeof(LEFT_NAME),LEFT_NAME);
  195. host_set_scan_name((char *)RIGHT_NAME,sizeof(RIGHT_NAME));
  196. DEBUG_LOG("scanName(%d):%s\n",sizeof(RIGHT_NAME),RIGHT_NAME);
  197. #else
  198. sprintf(buf,"%02X%02X%02X%02X%02X%02X",mFlash.macHost[0],mFlash.macHost[1],mFlash.macHost[2],mFlash.mClient.macAddr[3],mFlash.mClient.macAddr[4],mFlash.mClient.macAddr[5]);
  199. host_set_scan_name(buf,strlen(buf));
  200. memset(buf,0,sizeof(buf));
  201. sprintf(buf,"FUN_%02X%02X",mFlash.macHost[1], mFlash.macHost[0]);
  202. slave_set_adv_name(buf,strlen(buf));
  203. #endif
  204. }
  205. else {
  206. #if BleNameHoldOn_ENANBLE
  207. slave_set_adv_name((char *)RIGHT_NAME,sizeof(RIGHT_NAME));
  208. DEBUG_LOG("AdvName(%d):%s\n",sizeof(RIGHT_NAME),RIGHT_NAME);
  209. #else
  210. sprintf(buf,"%02X%02X%02X%02X%02X%02X",mFlash.macHost[0],mFlash.macHost[1],mFlash.macHost[2],mFlash.mClient.macAddr[3],mFlash.mClient.macAddr[4],mFlash.mClient.macAddr[5]);
  211. slave_set_adv_name(buf,strlen(buf));
  212. host_set_scan_name((char *)"**************",sizeof("*********************"));
  213. ST_scan_stop();
  214. #endif
  215. }
  216. slave_adv_init();
  217. advertising_start();
  218. }
  219. if(LR_FLAG_VALUE != mFlash.LR_FLAG){
  220. Mahony_Init(&Front_Mahony,104);
  221. Mahony_Init(&Mind_Mahony,104);
  222. hal_qma_setFrequency(QMA_104HZ);
  223. bll_imu_Resume_config_param(&all_bll_imu_param_t);
  224. Process_Start(100,"app_detect_LR",app_detect_LR_Porcess);
  225. bll_imu_register_data_notify_callback(BLL_IMU_DATA_NOTIFY_CB_PRIORITY_1, DetectLr_notify_cb);
  226. Process_SetHoldOn(app_detect_LR_Porcess,1);
  227. }
  228. else LRFlagReady=1;
  229. if(mFlash.isHost){DEBUG_LOG("======= Left shooe ======= \n");}
  230. else {DEBUG_LOG("======= Right shooe ======= \n");}
  231. DEBUG_LOG("======= mFlash.mClient.isConfig:%d ======= \n",mFlash.mClient.isConfig);
  232. }