app_step.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. //发送实时左右鞋步数到CLIENT端
  72. static void app_step_RealSendClient()
  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. //右鞋实时计步发送
  91. static void app_step_RealSendProcess()
  92. {
  93. if(mFlash.isHost)return;
  94. app_step_RealSendClient();
  95. }
  96. //IMU数据回调
  97. static void real_data_notify_cb(uint32_t dir_bit)
  98. {
  99. int16_t group_num = 0;
  100. bll_imu_data_t data={0};
  101. if(BLE_REALTIMESTEP_ENTER != realStepMode)return;
  102. if((dir_bit >> BLL_IMU_DIR_FRONT) & 0x01)
  103. {
  104. group_num = bll_imu_get_data_num(BLL_IMU_DIR_FRONT);
  105. if(group_num >IMU_MAX_GROUP_NUM)return;
  106. for(int i=0;i<group_num;i++){
  107. bll_imu_get_data(BLL_IMU_DIR_FRONT, i, &data);
  108. acc_front[i][0] = data.acc[0];acc_front[i][1] = data.acc[1];acc_front[i][2] = data.acc[2];
  109. mag6310_front[i][0] = data.mag[0];mag6310_front[i][1] = data.mag[1];mag6310_front[i][2] = data.mag[2];
  110. }
  111. if(bll_imu_get_data_num(BLL_IMU_DIR_BACK) >= 1){
  112. bll_imu_get_data(BLL_IMU_DIR_BACK, 0, &data);
  113. mag6310_back[0] = data.mag[0];mag6310_back[1] = data.mag[1];mag6310_back[2] = data.mag[2];
  114. }
  115. // DEBUG_LOG("======RealTimeStep=====%d\r\n",TIME_GetTicks());
  116. if(RealTimeStep((int16_t*)mag6310_front, (int16_t*)mag6310_back, (int16_t*)acc_front)){
  117. mFlash.mStep.stepCur[0]++;
  118. DEBUG_LOG("======RealTimeStep=====step %d\r\n",mFlash.mStep.stepCur[0]);
  119. }
  120. }
  121. }
  122. uint8_t app_step_Real_Get(void){
  123. if(realStepMode == BLE_REALTIMESTEP_ENTER)return 1;
  124. else return 0;
  125. }
  126. //右鞋实时计步连接管理
  127. void app_step_RealConnectProcess(void)
  128. {
  129. static uint8_t state =0;
  130. static uint8_t imu_configcnt =0;
  131. uint8_t mod =0;
  132. uint8_t front_CS =0,back_CS =0;
  133. switch(state){
  134. case 0:{//空闲
  135. if(RealStepFlag == BLE_REALTIMESTEP_ENTER){
  136. state =1;
  137. imu_configcnt =0;
  138. bll_imu_Resume_config_param(&realstep_bll_imu_param_t);
  139. }else{
  140. if(realStepTimCnt>0){ realStepTimCnt = 0;
  141. BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&RealStepFlag,1);
  142. }
  143. }
  144. break;
  145. }
  146. case 1:{//配置IMU
  147. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&realstep_bll_imu_param_t);
  148. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK, &realstep_bll_imu_param_t);
  149. if(BLL_IMU_CONFIG_FINISH == front_CS && BLL_IMU_CONFIG_FINISH == back_CS){
  150. DEBUG_LOG(">>>>>>into real step mode\n");
  151. state =2;
  152. realStepCur_L = mFlash.mStep.stepCur[0]; //重新计数
  153. realStepAdd_R = 0;
  154. realStepHeartCnt = 0;
  155. realStepMode = BLE_REALTIMESTEP_ENTER;
  156. }
  157. else if(BLL_IMU_CONFIG_DOING != front_CS || BLL_IMU_CONFIG_DOING != back_CS){
  158. bll_imu_Resume_config_param(&realstep_bll_imu_param_t);
  159. imu_configcnt++;
  160. if(imu_configcnt >=20){
  161. bll_imu_Resume_unregister_config_param(&realstep_bll_imu_param_t);
  162. state =0;
  163. RealStepFlag = BLE_REALTIMESTEP_EXIT;
  164. realStepMode = BLE_REALTIMESTEP_EXIT;
  165. }
  166. }
  167. break;
  168. }
  169. case 2://进入实时计步
  170. realStepHeartCnt++;
  171. if(realStepHeartCnt >= 600){realStepHeartCnt=0;
  172. mod = BLE_REALTIMESTEP_EXIT;
  173. BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&mod,1); //退出从机实时步数
  174. bll_imu_Resume_unregister_config_param(&realstep_bll_imu_param_t);
  175. realStepMode = BLE_REALTIMESTEP_EXIT;
  176. state =0;
  177. }else{
  178. if(realStepTimCnt > 0) realStepTimCnt--;
  179. if(realStepTimCnt==0) {
  180. mod = BLE_REALTIMESTEP_ENTER;
  181. BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&mod,1);
  182. }
  183. }
  184. if(RealStepFlag == BLE_REALTIMESTEP_EXIT){
  185. realStepMode = BLE_REALTIMESTEP_EXIT;
  186. bll_imu_Resume_unregister_config_param(&realstep_bll_imu_param_t);
  187. state =0;
  188. }
  189. break;
  190. default:RealStepFlag = BLE_REALTIMESTEP_EXIT;realStepMode = BLE_REALTIMESTEP_EXIT;state =0;break;
  191. }
  192. }
  193. //接收右鞋实时步数并发送到手机
  194. void cb_BLE_Host_R_REALTIMESTEP(void* handle)
  195. {
  196. BLE_Client_Rx_t* target = handle;
  197. if(target->pDat[0]==BLE_REALTIMESTEP_ENTER){
  198. if(realStepMode == BLE_REALTIMESTEP_ENTER){
  199. 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);
  200. app_step_RealSendClient();
  201. DEBUG_LOG(">>>>>>realStepCur_L:%ld,realStepAdd_R:%ld,\n",realStepCur_L,realStepAdd_R);
  202. }
  203. realStepTimCnt = 3;
  204. }
  205. }
  206. //接收手机命令
  207. void cb_BLE_Client_R_REALTIMESTEP(void* handle)
  208. {
  209. BLE_Client_Rx_t* target = handle;
  210. RealStepFlag = target->pDat[0];
  211. if(RealStepFlag == BLE_REALTIMESTEP_ENTER){
  212. realStepHeartCnt = 0;
  213. bll_imu_Resume_config_param(&realstep_bll_imu_param_t);
  214. }
  215. else{
  216. bll_imu_Resume_unregister_config_param(&realstep_bll_imu_param_t);
  217. }
  218. BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&RealStepFlag,1);
  219. }
  220. //日常计步
  221. static void app_math_DailyStep_Process(void)
  222. {
  223. int16_t acc[3];
  224. int16_t mag6310[3];
  225. int16_t group_num = 0;
  226. bll_imu_data_t data= {0};
  227. if(hal_wearshoes_is_wearshoes() && realStepMode != BLE_REALTIMESTEP_ENTER && !app_game_GetGameMode()){//游戏模式下,APP的SDK会计算脚步
  228. group_num = bll_imu_get_data_num(BLL_IMU_DIR_FRONT);
  229. for(int i=0; i < group_num; i++){
  230. bll_imu_get_data(BLL_IMU_DIR_FRONT, i, &data);
  231. mag6310[0] = data.mag[0];mag6310[1] = data.mag[1];mag6310[2] = data.mag[2];
  232. acc[0] = data.acc[0];acc[1] = data.acc[1];acc[2] = data.acc[2];
  233. // DEBUG_LOG("f_mx=%d\r,f_my=%d\r,f_mz=%d\r\n",mag6310[0],mag6310[1],mag6310[2]);
  234. if(1 == detect_step_by_mag(mag6310,acc[2])){
  235. mFlash.mStep.stepCur[0]++;
  236. DEBUG_LOG("DailyStep current step:%d\r\n",mFlash.mStep.stepCur[0]);
  237. break;
  238. }
  239. }
  240. }
  241. }
  242. void app_step_Init(void)
  243. {
  244. Process_Start(100,"step_RealSend",app_step_RealSendProcess);
  245. BLE_Client_Rx_Regist(BLE_REALTIMESTEP,cb_BLE_Client_R_REALTIMESTEP);
  246. Process_Start(100,"step_RealConnect",app_step_RealConnectProcess);
  247. BLE_Host_Rx_Regist(BLE_REALTIMESTEP,cb_BLE_Host_R_REALTIMESTEP);
  248. bll_imu_register_data_notify_callback(BLL_IMU_DATA_NOTIFY_CB_PRIORITY_1, real_data_notify_cb);
  249. Process_Start(0,"math_DailyStep",app_math_DailyStep_Process);
  250. }