hal_wearshoes.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. mag_norm = (int32_t)(sqrt((float) (mag[0] *mag[0] + mag[1] * mag[1] + mag[2] *mag[2])));
  87. if(1 == refreshFlag){refreshFlag =0;
  88. for(i=0;i< Bufflength;i++){
  89. buff[i]=mag_norm;
  90. }
  91. }else{
  92. for(i=0;i<(Bufflength-1);i++){
  93. buff[i]=buff[i+1];
  94. }
  95. buff[Bufflength-1] = mag_norm;
  96. }
  97. memcpy(LvBobuff,buff,sizeof(buff));
  98. for(uint8_t d=0;d<(Bufflength-1);d++){
  99. for(uint8_t b=0;b<(Bufflength-1-d);b++){
  100. if(LvBobuff[b]>LvBobuff[b+1]){
  101. temp=LvBobuff[b+1];
  102. LvBobuff[b+1]=LvBobuff[b];
  103. LvBobuff[b]=temp;
  104. }
  105. }
  106. }
  107. if((LvBobuff[Bufflength-1] - LvBobuff[0]) >= 1000){
  108. flag = 1;
  109. }
  110. return flag;
  111. }
  112. static uint8_t shoes_signal =WEARSHOES_INIT;
  113. static void hal_wearshoes_determine(void)
  114. {
  115. bll_imu_data_t data={0};
  116. uint8_t front_CS =0,back_CS =0;
  117. static uint32_t noshoestim = 0;
  118. static BLL_IMU_DIR_e BLL_IMU_Set =BLL_IMU_DIR_NUM;
  119. //朞嶝품신뻘角빈신돨늚제셕
  120. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&shoes_bll_imu_param_t);
  121. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&shoes_bll_imu_param_t);
  122. if(BLL_IMU_CONFIG_FINISH == front_CS && BLL_IMU_CONFIG_FINISH == back_CS){
  123. if(BLL_IMU_Set != BLL_IMU_DIR_FRONT){
  124. refreshFlag = 1;
  125. }
  126. BLL_IMU_Set =BLL_IMU_DIR_FRONT;
  127. }
  128. else{
  129. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&noshoes_bll_imu_param_t);
  130. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&noshoes_bll_imu_param_t);
  131. if(front_CS == BLL_IMU_CONFIG_FINISH && back_CS == BLL_IMU_CONFIG_FINISH){
  132. if(BLL_IMU_Set != BLL_IMU_DIR_BACK){
  133. refreshFlag = 1;
  134. }
  135. BLL_IMU_Set =BLL_IMU_DIR_BACK;
  136. }
  137. else {
  138. BLL_IMU_Set =BLL_IMU_DIR_NUM;
  139. return;
  140. }
  141. }
  142. int number = bll_imu_get_data_num(BLL_IMU_Set);
  143. if(number >= 1){
  144. int8_t result = bll_imu_get_data(BLL_IMU_Set, 0, &data);
  145. if(0 == data.mag[0] && 0 == data.mag[1] && 0 == data.mag[2])
  146. {
  147. DEBUG_LOG("!!!------->>>>>>>>>>>>>>>value:%d,%d,%d\r\n",number,result,BLL_IMU_Set);
  148. DEBUG_LOG("!!!------->>>>>>>>>>>>>>>mag_norm:%d,%d,%d\r\n",data.mag[0],data.mag[1],data.mag[2]);
  149. DEBUG_LOG("!!!------->>>>>>>>>>>>>>>acc_norm:%d,%d,%d\r\n",data.acc[0],data.acc[1],data.acc[2]);
  150. DEBUG_LOG("!!!------->>>>>>>>>>>>>>>gry_norm:%d,%d,%d\r\n",data.gry[0],data.gry[1],data.gry[2]);
  151. DEBUG_LOG("!!!------->>>>>>>>>>>>>>>fifo_timestamp:%d\r\n",data.fifo_timestamp);
  152. return;//늚제셕鑒앴끓�돨鑒앴궐속醵똑돨鑒앴찹,杰鹿삔唐寧몸珂쇌뀌
  153. }
  154. if(1 == IsWearShoes(BLL_IMU_Set,data.mag)){
  155. noshoestim = TIME_GetTicks();
  156. if(WEARSHOES_NO == isWearShoes && shoes_signal == WEARSHOES_INIT){
  157. shoes_signal = WEARSHOES_YES;
  158. }
  159. }else if(WEARSHOES_YES == isWearShoes) {
  160. if(shoes_signal == WEARSHOES_INIT && (TIME_GetTicks() -noshoestim >= HAL_WEARSHOES_TIMEOUT)) {
  161. shoes_signal = WEARSHOES_NO;
  162. }
  163. }
  164. }
  165. }
  166. static void hal_wearshoes_Process(void)
  167. {
  168. static uint8_t imu_configcnt =0;
  169. uint8_t front_CS =0,back_CS =0;
  170. static uint8_t errorsetFlag =0;
  171. switch(isWearShoes){
  172. case WEARSHOES_NO:{//꼇누糾
  173. if(WEARSHOES_INIT == shoes_signal){
  174. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&noshoes_bll_imu_param_t);
  175. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&noshoes_bll_imu_param_t);
  176. if(BLL_IMU_CONFIG_FINISH == front_CS && BLL_IMU_CONFIG_FINISH == back_CS){
  177. hal_wearshoes_determine();
  178. if(1 == errorsetFlag){errorsetFlag =0;
  179. Process_Start(100,"wearshoes",hal_wearshoes_Process);
  180. }
  181. }
  182. else if(BLL_IMU_CONFIG_DOING != front_CS || BLL_IMU_CONFIG_DOING != back_CS){
  183. DEBUG_LOG("hal_wearshoes_Process no shoes\r\n");
  184. bll_imu_Resume_unregister_config_param(&shoes_bll_imu_param_t);
  185. bll_imu_Resume_config_param(&noshoes_bll_imu_param_t);
  186. }
  187. }
  188. else {
  189. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&shoes_bll_imu_param_t);
  190. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&shoes_bll_imu_param_t);
  191. if(BLL_IMU_CONFIG_FINISH == front_CS && BLL_IMU_CONFIG_FINISH == back_CS){
  192. isWearShoes = WEARSHOES_YES;
  193. DEBUG_LOG("!!!------->>>>>I am WearShoes\r\n");
  194. shoes_signal =WEARSHOES_INIT;
  195. }
  196. else if(BLL_IMU_CONFIG_DOING != front_CS || BLL_IMU_CONFIG_DOING != back_CS){
  197. bll_imu_Resume_unregister_config_param(&noshoes_bll_imu_param_t);
  198. bll_imu_Resume_config_param(&shoes_bll_imu_param_t);
  199. DEBUG_LOG("hal_wearshoes_Process shoes\r\n");
  200. imu_configcnt++;
  201. if(imu_configcnt > 100){
  202. imu_configcnt =0;
  203. if(errorsetFlag ==0){errorsetFlag =1;
  204. Process_Start(10000,"wearshoes",hal_wearshoes_Process);
  205. }
  206. }
  207. }
  208. }
  209. }
  210. break;
  211. case WEARSHOES_YES:{//누糾榴檄
  212. if(WEARSHOES_INIT == shoes_signal){
  213. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&shoes_bll_imu_param_t);
  214. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&shoes_bll_imu_param_t);
  215. if(BLL_IMU_CONFIG_FINISH == front_CS && BLL_IMU_CONFIG_FINISH == back_CS){
  216. hal_wearshoes_determine();
  217. if(1 == errorsetFlag){errorsetFlag =0;
  218. Process_Start(100,"wearshoes",hal_wearshoes_Process);
  219. }
  220. }
  221. else if(BLL_IMU_CONFIG_DOING != front_CS || BLL_IMU_CONFIG_DOING != back_CS){
  222. bll_imu_Resume_unregister_config_param(&noshoes_bll_imu_param_t);
  223. bll_imu_Resume_config_param(&shoes_bll_imu_param_t);
  224. imu_configcnt++;
  225. if(imu_configcnt > 100){
  226. imu_configcnt =0;
  227. }
  228. if(errorsetFlag ==0){errorsetFlag =1;
  229. Process_Start(10000,"wearshoes",hal_wearshoes_Process);
  230. }
  231. }
  232. }
  233. else{
  234. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&noshoes_bll_imu_param_t);
  235. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&noshoes_bll_imu_param_t);
  236. if(BLL_IMU_CONFIG_FINISH == front_CS && BLL_IMU_CONFIG_FINISH == back_CS){
  237. isWearShoes = WEARSHOES_NO;
  238. DEBUG_LOG("I am not WearShoes\r\n");
  239. shoes_signal =WEARSHOES_INIT;
  240. }
  241. else if(BLL_IMU_CONFIG_DOING != front_CS || BLL_IMU_CONFIG_DOING != back_CS){
  242. bll_imu_Resume_unregister_config_param(&shoes_bll_imu_param_t);
  243. bll_imu_Resume_config_param(&noshoes_bll_imu_param_t);
  244. imu_configcnt++;
  245. if(imu_configcnt > 100){
  246. imu_configcnt =0;
  247. }
  248. }
  249. }
  250. }
  251. break;
  252. default :isWearShoes = WEARSHOES_NO;imu_configcnt=0;break;
  253. }
  254. }
  255. uint8_t hal_wearshoes_is_wearshoes(void)
  256. {
  257. if(WEARSHOES_YES == isWearShoes)return 1;
  258. else return 0;
  259. }
  260. void hal_wearshoes_Init(void)
  261. {
  262. if(isWearShoes == WEARSHOES_NO){
  263. bll_imu_Resume_config_param(&noshoes_bll_imu_param_t);
  264. }else if(isWearShoes == WEARSHOES_YES){
  265. bll_imu_Resume_config_param(&shoes_bll_imu_param_t);
  266. }
  267. Process_Start(100,"wearshoes",hal_wearshoes_Process);
  268. }