app_step.c 9.9 KB

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