bll_imu.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*Includes ----------------------------------------------*/
  2. #include "tool.h"
  3. #include "nrf_delay.h"
  4. #include "bsp_time.h"
  5. #include "exception.h"
  6. #include "system.h"
  7. #include "bll_imu.h"
  8. /*Private macro ------------------------------------------------------------------------------------------------------------------------------------*/
  9. #define BLL_IMU_DATA_GROUP_NUM_MAX 12 //IMU能存储的最大数据组数
  10. #define BLL_IMU_REGISTER_CONFIG_PARAM_MAX 12 //IMU能存储的最大数据组数
  11. #define BLL_IMU_REGISTER_DATA_NOTIFY_MAX 5 //IMU能数据通知回调的最大数量
  12. #define BLL_IMU_GET_MAX(a,b) (((a) > (b)) ? (a) : (b)) //对比大小宏
  13. /*STRUCTION ------------------------------------------------------------------------------------------------------------------------------------*/
  14. typedef struct bll_imu_data_notify
  15. {
  16. uint8_t priority; //优先级
  17. bll_imu_data_notify_cb cb; //回调函数
  18. }Bll_Imu_Data_Notify_t;
  19. typedef struct bll_imu
  20. {
  21. /*private member*/
  22. const bll_imu_param_t *register_config_param[BLL_IMU_REGISTER_CONFIG_PARAM_MAX]; //已注册的IMU配置参数地址
  23. Bll_Imu_Data_Notify_t register_data_notify[BLL_IMU_REGISTER_DATA_NOTIFY_MAX]; //已注册的IMU数据通知回调
  24. bll_imu_one_way_param_t highest_priority_config_param[BLL_IMU_DIR_NUM]; //最高优先级配置IMU参数
  25. BLL_IMU_CONFIG_RESULT config_result; //配置结果
  26. } Bll_Imu_t;
  27. /*Local Variable ------------------------------------------------------------------------------------------------------------------------------------*/
  28. static Bll_Imu_t ob_bll_imu;
  29. /*Local Functions ------------------------------------------------------------------------------------------------------------------------------------*/
  30. static void bll_imu_register_config_cb(uint32_t conf_result)
  31. {
  32. if(conf_result == FML_IMU_CONFIG_FLOW_DONE) //配置成功
  33. {
  34. ob_bll_imu.config_result = (BLL_IMU_CONFIG_FINISH);
  35. }
  36. else //配置失败
  37. {
  38. if(conf_result >= 0x0D && conf_result <= 0x11)
  39. {
  40. ob_bll_imu.config_result = (BLL_IMU_CONFIG_FAIL_FRONT_MAG); //前脚mag配置失败
  41. }
  42. else if(conf_result >= 0x1B && conf_result <= 0x1E)
  43. {
  44. ob_bll_imu.config_result = (BLL_IMU_CONFIG_FAIL_BACK_MAG); //后脚mag配置失败
  45. }
  46. else
  47. {
  48. ob_bll_imu.config_result = (BLL_IMU_CONFIG_FAIL_FRONT_SIX_AXIS); //前脚lsm配置失败
  49. }
  50. }
  51. }
  52. static void bll_imu_register_data_notify_cb(uint32_t dir_bit)
  53. {
  54. //从最高优先级开始筛选
  55. for(int i=BLL_IMU_DATA_NOTIFY_CB_PRIORITY_NUM - 1; i >= 0; i--)
  56. {
  57. for(int j=0; j<BLL_IMU_REGISTER_DATA_NOTIFY_MAX;j++)
  58. {
  59. if(ob_bll_imu.register_data_notify[j].priority == i && ob_bll_imu.register_data_notify[j].cb != NULL)
  60. {
  61. ob_bll_imu.register_data_notify[j].cb(dir_bit);
  62. }
  63. }
  64. }
  65. }
  66. static int bll_imu_get_highest_priority_config_param(bll_imu_one_way_param_t out_param[], const bll_imu_one_way_param_t int_param[])
  67. {
  68. if(out_param == NULL || int_param == NULL)return -1;
  69. for(uint8_t i=0; i<BLL_IMU_DIR_NUM; i++){
  70. out_param[i].acc_fs = BLL_IMU_GET_MAX(out_param[i].acc_fs, int_param[i].acc_fs);
  71. out_param[i].acc_odr = BLL_IMU_GET_MAX(out_param[i].acc_odr, int_param[i].acc_odr);
  72. out_param[i].acc_power_mode = BLL_IMU_GET_MAX(out_param[i].acc_power_mode,int_param[i].acc_power_mode);
  73. out_param[i].fifo_odr = BLL_IMU_GET_MAX(out_param[i].fifo_odr,int_param[i].fifo_odr);
  74. out_param[i].gry_fs = BLL_IMU_GET_MAX(out_param[i].gry_fs,int_param[i].gry_fs);
  75. out_param[i].gry_odr = BLL_IMU_GET_MAX(out_param[i].gry_odr,int_param[i].gry_odr);
  76. out_param[i].gry_power_mode = BLL_IMU_GET_MAX(out_param[i].gry_power_mode,int_param[i].gry_power_mode);
  77. out_param[i].mag_fs = BLL_IMU_GET_MAX(out_param[i].mag_fs,int_param[i].mag_fs);
  78. out_param[i].mag_odr = BLL_IMU_GET_MAX(out_param[i].mag_odr,int_param[i].mag_odr);
  79. out_param[i].timestamp_resolution = BLL_IMU_GET_MAX(out_param[i].timestamp_resolution,int_param[i].timestamp_resolution);
  80. out_param[i].timestamp_switch = BLL_IMU_GET_MAX(out_param[i].timestamp_switch,int_param[i].timestamp_switch);
  81. }
  82. return 0;
  83. }
  84. static int bll_imu_is_equal(bll_imu_one_way_param_t *param_1, bll_imu_one_way_param_t *param_2)
  85. {
  86. int diff = 0;
  87. if(param_1->acc_fs != param_2->acc_fs)diff++;
  88. if(param_1->acc_odr != param_2->acc_odr)diff++;
  89. if(param_1->acc_power_mode != param_2->acc_power_mode)diff++;
  90. if(param_1->fifo_odr != param_2->fifo_odr)diff++;
  91. if(param_1->gry_fs != param_2->gry_fs)diff++;
  92. if(param_1->gry_odr != param_2->gry_odr)diff++;
  93. if(param_1->gry_power_mode != param_2->gry_power_mode)diff++;
  94. if(param_1->mag_fs != param_2->mag_fs)diff++;
  95. if(param_1->mag_odr != param_2->mag_odr)diff++;
  96. if(param_1->timestamp_resolution != param_2->timestamp_resolution)diff++;
  97. if(param_1->timestamp_switch != param_2->timestamp_switch)diff++;
  98. return diff;
  99. }
  100. /*API ------------------------------------------------------------------------------------------------------------------------------------*/
  101. /**
  102. @brief 初始化IMU业务逻辑层
  103. @param 无
  104. @return 错误代码 - [out] -1失败,0成功
  105. */
  106. int bll_imu_Init(void)
  107. {
  108. int ret = 0;
  109. //初始化imu服务
  110. if(fml_imu_Init() == -1)ret = -1;
  111. //重置模式管理服务结构体
  112. memset(&ob_bll_imu, 0, sizeof(ob_bll_imu));
  113. //默认配置完成
  114. ob_bll_imu.config_result = (BLL_IMU_CONFIG_FINISH);
  115. if(ret == -1)return -1;
  116. return 0;
  117. }
  118. /**
  119. @brief 注册IMU要进行配置的参数
  120. @param param - [in] IMU要进行配置的参数地址
  121. @return 错误代码 - [out] 0失败,成功返回参数地址
  122. */
  123. const bll_imu_param_t* bll_imu_register_config_param(const bll_imu_param_t *param)
  124. {
  125. if(param == NULL)return 0;
  126. //若已存在,则返回该指针的地址作为句柄返回
  127. for(int i=0; i<BLL_IMU_REGISTER_CONFIG_PARAM_MAX; i++)
  128. {
  129. if(ob_bll_imu.register_config_param[i] == param)return param;
  130. }
  131. //若不存在,则存储起来,且将该指针的地址作为句柄返回
  132. for(int i=0; i<BLL_IMU_REGISTER_CONFIG_PARAM_MAX; i++)
  133. {
  134. if(ob_bll_imu.register_config_param[i] == 0)
  135. {
  136. ob_bll_imu.register_config_param[i] = param;
  137. return param;
  138. }
  139. }
  140. return 0;
  141. }
  142. /**
  143. @brief 注册IMU要进行配置的参数,并重新设置IMU
  144. @param param - [in] IMU要进行配置的参数地址
  145. @return 错误代码 - [out] 0失败,成功返回参数地址
  146. */
  147. const bll_imu_param_t* bll_imu_Resume_config_param(const bll_imu_param_t *param)
  148. {
  149. if(param == NULL)return 0;
  150. //若已存在,则返回该指针的地址作为句柄返回
  151. for(int i=0; i<BLL_IMU_REGISTER_CONFIG_PARAM_MAX; i++)
  152. {
  153. if(ob_bll_imu.register_config_param[i] == param){
  154. bll_imu_start_config();
  155. return param;
  156. }
  157. }
  158. //若不存在,则存储起来,且将该指针的地址作为句柄返回
  159. for(int i=0; i<BLL_IMU_REGISTER_CONFIG_PARAM_MAX; i++)
  160. {
  161. if(ob_bll_imu.register_config_param[i] == 0)
  162. {
  163. ob_bll_imu.register_config_param[i] = param;
  164. {
  165. bll_imu_start_config();
  166. return param;
  167. }
  168. }
  169. }
  170. return 0;
  171. }
  172. /**
  173. @brief 注销已注册的IMU配置参数
  174. @param param - [in] IMU配置参数的地址
  175. @return 错误代码 - [out] 0成功,-1地址异常,-2没有该注册的IMU配置参数
  176. */
  177. int bll_imu_unregister_config_param(const bll_imu_param_t* param)
  178. {
  179. if(param == NULL)return -1;
  180. for(int i=0; i<BLL_IMU_REGISTER_CONFIG_PARAM_MAX; i++)
  181. {
  182. if(ob_bll_imu.register_config_param[i] == param)
  183. {
  184. ob_bll_imu.register_config_param[i] = 0;
  185. return 0;
  186. }
  187. }
  188. return -2;
  189. }
  190. /**
  191. @brief 注销已注册的IMU配置参数并重新配置IMU
  192. @param param - [in] IMU配置参数的地址
  193. @return 错误代码 - [out] 0成功,-1地址异常,-2没有该注册的IMU配置参数
  194. */
  195. int bll_imu_Resume_unregister_config_param(const bll_imu_param_t* param)
  196. {
  197. if(param == NULL)return -1;
  198. for(int i=0; i<BLL_IMU_REGISTER_CONFIG_PARAM_MAX; i++)
  199. {
  200. if(ob_bll_imu.register_config_param[i] == param)
  201. {
  202. ob_bll_imu.register_config_param[i] = 0;
  203. bll_imu_start_config();
  204. return 0;
  205. }
  206. }
  207. return -2;
  208. }
  209. /**
  210. @brief 注册IMU数据通知回调函数
  211. @param priority - [in] 回调优先级
  212. @param cb - [in] 回调函数
  213. @return 错误代码 - [out] -1失败,0成功
  214. */
  215. int bll_imu_register_data_notify_callback(BLL_IMU_DATA_NOTIFY_CB_PRIORITY_e priority, bll_imu_data_notify_cb cb)
  216. {
  217. if(cb == NULL || priority>=BLL_IMU_DATA_NOTIFY_CB_PRIORITY_NUM)return -1;
  218. //若是已存在,更新优先级
  219. for(int i=0; i<BLL_IMU_REGISTER_DATA_NOTIFY_MAX; i++)
  220. {
  221. //已存在回调函数
  222. if(ob_bll_imu.register_data_notify[i].cb == cb)
  223. {
  224. //更新优先级
  225. ob_bll_imu.register_data_notify[i].priority = priority;
  226. return 0;
  227. }
  228. }
  229. //若是不存在
  230. for(int i=0; i<BLL_IMU_REGISTER_DATA_NOTIFY_MAX; i++)
  231. {
  232. //找到空位,进行赋值
  233. if(ob_bll_imu.register_data_notify[i].cb == 0)
  234. {
  235. ob_bll_imu.register_data_notify[i].cb = cb;
  236. ob_bll_imu.register_data_notify[i].priority = priority;
  237. return 0;
  238. }
  239. }
  240. //满了
  241. return -1;
  242. }
  243. /**
  244. @brief 开始配置。根据已注册IMU参数筛选出最高优先级进行配置,且只配置一次。
  245. @param 无
  246. @return 错误代码 - [out] -1失败,0成功
  247. */
  248. int bll_imu_start_config(void)
  249. {
  250. int ret= 0xff;
  251. uint8_t i=0;
  252. bll_imu_one_way_param_t cur_imu_param;
  253. int f_diff,b_diff;
  254. //清除当前最高优先级配置参数
  255. for(i=0; i<BLL_IMU_DIR_NUM; i++)
  256. memset(&ob_bll_imu.highest_priority_config_param[i],0,sizeof(bll_imu_one_way_param_t));
  257. //获取前脚和后脚最高优先级配置
  258. for(i=0; i<BLL_IMU_REGISTER_CONFIG_PARAM_MAX; i++){
  259. if(ob_bll_imu.register_config_param[i] != 0)
  260. {
  261. ret = bll_imu_get_highest_priority_config_param(ob_bll_imu.highest_priority_config_param, *ob_bll_imu.register_config_param[i]->config_param);
  262. if(ret == -1)return -1;
  263. }
  264. }
  265. fml_imu_get_config_param((FML_IMU_DIR_e)BLL_IMU_DIR_FRONT,&cur_imu_param);
  266. f_diff = bll_imu_is_equal(&cur_imu_param, &ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_FRONT]);
  267. fml_imu_get_config_param((FML_IMU_DIR_e)BLL_IMU_DIR_BACK,&cur_imu_param);
  268. b_diff = bll_imu_is_equal(&cur_imu_param, &ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_BACK]);
  269. if(f_diff == 0 && b_diff == 0)
  270. {
  271. return -1;
  272. }
  273. // DEBUG_LOG("FRONT :fifo_odr :%d,acc_odr :%d,gry_odr :%d,mag_odr :%d,acc_fs :%d,gry_fs :%d,mag_fs :%d\n",
  274. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_FRONT].fifo_odr,
  275. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_FRONT].acc_odr,
  276. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_FRONT].gry_odr,
  277. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_FRONT].mag_odr,
  278. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_FRONT].acc_fs,
  279. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_FRONT].gry_fs,
  280. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_FRONT].mag_fs
  281. // );
  282. //
  283. //
  284. // DEBUG_LOG("BACK :fifo_odr :%d,acc_odr :%d,gry_odr :%d,mag_odr :%d,acc_fs :%d,gry_fs :%d,mag_fs :%d\n",
  285. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_BACK].fifo_odr,
  286. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_BACK].acc_odr,
  287. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_BACK].gry_odr,
  288. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_BACK].mag_odr,
  289. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_BACK].acc_fs,
  290. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_BACK].gry_fs,
  291. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_BACK].mag_fs
  292. // );
  293. //设置前脚要配置的参数
  294. ret = fml_imu_config_param((FML_IMU_DIR_e)BLL_IMU_DIR_FRONT, &ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_FRONT]);
  295. if(ret == -1)return -1;
  296. //设置后脚要配置的参数
  297. ret = fml_imu_config_param((FML_IMU_DIR_e)BLL_IMU_DIR_BACK, &ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_BACK]);
  298. if(ret == -1)return -1;
  299. //注册配置回调函数
  300. ret = fml_imu_register_config_callback(bll_imu_register_config_cb);
  301. if(ret == -1)return -1;
  302. //注册数据通知回调函数
  303. ret = fml_imu_register_data_notify_callback(bll_imu_register_data_notify_cb);
  304. if(ret == -1)return -1;
  305. //开始配置IMU
  306. ret = fml_imu_start_config();
  307. if(ret == 0)
  308. {
  309. //设置为配置中...
  310. ob_bll_imu.config_result = (BLL_IMU_CONFIG_DOING);
  311. }
  312. return 0;
  313. }
  314. /**
  315. @brief 查询IMU配置参数是否准备好
  316. @param dir - [in] 方向
  317. @param param - [in] 需要查询的IMU配置参数
  318. @return 查询结果 - [out] BLL_IMU_CONFIG_RESULT
  319. */
  320. BLL_IMU_CONFIG_RESULT bll_imu_query_config_param_is_ready(BLL_IMU_DIR_e dir, const bll_imu_param_t *param)
  321. {
  322. BLL_IMU_CONFIG_RESULT result;
  323. if(param == NULL || dir >= BLL_IMU_DIR_NUM)
  324. {
  325. result = (BLL_IMU_CONFIG_VAILERROR);
  326. return result;
  327. }
  328. const bll_imu_one_way_param_t *front_bll = param->config_param[BLL_IMU_DIR_FRONT];
  329. const bll_imu_one_way_param_t *back_bll = param->config_param[BLL_IMU_DIR_BACK];
  330. const bll_imu_one_way_param_t *highest_front_bll = &ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_FRONT];
  331. const bll_imu_one_way_param_t *highest_back_bll = &ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_BACK];
  332. //当前最高配置参数小于输入的配置参数
  333. switch(dir)
  334. {
  335. case BLL_IMU_DIR_FRONT:
  336. //前脚
  337. if(front_bll->acc_fs > highest_front_bll->acc_fs)return result = (BLL_IMU_CONFIG_VAILERROR);
  338. if(front_bll->acc_odr > highest_front_bll->acc_odr)return result = (BLL_IMU_CONFIG_VAILERROR);
  339. if(front_bll->acc_power_mode > highest_front_bll->acc_power_mode)return result = (BLL_IMU_CONFIG_VAILERROR);
  340. if(front_bll->fifo_odr > highest_front_bll->fifo_odr)return result = (BLL_IMU_CONFIG_VAILERROR);
  341. if(front_bll->gry_fs > highest_front_bll->gry_fs)return result = (BLL_IMU_CONFIG_VAILERROR);
  342. if(front_bll->gry_odr > highest_front_bll->gry_odr)return result = (BLL_IMU_CONFIG_VAILERROR);
  343. if(front_bll->gry_power_mode > highest_front_bll->gry_power_mode)return result = (BLL_IMU_CONFIG_VAILERROR);
  344. if(front_bll->mag_fs > highest_front_bll->mag_fs)return result = ( BLL_IMU_CONFIG_VAILERROR);
  345. if(front_bll->mag_odr > highest_front_bll->mag_odr)return result = ( BLL_IMU_CONFIG_VAILERROR);
  346. if(front_bll->timestamp_resolution > highest_front_bll->timestamp_resolution)return result = ( BLL_IMU_CONFIG_VAILERROR);
  347. if(front_bll->timestamp_switch > highest_front_bll->timestamp_switch)return result = ( BLL_IMU_CONFIG_VAILERROR);
  348. break;
  349. case BLL_IMU_DIR_BACK:
  350. //后脚
  351. if(back_bll->acc_fs > highest_back_bll->acc_fs)return result = ( BLL_IMU_CONFIG_VAILERROR);
  352. if(back_bll->acc_odr > highest_back_bll->acc_odr)return result = ( BLL_IMU_CONFIG_VAILERROR);
  353. if(back_bll->acc_power_mode > highest_back_bll->acc_power_mode)return result = ( BLL_IMU_CONFIG_VAILERROR);
  354. if(back_bll->fifo_odr > highest_back_bll->fifo_odr)return result = ( BLL_IMU_CONFIG_VAILERROR);
  355. if(back_bll->gry_fs > highest_back_bll->gry_fs)return result = ( BLL_IMU_CONFIG_VAILERROR);
  356. if(back_bll->gry_odr > highest_back_bll->gry_odr)return result = ( BLL_IMU_CONFIG_VAILERROR);
  357. if(back_bll->gry_power_mode > highest_back_bll->gry_power_mode)return result = ( BLL_IMU_CONFIG_VAILERROR);
  358. if(back_bll->mag_fs > highest_back_bll->mag_fs)return result = ( BLL_IMU_CONFIG_VAILERROR);
  359. if(back_bll->mag_odr > highest_back_bll->mag_odr)return result = ( BLL_IMU_CONFIG_VAILERROR);
  360. if(back_bll->timestamp_resolution > highest_back_bll->timestamp_resolution)return result = ( BLL_IMU_CONFIG_VAILERROR);
  361. if(back_bll->timestamp_switch > highest_back_bll->timestamp_switch)return result = ( BLL_IMU_CONFIG_VAILERROR);
  362. break;
  363. default:
  364. break;
  365. }
  366. result = ob_bll_imu.config_result;
  367. return result;
  368. }
  369. /**
  370. @brief 读取当前IMU数据的数量
  371. @param dir - [in] 方向
  372. @return 返回当前IMU数据的数量
  373. */
  374. int bll_imu_get_data_num(BLL_IMU_DIR_e dir)
  375. {
  376. return fml_imu_get_data_num((FML_IMU_DIR_e)dir);
  377. }
  378. /**
  379. @brief 获取当前IMU数据
  380. @param dir - [in] 方向
  381. @param index - [in] 数据索引
  382. @param pdata - [out] 返回的IMU数据指针
  383. @return 错误代码 - [out] -1失败,0成功
  384. */
  385. int bll_imu_get_data(BLL_IMU_DIR_e dir, int index, bll_imu_data_t *pdata)
  386. {
  387. return fml_imu_get_data((FML_IMU_DIR_e)dir, index, pdata);
  388. }
  389. /**
  390. @brief 关闭IMU
  391. @return 错误代码 - [out] -1失败,0成功
  392. */
  393. int bll_imu_close(void)
  394. {
  395. int ret;
  396. //断电前后脚
  397. ret = fml_imu_close(FML_IMU_DIR_FRONT);
  398. if(ret == -1)return -1;
  399. ret = fml_imu_close(FML_IMU_DIR_BACK);
  400. if(ret == -1)return -1;
  401. //重置模式管理服务结构体
  402. memset(&ob_bll_imu, 0, sizeof(ob_bll_imu));
  403. //默认配置完成
  404. ob_bll_imu.config_result = (BLL_IMU_CONFIG_FINISH);
  405. return 0;
  406. }