app_step.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #include "app_step.h"
  2. #include "nrf_gpio.h"
  3. #include "usr_config.h"
  4. #include "bsp_time.h"
  5. #include "system.h"
  6. #include "hal_mt.h"
  7. #include "app_host.h"
  8. #include "app_charge.h"
  9. #include "hal_ble_client.h"
  10. #include "hal_ble_host.h"
  11. #include "nrf_delay.h"
  12. #include "app_flash.h"
  13. #include "ble_comm.h"
  14. #include "exception.h"
  15. #include "bll_imu.h"
  16. #include "detect_zero_vel.h"
  17. #include "detect_step_by_mag.h"
  18. #include "pdrStatus.h"
  19. #include "hal_wearshoes.h"
  20. #include "app_game.h"
  21. //实时计步模式
  22. static const bll_imu_one_way_param_t realstep_front_param={
  23. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //前脚 - 加速度正常模式
  24. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //前脚 - 陀螺仪正常模式
  25. .fifo_odr = FML_IMU_FIFO_ODR_104HZ, //前脚 --FIFO采集频率
  26. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //前脚 - 时间戳25US精度
  27. .timestamp_switch = FML_IMU_TIMESTAMP_ON, //前脚 - 时间戳开启
  28. .acc_odr = FML_IMU_ACC_ODR_104HZ, //前脚 - 加速度采样频率 - 104HZ
  29. .gry_odr = FML_IMU_GRY_ODR_104HZ, //前脚 - 陀螺仪采样频率 - 104HZ
  30. .mag_odr = FML_IMU_MAG_ODR_200HZ, //前脚 - 地磁计采样频率 - 200HZ
  31. .acc_fs = FML_IMU_ACC_FS_16G, //前脚 - 加速度量程 - 16G
  32. .gry_fs = FML_IMU_GRY_FS_2000DPS, //前脚 - 陀螺仪量程 - 2000DPS
  33. .mag_fs = FML_IMU_MAG_FS_30GS, //前脚 - 地磁计量程 - 30GS
  34. };
  35. static const bll_imu_one_way_param_t realstep_back_param={
  36. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //后脚 - 加速度正常模式
  37. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //后脚 - 陀螺仪正常模式
  38. .fifo_odr = FML_IMU_FIFO_ODR_OFF, //后脚 -- FIFO采集频率
  39. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //后脚 - 时间戳25US精度
  40. .timestamp_switch = FML_IMU_TIMESTAMP_OFF, //后脚 - 时间戳关闭
  41. .acc_odr = FML_IMU_ACC_ODR_OFF, //后脚 - 加速度采样频率 - 关闭
  42. .gry_odr = FML_IMU_GRY_ODR_OFF, //后脚 - 陀螺仪采样频率 - 关闭
  43. .mag_odr = FML_IMU_MAG_ODR_200HZ, //后脚 - 地磁计采样频率 - 200HZ
  44. .acc_fs = FML_IMU_ACC_FS_16G, //后脚 - 加速度量程 - 16G
  45. .gry_fs = FML_IMU_GRY_FS_2000DPS, //后脚 - 陀螺仪量程 - 2000DPS
  46. .mag_fs = FML_IMU_MAG_FS_30GS, //后脚 - 地磁计量程 - 30GS
  47. };
  48. static const bll_imu_param_t realstep_bll_imu_param_t={
  49. .config_param[FML_IMU_DIR_FRONT] = &realstep_front_param,
  50. .config_param[FML_IMU_DIR_BACK] = &realstep_back_param,
  51. };
  52. static uint8_t realStepMode = BLE_REALTIMESTEP_EXIT; //是否为实时计步模式
  53. static uint32_t realStepCur_L = 0; //左鞋步数记录点
  54. static uint32_t realStepAdd_R = 0; //右鞋步数
  55. static uint8_t realStepTimCnt = 0; //右鞋实时计步的心跳
  56. static uint16_t realStepHeartCnt = 0; //实时计步模式心跳计时
  57. static uint8_t RealStepFlag =BLE_REALTIMESTEP_EXIT; //实时计步的信号。
  58. #define IMU_MAX_GROUP_NUM 5
  59. static int16_t acc_front[IMU_MAX_GROUP_NUM][3];
  60. static int16_t mag6310_front[IMU_MAX_GROUP_NUM][3];
  61. static int16_t mag6310_back[3];
  62. /********************** 函数声明区 *************************/
  63. uint32_t app_step_GetStep_L(void)
  64. {
  65. return mFlash.mStep.stepCur[0];
  66. }
  67. uint32_t app_step_GetStep_R(void)
  68. {
  69. return mFlash.mStep.stepCur[1];
  70. }
  71. //右鞋实时计步发送
  72. static void app_step_RealSendProcess(void)
  73. {
  74. if(realStepMode == BLE_REALTIMESTEP_ENTER){
  75. uint8_t buf[8]={0};
  76. uint8_t L = 0;
  77. buf[L++] = BLE_REALTIMESTEP_ENTER;
  78. buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>24);
  79. buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>16);
  80. buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>8);
  81. buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>0);
  82. buf[L++] = (uint8_t)(realStepAdd_R>>24);
  83. buf[L++] = (uint8_t)(realStepAdd_R>>16);
  84. buf[L++] = (uint8_t)(realStepAdd_R>>8);
  85. buf[L++] = (uint8_t)(realStepAdd_R>>0);
  86. // DEBUG_LOG("mFlash.mStep.stepCur[0]:%d,realStepCur_L:%d,realStepAdd_R:%d\n",mFlash.mStep.stepCur[0],realStepCur_L,realStepAdd_R);
  87. BLE_Client_Tx_Send(0,BLE_REALTIMESTEP,buf,L);
  88. }
  89. }
  90. //IMU数据回调
  91. static void real_data_notify_cb(uint32_t dir_bit)
  92. {
  93. int16_t group_num = 0;
  94. bll_imu_data_t data={0};
  95. if(BLE_REALTIMESTEP_ENTER != realStepMode)return;
  96. if((dir_bit >> BLL_IMU_DIR_FRONT) & 0x01)
  97. {
  98. group_num = bll_imu_get_data_num(BLL_IMU_DIR_FRONT);
  99. if(group_num >IMU_MAX_GROUP_NUM)return;
  100. for(int i=0;i<group_num;i++){
  101. bll_imu_get_data(BLL_IMU_DIR_FRONT, i, &data);
  102. acc_front[i][0] = data.acc[0];acc_front[i][1] = data.acc[1];acc_front[i][2] = data.acc[2];
  103. mag6310_front[i][0] = data.mag[0];mag6310_front[i][1] = data.mag[1];mag6310_front[i][2] = data.mag[2];
  104. }
  105. if(bll_imu_get_data_num(BLL_IMU_DIR_BACK) >= 1){
  106. bll_imu_get_data(BLL_IMU_DIR_BACK, 0, &data);
  107. mag6310_back[0] = data.mag[0];mag6310_back[1] = data.mag[1];mag6310_back[2] = data.mag[2];
  108. }
  109. // DEBUG_LOG("======RealTimeStep=====%d\r\n",TIME_GetTicks());
  110. if(RealTimeStep((int16_t*)mag6310_front, (int16_t*)mag6310_back, (int16_t*)acc_front)){
  111. mFlash.mStep.stepCur[0]++;
  112. DEBUG_LOG("======RealTimeStep=====step %d\r\n",mFlash.mStep.stepCur[0]);
  113. }
  114. }
  115. }
  116. uint8_t app_step_Real_Get(void){
  117. if(realStepMode == BLE_REALTIMESTEP_ENTER)return 1;
  118. else return 0;
  119. }
  120. //右鞋实时计步连接管理
  121. void app_step_RealConnectProcess(void)
  122. {
  123. static uint8_t state =0;
  124. static uint8_t imu_configcnt =0;
  125. uint8_t mod =0;
  126. uint8_t front_CS =0,back_CS =0;
  127. switch(state){
  128. case 0:{//空闲
  129. if(RealStepFlag == BLE_REALTIMESTEP_ENTER){
  130. state =1;
  131. imu_configcnt =0;
  132. bll_imu_Resume_config_param(&realstep_bll_imu_param_t);
  133. }else{
  134. if(realStepTimCnt>0){ realStepTimCnt = 0;
  135. BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&RealStepFlag,1);
  136. }
  137. }
  138. break;
  139. }
  140. case 1:{//配置IMU
  141. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&realstep_bll_imu_param_t);
  142. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK, &realstep_bll_imu_param_t);
  143. if(BLL_IMU_CONFIG_FINISH == front_CS && BLL_IMU_CONFIG_FINISH == back_CS){
  144. DEBUG_LOG(">>>>>>into real step mode\n");
  145. state =2;
  146. realStepCur_L = mFlash.mStep.stepCur[0]; //重新计数
  147. realStepAdd_R = 0;
  148. realStepHeartCnt = 0;
  149. realStepMode = BLE_REALTIMESTEP_ENTER;
  150. }
  151. else if(BLL_IMU_CONFIG_DOING != front_CS || BLL_IMU_CONFIG_DOING != back_CS){
  152. bll_imu_Resume_config_param(&realstep_bll_imu_param_t);
  153. imu_configcnt++;
  154. if(imu_configcnt >=20){
  155. bll_imu_Resume_unregister_config_param(&realstep_bll_imu_param_t);
  156. state =0;
  157. RealStepFlag = BLE_REALTIMESTEP_EXIT;
  158. realStepMode = BLE_REALTIMESTEP_EXIT;
  159. }
  160. }
  161. break;
  162. }
  163. case 2://进入实时计步
  164. realStepHeartCnt++;
  165. if(realStepHeartCnt >= 600){realStepHeartCnt=0;
  166. mod = BLE_REALTIMESTEP_EXIT;
  167. BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&mod,1); //退出从机实时步数
  168. bll_imu_Resume_unregister_config_param(&realstep_bll_imu_param_t);
  169. realStepMode = BLE_REALTIMESTEP_EXIT;
  170. state =0;
  171. }else{
  172. if(realStepTimCnt > 0) realStepTimCnt--;
  173. if(realStepTimCnt==0) {
  174. mod = BLE_REALTIMESTEP_ENTER;
  175. BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&mod,1);
  176. }
  177. }
  178. if(RealStepFlag == BLE_REALTIMESTEP_EXIT){
  179. realStepMode = BLE_REALTIMESTEP_EXIT;
  180. bll_imu_Resume_unregister_config_param(&realstep_bll_imu_param_t);
  181. state =0;
  182. }
  183. break;
  184. default:RealStepFlag = BLE_REALTIMESTEP_EXIT;realStepMode = BLE_REALTIMESTEP_EXIT;state =0;break;
  185. }
  186. }
  187. //接收右鞋实时步数并发送到手机
  188. void cb_BLE_Host_R_REALTIMESTEP(void* handle)
  189. {
  190. BLE_Client_Rx_t* target = handle;
  191. if(target->pDat[0]==BLE_REALTIMESTEP_ENTER){
  192. if(realStepMode == BLE_REALTIMESTEP_ENTER){
  193. realStepAdd_R = ((uint32_t)target->pDat[1]<<24) | (uint32_t)(target->pDat[2]<<16) | (uint32_t)(target->pDat[3]<<8) | ((uint32_t)target->pDat[4]<<0);
  194. app_step_RealSendProcess();
  195. // DEBUG_LOG(">>>>>>realStepCur_L:%ld,realStepAdd_R:%ld,\n",realStepCur_L,realStepAdd_R);
  196. }
  197. realStepTimCnt = 3;
  198. }
  199. }
  200. //接收手机命令
  201. void cb_BLE_Client_R_REALTIMESTEP(void* handle)
  202. {
  203. BLE_Client_Rx_t* target = handle;
  204. RealStepFlag = target->pDat[0];
  205. if(RealStepFlag == BLE_REALTIMESTEP_ENTER){
  206. realStepHeartCnt = 0;
  207. bll_imu_Resume_config_param(&realstep_bll_imu_param_t);
  208. }
  209. else{
  210. bll_imu_Resume_unregister_config_param(&realstep_bll_imu_param_t);
  211. }
  212. BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&RealStepFlag,1);
  213. }
  214. //日常计步
  215. static void app_math_DailyStep_Process(void)
  216. {
  217. int16_t acc[3];
  218. int16_t mag6310[3];
  219. int16_t group_num = 0;
  220. bll_imu_data_t data= {0};
  221. if(hal_wearshoes_is_wearshoes() && realStepMode != BLE_REALTIMESTEP_ENTER && !app_game_GetGameMode()){//游戏模式下,APP的SDK会计算脚步
  222. group_num = bll_imu_get_data_num(BLL_IMU_DIR_FRONT);
  223. for(int i=0; i < group_num; i++){
  224. bll_imu_get_data(BLL_IMU_DIR_FRONT, i, &data);
  225. mag6310[0] = data.mag[0];mag6310[1] = data.mag[1];mag6310[2] = data.mag[2];
  226. acc[0] = data.acc[0];acc[1] = data.acc[1];acc[2] = data.acc[2];
  227. // DEBUG_LOG("f_mx=%d\r,f_my=%d\r,f_mz=%d\r\n",mag6310[0],mag6310[1],mag6310[2]);
  228. if(1 == detect_step_by_mag(mag6310,acc[2])){
  229. mFlash.mStep.stepCur[0]++;
  230. DEBUG_LOG("DailyStep current step:%d\r\n",mFlash.mStep.stepCur[0]);
  231. break;
  232. }
  233. }
  234. }
  235. }
  236. void app_step_Init(void)
  237. {
  238. if(0 == mFlash.isHost){
  239. Process_Start(100,"step_RealSend",app_step_RealSendProcess);
  240. }
  241. BLE_Client_Rx_Regist(BLE_REALTIMESTEP,cb_BLE_Client_R_REALTIMESTEP);
  242. Process_Start(100,"step_RealConnect",app_step_RealConnectProcess);
  243. BLE_Host_Rx_Regist(BLE_REALTIMESTEP,cb_BLE_Host_R_REALTIMESTEP);
  244. bll_imu_register_data_notify_callback(BLL_IMU_DATA_NOTIFY_CB_PRIORITY_1, real_data_notify_cb);
  245. Process_Start(0,"app_math_DailyStep_Process",app_math_DailyStep_Process);
  246. }