app_detectIsHost.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. #define Bufflength 6
  87. static int32_t buff[Bufflength]={0};
  88. static uint8_t firtRunflag =0;
  89. int32_t Buff_Max =0,Buff_Min =0;
  90. uint8_t i=0;
  91. int32_t acc_norm =0;
  92. //获取中间加速度值
  93. ret = drv_qma_get_acc_data(&qma_data);
  94. if(-1 == ret || (0 == qma_data.acc[0] && 0 == qma_data.acc[1] && 0== qma_data.acc[2]))return;
  95. //获取前脚角速度值的值
  96. if(bll_imu_get_data_num(BLL_IMU_DIR_FRONT) >= 0){
  97. if(-1 == bll_imu_get_data(BLL_IMU_DIR_FRONT, 0, &f_data))
  98. return;
  99. }
  100. 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]);
  101. //第一次启动给所有的BUFF赋值
  102. if( 0 == firtRunflag) {firtRunflag =1;
  103. for(i=0;i< Bufflength;i++){
  104. buff[i]=acc_norm;
  105. }
  106. }
  107. for(i=0;i<(Bufflength-1);i++){
  108. buff[i]=buff[i+1];
  109. }
  110. buff[Bufflength-1] = acc_norm;
  111. //找出最大、最小值
  112. Buff_Min = buff[0];
  113. Buff_Max = buff[0];
  114. for(i=0;i<(Bufflength-1);i++){
  115. if(Buff_Max <= buff[i]){
  116. Buff_Max = buff[i];
  117. }
  118. if(Buff_Min >= buff[i]){
  119. Buff_Min = buff[i];
  120. }
  121. }
  122. // DEBUG_LOG("f_data.acc:%d,%d,%d\r\n",f_data.acc[0],f_data.acc[1],f_data.acc[2]);
  123. //
  124. // DEBUG_LOG("buff:%d,%d,%d,%d,%d,%d\r\n",buff[0],buff[1],buff[2],buff[3],buff[4],buff[5]);
  125. // DEBUG_LOG("acc_norm:%d,offest:%d\r\n",acc_norm,(Buff_Max-Buff_Min));
  126. if((Buff_Max-Buff_Min) < 100){
  127. // DEBUG_LOG("f_data.acc[0]:%6d,qma_data.acc[0]:%6d\r\n",f_data.acc[0],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] > -2250 ){//平放的时候才判断左右鞋
  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 {
  158. if(0 != counter){
  159. for(i=0;i<(Bufflength-1);i++){
  160. buff[i] = Buff_Min;
  161. }
  162. counter =0;
  163. }
  164. }
  165. if(counter >= DETECT_LR_TIMEOUT){counter = 0;
  166. DEBUG_LOG("direct_detect_host:%d\n",direct_detect_LR);
  167. if(direct_detect_LR != DETECT_LR_INIT && 0x55 != mFlash.LR_FLAG){
  168. if(direct_detect_LR == DETECT_LR_IS_LEFT)
  169. mFlash.isHost = 1;
  170. else
  171. mFlash.isHost = 0;
  172. mFlash.LR_FLAG =0x55;
  173. Flash_SaveInfomation();
  174. SetDeviceNameFlag =1;
  175. MT_Run(500);
  176. mBackup.isHost= mFlash.isHost;
  177. mBackup.LR_FLAG = 0x55;
  178. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_FLASH,"app_detect_LR save backup fail");
  179. Process_Stop(app_detect_LR_Porcess);
  180. }
  181. }
  182. }
  183. /**
  184. @brief 返回主机标志位
  185. @param 无
  186. @return 主机标志位
  187. */
  188. uint8_t app_Get_isHost(void)
  189. {
  190. return mFlash.isHost;
  191. }
  192. ////全功率模式
  193. //static const bll_imu_one_way_param_t all_front_param={
  194. // .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //前脚 - 加速度正常模式
  195. // .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //前脚 - 陀螺仪正常模式
  196. // .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //前脚 - 时间戳25US精度
  197. // .timestamp_switch = FML_IMU_TIMESTAMP_ON, //前脚 - 时间戳开启
  198. // .acc_fs = FML_IMU_ACC_FS_16G, //前脚 - 加速度量程 - 16G
  199. // .gry_fs = FML_IMU_GRY_FS_2000DPS, //前脚 - 陀螺仪量程 - 2000DPS
  200. // .mag_fs = FML_IMU_MAG_FS_30GS, //前脚 - 地磁计量程 - 30GS
  201. // .acc_odr = FML_IMU_ACC_ODR_104HZ, //前脚 - 加速度采样频率 - 104HZ
  202. // .gry_odr = FML_IMU_GRY_ODR_104HZ, //前脚 - 陀螺仪采样频率 - 104HZ
  203. // .mag_odr = FML_IMU_MAG_ODR_200HZ, //前脚 - 地磁计采样频率 - 200HZ
  204. // .fifo_odr = FML_IMU_FIFO_ODR_104HZ,
  205. //};
  206. //static const bll_imu_one_way_param_t all_back_param={
  207. // .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //后脚 - 加速度正常模式
  208. // .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //后脚 - 陀螺仪正常模式
  209. // .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //后脚 - 时间戳25US精度
  210. // .timestamp_switch = FML_IMU_TIMESTAMP_OFF, //后脚 - 时间戳关闭
  211. // .acc_fs = FML_IMU_ACC_FS_16G, //后脚 - 加速度量程 - 16G
  212. // .gry_fs = FML_IMU_GRY_FS_2000DPS, //后脚 - 陀螺仪量程 - 2000DPS
  213. // .mag_fs = FML_IMU_MAG_FS_30GS, //后脚 - 地磁计量程 - 30GS
  214. // .acc_odr = FML_IMU_ACC_ODR_OFF, //后脚 - 加速度采样频率 - 关闭
  215. // .gry_odr = FML_IMU_GRY_ODR_OFF, //后脚 - 陀螺仪采样频率 - 关闭
  216. // .mag_odr = FML_IMU_MAG_ODR_200HZ, //后脚 - 地磁计采样频率 - 200HZ
  217. // .fifo_odr = FML_IMU_FIFO_ODR_OFF,
  218. //};
  219. //static const bll_imu_param_t all_bll_imu_param_t={
  220. // .config_param[FML_IMU_DIR_FRONT] = &all_front_param,
  221. // .config_param[FML_IMU_DIR_BACK] = &all_back_param,
  222. //};
  223. //#include "MahonyAHRS.h"
  224. ////IMU数据回调
  225. //static void all_data_notify_cb(uint32_t dir_bit)
  226. //{
  227. // int16_t group_num = 0;
  228. // bll_imu_data_t data={0};
  229. // qma_data_t qma_data={0};
  230. // int ret = drv_qma_get_acc_data(&qma_data);
  231. // if(-1 == ret || (0 == qma_data.acc[0] && 0 == qma_data.acc[1] && 0== qma_data.acc[2]))return;
  232. // if((dir_bit >> BLL_IMU_DIR_FRONT) & 0x01){
  233. // group_num = bll_imu_get_data_num(BLL_IMU_DIR_FRONT);
  234. // for(int i=0;i<group_num;i++){
  235. // bll_imu_get_data(BLL_IMU_DIR_FRONT, i, &data);
  236. // //获取中间加速度值
  237. //// app_send_adc(data.acc,qma_data.acc);
  238. // Mahony_process(0,0,0,qma_data.acc[0],qma_data.acc[1],qma_data.acc[2],0,0,0);
  239. // }
  240. // }
  241. //}
  242. void app_detect_Init(void)
  243. {
  244. SetDeviceNameFlag =1;
  245. Process_Start(100,"app_SetDeviceName_Porcess",app_SetDeviceName_Porcess);
  246. if(0x55 != mFlash.LR_FLAG)Process_Start(1000,"app_detect_LR_Init_Porcess",app_detect_LR_Porcess);
  247. Mahony_Init(10);
  248. // Process_SetHoldOn(app_detect_LR_Porcess,1);
  249. if(mFlash.isHost){
  250. DEBUG_LOG("======= Left shooe ======= \n");
  251. }else{
  252. DEBUG_LOG("======= Right shooe ======= \n");
  253. }
  254. DEBUG_LOG("======= mFlash.mClient.isConfig:%d ======= \n",mFlash.mClient.isConfig);
  255. // Mahony_Init(100);
  256. // Process_SetHoldOn(app_detect_LR_Porcess,1);
  257. // bll_imu_Resume_config_param(&all_bll_imu_param_t);
  258. // bll_imu_register_data_notify_callback(BLL_IMU_DATA_NOTIFY_CB_PRIORITY_1, all_data_notify_cb);
  259. }