bll_imu.c 15 KB

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