hal_wearshoes.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #include "hal_wearshoes.h"
  2. #include "bsp_time.h"
  3. #include "system.h"
  4. #include "tool.h"
  5. #include "exception.h"
  6. #include "bll_imu.h"
  7. #include "app_flash.h"
  8. #define HAL_WEARSHOES_TIMEOUT 60000
  9. #define Bufflength 6
  10. static int32_t buff[Bufflength]={0};
  11. static uint8_t refreshFlag =1;
  12. //누糾
  13. static const bll_imu_one_way_param_t shoes_front_param={
  14. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //품신 - 속醵똑攣끽친駕
  15. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //품신 - 顧쭁老攣끽친駕
  16. .fifo_odr = FML_IMU_FIFO_ODR_104HZ, //품신 --FIFO꽃섞틉쪽
  17. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //품신 - 珂쇌늑25US쑹똑
  18. .timestamp_switch = FML_IMU_TIMESTAMP_OFF, //품신 - 珂쇌늑밑균
  19. .acc_odr = FML_IMU_ACC_ODR_104HZ, //품신 - 속醵똑꽃湳틉쪽 - 104HZ
  20. .gry_odr = FML_IMU_GRY_ODR_OFF, //품신 - 顧쭁老꽃湳틉쪽 밑균
  21. .mag_odr = FML_IMU_MAG_ODR_100HZ, //품신 - 뒈늚셕꽃湳틉쪽 - 100HZ
  22. .acc_fs = FML_IMU_ACC_FS_16G, //품신 - 속醵똑좆넋 - 16G
  23. .gry_fs = FML_IMU_GRY_FS_2000DPS, //품신 - 顧쭁老좆넋 - 2000DPS
  24. .mag_fs = FML_IMU_MAG_FS_30GS, //품신 - 뒈늚셕좆넋 - 30GS
  25. };
  26. static const bll_imu_one_way_param_t shoes_back_param={
  27. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //빈신 - 속醵똑攣끽친駕
  28. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //빈신 - 顧쭁老攣끽친駕
  29. .fifo_odr = FML_IMU_FIFO_ODR_OFF, //빈신 --FIFO꽃섞틉쪽
  30. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //빈신 - 珂쇌늑25US쑹똑
  31. .timestamp_switch = FML_IMU_TIMESTAMP_OFF, //빈신 - 珂쇌늑밑균
  32. .acc_odr = FML_IMU_ACC_ODR_OFF, //빈신 - 속醵똑꽃湳틉쪽 - 밑균
  33. .gry_odr = FML_IMU_GRY_ODR_OFF, //빈신 - 顧쭁老꽃湳틉쪽 - 밑균
  34. .mag_odr = FML_IMU_MAG_ODR_OFF, //빈신 - 뒈늚셕꽃湳틉쪽 - 200HZ
  35. .acc_fs = FML_IMU_ACC_FS_16G, //빈신 - 속醵똑좆넋 - 16G
  36. .gry_fs = FML_IMU_GRY_FS_2000DPS, //빈신 - 顧쭁老좆넋 - 2000DPS
  37. .mag_fs = FML_IMU_MAG_FS_30GS, //빈신 - 뒈늚셕좆넋 - 30GS
  38. };
  39. static const bll_imu_param_t shoes_bll_imu_param_t={
  40. .config_param[FML_IMU_DIR_FRONT] = (bll_imu_one_way_param_t *)&shoes_front_param,
  41. .config_param[FML_IMU_DIR_BACK] = (bll_imu_one_way_param_t *)&shoes_back_param,
  42. };
  43. static const fml_imu_param_t noshoes_front_param={
  44. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //품신 - 속醵똑攣끽친駕
  45. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //품신 - 顧쭁老攣끽친駕
  46. .fifo_odr = FML_IMU_FIFO_ODR_OFF, //품신 - FIFO꽃湳틉쪽 - 104HZ
  47. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //품신 - 珂쇌늑25US쑹똑
  48. .timestamp_switch = FML_IMU_TIMESTAMP_OFF, //품신 - 珂쇌늑밑균
  49. .acc_odr = FML_IMU_ACC_ODR_12HZ5, //품신 - 속醵똑꽃湳틉쪽 - 12.5HZ
  50. .gry_odr = FML_IMU_GRY_ODR_OFF, //품신 - 顧쭁老꽃湳틉쪽 - 밑균
  51. .mag_odr = FML_IMU_MAG_ODR_OFF, //품신 - 뒈늚셕꽃湳틉쪽 - 밑균
  52. .acc_fs = FML_IMU_ACC_FS_16G, //품신 - 속醵똑좆넋 - 16G
  53. .gry_fs = FML_IMU_GRY_FS_2000DPS, //품신 - 顧쭁老좆넋 - 2000DPS
  54. .mag_fs = FML_IMU_MAG_FS_30GS, //품신 - 뒈늚셕좆넋 - 30GS
  55. };
  56. static const fml_imu_param_t noshoes_back_param={
  57. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //빈신 - 속醵똑攣끽친駕
  58. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //빈신 - 顧쭁老攣끽친駕
  59. .fifo_odr = FML_IMU_FIFO_ODR_OFF, //빈신 - FIFO꽃湳틉쪽 - 밑균
  60. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //빈신 - 珂쇌늑25US쑹똑
  61. .timestamp_switch = FML_IMU_TIMESTAMP_OFF, //빈신 - 珂쇌늑밑균
  62. .acc_odr = FML_IMU_ACC_ODR_OFF, //빈신 - 속醵똑꽃湳틉쪽 - 밑균
  63. .gry_odr = FML_IMU_GRY_ODR_OFF, //빈신 - 顧쭁老꽃湳틉쪽 - 밑균
  64. .mag_odr = FML_IMU_MAG_ODR_10HZ, //빈신 - 뒈늚셕꽃湳틉쪽 - 10HZ
  65. .acc_fs = FML_IMU_ACC_FS_16G, //빈신 - 속醵똑좆넋 - 16G
  66. .gry_fs = FML_IMU_GRY_FS_2000DPS, //빈신 - 顧쭁老좆넋 - 2000DPS
  67. .mag_fs = FML_IMU_MAG_FS_30GS, //빈신 - 뒈늚셕좆넋 - 30GS
  68. };
  69. static const bll_imu_param_t noshoes_bll_imu_param_t={
  70. .config_param[FML_IMU_DIR_FRONT] = (bll_imu_one_way_param_t *)&noshoes_front_param,
  71. .config_param[FML_IMU_DIR_BACK] = (bll_imu_one_way_param_t *)&noshoes_back_param,
  72. };
  73. enum{
  74. WEARSHOES_INIT=0,
  75. WEARSHOES_NO,
  76. WEARSHOES_YES
  77. };
  78. static uint8_t isWearShoes = WEARSHOES_NO;
  79. static uint8_t IsWearShoes(BLL_IMU_DIR_e dir, int16_t mag[])
  80. {
  81. uint8_t flag = 0;
  82. int32_t mag_norm =0;
  83. int32_t LvBobuff[Bufflength]={0};
  84. int32_t temp =0;
  85. uint8_t i=0;
  86. char buf[256];
  87. static uint32_t tim = 0;
  88. static int32_t min_l_f_mag_norm = 90000;
  89. static int32_t max_l_f_mag_norm = 0;
  90. static int32_t min_r_f_mag_norm = 90000;
  91. static int32_t max_r_f_mag_norm = 0;
  92. static int32_t min_l_b_mag_norm = 90000;
  93. static int32_t max_l_b_mag_norm = 0;
  94. static int32_t min_r_b_mag_norm = 90000;
  95. static int32_t max_r_b_mag_norm = 0;
  96. mag_norm = (int32_t)(sqrt((float) (mag[0] *mag[0] + mag[1] * mag[1] + mag[2] *mag[2])));
  97. if(TIME_GetTicks()-tim>=10000)
  98. {
  99. tim = TIME_GetTicks();
  100. if(dir == BLL_IMU_DIR_FRONT)
  101. {
  102. if(mFlash.isHost)
  103. {
  104. min_l_f_mag_norm = (min_l_f_mag_norm < mag_norm)? min_l_f_mag_norm : mag_norm;
  105. max_l_f_mag_norm = (max_l_f_mag_norm > mag_norm)? max_l_f_mag_norm : mag_norm;
  106. sprintf(buf,"L:front_norm:%d,%d,%d\r\n",min_l_f_mag_norm,mag_norm,max_l_f_mag_norm);
  107. Except_TxError(EXCEPT_EXCEPTION,buf);
  108. }
  109. else
  110. {
  111. min_r_f_mag_norm = (min_r_f_mag_norm < mag_norm)? min_r_f_mag_norm : mag_norm;
  112. max_r_f_mag_norm = (max_r_f_mag_norm > mag_norm)? max_r_f_mag_norm : mag_norm;
  113. sprintf(buf,"R:front_norm:%d,%d,%d\r\n",min_r_f_mag_norm,mag_norm,max_r_f_mag_norm);
  114. Except_TxError(EXCEPT_EXCEPTION,buf);
  115. }
  116. }
  117. else if(dir == BLL_IMU_DIR_BACK)
  118. {
  119. if(mFlash.isHost)
  120. {
  121. min_l_b_mag_norm = (min_l_b_mag_norm < mag_norm)? min_l_b_mag_norm : mag_norm;
  122. max_l_b_mag_norm = (max_l_b_mag_norm > mag_norm)? max_l_b_mag_norm : mag_norm;
  123. sprintf(buf,"L:back_norm:%d,%d,%d\r\n",min_l_b_mag_norm,mag_norm,max_l_b_mag_norm);
  124. Except_TxError(EXCEPT_EXCEPTION,buf);
  125. }
  126. else
  127. {
  128. min_r_b_mag_norm = (min_r_b_mag_norm < mag_norm)? min_r_b_mag_norm : mag_norm;
  129. max_r_b_mag_norm = (max_r_b_mag_norm > mag_norm)? max_r_b_mag_norm : mag_norm;
  130. sprintf(buf,"R:back_norm:%d,%d,%d\r\n",min_r_b_mag_norm,mag_norm,max_r_b_mag_norm);
  131. Except_TxError(EXCEPT_EXCEPTION,buf);
  132. }
  133. }
  134. }
  135. if(1 == refreshFlag){refreshFlag =0;
  136. for(i=0;i< Bufflength;i++){
  137. buff[i]=mag_norm;
  138. }
  139. }else{
  140. for(i=0;i<(Bufflength-1);i++){
  141. buff[i]=buff[i+1];
  142. }
  143. buff[Bufflength-1] = mag_norm;
  144. }
  145. memcpy(LvBobuff,buff,sizeof(buff));
  146. for(uint8_t d=0;d<(Bufflength-1);d++){
  147. for(uint8_t b=0;b<(Bufflength-1-d);b++){
  148. if(LvBobuff[b]>LvBobuff[b+1]){
  149. temp=LvBobuff[b+1];
  150. LvBobuff[b+1]=LvBobuff[b];
  151. LvBobuff[b]=temp;
  152. }
  153. }
  154. }
  155. if((LvBobuff[Bufflength-1] - LvBobuff[0]) >= 1000){
  156. flag = 1;
  157. }
  158. return flag;
  159. }
  160. static uint8_t shoes_signal =WEARSHOES_INIT;
  161. static void hal_wearshoes_determine(void)
  162. {
  163. bll_imu_data_t data={0};
  164. uint8_t front_CS =0,back_CS =0;
  165. static uint32_t noshoestim = 0;
  166. static BLL_IMU_DIR_e BLL_IMU_Set =BLL_IMU_DIR_NUM;
  167. //朞嶝품신뻘角빈신돨늚제셕
  168. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&shoes_bll_imu_param_t);
  169. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&shoes_bll_imu_param_t);
  170. if(BLL_IMU_CONFIG_FINISH == front_CS && BLL_IMU_CONFIG_FINISH == back_CS){
  171. if(BLL_IMU_Set != BLL_IMU_DIR_FRONT){
  172. refreshFlag = 1;
  173. }
  174. BLL_IMU_Set =BLL_IMU_DIR_FRONT;
  175. }
  176. else{
  177. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&noshoes_bll_imu_param_t);
  178. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&noshoes_bll_imu_param_t);
  179. if(front_CS == BLL_IMU_CONFIG_FINISH && back_CS == BLL_IMU_CONFIG_FINISH){
  180. if(BLL_IMU_Set != BLL_IMU_DIR_BACK){
  181. refreshFlag = 1;
  182. }
  183. BLL_IMU_Set =BLL_IMU_DIR_BACK;
  184. }
  185. else {
  186. BLL_IMU_Set =BLL_IMU_DIR_NUM;
  187. return;
  188. }
  189. }
  190. int number = bll_imu_get_data_num(BLL_IMU_Set);
  191. if(number >= 1){
  192. int8_t result = bll_imu_get_data(BLL_IMU_Set, 0, &data);
  193. if(0 == data.mag[0] && 0 == data.mag[1] && 0 == data.mag[2])
  194. {
  195. DEBUG_LOG("!!!------->>>>>>>>>>>>>>>value:%d,%d,%d\r\n",number,result,BLL_IMU_Set);
  196. DEBUG_LOG("!!!------->>>>>>>>>>>>>>>mag_norm:%d,%d,%d\r\n",data.mag[0],data.mag[1],data.mag[2]);
  197. DEBUG_LOG("!!!------->>>>>>>>>>>>>>>acc_norm:%d,%d,%d\r\n",data.acc[0],data.acc[1],data.acc[2]);
  198. DEBUG_LOG("!!!------->>>>>>>>>>>>>>>gry_norm:%d,%d,%d\r\n",data.gry[0],data.gry[1],data.gry[2]);
  199. DEBUG_LOG("!!!------->>>>>>>>>>>>>>>fifo_timestamp:%d\r\n",data.fifo_timestamp);
  200. return;//늚제셕鑒앴끓�돨鑒앴궐속醵똑돨鑒앴찹,杰鹿삔唐寧몸珂쇌뀌
  201. }
  202. if(1 == IsWearShoes(BLL_IMU_Set,data.mag)){
  203. noshoestim = TIME_GetTicks();
  204. if(WEARSHOES_NO == isWearShoes && shoes_signal == WEARSHOES_INIT){
  205. shoes_signal = WEARSHOES_YES;
  206. }
  207. }else if(WEARSHOES_YES == isWearShoes) {
  208. if(shoes_signal == WEARSHOES_INIT && (TIME_GetTicks() -noshoestim >= HAL_WEARSHOES_TIMEOUT)) {
  209. shoes_signal = WEARSHOES_NO;
  210. }
  211. }
  212. }
  213. }
  214. static void hal_wearshoes_Process(void)
  215. {
  216. static uint8_t imu_configcnt =0;
  217. uint8_t front_CS =0,back_CS =0;
  218. static uint8_t errorsetFlag =0;
  219. switch(isWearShoes){
  220. case WEARSHOES_NO:{//꼇누糾
  221. if(WEARSHOES_INIT == shoes_signal){
  222. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&noshoes_bll_imu_param_t);
  223. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&noshoes_bll_imu_param_t);
  224. if(BLL_IMU_CONFIG_FINISH == front_CS && BLL_IMU_CONFIG_FINISH == back_CS){
  225. hal_wearshoes_determine();
  226. if(1 == errorsetFlag){errorsetFlag =0;
  227. Process_Start(100,"hal_wearshoes_Process",hal_wearshoes_Process);
  228. }
  229. }
  230. else if(BLL_IMU_CONFIG_DOING != front_CS || BLL_IMU_CONFIG_DOING != back_CS){
  231. DEBUG_LOG("hal_wearshoes_Process no shoes\r\n");
  232. bll_imu_Resume_unregister_config_param(&shoes_bll_imu_param_t);
  233. bll_imu_Resume_config_param(&noshoes_bll_imu_param_t);
  234. }
  235. }
  236. else {
  237. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&shoes_bll_imu_param_t);
  238. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&shoes_bll_imu_param_t);
  239. if(BLL_IMU_CONFIG_FINISH == front_CS && BLL_IMU_CONFIG_FINISH == back_CS){
  240. isWearShoes = WEARSHOES_YES;
  241. DEBUG_LOG("!!!------->>>>>I am WearShoes\r\n");
  242. shoes_signal =WEARSHOES_INIT;
  243. }
  244. else if(BLL_IMU_CONFIG_DOING != front_CS || BLL_IMU_CONFIG_DOING != back_CS){
  245. bll_imu_Resume_unregister_config_param(&noshoes_bll_imu_param_t);
  246. bll_imu_Resume_config_param(&shoes_bll_imu_param_t);
  247. DEBUG_LOG("hal_wearshoes_Process shoes\r\n");
  248. imu_configcnt++;
  249. if(imu_configcnt > 100){
  250. imu_configcnt =0;
  251. if(errorsetFlag ==0){errorsetFlag =1;
  252. Process_Start(10000,"hal_wearshoes_Process",hal_wearshoes_Process);
  253. }
  254. }
  255. }
  256. }
  257. }
  258. break;
  259. case WEARSHOES_YES:{//누糾榴檄
  260. if(WEARSHOES_INIT == shoes_signal){
  261. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&shoes_bll_imu_param_t);
  262. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&shoes_bll_imu_param_t);
  263. if(BLL_IMU_CONFIG_FINISH == front_CS && BLL_IMU_CONFIG_FINISH == back_CS){
  264. hal_wearshoes_determine();
  265. if(1 == errorsetFlag){errorsetFlag =0;
  266. Process_Start(100,"hal_wearshoes_Process",hal_wearshoes_Process);
  267. }
  268. }
  269. else if(BLL_IMU_CONFIG_DOING != front_CS || BLL_IMU_CONFIG_DOING != back_CS){
  270. bll_imu_Resume_unregister_config_param(&noshoes_bll_imu_param_t);
  271. bll_imu_Resume_config_param(&shoes_bll_imu_param_t);
  272. imu_configcnt++;
  273. if(imu_configcnt > 100){
  274. imu_configcnt =0;
  275. }
  276. if(errorsetFlag ==0){errorsetFlag =1;
  277. Process_Start(10000,"hal_wearshoes_Process",hal_wearshoes_Process);
  278. }
  279. }
  280. }
  281. else{
  282. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&noshoes_bll_imu_param_t);
  283. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&noshoes_bll_imu_param_t);
  284. if(BLL_IMU_CONFIG_FINISH == front_CS && BLL_IMU_CONFIG_FINISH == back_CS){
  285. isWearShoes = WEARSHOES_NO;
  286. DEBUG_LOG("I am not WearShoes\r\n");
  287. shoes_signal =WEARSHOES_INIT;
  288. }
  289. else if(BLL_IMU_CONFIG_DOING != front_CS || BLL_IMU_CONFIG_DOING != back_CS){
  290. bll_imu_Resume_unregister_config_param(&shoes_bll_imu_param_t);
  291. bll_imu_Resume_config_param(&noshoes_bll_imu_param_t);
  292. imu_configcnt++;
  293. if(imu_configcnt > 100){
  294. imu_configcnt =0;
  295. }
  296. }
  297. }
  298. }
  299. break;
  300. default :isWearShoes = WEARSHOES_NO;imu_configcnt=0;break;
  301. }
  302. }
  303. uint8_t hal_wearshoes_is_wearshoes(void)
  304. {
  305. if(WEARSHOES_YES == isWearShoes)return 1;
  306. else return 0;
  307. }
  308. void hal_wearshoes_Init(void)
  309. {
  310. if(isWearShoes == WEARSHOES_NO){
  311. bll_imu_Resume_config_param(&noshoes_bll_imu_param_t);
  312. }else if(isWearShoes == WEARSHOES_YES){
  313. bll_imu_Resume_config_param(&shoes_bll_imu_param_t);
  314. }
  315. Process_Start(100,"hal_wearshoes_Process",hal_wearshoes_Process);
  316. }