app_detectIsHost.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 SetDeviceNameFlag = 0;
  50. void app_ReSetDeviceName(void){
  51. // SetDeviceNameFlag = 1;
  52. }
  53. //static void app_SetDeviceName_Porcess(void){
  54. // static uint8_t state =0;
  55. // char buf[16];
  56. // memset(buf,0,16);
  57. // switch(state){
  58. // case 0:
  59. // if(1 == SetDeviceNameFlag){state =1;}
  60. // break;
  61. // case 1:
  62. // if(host_isconnect()){host_disconnect();}
  63. // else{
  64. // if(slave_isconnect()) slave_disconnect();
  65. // else{
  66. // advertising_stop();
  67. // ST_scan_stop();
  68. // state =2;
  69. // }
  70. // }
  71. // break;
  72. // case 2:
  73. // if(app_Get_isHost()){ //
  74. // #if BleNameHoldOn_ENANBLE
  75. // slave_set_adv_name((char *) LEFT_NAME,sizeof(LEFT_NAME));
  76. // DEBUG_LOG("AdvName(%d):%s\n",sizeof(LEFT_NAME),LEFT_NAME);
  77. // host_set_scan_name((char *)RIGHT_NAME,sizeof(RIGHT_NAME));
  78. // DEBUG_LOG("scanName(%d):%s\n",sizeof(RIGHT_NAME),RIGHT_NAME);
  79. // #else
  80. // if(mFlash.mClient.isConfig == 'C'){
  81. // 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]);
  82. // DEBUG_LOG("scanName(%d):%s\n",strlen(buf),buf);
  83. // host_set_scan_name(buf,strlen(buf));
  84. //
  85. // advertising_stop();
  86. // memset(buf,0,sizeof(buf));
  87. // sprintf(buf,"SH_%02X%02X",mFlash.macHost[1], mFlash.macHost[0]);
  88. // slave_set_adv_name(buf,strlen(buf));
  89. // }
  90. // #endif
  91. // }else{
  92. // #if BleNameHoldOn_ENANBLE
  93. // slave_set_adv_name((char *)RIGHT_NAME,sizeof(RIGHT_NAME));
  94. // DEBUG_LOG("AdvName(%d):%s\n",sizeof(RIGHT_NAME),RIGHT_NAME);
  95. // #else
  96. // if(mFlash.mClient.isConfig=='C'){ //
  97. // sprintf(buf,"%02X%02X%02X%02X%02X%02X",mFlash.mClient.macAddr[0],mFlash.mClient.macAddr[1],mFlash.mClient.macAddr[2],mFlash.macHost[3],mFlash.macHost[4],mFlash.macHost[5]);
  98. // DEBUG_LOG("advName(%d):%s\n",strlen(buf),buf);
  99. // slave_set_adv_name(buf,strlen(buf));
  100. // }
  101. // host_set_scan_name((char *)"12321321312",sizeof("12321321312"));
  102. // ST_scan_stop();
  103. // #endif
  104. // }
  105. //
  106. // slave_adv_init();
  107. // advertising_start();
  108. // state =0;
  109. // SetDeviceNameFlag = 0;
  110. // break;
  111. // default:state =0;SetDeviceNameFlag = 0;break;
  112. // }
  113. //}
  114. static uint8_t app_get_Front_Number(int *number){
  115. uint8_t front_CS =0,back_CS =0;
  116. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&all_bll_imu_param_t);
  117. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&all_bll_imu_param_t);
  118. // DEBUG_LOG("front_CS:%d back_CS:%d\r\n",front_CS,back_CS);
  119. if(BLL_IMU_CONFIG_FINISH == front_CS && BLL_IMU_CONFIG_FINISH == back_CS){
  120. *number = bll_imu_get_data_num(BLL_IMU_DIR_FRONT);
  121. if(*number >= 1)return 1;
  122. else return 0;
  123. }
  124. else return 0;
  125. }
  126. /**
  127. @brief 返回主机标志位
  128. @param 无
  129. @return 主机标志位
  130. */
  131. uint8_t app_Get_isHost(void)
  132. {
  133. return mFlash.isHost;
  134. }
  135. static MahonyAHRS_t Front_Mahony={0};
  136. static MahonyAHRS_t Mind_Mahony={0};
  137. float Mahony_M_GetRoll(void) {return Mind_Mahony.roll;}
  138. float Mahony_M_GetPitch(void) {return Mind_Mahony.pitch;}
  139. float Mahony_M_GetYaw(void) {return Mind_Mahony.yaw;}
  140. //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)
  141. //{
  142. // uint8_t buf[32];
  143. // uint8_t L=0;
  144. //
  145. // buf[L++] = (uint8_t)(ax>>8);
  146. // buf[L++] = (uint8_t)(ax>>0);
  147. // buf[L++] = (uint8_t)(ay>>8);
  148. // buf[L++] = (uint8_t)(ay>>0);
  149. // buf[L++] = (uint8_t)(az>>8);
  150. // buf[L++] = (uint8_t)(az>>0);
  151. //
  152. // buf[L++] = (uint8_t)(gx>>8);
  153. // buf[L++] = (uint8_t)(gx>>0);
  154. // buf[L++] = (uint8_t)(gy>>8);
  155. // buf[L++] = (uint8_t)(gy>>0);
  156. // buf[L++] = (uint8_t)(gz>>8);
  157. // buf[L++] = (uint8_t)(gz>>0);
  158. //
  159. // buf[L++] = (uint8_t)(mx>>8);
  160. // buf[L++] = (uint8_t)(mx>>0);
  161. // buf[L++] = (uint8_t)(my>>8);
  162. // buf[L++] = (uint8_t)(my>>0);
  163. // buf[L++] = (uint8_t)(mz>>8);
  164. // buf[L++] = (uint8_t)(mz>>0);
  165. // BLE_Client_Tx_Send(0,BLE_HEART,buf,L);
  166. //}
  167. static volatile uint8_t LRFlagReady =0;
  168. uint8_t get_LR_readly(void)
  169. {
  170. return LRFlagReady;
  171. }
  172. static int8_t isLeftOrRight(int16_t* imuAcc, int16_t* midAcc)
  173. { //10ms读一次imu和中间加速度数据,调用一次
  174. static int16_t LR = 0;
  175. static int16_t imuAcc_update[3];
  176. static int16_t midAcc_update[3];
  177. imuAcc_update[0] = imuAcc[0];
  178. imuAcc_update[1] = imuAcc[1];
  179. imuAcc_update[2] = imuAcc[2];
  180. midAcc_update[0] = midAcc[1];
  181. midAcc_update[1] = midAcc[0];
  182. midAcc_update[2] = -midAcc[2];
  183. Mahony_update(&Front_Mahony,0,0,0,imuAcc_update[0],imuAcc_update[1],imuAcc_update[2],0,0,0);
  184. Mahony_update(&Mind_Mahony, 0,0,0,midAcc_update[0],midAcc_update[1],midAcc_update[2],0,0,0);
  185. int16_t d_roll = abs((int16_t)(Front_Mahony.roll*1) - (int16_t)(Mind_Mahony.roll*1));
  186. if(d_roll>300) d_roll = 360 - d_roll;
  187. int16_t pit = abs((int16_t)(Mahony_M_GetPitch()*1));
  188. // DEBUG_LOG("pit:%d,LR:%d,LRFlagReady:%d,d_roll:%d\n",pit,LR,LRFlagReady,d_roll);
  189. if(pit<60){
  190. if(d_roll>150){ //反着放,右鞋
  191. if(LR<50){
  192. LR++;
  193. }
  194. // if((LR == 50) && LRFlagReady) LED_Start(LED_LRCHECK,COLOR_CYAN);
  195. }else{ //正着放,左鞋
  196. if(LR>-50 ){
  197. LR--;
  198. }
  199. // if((LR == -50 ) && LRFlagReady) LED_Start(LED_LRCHECK,COLOR_PURPLE);
  200. }
  201. }
  202. // ANO_Send_Status(Mind_Mahony.roll,Mind_Mahony.pitch,Front_Mahony.roll,0,0,0,0);
  203. // ANO_Send_Senser(midAcc[0],midAcc[1],midAcc[2],imuAcc[0],imuAcc[1],imuAcc[2],d_roll,LR,0);
  204. if(LR == 50) return 0; //右鞋
  205. if(LR == -50) return 1; //左鞋
  206. return -1; //左鞋
  207. }
  208. static void DetectLr_notify_cb(uint32_t dir_bit){
  209. int number =0;
  210. int ret =0;
  211. bll_imu_data_t f_data={0};
  212. qma_data_t qma_data={0};
  213. int8_t result =0;
  214. if(!((dir_bit >> BLL_IMU_DIR_FRONT) & 0x01))return;
  215. //中间加速度没有设置成功
  216. if(QMA_104HZ != hal_get_QmaFrequency()){
  217. return ;
  218. }
  219. //获取前脚角速度值的值
  220. if(!app_get_Front_Number(&number)){
  221. return;
  222. }
  223. ret = drv_qma_get_acc_data(&qma_data);
  224. if(-1 == ret || (0 == qma_data.acc[0] && 0 == qma_data.acc[1] && 0== qma_data.acc[2])){
  225. return;
  226. }
  227. if(-1 != bll_imu_get_data(BLL_IMU_DIR_FRONT, number-1, &f_data)){
  228. result = isLeftOrRight(f_data.acc,qma_data.acc);
  229. if(result >=0){
  230. if(0 == LRFlagReady){ LRFlagReady =1; }
  231. if(mFlash.isHost != result)
  232. app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_CHARGING_UNPLUG);
  233. mFlash.isHost = result;
  234. }
  235. }
  236. }
  237. static void app_detect_LR_Porcess(void){
  238. uint8_t front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&all_bll_imu_param_t);
  239. uint8_t back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&all_bll_imu_param_t);
  240. if(BLL_IMU_CONFIG_DOING != front_CS || BLL_IMU_CONFIG_DOING != back_CS){
  241. bll_imu_Resume_config_param(&all_bll_imu_param_t);
  242. }
  243. }
  244. void app_detect_LR_process_stop(void){
  245. bll_imu_Resume_unregister_config_param(&all_bll_imu_param_t);
  246. Process_Stop(app_detect_LR_Porcess);
  247. LED_Stop(LED_LRCHECK);
  248. hal_qma_setFrequency(QMA_OFF);
  249. LED_Stop(LED_LRCHECK);
  250. }
  251. void app_detect_Init(void)
  252. {
  253. char buf[16];
  254. memset(buf,0,16);
  255. if(mFlash.mClient.isConfig=='C'){
  256. if(host_isconnect()){host_disconnect();}
  257. if(slave_isconnect()) slave_disconnect();
  258. advertising_stop();
  259. ST_scan_stop();
  260. if(app_Get_isHost()){
  261. #if BleNameHoldOn_ENANBLE
  262. slave_set_adv_name((char *) LEFT_NAME,sizeof(LEFT_NAME));
  263. DEBUG_LOG("AdvName(%d):%s\n",sizeof(LEFT_NAME),LEFT_NAME);
  264. host_set_scan_name((char *)RIGHT_NAME,sizeof(RIGHT_NAME));
  265. DEBUG_LOG("scanName(%d):%s\n",sizeof(RIGHT_NAME),RIGHT_NAME);
  266. #else
  267. 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]);
  268. DEBUG_LOG("scanName(%d):%s\n",strlen(buf),buf);
  269. host_set_scan_name(buf,strlen(buf));
  270. memset(buf,0,sizeof(buf));
  271. sprintf(buf,"SH_%02X%02X",mFlash.macHost[1], mFlash.macHost[0]);
  272. slave_set_adv_name(buf,strlen(buf));
  273. #endif
  274. }
  275. else {
  276. #if BleNameHoldOn_ENANBLE
  277. slave_set_adv_name((char *)RIGHT_NAME,sizeof(RIGHT_NAME));
  278. DEBUG_LOG("AdvName(%d):%s\n",sizeof(RIGHT_NAME),RIGHT_NAME);
  279. #else
  280. sprintf(buf,"%02X%02X%02X%02X%02X%02X",mFlash.mClient.macAddr[0],mFlash.mClient.macAddr[1],mFlash.mClient.macAddr[2],mFlash.macHost[3],mFlash.macHost[4],mFlash.macHost[5]);
  281. DEBUG_LOG("advName(%d):%s\n",strlen(buf),buf);
  282. slave_set_adv_name(buf,strlen(buf));
  283. host_set_scan_name((char *)"12321321312",sizeof("12321321312"));
  284. ST_scan_stop();
  285. #endif
  286. }
  287. slave_adv_init();
  288. advertising_start();
  289. }
  290. if(LR_FLAG_VALUE != mFlash.LR_FLAG){
  291. Mahony_Init(&Front_Mahony,104);
  292. Mahony_Init(&Mind_Mahony,104);
  293. hal_qma_setFrequency(QMA_104HZ);
  294. bll_imu_Resume_config_param(&all_bll_imu_param_t);
  295. Process_Start(100,"app_detect_LR",app_detect_LR_Porcess);
  296. bll_imu_register_data_notify_callback(BLL_IMU_DATA_NOTIFY_CB_PRIORITY_1, DetectLr_notify_cb);
  297. Process_SetHoldOn(app_detect_LR_Porcess,1);
  298. }
  299. else LRFlagReady=1;
  300. if(mFlash.isHost){DEBUG_LOG("======= Left shooe ======= \n");}
  301. else {DEBUG_LOG("======= Right shooe ======= \n");}
  302. DEBUG_LOG("======= mFlash.mClient.isConfig:%d ======= \n",mFlash.mClient.isConfig);
  303. }