app_detectIsHost.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 "MahonyAHRS.h"
  13. #include "hal_charge.h"
  14. #include "bsp_time.h"
  15. #define DETECT_LR_TIMEOUT 20
  16. //全功率模式
  17. static const bll_imu_one_way_param_t all_front_param={
  18. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //前脚 - 加速度正常模式
  19. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //前脚 - 陀螺仪正常模式
  20. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //前脚 - 时间戳25US精度
  21. .timestamp_switch = FML_IMU_TIMESTAMP_ON, //前脚 - 时间戳开启
  22. .acc_fs = FML_IMU_ACC_FS_16G, //前脚 - 加速度量程 - 16G
  23. .gry_fs = FML_IMU_GRY_FS_2000DPS, //前脚 - 陀螺仪量程 - 2000DPS
  24. .mag_fs = FML_IMU_MAG_FS_30GS, //前脚 - 地磁计量程 - 30GS
  25. .acc_odr = FML_IMU_ACC_ODR_104HZ, //前脚 - 加速度采样频率 - 104HZ
  26. .gry_odr = FML_IMU_GRY_ODR_104HZ, //前脚 - 陀螺仪采样频率 - 104HZ
  27. .mag_odr = FML_IMU_MAG_ODR_200HZ, //前脚 - 地磁计采样频率 - 200HZ
  28. .fifo_odr = FML_IMU_FIFO_ODR_104HZ,
  29. };
  30. static const bll_imu_one_way_param_t all_back_param={
  31. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //后脚 - 加速度正常模式
  32. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //后脚 - 陀螺仪正常模式
  33. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //后脚 - 时间戳25US精度
  34. .timestamp_switch = FML_IMU_TIMESTAMP_OFF, //后脚 - 时间戳关闭
  35. .acc_fs = FML_IMU_ACC_FS_16G, //后脚 - 加速度量程 - 16G
  36. .gry_fs = FML_IMU_GRY_FS_2000DPS, //后脚 - 陀螺仪量程 - 2000DPS
  37. .mag_fs = FML_IMU_MAG_FS_30GS, //后脚 - 地磁计量程 - 30GS
  38. .acc_odr = FML_IMU_ACC_ODR_OFF, //后脚 - 加速度采样频率 - 关闭
  39. .gry_odr = FML_IMU_GRY_ODR_OFF, //后脚 - 陀螺仪采样频率 - 关闭
  40. .mag_odr = FML_IMU_MAG_ODR_OFF, //后脚 - 地磁计采样频率 - 200HZ
  41. .fifo_odr = FML_IMU_FIFO_ODR_OFF,
  42. };
  43. static const bll_imu_param_t all_bll_imu_param_t={
  44. .config_param[FML_IMU_DIR_FRONT] = &all_front_param,
  45. .config_param[FML_IMU_DIR_BACK] = &all_back_param,
  46. };
  47. static uint8_t SetDeviceNameFlag = 0;
  48. static void app_SetDeviceName_Porcess(void){
  49. static uint8_t state =0;
  50. char buf[16];
  51. memset(buf,0,16);
  52. switch(state){
  53. case 0:
  54. if(1 == SetDeviceNameFlag){state =1;}
  55. break;
  56. case 1:
  57. if(host_isconnect()){host_disconnect();}
  58. else{
  59. if(slave_isconnect()) slave_disconnect();
  60. else{
  61. advertising_stop();
  62. ST_scan_stop();
  63. state =2;
  64. }
  65. }
  66. break;
  67. case 2:
  68. if(app_Get_isHost()){ //
  69. #if BleNameHoldOn_ENANBLE
  70. slave_set_adv_name((char *) LEFT_NAME,sizeof(LEFT_NAME));
  71. DEBUG_LOG("AdvName(%d):%s\n",sizeof(LEFT_NAME),LEFT_NAME);
  72. host_set_scan_name((char *)RIGHT_NAME,sizeof(RIGHT_NAME));
  73. DEBUG_LOG("scanName(%d):%s\n",sizeof(RIGHT_NAME),RIGHT_NAME);
  74. #else
  75. if(mFlash.mClient.isConfig == 'C'){
  76. 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]);
  77. DEBUG_LOG("scanName(%d):%s\n",strlen(buf),buf);
  78. host_set_scan_name(buf,strlen(buf));
  79. memset(buf,0,sizeof(buf));
  80. sprintf(buf,"SH_%02X%02X",mFlash.macHost[1], mFlash.macHost[0]);
  81. slave_set_adv_name(buf,strlen(buf));
  82. }
  83. #endif
  84. }else{
  85. #if BleNameHoldOn_ENANBLE
  86. slave_set_adv_name((char *)RIGHT_NAME,sizeof(RIGHT_NAME));
  87. DEBUG_LOG("AdvName(%d):%s\n",sizeof(RIGHT_NAME),RIGHT_NAME);
  88. #else
  89. if(mFlash.mClient.isConfig=='C'){ //
  90. 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]);
  91. DEBUG_LOG("advName(%d):%s\n",strlen(buf),buf);
  92. slave_set_adv_name(buf,strlen(buf));
  93. }
  94. host_set_scan_name((char *)"12321321312",sizeof("12321321312"));
  95. ST_scan_stop();
  96. #endif
  97. }
  98. slave_adv_init();
  99. advertising_start();
  100. state =0;
  101. SetDeviceNameFlag = 0;
  102. break;
  103. default:state =0;SetDeviceNameFlag = 0;break;
  104. }
  105. }
  106. //判断鞋子是否为静止
  107. //返回值 0
  108. static uint8_t app_shoes_still(bll_imu_data_t f_data){
  109. #define Bufflength 6
  110. static int32_t buff[Bufflength]={0};
  111. static uint8_t firtRunflag =0;
  112. int32_t Buff_Max =0,Buff_Min =0;
  113. uint8_t i=0;
  114. int32_t acc_norm =0;
  115. acc_norm = sqrt(f_data.acc[0] * f_data.acc[0] +f_data.acc[1] * f_data.acc[1] + f_data.acc[2] * f_data.acc[2]);
  116. //第一次启动给所有的BUFF赋值
  117. if(firtRunflag < Bufflength){firtRunflag++;
  118. buff[firtRunflag]=acc_norm;
  119. return 0;
  120. }
  121. for(i=0;i<(Bufflength-1);i++){
  122. buff[i]=buff[i+1];
  123. }
  124. buff[Bufflength-1] = acc_norm;
  125. //找出最大、最小值
  126. Buff_Min = buff[0];
  127. Buff_Max = buff[0];
  128. for(i=0;i<(Bufflength-1);i++){
  129. if(Buff_Max <= buff[i]){
  130. Buff_Max = buff[i];
  131. }
  132. if(Buff_Min >= buff[i]){
  133. Buff_Min = buff[i];
  134. }
  135. }
  136. if((Buff_Max-Buff_Min) < 200)return 1;
  137. else return 0;
  138. }
  139. static uint8_t app_get_Front_data(bll_imu_data_t *f_data){
  140. uint8_t front_CS =0,back_CS =0;
  141. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&all_bll_imu_param_t);
  142. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&all_bll_imu_param_t);
  143. // DEBUG_LOG("front_CS:%d back_CS:%d\r\n",front_CS,back_CS);
  144. if(BLL_IMU_CONFIG_FINISH == front_CS && BLL_IMU_CONFIG_FINISH == back_CS){
  145. if(bll_imu_get_data_num(BLL_IMU_DIR_FRONT) >= 0){
  146. if(-1 == bll_imu_get_data(BLL_IMU_DIR_FRONT, 0, f_data))return 0;
  147. else return 1;
  148. }
  149. else return 0;
  150. }
  151. else if(BLL_IMU_CONFIG_DOING != front_CS || BLL_IMU_CONFIG_DOING != back_CS){
  152. bll_imu_Resume_config_param(&all_bll_imu_param_t);
  153. return 0 ;
  154. }
  155. else return 0;
  156. }
  157. //计算中间加速度Roll和是否静止的检测
  158. static uint8_t shoes_StopFlag =0;//鞋子静止标志位
  159. static void app_Roll_And_Stop_process(void){
  160. int ret =-1;
  161. qma_data_t qma_data={0};
  162. bll_imu_data_t f_data={0};
  163. //获取中间加速度值
  164. if(QMA_104HZ != hal_get_QmaFrequency())return;
  165. ret = drv_qma_get_acc_data(&qma_data);
  166. if(-1 == ret || (0 == qma_data.acc[0] && 0 == qma_data.acc[1] && 0== qma_data.acc[2])){
  167. return;
  168. }
  169. Mahony_process(0,0,0,qma_data.acc[0],qma_data.acc[1],qma_data.acc[2],0,0,0);
  170. //500ms采集一次前脚加速度并用于判断鞋子是否静止
  171. static uint8_t getAccCnt =0;
  172. if(getAccCnt++ >= 25){getAccCnt =0;
  173. if(!app_get_Front_data(&f_data))return;
  174. if(app_shoes_still(f_data))shoes_StopFlag =1;
  175. else shoes_StopFlag =0;
  176. }
  177. }
  178. //判断前后脚磁力计是否存在
  179. uint8_t app_mag_still(void){
  180. return 1;
  181. }
  182. //检测Roll值是否会稳定
  183. static uint8_t rol_holdFlag =0;//rol稳定标志位
  184. static void app_Rol_stale(int16_t rol){
  185. #define RollBuffLengh 5
  186. static int16_t buff[RollBuffLengh]={0};
  187. static uint8_t Length =0;
  188. int16_t Buff_Max =0,Buff_Min =0;
  189. uint16_t i =0;
  190. static uint8_t cnt =0;
  191. if(cnt++ >=5){cnt = 0;
  192. //第一次启动给所有的BUFF赋值
  193. if(Length < RollBuffLengh){Length++;
  194. buff[Length]=rol;
  195. rol_holdFlag = 0;
  196. }
  197. for(i=0;i<(Bufflength-1);i++){
  198. buff[i]=buff[i+1];
  199. }
  200. buff[RollBuffLengh-1] = rol;
  201. //找出最大、最小值
  202. Buff_Min = buff[0];
  203. Buff_Max = buff[0];
  204. for(i=0;i<(Bufflength-1);i++){
  205. if(Buff_Max <= buff[i]){
  206. Buff_Max = buff[i];
  207. }
  208. if(Buff_Min >= buff[i]){
  209. Buff_Min = buff[i];
  210. }
  211. }
  212. if((Buff_Max-Buff_Min) < 2){
  213. rol_holdFlag = 1;
  214. }
  215. else rol_holdFlag = 0;
  216. }
  217. }
  218. static void app_detect_LR_Porcess(void)
  219. {
  220. static uint8_t StateHoldTime = 0;
  221. bll_imu_data_t f_data={0};
  222. static uint8_t charge_state =0;
  223. static DETECT_LR_e direct_detect_LR = DETECT_LR_INIT;
  224. #define ReDacheck_Max 10
  225. #define ReXiaocheck_Max 5
  226. static uint8_t ReCheckDaCnt = 0;
  227. static uint8_t ReCheckXiaoCnt = 0;
  228. static uint32_t tim =0;
  229. static uint8_t RecheckDa_Buff[ReDacheck_Max]={0};
  230. static uint8_t RecheckXiao_Buff[ReXiaocheck_Max]={0};
  231. uint16_t ReCheckHostCnt =0,ReCheckClientCnt =0;
  232. //是否充电
  233. if((hal_charge_Getstate() != BLE_CHARGE_PULLOUT)){
  234. if(0 == charge_state){charge_state =1;
  235. DEBUG_LOG("app_detect_roll charge_state 0\n");
  236. Process_Start(10,"Roll_And_Stop",app_Roll_And_Stop_process);
  237. Process_Start(10,"detect_LR",app_detect_LR_Porcess);
  238. Process_SetHoldOn(app_detect_LR_Porcess,1);
  239. hal_qma_setFrequency(QMA_104HZ);
  240. bll_imu_Resume_config_param(&all_bll_imu_param_t);
  241. StateHoldTime = 0;
  242. return;
  243. }
  244. }else{
  245. if(1 == charge_state){charge_state =0;
  246. Process_UpdatePeroid(app_Roll_And_Stop_process,1000);
  247. Process_Stop(app_Roll_And_Stop_process);
  248. Process_SetHoldOn(app_detect_LR_Porcess,0);
  249. StateHoldTime = 0;
  250. bll_imu_Resume_unregister_config_param(&all_bll_imu_param_t);
  251. hal_qma_setFrequency(QMA_OFF);
  252. DEBUG_LOG("app_detect_LR_Porcess charge_state 1\n");
  253. }
  254. return ;
  255. }
  256. //获取前脚角速度值的值
  257. if(!app_get_Front_data(&f_data)){
  258. StateHoldTime =0;
  259. return;
  260. }
  261. //中间加速度没有设置成功
  262. if(QMA_104HZ != hal_get_QmaFrequency()){
  263. StateHoldTime =0;
  264. return ;
  265. }
  266. int16_t rol = (int16_t)(getRoll());
  267. app_Rol_stale(rol);
  268. if(shoes_StopFlag && rol_holdFlag && app_mag_still() && (hal_charge_Getstate() != BLE_CHARGE_PULLOUT) && f_data.acc[2] < -1850 && f_data.acc[2] > -2250){
  269. if(abs(rol) >90){//反向
  270. #if _SAME_DIRECTION
  271. if(DETECT_LR_IS_RIGHT == direct_detect_LR)StateHoldTime++;
  272. else StateHoldTime =0;
  273. direct_detect_LR = DETECT_LR_IS_RIGHT;
  274. #else
  275. if(DETECT_LR_IS_LEFT == direct_detect_LR)counter++;
  276. else counter =0;
  277. direct_detect_LR = DETECT_LR_IS_LEFT;
  278. #endif
  279. }
  280. else {//同向
  281. #if _SAME_DIRECTION
  282. if(DETECT_LR_IS_LEFT == direct_detect_LR)StateHoldTime++;
  283. else StateHoldTime =0;
  284. direct_detect_LR = DETECT_LR_IS_LEFT;
  285. #else
  286. if(DETECT_LR_IS_RIGHT == direct_detect_LR)counter++;
  287. else counter =0;
  288. direct_detect_LR = DETECT_LR_IS_RIGHT;
  289. #endif
  290. }
  291. }
  292. else StateHoldTime =0;
  293. if(StateHoldTime >= DETECT_LR_TIMEOUT){StateHoldTime = 0;
  294. // DEBUG_LOG("direct_detect_host:%d\n",direct_detect_LR);
  295. if(direct_detect_LR != DETECT_LR_INIT){//第一次识别
  296. if(0xF5 != mFlash.LR_FLAG){
  297. if(direct_detect_LR == DETECT_LR_IS_LEFT)
  298. mFlash.isHost = 1;
  299. else
  300. mFlash.isHost = 0;
  301. mFlash.LR_FLAG = 0xF5;
  302. Flash_SaveInfomation();
  303. SetDeviceNameFlag =1;
  304. MT_Run(500);
  305. mBackup.isHost= mFlash.isHost;
  306. mBackup.LR_FLAG = mFlash.LR_FLAG;
  307. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_FLASH,"app_detect_LR save backup fail");
  308. tim = TIME_GetTicks();
  309. }
  310. else{
  311. if(TIME_GetTicks() -tim >= 5000){tim = TIME_GetTicks();
  312. // DEBUG_LOG("direct_detect_host:%d\n",direct_detect_LR);
  313. if(ReCheckDaCnt < ReDacheck_Max){
  314. if(ReCheckXiaoCnt < ReXiaocheck_Max){
  315. if(direct_detect_LR == DETECT_LR_IS_LEFT){
  316. RecheckXiao_Buff[ReCheckXiaoCnt] = 1;
  317. }
  318. else RecheckXiao_Buff[ReCheckXiaoCnt] = 0;
  319. ReCheckXiaoCnt++;
  320. }
  321. else{
  322. ReCheckXiaoCnt =0;
  323. ReCheckHostCnt =0;
  324. ReCheckClientCnt =0;
  325. for(uint8_t i=0; i < ReXiaocheck_Max;i++){
  326. // DEBUG_LOG(">>>>>>> RecheckXiao_Buff,%d\n",RecheckXiao_Buff[i]);
  327. if(RecheckXiao_Buff[i] ==1)ReCheckHostCnt++;
  328. else ReCheckClientCnt++;
  329. }
  330. if(ReCheckHostCnt > (ReXiaocheck_Max-1)){
  331. RecheckDa_Buff[ReCheckDaCnt++] = 1;
  332. }
  333. else if(ReCheckClientCnt > (ReXiaocheck_Max-1)){
  334. RecheckDa_Buff[ReCheckDaCnt++] = 0;
  335. }
  336. }
  337. }
  338. else{
  339. ReCheckHostCnt =0;
  340. ReCheckClientCnt =0;
  341. for(uint8_t i=0; i < ReDacheck_Max;i++){
  342. if(RecheckDa_Buff[i] ==1)ReCheckHostCnt++;
  343. else ReCheckClientCnt++;
  344. }
  345. // DEBUG_LOG("ReCheckHostCnt:%d,ReCheckHostCnt:%d\n",ReCheckHostCnt,ReCheckClientCnt);
  346. if((ReCheckHostCnt*100 /ReDacheck_Max) > 60 && mFlash.isHost == 0){
  347. mFlash.isHost = 1;
  348. SetDeviceNameFlag =1;
  349. }
  350. else if((ReCheckClientCnt *100 /ReDacheck_Max) > 60 && mFlash.isHost == 1){
  351. mFlash.isHost = 0;
  352. SetDeviceNameFlag =1;
  353. }
  354. mFlash.LR_FLAG = 0x55;
  355. Flash_SaveInfomation();
  356. // MT_Run(500);
  357. mBackup.isHost= mFlash.isHost;
  358. mBackup.LR_FLAG = mFlash.LR_FLAG;
  359. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_FLASH,"app_detect_LR save backup fail");
  360. bll_imu_Resume_unregister_config_param(&all_bll_imu_param_t);
  361. Process_Stop(app_detect_LR_Porcess);
  362. Process_Stop(app_Roll_And_Stop_process);
  363. Process_SetHoldOn(app_detect_LR_Porcess,0);
  364. hal_qma_setFrequency(QMA_OFF);
  365. }
  366. }
  367. }
  368. }
  369. }
  370. }
  371. /**
  372. @brief 返回主机标志位
  373. @param 无
  374. @return 主机标志位
  375. */
  376. uint8_t app_Get_isHost(void)
  377. {
  378. return mFlash.isHost;
  379. }
  380. void app_detect_Init(void)
  381. {
  382. SetDeviceNameFlag =1;//开机设置一次蓝牙名字
  383. Process_Start(100,"SetDeviceName",app_SetDeviceName_Porcess);
  384. if(0x55 != mFlash.LR_FLAG){
  385. Mahony_Init(50);
  386. Process_Start(1000,"detect_LR_Init",app_detect_LR_Porcess);
  387. }
  388. if(mFlash.isHost){DEBUG_LOG("======= Left shooe ======= \n");}
  389. else {DEBUG_LOG("======= Right shooe ======= \n");}
  390. DEBUG_LOG("======= mFlash.mClient.isConfig:%d ======= \n",mFlash.mClient.isConfig);
  391. }