hal_wearshoes.c 12 KB

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