bll_imu.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. /*API ------------------------------------------------------------------------------------------------------------------------------------*/
  85. /**
  86. @brief 初始化IMU业务逻辑层
  87. @param 无
  88. @return 错误代码 - [out] -1失败,0成功
  89. */
  90. int bll_imu_Init(void)
  91. {
  92. int ret = 0;
  93. //初始化imu服务
  94. if(fml_imu_Init() == -1)ret = -1;
  95. //重置模式管理服务结构体
  96. memset(&ob_bll_imu, 0, sizeof(ob_bll_imu));
  97. //默认配置完成
  98. ob_bll_imu.config_result = (BLL_IMU_CONFIG_FINISH);
  99. if(ret == -1)return -1;
  100. return 0;
  101. }
  102. /**
  103. @brief 注册IMU要进行配置的参数
  104. @param param - [in] IMU要进行配置的参数地址
  105. @return 错误代码 - [out] 0失败,成功返回参数地址
  106. */
  107. const bll_imu_param_t* bll_imu_register_config_param(const bll_imu_param_t *param)
  108. {
  109. if(param == NULL)return 0;
  110. //若已存在,则返回该指针的地址作为句柄返回
  111. for(int i=0; i<BLL_IMU_REGISTER_CONFIG_PARAM_MAX; i++)
  112. {
  113. if(ob_bll_imu.register_config_param[i] == param)return param;
  114. }
  115. //若不存在,则存储起来,且将该指针的地址作为句柄返回
  116. for(int i=0; i<BLL_IMU_REGISTER_CONFIG_PARAM_MAX; i++)
  117. {
  118. if(ob_bll_imu.register_config_param[i] == 0)
  119. {
  120. ob_bll_imu.register_config_param[i] = param;
  121. return param;
  122. }
  123. }
  124. return 0;
  125. }
  126. /**
  127. @brief 注册IMU要进行配置的参数,并重新设置IMU
  128. @param param - [in] IMU要进行配置的参数地址
  129. @return 错误代码 - [out] 0失败,成功返回参数地址
  130. */
  131. const bll_imu_param_t* bll_imu_Resume_config_param(const bll_imu_param_t *param)
  132. {
  133. if(param == NULL)return 0;
  134. //若已存在,则返回该指针的地址作为句柄返回
  135. for(int i=0; i<BLL_IMU_REGISTER_CONFIG_PARAM_MAX; i++)
  136. {
  137. if(ob_bll_imu.register_config_param[i] == param){
  138. bll_imu_start_config();
  139. return param;
  140. }
  141. }
  142. //若不存在,则存储起来,且将该指针的地址作为句柄返回
  143. for(int i=0; i<BLL_IMU_REGISTER_CONFIG_PARAM_MAX; i++)
  144. {
  145. if(ob_bll_imu.register_config_param[i] == 0)
  146. {
  147. ob_bll_imu.register_config_param[i] = param;
  148. {
  149. bll_imu_start_config();
  150. return param;
  151. }
  152. }
  153. }
  154. return 0;
  155. }
  156. /**
  157. @brief 注销已注册的IMU配置参数
  158. @param param - [in] IMU配置参数的地址
  159. @return 错误代码 - [out] 0成功,-1地址异常,-2没有该注册的IMU配置参数
  160. */
  161. int bll_imu_unregister_config_param(const bll_imu_param_t* param)
  162. {
  163. if(param == NULL)return -1;
  164. for(int i=0; i<BLL_IMU_REGISTER_CONFIG_PARAM_MAX; i++)
  165. {
  166. if(ob_bll_imu.register_config_param[i] == param)
  167. {
  168. ob_bll_imu.register_config_param[i] = 0;
  169. return 0;
  170. }
  171. }
  172. return -2;
  173. }
  174. /**
  175. @brief 注销已注册的IMU配置参数并重新配置IMU
  176. @param param - [in] IMU配置参数的地址
  177. @return 错误代码 - [out] 0成功,-1地址异常,-2没有该注册的IMU配置参数
  178. */
  179. int bll_imu_Resume_unregister_config_param(const bll_imu_param_t* param)
  180. {
  181. if(param == NULL)return -1;
  182. for(int i=0; i<BLL_IMU_REGISTER_CONFIG_PARAM_MAX; i++)
  183. {
  184. if(ob_bll_imu.register_config_param[i] == param)
  185. {
  186. ob_bll_imu.register_config_param[i] = 0;
  187. bll_imu_start_config();
  188. return 0;
  189. }
  190. }
  191. return -2;
  192. }
  193. /**
  194. @brief 注册IMU数据通知回调函数
  195. @param priority - [in] 回调优先级
  196. @param cb - [in] 回调函数
  197. @return 错误代码 - [out] -1失败,0成功
  198. */
  199. int bll_imu_register_data_notify_callback(BLL_IMU_DATA_NOTIFY_CB_PRIORITY_e priority, bll_imu_data_notify_cb cb)
  200. {
  201. if(cb == NULL || priority>=BLL_IMU_DATA_NOTIFY_CB_PRIORITY_NUM)return -1;
  202. //若是已存在,更新优先级
  203. for(int i=0; i<BLL_IMU_REGISTER_DATA_NOTIFY_MAX; i++)
  204. {
  205. //已存在回调函数
  206. if(ob_bll_imu.register_data_notify[i].cb == cb)
  207. {
  208. //更新优先级
  209. ob_bll_imu.register_data_notify[i].priority = priority;
  210. return 0;
  211. }
  212. }
  213. //若是不存在
  214. for(int i=0; i<BLL_IMU_REGISTER_DATA_NOTIFY_MAX; i++)
  215. {
  216. //找到空位,进行赋值
  217. if(ob_bll_imu.register_data_notify[i].cb == 0)
  218. {
  219. ob_bll_imu.register_data_notify[i].cb = cb;
  220. ob_bll_imu.register_data_notify[i].priority = priority;
  221. return 0;
  222. }
  223. }
  224. //满了
  225. return -1;
  226. }
  227. /**
  228. @brief 开始配置。根据已注册IMU参数筛选出最高优先级进行配置,且只配置一次。
  229. @param 无
  230. @return 错误代码 - [out] -1失败,0成功
  231. */
  232. int bll_imu_start_config(void)
  233. {
  234. int ret= 0xff;
  235. uint8_t i=0;
  236. //清除当前最高优先级配置参数
  237. for(i=0; i<BLL_IMU_DIR_NUM; i++)
  238. memset(&ob_bll_imu.highest_priority_config_param[i],0,sizeof(bll_imu_one_way_param_t));
  239. //获取前脚和后脚最高优先级配置
  240. for(i=0; i<BLL_IMU_REGISTER_CONFIG_PARAM_MAX; i++){
  241. if(ob_bll_imu.register_config_param[i] != 0)
  242. {
  243. ret = bll_imu_get_highest_priority_config_param(ob_bll_imu.highest_priority_config_param, *ob_bll_imu.register_config_param[i]->config_param);
  244. if(ret == -1)return -1;
  245. }
  246. }
  247. // 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",
  248. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_FRONT].fifo_odr,
  249. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_FRONT].acc_odr,
  250. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_FRONT].gry_odr,
  251. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_FRONT].mag_odr,
  252. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_FRONT].acc_fs,
  253. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_FRONT].gry_fs,
  254. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_FRONT].mag_fs
  255. // );
  256. //
  257. //
  258. // 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",
  259. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_BACK].fifo_odr,
  260. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_BACK].acc_odr,
  261. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_BACK].gry_odr,
  262. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_BACK].mag_odr,
  263. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_BACK].acc_fs,
  264. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_BACK].gry_fs,
  265. // ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_BACK].mag_fs
  266. // );
  267. //设置前脚要配置的参数
  268. ret = fml_imu_config_param((FML_IMU_DIR_e)BLL_IMU_DIR_FRONT, &ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_FRONT]);
  269. if(ret == -1)return -1;
  270. //设置后脚要配置的参数
  271. ret = fml_imu_config_param((FML_IMU_DIR_e)BLL_IMU_DIR_BACK, &ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_BACK]);
  272. if(ret == -1)return -1;
  273. //注册配置回调函数
  274. ret = fml_imu_register_config_callback(bll_imu_register_config_cb);
  275. if(ret == -1)return -1;
  276. //注册数据通知回调函数
  277. ret = fml_imu_register_data_notify_callback(bll_imu_register_data_notify_cb);
  278. if(ret == -1)return -1;
  279. //开始配置IMU
  280. ret = fml_imu_start_config();
  281. if(ret == 0)
  282. {
  283. //设置为配置中...
  284. ob_bll_imu.config_result = (BLL_IMU_CONFIG_DOING);
  285. }
  286. return 0;
  287. }
  288. /**
  289. @brief 查询IMU配置参数是否准备好
  290. @param dir - [in] 方向
  291. @param param - [in] 需要查询的IMU配置参数
  292. @return 查询结果 - [out] BLL_IMU_CONFIG_RESULT
  293. */
  294. BLL_IMU_CONFIG_RESULT bll_imu_query_config_param_is_ready(BLL_IMU_DIR_e dir, const bll_imu_param_t *param)
  295. {
  296. BLL_IMU_CONFIG_RESULT result;
  297. if(param == NULL || dir >= BLL_IMU_DIR_NUM)
  298. {
  299. result = (BLL_IMU_CONFIG_VAILERROR);
  300. return result;
  301. }
  302. const bll_imu_one_way_param_t *front_bll = param->config_param[BLL_IMU_DIR_FRONT];
  303. const bll_imu_one_way_param_t *back_bll = param->config_param[BLL_IMU_DIR_BACK];
  304. const bll_imu_one_way_param_t *highest_front_bll = &ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_FRONT];
  305. const bll_imu_one_way_param_t *highest_back_bll = &ob_bll_imu.highest_priority_config_param[BLL_IMU_DIR_BACK];
  306. //当前最高配置参数小于输入的配置参数
  307. switch(dir)
  308. {
  309. case BLL_IMU_DIR_FRONT:
  310. //前脚
  311. if(front_bll->acc_fs > highest_front_bll->acc_fs)return result = (BLL_IMU_CONFIG_VAILERROR);
  312. if(front_bll->acc_odr > highest_front_bll->acc_odr)return result = (BLL_IMU_CONFIG_VAILERROR);
  313. if(front_bll->acc_power_mode > highest_front_bll->acc_power_mode)return result = (BLL_IMU_CONFIG_VAILERROR);
  314. if(front_bll->fifo_odr > highest_front_bll->fifo_odr)return result = (BLL_IMU_CONFIG_VAILERROR);
  315. if(front_bll->gry_fs > highest_front_bll->gry_fs)return result = (BLL_IMU_CONFIG_VAILERROR);
  316. if(front_bll->gry_odr > highest_front_bll->gry_odr)return result = (BLL_IMU_CONFIG_VAILERROR);
  317. if(front_bll->gry_power_mode > highest_front_bll->gry_power_mode)return result = (BLL_IMU_CONFIG_VAILERROR);
  318. if(front_bll->mag_fs > highest_front_bll->mag_fs)return result = ( BLL_IMU_CONFIG_VAILERROR);
  319. if(front_bll->mag_odr > highest_front_bll->mag_odr)return result = ( BLL_IMU_CONFIG_VAILERROR);
  320. if(front_bll->timestamp_resolution > highest_front_bll->timestamp_resolution)return result = ( BLL_IMU_CONFIG_VAILERROR);
  321. if(front_bll->timestamp_switch > highest_front_bll->timestamp_switch)return result = ( BLL_IMU_CONFIG_VAILERROR);
  322. break;
  323. case BLL_IMU_DIR_BACK:
  324. //后脚
  325. if(back_bll->acc_fs > highest_back_bll->acc_fs)return result = ( BLL_IMU_CONFIG_VAILERROR);
  326. if(back_bll->acc_odr > highest_back_bll->acc_odr)return result = ( BLL_IMU_CONFIG_VAILERROR);
  327. if(back_bll->acc_power_mode > highest_back_bll->acc_power_mode)return result = ( BLL_IMU_CONFIG_VAILERROR);
  328. if(back_bll->fifo_odr > highest_back_bll->fifo_odr)return result = ( BLL_IMU_CONFIG_VAILERROR);
  329. if(back_bll->gry_fs > highest_back_bll->gry_fs)return result = ( BLL_IMU_CONFIG_VAILERROR);
  330. if(back_bll->gry_odr > highest_back_bll->gry_odr)return result = ( BLL_IMU_CONFIG_VAILERROR);
  331. if(back_bll->gry_power_mode > highest_back_bll->gry_power_mode)return result = ( BLL_IMU_CONFIG_VAILERROR);
  332. if(back_bll->mag_fs > highest_back_bll->mag_fs)return result = ( BLL_IMU_CONFIG_VAILERROR);
  333. if(back_bll->mag_odr > highest_back_bll->mag_odr)return result = ( BLL_IMU_CONFIG_VAILERROR);
  334. if(back_bll->timestamp_resolution > highest_back_bll->timestamp_resolution)return result = ( BLL_IMU_CONFIG_VAILERROR);
  335. if(back_bll->timestamp_switch > highest_back_bll->timestamp_switch)return result = ( BLL_IMU_CONFIG_VAILERROR);
  336. break;
  337. default:
  338. break;
  339. }
  340. result = ob_bll_imu.config_result;
  341. return result;
  342. }
  343. /**
  344. @brief 读取当前IMU数据的数量
  345. @param dir - [in] 方向
  346. @return 返回当前IMU数据的数量
  347. */
  348. int bll_imu_get_data_num(BLL_IMU_DIR_e dir)
  349. {
  350. return fml_imu_get_data_num((FML_IMU_DIR_e)dir);
  351. }
  352. /**
  353. @brief 获取当前IMU数据
  354. @param dir - [in] 方向
  355. @param index - [in] 数据索引
  356. @param pdata - [out] 返回的IMU数据指针
  357. @return 错误代码 - [out] -1失败,0成功
  358. */
  359. int bll_imu_get_data(BLL_IMU_DIR_e dir, int index, bll_imu_data_t *pdata)
  360. {
  361. return fml_imu_get_data((FML_IMU_DIR_e)dir, index, pdata);
  362. }
  363. /**
  364. @brief 关闭IMU
  365. @return 错误代码 - [out] -1失败,0成功
  366. */
  367. int bll_imu_close(void)
  368. {
  369. int ret;
  370. //断电前后脚
  371. ret = fml_imu_close(FML_IMU_DIR_FRONT);
  372. if(ret == -1)return -1;
  373. ret = fml_imu_close(FML_IMU_DIR_BACK);
  374. if(ret == -1)return -1;
  375. //重置模式管理服务结构体
  376. memset(&ob_bll_imu, 0, sizeof(ob_bll_imu));
  377. //默认配置完成
  378. ob_bll_imu.config_result = (BLL_IMU_CONFIG_FINISH);
  379. return 0;
  380. }