app_detectIsHost.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #define DETECT_LR_TIMEOUT 10
  14. static uint8_t SetDeviceNameFlag = 0;
  15. static void app_SetDeviceName_Porcess(void){
  16. static uint8_t state =0;
  17. char buf[16];
  18. memset(buf,0,16);
  19. switch(state){
  20. case 0:
  21. if(1 == SetDeviceNameFlag){
  22. state =1;
  23. }
  24. break;
  25. case 1:
  26. if(host_isconnect()){
  27. host_disconnect();
  28. }
  29. else{
  30. if(slave_isconnect())
  31. slave_disconnect();
  32. else{
  33. advertising_stop();
  34. ST_scan_stop();
  35. state =2;
  36. }
  37. }
  38. break;
  39. case 2:
  40. if(app_Get_isHost()){ //
  41. #if BleNameHoldOn_ENANBLE
  42. slave_set_adv_name((char *) LEFT_NAME,sizeof(LEFT_NAME));
  43. DEBUG_LOG("AdvName(%d):%s\n",sizeof(LEFT_NAME),LEFT_NAME);
  44. host_set_scan_name((char *)RIGHT_NAME,sizeof(RIGHT_NAME));
  45. DEBUG_LOG("scanName(%d):%s\n",sizeof(RIGHT_NAME),RIGHT_NAME);
  46. #else
  47. if(mFlash.mClient.isConfig == 'C'){
  48. 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]);
  49. DEBUG_LOG("scanName(%d):%s\n",strlen(buf),buf);
  50. host_set_scan_name(buf,strlen(buf));
  51. memset(buf,0,sizeof(buf));
  52. sprintf(buf,"SH_%02X%02X",mFlash.macHost[1], mFlash.macHost[0]);
  53. slave_set_adv_name(buf,strlen(buf));
  54. }
  55. #endif
  56. }else{ //
  57. #if BleNameHoldOn_ENANBLE
  58. slave_set_adv_name((char *)RIGHT_NAME,sizeof(RIGHT_NAME));
  59. DEBUG_LOG("AdvName(%d):%s\n",sizeof(RIGHT_NAME),RIGHT_NAME);
  60. #else
  61. if(mFlash.mClient.isConfig=='C'){ //
  62. 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]);
  63. DEBUG_LOG("advName(%d):%s\n",strlen(buf),buf);
  64. slave_set_adv_name(buf,strlen(buf));
  65. }
  66. host_set_scan_name((char *)"12321321312",sizeof("12321321312"));
  67. ST_scan_stop();
  68. #endif
  69. }
  70. slave_adv_init();
  71. advertising_start();
  72. state =0;
  73. SetDeviceNameFlag = 0;
  74. break;
  75. default:state =0;SetDeviceNameFlag = 0;break;
  76. }
  77. }
  78. static void app_detect_LR_Porcess(void)
  79. {
  80. static uint8_t runtime =0;
  81. int ret =-1;
  82. static uint8_t counter = 0;
  83. bll_imu_data_t f_data = {0};
  84. qma_data_t qma_data={0};
  85. static DETECT_LR_e direct_detect_LR = DETECT_LR_INIT;
  86. uint8_t direct_detect_host = 0;
  87. #define Bufflength 6
  88. static int32_t buff[Bufflength]={0};
  89. int32_t LvBobuff[Bufflength]={0};
  90. uint8_t i=0;
  91. int32_t acc_norm =0;
  92. int32_t temp =0;
  93. static uint8_t firtRunflag =0;
  94. //获取中间加速度值
  95. ret = drv_qma_get_acc_data(&qma_data);
  96. if(-1 == ret || (0 == qma_data.acc[0] && 0 == qma_data.acc[1] && 0== qma_data.acc[2]))return;
  97. acc_norm = sqrt(qma_data.acc[0] * qma_data.acc[0] +qma_data.acc[1] * qma_data.acc[1] + qma_data.acc[2] * qma_data.acc[2]);
  98. //获取前脚IMU的值
  99. if(bll_imu_get_data_num(BLL_IMU_DIR_FRONT) >= 0){
  100. if(-1 == bll_imu_get_data(BLL_IMU_DIR_FRONT, 0, &f_data))
  101. return;
  102. }
  103. //第一次启动给所有的BUFF赋值
  104. if( 0 == firtRunflag) {firtRunflag =1;
  105. for(i=0;i< Bufflength;i++){
  106. buff[i]=acc_norm;
  107. }
  108. }
  109. for(i=0;i<(Bufflength-1);i++){
  110. buff[i]=buff[i+1];
  111. }
  112. buff[Bufflength-1] = acc_norm;
  113. //数据排序
  114. memcpy(LvBobuff,buff,sizeof(buff));
  115. for(uint8_t d=0;d<(Bufflength-1);d++){
  116. for(uint8_t b=0;b<(Bufflength-1-d);b++){
  117. if(LvBobuff[b]>LvBobuff[b+1]){
  118. temp=LvBobuff[b+1];
  119. LvBobuff[b+1]=LvBobuff[b];
  120. LvBobuff[b]=temp;
  121. }
  122. }
  123. }
  124. // DEBUG_LOG("LvBobuff:%d,%d\r\n",LvBobuff[Bufflength-1],LvBobuff[0]);
  125. if((LvBobuff[Bufflength-1] - LvBobuff[0]) < 500){
  126. // DEBUG_LOG("f_data.acc[0]:%6d,qma_data.acc[0]:%6d\r\n",f_data.acc[0],qma_data.acc[0]);
  127. Mahony_process(0,0,0,qma_data.acc[0],qma_data.acc[1],qma_data.acc[2],0,0,0);
  128. if(f_data.acc[2] < -1850 && f_data.acc[2] > -2050 ){//平放的时候才判断左右鞋
  129. if(runtime <15){runtime++;return;}
  130. int16_t rol = (int16_t)(getRoll());
  131. // DEBUG_LOG("getRoll:%d\n",rol);
  132. if(abs(rol) >90){//反向
  133. #if _SAME_DIRECTION
  134. if(DETECT_LR_IS_RIGHT == direct_detect_LR)counter++;
  135. else counter =0;
  136. direct_detect_LR = DETECT_LR_IS_RIGHT;
  137. #else
  138. if(DETECT_LR_IS_LEFT == direct_detect_LR)counter++;
  139. else counter =0;
  140. direct_detect_LR = DETECT_LR_IS_LEFT;
  141. #endif
  142. }
  143. else {//同向
  144. #if _SAME_DIRECTION
  145. if(DETECT_LR_IS_LEFT == direct_detect_LR)counter++;
  146. else counter =0;
  147. direct_detect_LR = DETECT_LR_IS_LEFT;
  148. #else
  149. if(DETECT_LR_IS_RIGHT == direct_detect_LR)counter++;
  150. else counter =0;
  151. direct_detect_LR = DETECT_LR_IS_RIGHT;
  152. #endif
  153. }
  154. }
  155. }
  156. else counter =0;
  157. // DEBUG_LOG("counter:%d\n",counter);
  158. if(counter >= DETECT_LR_TIMEOUT){counter = 0;
  159. // DEBUG_LOG("direct_detect_host:%d\n",direct_detect_host);
  160. if(direct_detect_LR != DETECT_LR_INIT && 0x55 != mFlash.LR_FLAG){
  161. if(direct_detect_LR == DETECT_LR_IS_LEFT)
  162. direct_detect_host = 1;
  163. else
  164. direct_detect_host = 0;
  165. mFlash.isHost = direct_detect_host;
  166. mFlash.LR_FLAG = 0x55;
  167. Flash_SaveInfomation();
  168. SetDeviceNameFlag =1;
  169. MT_Run(500);
  170. mBackup.isHost= mFlash.isHost;
  171. mBackup.LR_FLAG = 0x55;
  172. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_FLASH,"app_detect_LR save backup fail");
  173. Process_Stop(app_detect_LR_Porcess);
  174. }
  175. }
  176. }
  177. /**
  178. @brief 返回主机标志位
  179. @param 无
  180. @return 主机标志位
  181. */
  182. uint8_t app_Get_isHost(void)
  183. {
  184. return mFlash.isHost;
  185. }
  186. ////全功率模式
  187. //static const bll_imu_one_way_param_t all_front_param={
  188. // .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //前脚 - 加速度正常模式
  189. // .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //前脚 - 陀螺仪正常模式
  190. // .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //前脚 - 时间戳25US精度
  191. // .timestamp_switch = FML_IMU_TIMESTAMP_ON, //前脚 - 时间戳开启
  192. // .acc_fs = FML_IMU_ACC_FS_16G, //前脚 - 加速度量程 - 16G
  193. // .gry_fs = FML_IMU_GRY_FS_2000DPS, //前脚 - 陀螺仪量程 - 2000DPS
  194. // .mag_fs = FML_IMU_MAG_FS_30GS, //前脚 - 地磁计量程 - 30GS
  195. // .acc_odr = FML_IMU_ACC_ODR_104HZ, //前脚 - 加速度采样频率 - 104HZ
  196. // .gry_odr = FML_IMU_GRY_ODR_104HZ, //前脚 - 陀螺仪采样频率 - 104HZ
  197. // .mag_odr = FML_IMU_MAG_ODR_200HZ, //前脚 - 地磁计采样频率 - 200HZ
  198. // .fifo_odr = FML_IMU_FIFO_ODR_104HZ,
  199. //};
  200. //static const bll_imu_one_way_param_t all_back_param={
  201. // .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //后脚 - 加速度正常模式
  202. // .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //后脚 - 陀螺仪正常模式
  203. // .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //后脚 - 时间戳25US精度
  204. // .timestamp_switch = FML_IMU_TIMESTAMP_OFF, //后脚 - 时间戳关闭
  205. // .acc_fs = FML_IMU_ACC_FS_16G, //后脚 - 加速度量程 - 16G
  206. // .gry_fs = FML_IMU_GRY_FS_2000DPS, //后脚 - 陀螺仪量程 - 2000DPS
  207. // .mag_fs = FML_IMU_MAG_FS_30GS, //后脚 - 地磁计量程 - 30GS
  208. // .acc_odr = FML_IMU_ACC_ODR_OFF, //后脚 - 加速度采样频率 - 关闭
  209. // .gry_odr = FML_IMU_GRY_ODR_OFF, //后脚 - 陀螺仪采样频率 - 关闭
  210. // .mag_odr = FML_IMU_MAG_ODR_200HZ, //后脚 - 地磁计采样频率 - 200HZ
  211. // .fifo_odr = FML_IMU_FIFO_ODR_OFF,
  212. //};
  213. //static const bll_imu_param_t all_bll_imu_param_t={
  214. // .config_param[FML_IMU_DIR_FRONT] = &all_front_param,
  215. // .config_param[FML_IMU_DIR_BACK] = &all_back_param,
  216. //};
  217. //#include "MahonyAHRS.h"
  218. ////IMU数据回调
  219. //static void all_data_notify_cb(uint32_t dir_bit)
  220. //{
  221. // int16_t group_num = 0;
  222. // bll_imu_data_t data={0};
  223. // qma_data_t qma_data={0};
  224. // int ret = drv_qma_get_acc_data(&qma_data);
  225. // if(-1 == ret || (0 == qma_data.acc[0] && 0 == qma_data.acc[1] && 0== qma_data.acc[2]))return;
  226. // if((dir_bit >> BLL_IMU_DIR_FRONT) & 0x01){
  227. // group_num = bll_imu_get_data_num(BLL_IMU_DIR_FRONT);
  228. // for(int i=0;i<group_num;i++){
  229. // bll_imu_get_data(BLL_IMU_DIR_FRONT, i, &data);
  230. // //获取中间加速度值
  231. //// app_send_adc(data.acc,qma_data.acc);
  232. // Mahony_process(0,0,0,qma_data.acc[0],qma_data.acc[1],qma_data.acc[2],0,0,0);
  233. // }
  234. // }
  235. //}
  236. void app_detect_Init(void)
  237. {
  238. SetDeviceNameFlag =1;
  239. Process_Start(100,"app_SetDeviceName_Porcess",app_SetDeviceName_Porcess);
  240. if(0x55 != mFlash.LR_FLAG)Process_Start(1000,"app_detect_LR_Init_Porcess",app_detect_LR_Porcess);
  241. Mahony_Init(10);
  242. // Process_SetHoldOn(app_detect_LR_Porcess,1);
  243. if(mFlash.isHost){
  244. DEBUG_LOG("======= Left shooe ======= \n");
  245. }else{
  246. DEBUG_LOG("======= Right shooe ======= \n");
  247. }
  248. DEBUG_LOG("======= mFlash.mClient.isConfig:%d ======= \n",mFlash.mClient.isConfig);
  249. // Mahony_Init(100);
  250. // Process_SetHoldOn(app_detect_LR_Porcess,1);
  251. // bll_imu_Resume_config_param(&all_bll_imu_param_t);
  252. // bll_imu_register_data_notify_callback(BLL_IMU_DATA_NOTIFY_CB_PRIORITY_1, all_data_notify_cb);
  253. }