app_detectIsHost.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. #define DETECT_LR_TIMEOUT 20
  15. //全功率模式
  16. static const bll_imu_one_way_param_t all_front_param={
  17. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //前脚 - 加速度正常模式
  18. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //前脚 - 陀螺仪正常模式
  19. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //前脚 - 时间戳25US精度
  20. .timestamp_switch = FML_IMU_TIMESTAMP_ON, //前脚 - 时间戳开启
  21. .acc_fs = FML_IMU_ACC_FS_16G, //前脚 - 加速度量程 - 16G
  22. .gry_fs = FML_IMU_GRY_FS_2000DPS, //前脚 - 陀螺仪量程 - 2000DPS
  23. .mag_fs = FML_IMU_MAG_FS_30GS, //前脚 - 地磁计量程 - 30GS
  24. .acc_odr = FML_IMU_ACC_ODR_104HZ, //前脚 - 加速度采样频率 - 104HZ
  25. .gry_odr = FML_IMU_GRY_ODR_104HZ, //前脚 - 陀螺仪采样频率 - 104HZ
  26. .mag_odr = FML_IMU_MAG_ODR_200HZ, //前脚 - 地磁计采样频率 - 200HZ
  27. .fifo_odr = FML_IMU_FIFO_ODR_104HZ,
  28. };
  29. static const bll_imu_one_way_param_t all_back_param={
  30. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //后脚 - 加速度正常模式
  31. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //后脚 - 陀螺仪正常模式
  32. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //后脚 - 时间戳25US精度
  33. .timestamp_switch = FML_IMU_TIMESTAMP_OFF, //后脚 - 时间戳关闭
  34. .acc_fs = FML_IMU_ACC_FS_16G, //后脚 - 加速度量程 - 16G
  35. .gry_fs = FML_IMU_GRY_FS_2000DPS, //后脚 - 陀螺仪量程 - 2000DPS
  36. .mag_fs = FML_IMU_MAG_FS_30GS, //后脚 - 地磁计量程 - 30GS
  37. .acc_odr = FML_IMU_ACC_ODR_OFF, //后脚 - 加速度采样频率 - 关闭
  38. .gry_odr = FML_IMU_GRY_ODR_OFF, //后脚 - 陀螺仪采样频率 - 关闭
  39. .mag_odr = FML_IMU_MAG_ODR_OFF, //后脚 - 地磁计采样频率 - 200HZ
  40. .fifo_odr = FML_IMU_FIFO_ODR_OFF,
  41. };
  42. static const bll_imu_param_t all_bll_imu_param_t={
  43. .config_param[FML_IMU_DIR_FRONT] = &all_front_param,
  44. .config_param[FML_IMU_DIR_BACK] = &all_back_param,
  45. };
  46. static uint8_t SetDeviceNameFlag = 0;
  47. static void app_SetDeviceName_Porcess(void){
  48. static uint8_t state =0;
  49. char buf[16];
  50. memset(buf,0,16);
  51. switch(state){
  52. case 0:
  53. if(1 == SetDeviceNameFlag){state =1;}
  54. break;
  55. case 1:
  56. if(host_isconnect()){host_disconnect();}
  57. else{
  58. if(slave_isconnect()) slave_disconnect();
  59. else{
  60. advertising_stop();
  61. ST_scan_stop();
  62. state =2;
  63. }
  64. }
  65. break;
  66. case 2:
  67. if(app_Get_isHost()){ //
  68. #if BleNameHoldOn_ENANBLE
  69. slave_set_adv_name((char *) LEFT_NAME,sizeof(LEFT_NAME));
  70. DEBUG_LOG("AdvName(%d):%s\n",sizeof(LEFT_NAME),LEFT_NAME);
  71. host_set_scan_name((char *)RIGHT_NAME,sizeof(RIGHT_NAME));
  72. DEBUG_LOG("scanName(%d):%s\n",sizeof(RIGHT_NAME),RIGHT_NAME);
  73. #else
  74. if(mFlash.mClient.isConfig == 'C'){
  75. 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]);
  76. DEBUG_LOG("scanName(%d):%s\n",strlen(buf),buf);
  77. host_set_scan_name(buf,strlen(buf));
  78. memset(buf,0,sizeof(buf));
  79. sprintf(buf,"SH_%02X%02X",mFlash.macHost[1], mFlash.macHost[0]);
  80. slave_set_adv_name(buf,strlen(buf));
  81. }
  82. #endif
  83. }else{
  84. #if BleNameHoldOn_ENANBLE
  85. slave_set_adv_name((char *)RIGHT_NAME,sizeof(RIGHT_NAME));
  86. DEBUG_LOG("AdvName(%d):%s\n",sizeof(RIGHT_NAME),RIGHT_NAME);
  87. #else
  88. if(mFlash.mClient.isConfig=='C'){ //
  89. 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]);
  90. DEBUG_LOG("advName(%d):%s\n",strlen(buf),buf);
  91. slave_set_adv_name(buf,strlen(buf));
  92. }
  93. host_set_scan_name((char *)"12321321312",sizeof("12321321312"));
  94. ST_scan_stop();
  95. #endif
  96. }
  97. slave_adv_init();
  98. advertising_start();
  99. state =0;
  100. SetDeviceNameFlag = 0;
  101. break;
  102. default:state =0;SetDeviceNameFlag = 0;break;
  103. }
  104. }
  105. //判断鞋子是否为静止
  106. //返回值 0
  107. static uint8_t app_shoes_still(bll_imu_data_t f_data){
  108. #define Bufflength 6
  109. static int32_t buff[Bufflength]={0};
  110. static uint8_t firtRunflag =0;
  111. int32_t Buff_Max =0,Buff_Min =0;
  112. uint8_t i=0;
  113. int32_t acc_norm =0;
  114. 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]);
  115. //第一次启动给所有的BUFF赋值
  116. if(firtRunflag < Bufflength){firtRunflag++;
  117. buff[firtRunflag]=acc_norm;
  118. return 0;
  119. }
  120. for(i=0;i<(Bufflength-1);i++){
  121. buff[i]=buff[i+1];
  122. }
  123. buff[Bufflength-1] = acc_norm;
  124. //找出最大、最小值
  125. Buff_Min = buff[0];
  126. Buff_Max = buff[0];
  127. for(i=0;i<(Bufflength-1);i++){
  128. if(Buff_Max <= buff[i]){
  129. Buff_Max = buff[i];
  130. }
  131. if(Buff_Min >= buff[i]){
  132. Buff_Min = buff[i];
  133. }
  134. }
  135. if((Buff_Max-Buff_Min) < 200)return 1;
  136. else return 0;
  137. }
  138. static uint8_t app_get_Front_data(bll_imu_data_t *f_data){
  139. uint8_t front_CS =0,back_CS =0;
  140. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&all_bll_imu_param_t);
  141. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&all_bll_imu_param_t);
  142. // DEBUG_LOG("front_CS:%d back_CS:%d\r\n",front_CS,back_CS);
  143. if(BLL_IMU_CONFIG_FINISH == front_CS && BLL_IMU_CONFIG_FINISH == back_CS){
  144. if(bll_imu_get_data_num(BLL_IMU_DIR_FRONT) >= 0){
  145. if(-1 == bll_imu_get_data(BLL_IMU_DIR_FRONT, 0, f_data))return 0;
  146. else return 1;
  147. }
  148. else return 0;
  149. }
  150. else if(BLL_IMU_CONFIG_DOING != front_CS || BLL_IMU_CONFIG_DOING != back_CS){
  151. bll_imu_Resume_config_param(&all_bll_imu_param_t);
  152. return 0 ;
  153. }
  154. else return 0;
  155. }
  156. //计算中间加速度Roll和是否静止的检测
  157. static uint8_t shoes_StopFlag =0;//鞋子静止标志位
  158. static void app_Roll_And_Stop_process(void){
  159. int ret =-1;
  160. qma_data_t qma_data={0};
  161. bll_imu_data_t f_data={0};
  162. //获取中间加速度值
  163. ret = drv_qma_get_acc_data(&qma_data);
  164. if(-1 == ret || (0 == qma_data.acc[0] && 0 == qma_data.acc[1] && 0== qma_data.acc[2]))return;
  165. Mahony_process(0,0,0,qma_data.acc[0],qma_data.acc[1],qma_data.acc[2],0,0,0);
  166. //50ms采集一次前脚加速度并用于判断鞋子是否静止
  167. static uint8_t getAccCnt =0;
  168. if(getAccCnt++ >= 4){getAccCnt =0;
  169. if(!app_get_Front_data(&f_data))return;
  170. if(app_shoes_still(f_data))shoes_StopFlag =1;
  171. else shoes_StopFlag =0;
  172. }
  173. }
  174. //判断前后脚磁力计是否存在
  175. uint8_t app_mag_still(void){
  176. return 1;
  177. }
  178. //static void app_SendAcc_Roll_charge(int16_t rol,int32_t mag_norm,int8_t shoes_stillcnt,int8_t result)
  179. //{
  180. // uint8_t sbuf[60]={0};
  181. // uint8_t L = 0;
  182. // sbuf[L++] = (uint8_t)(rol>>8);
  183. // sbuf[L++] = (uint8_t)(rol>>0);
  184. // sbuf[L++] = (uint8_t)(mag_norm>>24);
  185. // sbuf[L++] = (uint8_t)(mag_norm>>16);
  186. // sbuf[L++] = (uint8_t)(mag_norm>>8);
  187. // sbuf[L++] = (uint8_t)(mag_norm>>0);
  188. // sbuf[L++] = shoes_stillcnt;
  189. // sbuf[L++] = result;
  190. // L=sizeof(sbuf);
  191. // BLE_Client_Tx_Send(0,0x04,sbuf,L);
  192. //}
  193. static void app_detect_LR_Porcess(void)
  194. {
  195. static uint8_t StateHoldTime = 0;
  196. bll_imu_data_t f_data={0};
  197. static uint8_t charge_state =0;
  198. static DETECT_LR_e direct_detect_LR = DETECT_LR_INIT;
  199. //是否充电
  200. if((hal_charge_Getstate() != BLE_CHARGE_PULLOUT)){
  201. if(0 == charge_state){charge_state =1;
  202. DEBUG_LOG("app_detect_roll charge_state 0\n");
  203. Process_Start(10,"Roll_And_Stop",app_Roll_And_Stop_process);
  204. Process_Start(10,"detect_LR",app_detect_LR_Porcess);
  205. Process_SetHoldOn(app_detect_LR_Porcess,1);
  206. bll_imu_Resume_config_param(&all_bll_imu_param_t);
  207. StateHoldTime = 0;
  208. return;
  209. }
  210. }else{
  211. if(1 == charge_state){charge_state =0;
  212. Process_UpdatePeroid(app_Roll_And_Stop_process,1000);
  213. Process_Stop(app_Roll_And_Stop_process);
  214. Process_SetHoldOn(app_detect_LR_Porcess,0);
  215. StateHoldTime = 0;
  216. bll_imu_Resume_unregister_config_param(&all_bll_imu_param_t);
  217. DEBUG_LOG("app_detect_LR_Porcess charge_state 1\n");
  218. }
  219. return ;
  220. }
  221. //获取前脚角速度值的值
  222. if(!app_get_Front_data(&f_data))return;
  223. int16_t rol = (int16_t)(getRoll());
  224. if(shoes_StopFlag && app_mag_still() && (hal_charge_Getstate() != BLE_CHARGE_PULLOUT) && f_data.acc[2] < -1850 && f_data.acc[2] > -2250){
  225. if(abs(rol) >90){//反向
  226. #if _SAME_DIRECTION
  227. if(DETECT_LR_IS_RIGHT == direct_detect_LR)StateHoldTime++;
  228. else StateHoldTime =0;
  229. direct_detect_LR = DETECT_LR_IS_RIGHT;
  230. #else
  231. if(DETECT_LR_IS_LEFT == direct_detect_LR)counter++;
  232. else counter =0;
  233. direct_detect_LR = DETECT_LR_IS_LEFT;
  234. #endif
  235. }
  236. else {//同向
  237. #if _SAME_DIRECTION
  238. if(DETECT_LR_IS_LEFT == direct_detect_LR)StateHoldTime++;
  239. else StateHoldTime =0;
  240. direct_detect_LR = DETECT_LR_IS_LEFT;
  241. #else
  242. if(DETECT_LR_IS_RIGHT == direct_detect_LR)counter++;
  243. else counter =0;
  244. direct_detect_LR = DETECT_LR_IS_RIGHT;
  245. #endif
  246. }
  247. }
  248. else StateHoldTime =0;
  249. if(StateHoldTime >= DETECT_LR_TIMEOUT){StateHoldTime = 0;
  250. DEBUG_LOG("direct_detect_host:%d\n",direct_detect_LR);
  251. if(direct_detect_LR != DETECT_LR_INIT && 0x55 != mFlash.LR_FLAG){
  252. if(direct_detect_LR == DETECT_LR_IS_LEFT)
  253. mFlash.isHost = 1;
  254. else
  255. mFlash.isHost = 0;
  256. mFlash.LR_FLAG = 0x55;
  257. Flash_SaveInfomation();
  258. SetDeviceNameFlag =1;
  259. MT_Run(500);
  260. mBackup.isHost= mFlash.isHost;
  261. mBackup.LR_FLAG = mFlash.LR_FLAG;
  262. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_FLASH,"app_detect_LR save backup fail");
  263. bll_imu_Resume_unregister_config_param(&all_bll_imu_param_t);
  264. Process_Stop(app_detect_LR_Porcess);
  265. Process_Stop(app_Roll_And_Stop_process);
  266. Process_SetHoldOn(app_detect_LR_Porcess,0);
  267. }
  268. }
  269. // int32_t 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]);
  270. // app_SendAcc_Roll_charge(rol,acc_norm,shoes_StopFlag,result);
  271. }
  272. /**
  273. @brief 返回主机标志位
  274. @param 无
  275. @return 主机标志位
  276. */
  277. uint8_t app_Get_isHost(void)
  278. {
  279. return mFlash.isHost;
  280. }
  281. void app_detect_Init(void)
  282. {
  283. SetDeviceNameFlag =1;//开机设置一次蓝牙名字
  284. Process_Start(100,"SetDeviceName",app_SetDeviceName_Porcess);
  285. if(0x55 != mFlash.LR_FLAG){
  286. Mahony_Init(100);
  287. Process_Start(1000,"detect_LR_Init",app_detect_LR_Porcess);
  288. }
  289. if(mFlash.isHost){DEBUG_LOG("======= Left shooe ======= \n");}
  290. else {DEBUG_LOG("======= Right shooe ======= \n");}
  291. DEBUG_LOG("======= mFlash.mClient.isConfig:%d ======= \n",mFlash.mClient.isConfig);
  292. }