app_detectIsHost - 副本 (2).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 DETECT_LR_e ob_detect_LR = DETECT_LR_INIT;
  15. static uint8_t SetDeviceNameFlag = 0;
  16. static void app_SetDeviceName_Porcess(void){
  17. static uint8_t state =0;
  18. char buf[16];
  19. memset(buf,0,16);
  20. switch(state){
  21. case 0:
  22. if(1 == SetDeviceNameFlag){
  23. state =1;
  24. }
  25. break;
  26. case 1:
  27. if(host_isconnect()){
  28. host_disconnect();
  29. }
  30. else{
  31. if(slave_isconnect())
  32. slave_disconnect();
  33. else{
  34. advertising_stop();
  35. ST_scan_stop();
  36. state =2;
  37. }
  38. }
  39. break;
  40. case 2:
  41. if(app_Get_isHost()){ //
  42. #if BleNameHoldOn_ENANBLE
  43. slave_set_adv_name((char *) LEFT_NAME,sizeof(LEFT_NAME));
  44. DEBUG_LOG("AdvName(%d):%s\n",sizeof(LEFT_NAME),LEFT_NAME);
  45. host_set_scan_name((char *)RIGHT_NAME,sizeof(RIGHT_NAME));
  46. DEBUG_LOG("scanName(%d):%s\n",sizeof(RIGHT_NAME),RIGHT_NAME);
  47. #else
  48. if(mFlash.mClient.isConfig == 'C'){
  49. 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]);
  50. DEBUG_LOG("scanName(%d):%s\n",strlen(buf),buf);
  51. host_set_scan_name(buf,strlen(buf));
  52. memset(buf,0,sizeof(buf));
  53. sprintf(buf,"SH_%02X%02X",mFlash.macHost[1], mFlash.macHost[0]);
  54. slave_set_adv_name(buf,strlen(buf));
  55. }
  56. #endif
  57. }else{ //
  58. #if BleNameHoldOn_ENANBLE
  59. slave_set_adv_name((char *)RIGHT_NAME,sizeof(RIGHT_NAME));
  60. DEBUG_LOG("AdvName(%d):%s\n",sizeof(RIGHT_NAME),RIGHT_NAME);
  61. #else
  62. if(mFlash.mClient.isConfig=='C'){ //
  63. 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]);
  64. DEBUG_LOG("advName(%d):%s\n",strlen(buf),buf);
  65. slave_set_adv_name(buf,strlen(buf));
  66. }
  67. host_set_scan_name((char *)"12321321312",sizeof("12321321312"));
  68. ST_scan_stop();
  69. #endif
  70. }
  71. slave_adv_init();
  72. advertising_start();
  73. state =0;
  74. SetDeviceNameFlag = 0;
  75. break;
  76. default:state =0;SetDeviceNameFlag = 0;break;
  77. }
  78. }
  79. static void app_detect_LR_Porcess(void)
  80. {
  81. static uint8_t runtime =0;
  82. int ret =-1;
  83. static uint8_t counter = 0;
  84. bll_imu_data_t f_data = {0};
  85. qma_data_t qma_data={0};
  86. static DETECT_LR_e direct_detect_LR = DETECT_LR_INIT;
  87. uint8_t direct_detect_host = 0;
  88. #define Bufflength 6
  89. static int32_t buff[Bufflength]={0};
  90. int32_t LvBobuff[Bufflength]={0};
  91. uint8_t i=0;
  92. int32_t acc_norm =0;
  93. int32_t temp =0;
  94. static uint8_t firtRunflag =0;
  95. //获取中间加速度值
  96. ret = drv_qma_get_acc_data(&qma_data);
  97. if(-1 == ret || (0 == qma_data.acc[0] && 0 == qma_data.acc[1] && 0== qma_data.acc[2]))return;
  98. 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]);
  99. //获取前脚IMU的值
  100. if(bll_imu_get_data_num(BLL_IMU_DIR_FRONT) >= 0){
  101. if(-1 == bll_imu_get_data(BLL_IMU_DIR_FRONT, 0, &f_data))
  102. return;
  103. }
  104. //第一次启动给所有的BUFF赋值
  105. if( 0 == firtRunflag) {firtRunflag =1;
  106. for(i=0;i< Bufflength;i++){
  107. buff[i]=acc_norm;
  108. }
  109. }
  110. for(i=0;i<(Bufflength-1);i++){
  111. buff[i]=buff[i+1];
  112. }
  113. buff[Bufflength-1] = acc_norm;
  114. //数据排序
  115. memcpy(LvBobuff,buff,sizeof(buff));
  116. for(uint8_t d=0;d<(Bufflength-1);d++){
  117. for(uint8_t b=0;b<(Bufflength-1-d);b++){
  118. if(LvBobuff[b]>LvBobuff[b+1]){
  119. temp=LvBobuff[b+1];
  120. LvBobuff[b+1]=LvBobuff[b];
  121. LvBobuff[b]=temp;
  122. }
  123. }
  124. }
  125. // DEBUG_LOG("LvBobuff:%d,%d\r\n",LvBobuff[Bufflength-1],LvBobuff[0]);
  126. if((LvBobuff[Bufflength-1] - LvBobuff[0]) < 500){
  127. // DEBUG_LOG("f_data.acc[2]:%6d,qma_data.acc[0]:%6d\r\n",f_data.acc[2],qma_data.acc[0]);
  128. Mahony_process(0,0,0,qma_data.acc[0],qma_data.acc[1],qma_data.acc[2],0,0,0);
  129. // if(f_data.acc[2] < -1850 && f_data.acc[2] > -2200 ){//平放的时候才判断左右鞋
  130. if(runtime <15){runtime++;return;}
  131. int16_t rol = (int16_t)(getRoll());
  132. // DEBUG_LOG("getRoll:%d\n",rol);
  133. if(abs(rol) > 90){//反向
  134. #if _SAME_DIRECTION
  135. if(DETECT_LR_IS_RIGHT == direct_detect_LR)counter++;
  136. else counter =0;
  137. direct_detect_LR = DETECT_LR_IS_RIGHT;
  138. #else
  139. if(DETECT_LR_IS_LEFT == direct_detect_LR)counter++;
  140. else counter =0;
  141. direct_detect_LR = DETECT_LR_IS_LEFT;
  142. #endif
  143. }
  144. else {//同向
  145. #if _SAME_DIRECTION
  146. if(DETECT_LR_IS_LEFT == direct_detect_LR)counter++;
  147. else counter =0;
  148. direct_detect_LR = DETECT_LR_IS_LEFT;
  149. #else
  150. if(DETECT_LR_IS_RIGHT == direct_detect_LR)counter++;
  151. else counter =0;
  152. direct_detect_LR = DETECT_LR_IS_RIGHT;
  153. #endif
  154. }
  155. // }
  156. }
  157. else counter =0;
  158. if(counter >= DETECT_LR_TIMEOUT){counter = 0;
  159. // DEBUG_LOG("direct_detect_host:%d\n",direct_detect_host);
  160. if(direct_detect_LR != ob_detect_LR){ob_detect_LR = direct_detect_LR;
  161. if(ob_detect_LR == DETECT_LR_IS_LEFT)
  162. direct_detect_host = 1;
  163. else
  164. direct_detect_host = 0;
  165. if(direct_detect_host != mFlash.isHost){
  166. mFlash.isHost = direct_detect_host;
  167. Flash_SaveInfomation();
  168. SetDeviceNameFlag =1;
  169. MT_Run(500);
  170. mBackup.isHost= mFlash.isHost;
  171. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_FLASH,"app_detect_LR save backup fail");
  172. }
  173. direct_detect_LR = DETECT_LR_INIT;
  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. 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. }