fml_imu.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. /**
  2. 功耗:
  3. 闲置模式功耗为250+ua
  4. 低功耗模式1,若前后脚地磁开启,板子地磁不开启,且不读取数据,功耗为330+ua ; 若只是读取前后脚地磁,则功耗为450+ua, 若读取前后脚地磁后再reset + 配置,则功耗为700+ua
  5. 低功耗模式2,若只开启后脚地磁,板子地磁不开启,且不读取数据,功耗为300+ua ; 若只是读取后脚地磁,则功耗为370-390+ua, 若读取后脚地磁后再reset + 配置, 则功耗为500+ua
  6. //以上低功耗测试都是没开六轴,只开地磁的情况
  7. 游戏模式spi_lsm,8.5ma
  8. 游戏模式i2c_lsm,8.7ma
  9. 前脚地磁供电关闭,后脚地磁供电开启,因为sda(标准输出)线共用,当sda输出0时,前脚地磁的供电IO和sda导通,导致短路,功耗为11ma,且后脚地磁数据异常。
  10. 所以通过开关电源来复位地磁失败!!!
  11. 低功耗模式3,只开启后脚地磁,板子地磁不开启,读取完地磁和低功耗加速度后(都是用SPI读取),在睡眠前重新配置(不reset)后脚地磁,功耗为370 - 400ua
  12. PS:低功耗模式3中,读取低功耗加速度用SPI时,速度为152us,用模拟I2C读取,速度为457us,由于时间变长,功耗为400 - 430ua
  13. 更新时间:2021-10-12:
  14. 低功耗模式4,更改了结构板,将IMU移到前脚,同时I2C的SCL公用,SDA独立,取消每次读取地磁后reset+配置。
  15. 这时,低功耗模式(没跑算法)功耗为:350-380ua 、 闲置模式功耗为:180-270ua 、 游戏模式功耗为:12-13ma,若再扣去小板上的六轴挂起功耗(22ua)+地磁挂起功耗(9ua)的31ua,
  16. 则各个模式功耗为:低功耗模式(没跑算法)功耗为:319-349ua 、 闲置模式功耗为:149-239ua 、 游戏模式功耗为:12-13ma
  17. 更新时间:2021-11-25:
  18. IMU的FIFO读取地磁,会出现重复现象,目前猜测是IMU的HZ与地磁的HZ不同步,地磁是100HZ,IMU是104HZ,其次,地磁无出现数据0的现象。
  19. 更新时间:2021-11-29:
  20. 版本:1.9.1
  21. 板子本身功耗:12-14ua
  22. 测试,低功耗FIFO功耗(没跑日常计步算法):537-549ua
  23. 测试,低功耗FIFO功耗(没跑日常计步算法),不读取IMUFIFO数据:454-464ua
  24. 测试,单纯设置外设,不跑IMU线程,功耗:453-462ua
  25. 测试,单纯设置外设(断电后脚外设供电,虽然其本身就是设置完后就挂起),不跑IMU线程,功耗:433-444ua
  26. 测试,断电前后脚外设,不跑IMU线程,功耗:175-224ua
  27. 测试,开启维持一个串口,功耗增加为:473ua
  28. 结论:
  29. 低功耗FIFO模式(100ms唤醒)下,不跑日常计步算法,总功耗为:537-549ua
  30. 外设功耗为:后脚地磁挂起(20ua)+前脚外设的IMU_104HZ_acc_timestamp_sensorhub_slv0123(128ua)+前脚外设地磁100HZ(150ua)
  31. 线程功耗为:IMU线程(84ua->读取IMU的FIFO数据10组,耗时1.1~1.2ms)+bat线程(5ua)+其余线程和广播(170ua)
  32. 游戏模式下(没焊接单线灯)功耗为:
  33. 设备直接进入:7.4ma
  34. 链接右鞋后,手机下发游戏指令,进入:8.5-8.6ma
  35. 更新时间:2022-3-7
  36. 版本:2.1
  37. 待机模式(不穿鞋)功耗:270ua ~ 467ua
  38. 正常模式(穿鞋)功耗:570ua ~ 630u
  39. 游戏模式功耗:
  40. */
  41. /*Includes ----------------------------------------------*/
  42. #include "tool.h"
  43. #include "nrf_delay.h"
  44. #include "bsp_time.h"
  45. #include "exception.h"
  46. #include "system.h"
  47. #include "drv_qmc6310_v2.h"
  48. #include "drv_lsm6ds3tr_c.h"
  49. #include "fml_imu.h"
  50. /*Private macro ------------------------------------------------------------------------------------------------------------------------------------*/
  51. #define FML_IMU_DATA_GROUP_NUM_MAX 20 //IMU能存储的最大数据组数
  52. #define FML_IMU_MONITOR_DATA_ERR_SUM_MAX 10 //数据监测错误累计最大值
  53. /*STRUCTION ------------------------------------------------------------------------------------------------------------------------------------*/
  54. typedef enum {
  55. FML_IMU_CONFIG_STAGE_DONE, //配置完成阶段
  56. FML_IMU_CONFIG_STAGE_IN_PROGRESS, //配置进行阶段
  57. FML_IMU_CONFIG_STAGE_FAIL, //配置失败阶段
  58. } FML_IMU_CONFIG_STAGE_e;
  59. typedef union
  60. {
  61. drv_lsm_config_param_t lsm; //当前的LSM配置参数
  62. drv_qmc_config_param_t qmc; //当前的QMC配置参数
  63. } drv_param_u;
  64. typedef struct fml_imu
  65. {
  66. /*private member*/
  67. FML_IMU_CONFIG_STAGE_e stage; //配置流程状态
  68. drv_param_u drv_param[FML_IMU_DIR_NUM]; //驱动参数组
  69. fml_imu_param_t config_param[FML_IMU_DIR_NUM]; //配置参数组
  70. int cur_data_num[FML_IMU_DIR_NUM]; //当前的数据量
  71. fml_imu_data_t cur_data[FML_IMU_DIR_NUM][FML_IMU_DATA_GROUP_NUM_MAX]; //当前的IMU数据缓存区
  72. uint32_t config_flow; //配置流程
  73. fml_imu_config_cb config_cb; //配置回调
  74. fml_imu_data_notify_cb data_notify_cb; //数据通知回调
  75. int16_t last_f_acc[3]; //上一次的前脚加速度值
  76. int16_t last_f_gry[3]; //上一次的前脚陀螺仪值
  77. int16_t last_f_mag[3]; //上一次的前脚地磁计值
  78. int16_t last_b_mag[3]; //上一次的后脚地磁计值
  79. int16_t last_f_acc_err_sum; //上一次的前脚加速度值错误累计
  80. int16_t last_f_gry_err_sum; //上一次的前脚陀螺仪值错误累计
  81. int16_t last_f_mag_err_sum; //上一次的前脚地磁计值错误累计
  82. int16_t last_b_mag_err_sum; //上一次的后脚地磁计值错误累计
  83. int16_t except_data_occur_sum[FML_IMU_DIR_NUM]; //异常数据产生次数累计
  84. } Fml_Imu_t;
  85. /*Local Variable ------------------------------------------------------------------------------------------------------------------------------------*/
  86. static Fml_Imu_t ob_fml_imu;
  87. /*Local Functions ------------------------------------------------------------------------------------------------------------------------------------*/
  88. static void fml_imu_macro_conversion(FML_IMU_DIR_e dir, fml_imu_param_t *config_param, drv_param_u *drv_param)
  89. {
  90. switch(dir)
  91. {
  92. case FML_IMU_DIR_FRONT:
  93. //acc_odr
  94. switch(config_param->acc_odr)
  95. {
  96. case FML_IMU_ACC_ODR_OFF:
  97. drv_param->lsm.acc_odr = LSM_ACC_ODR_OFF;
  98. break;
  99. case FML_IMU_ACC_ODR_104HZ:
  100. drv_param->lsm.acc_odr = LSM_ACC_ODR_104HZ;
  101. break;
  102. case FML_IMU_ACC_ODR_12HZ5:
  103. drv_param->lsm.acc_odr = LSM_ACC_ODR_12HZ5;
  104. break;
  105. }
  106. //gry_odr
  107. switch(config_param->gry_odr)
  108. {
  109. case FML_IMU_GRY_ODR_OFF:
  110. drv_param->lsm.gry_odr = LSM_GRY_ODR_OFF;
  111. break;
  112. case FML_IMU_GRY_ODR_104HZ:
  113. drv_param->lsm.gry_odr = LSM_GRY_ODR_104HZ;
  114. break;
  115. case FML_IMU_GRY_ODR_12HZ5:
  116. drv_param->lsm.gry_odr = LSM_GRY_ODR_12HZ5;
  117. break;
  118. }
  119. //mag_odr
  120. switch(config_param->mag_odr)
  121. {
  122. case FML_IMU_MAG_ODR_OFF:
  123. drv_param->lsm.mag_odr = LSM_MAG_ODR_OFF;
  124. break;
  125. case FML_IMU_MAG_ODR_10HZ:
  126. drv_param->lsm.mag_odr = LSM_MAG_ODR_10HZ;
  127. break;
  128. case FML_IMU_MAG_ODR_100HZ:
  129. drv_param->lsm.mag_odr = LSM_MAG_ODR_100HZ;
  130. break;
  131. case FML_IMU_MAG_ODR_200HZ:
  132. drv_param->lsm.mag_odr = LSM_MAG_ODR_200HZ;
  133. break;
  134. }
  135. //fifo_odr
  136. switch(config_param->fifo_odr)
  137. {
  138. case FML_IMU_FIFO_ODR_OFF:
  139. drv_param->lsm.fifo_odr = LSM_FIFO_ODR_OFF;
  140. break;
  141. case FML_IMU_FIFO_ODR_104HZ:
  142. drv_param->lsm.fifo_odr = LSM_FIFO_ODR_104HZ;
  143. break;
  144. }
  145. //acc_power_mode
  146. switch(config_param->acc_power_mode)
  147. {
  148. case FML_IMU_ACC_POWER_MODE_HIGH_PERFORMANCE:
  149. drv_param->lsm.acc_power_mode = LSM_ACC_POWER_MODE_HIGH_PERFORMANCE;
  150. break;
  151. case FML_IMU_ACC_POWER_MODE_NORMAL:
  152. drv_param->lsm.acc_power_mode = LSM_ACC_POWER_MODE_NORMAL;
  153. break;
  154. }
  155. //gry_power_mode
  156. switch(config_param->gry_power_mode)
  157. {
  158. case FML_IMU_GRY_POWER_MODE_HIGH_PERFORMANCE:
  159. drv_param->lsm.gry_power_mode = LSM_GRY_POWER_MODE_HIGH_PERFORMANCE;
  160. break;
  161. case FML_IMU_GRY_POWER_MODE_NORMAL:
  162. drv_param->lsm.gry_power_mode = LSM_GRY_POWER_MODE_NORMAL;
  163. break;
  164. }
  165. //acc_fs
  166. switch(config_param->acc_fs)
  167. {
  168. case FML_IMU_ACC_FS_2G:
  169. drv_param->lsm.acc_fs = LSM_ACC_FS_2G;
  170. break;
  171. case FML_IMU_ACC_FS_16G:
  172. drv_param->lsm.acc_fs = LSM_ACC_FS_16G;
  173. break;
  174. }
  175. //gry_fs
  176. switch(config_param->gry_fs)
  177. {
  178. case FML_IMU_GRY_FS_250DPS:
  179. drv_param->lsm.gry_fs = LSM_GRY_FS_250DPS;
  180. break;
  181. case FML_IMU_GRY_FS_2000DPS:
  182. drv_param->lsm.gry_fs = LSM_GRY_FS_2000DPS;
  183. break;
  184. }
  185. //mag_fs
  186. switch(config_param->mag_fs)
  187. {
  188. case FML_IMU_MAG_FS_30GS:
  189. drv_param->lsm.mag_fs = LSM_MAG_FS_30GS;
  190. break;
  191. }
  192. //timestamp_resolution
  193. switch(config_param->timestamp_resolution)
  194. {
  195. case FML_IMU_TIMESTAMP_6MS4:
  196. drv_param->lsm.timestamp_resolution = LSM_TIMESTAMP_6MS4;
  197. break;
  198. case FML_IMU_TIMESTAMP_25US:
  199. drv_param->lsm.timestamp_resolution = LSM_TIMESTAMP_25US;
  200. break;
  201. }
  202. //timestamp_switch
  203. switch(config_param->timestamp_switch)
  204. {
  205. case FML_IMU_TIMESTAMP_OFF:
  206. drv_param->lsm.timestamp_switch = LSM_TIMESTAMP_OFF;
  207. break;
  208. case FML_IMU_TIMESTAMP_ON:
  209. drv_param->lsm.timestamp_switch = LSM_TIMESTAMP_ON;
  210. break;
  211. }
  212. break;
  213. case FML_IMU_DIR_BACK:
  214. //mag_odr
  215. switch(config_param->mag_odr)
  216. {
  217. case FML_IMU_MAG_ODR_OFF:
  218. drv_param->qmc.mag_odr = QMC_MAG_ODR_OFF;
  219. break;
  220. case FML_IMU_MAG_ODR_10HZ:
  221. drv_param->qmc.mag_odr = QMC_MAG_ODR_10HZ;
  222. break;
  223. case FML_IMU_MAG_ODR_100HZ:
  224. drv_param->qmc.mag_odr = QMC_MAG_ODR_100HZ;
  225. break;
  226. case FML_IMU_MAG_ODR_200HZ:
  227. drv_param->qmc.mag_odr = QMC_MAG_ODR_200HZ;
  228. break;
  229. }
  230. //mag_fs
  231. switch(config_param->mag_fs)
  232. {
  233. case FML_IMU_MAG_FS_30GS:
  234. drv_param->qmc.mag_fs = QMC_MAG_FS_30GS;
  235. break;
  236. }
  237. break;
  238. default:
  239. break;
  240. }
  241. }
  242. static int fml_imu_intergrated_setting(void)
  243. {
  244. int ret;
  245. int lsm_mag_flow = drv_lsm_get_mag_odr_flow();
  246. int qmc_mag_flow = drv_qmc6310_get_mag_odr_flow();
  247. drv_param_u lsm_drv_param;
  248. drv_param_u qmc_drv_param;
  249. static uint32_t tim = 0;
  250. switch(ob_fml_imu.config_flow)
  251. {
  252. case 0:
  253. //断电lsm
  254. ret = drv_lsm_power_off();
  255. if(ret != 0)return -1;
  256. //断电qmc
  257. ret = drv_qmc6310_power_off();
  258. if(ret != 0)return -1;
  259. //清空读取缓存
  260. ob_fml_imu.cur_data_num[FML_IMU_DIR_FRONT] = 0;
  261. //清空读取缓存
  262. ob_fml_imu.cur_data_num[FML_IMU_DIR_BACK] = 0;
  263. tim = 0;
  264. ob_fml_imu.config_flow = 1;
  265. break;
  266. case 1:
  267. //等待200ms
  268. if(tim == 0){
  269. tim = TIME_GetTicks();
  270. }else if(TIME_GetTicks()-tim>=200){
  271. tim = 0;
  272. ob_fml_imu.config_flow = 2;
  273. }
  274. break;
  275. case 2:
  276. //上电lsm
  277. ret = drv_lsm_power_on();
  278. if(ret != 0)return -1;
  279. //上电qmc
  280. ret = drv_qmc6310_power_on();
  281. if(ret != 0)return -1;
  282. tim = 0;
  283. ob_fml_imu.config_flow = 3;
  284. break;
  285. case 3:
  286. //等待200ms
  287. if(tim == 0){
  288. tim = TIME_GetTicks();
  289. }else if(TIME_GetTicks()-tim>=200){
  290. tim = 0;
  291. ob_fml_imu.config_flow = 4;
  292. }
  293. break;
  294. case 4:
  295. //lsm进行自检步骤1
  296. ret = drv_lsm_self_check_1();
  297. if(ret != 0)return -1;
  298. tim = 0;
  299. ob_fml_imu.config_flow = 5;
  300. break;
  301. case 5:
  302. //等待100ms
  303. if(tim == 0){
  304. tim = TIME_GetTicks();
  305. }else if(TIME_GetTicks()-tim>=100){
  306. tim = 0;
  307. ob_fml_imu.config_flow = 6;
  308. }
  309. break;
  310. case 6:
  311. //lsm进行自检步骤2
  312. ret = drv_lsm_self_check_2();
  313. if(ret != 0)return -1;
  314. tim = 0;
  315. ob_fml_imu.config_flow = 7;
  316. break;
  317. case 7:
  318. //等待100ms
  319. if(tim == 0){
  320. tim = TIME_GetTicks();
  321. }else if(TIME_GetTicks()-tim>=100){
  322. tim = 0;
  323. ob_fml_imu.config_flow = 8;
  324. }
  325. break;
  326. case 8:
  327. //lsm进行自检步骤3
  328. ret = drv_lsm_self_check_3();
  329. if(ret != 0)return -1;
  330. tim = 0;
  331. ob_fml_imu.config_flow = 9;
  332. break;
  333. case 9:
  334. //等待150ms
  335. if(tim == 0){
  336. tim = TIME_GetTicks();
  337. }else if(TIME_GetTicks()-tim>=150){
  338. tim = 0;
  339. ob_fml_imu.config_flow = 10;
  340. }
  341. break;
  342. case 10:
  343. //lsm进行自检步骤4
  344. ret = drv_lsm_self_check_4();
  345. if(ret != 0)return -1;
  346. tim = 0;
  347. ob_fml_imu.config_flow = 11;
  348. break;
  349. case 11:
  350. //等待50ms
  351. if(tim == 0){
  352. tim = TIME_GetTicks();
  353. }else if(TIME_GetTicks()-tim>=50){
  354. tim = 0;
  355. ob_fml_imu.config_flow = 12;
  356. }
  357. break;
  358. case 12:
  359. //lsm进行自检步骤5
  360. ret = drv_lsm_self_check_5();
  361. if(ret != 0)return -1;
  362. tim = 0;
  363. ob_fml_imu.config_flow = 13;
  364. break;
  365. case FML_IMU_CONFIG_FLOW_DONE:
  366. //配置完成才能进来此处。。。。。。。
  367. break;
  368. default:
  369. //转换参数,用于配置驱动
  370. fml_imu_macro_conversion(FML_IMU_DIR_FRONT, &ob_fml_imu.config_param[FML_IMU_DIR_FRONT], &lsm_drv_param);
  371. fml_imu_macro_conversion(FML_IMU_DIR_BACK, &ob_fml_imu.config_param[FML_IMU_DIR_BACK], &qmc_drv_param);
  372. //配置lsm的地磁计采样频率
  373. if(ob_fml_imu.config_param[FML_IMU_DIR_FRONT].fifo_odr != FML_IMU_FIFO_ODR_OFF)
  374. {
  375. for(int i=1; i <=lsm_mag_flow;i++)
  376. {
  377. ret = drv_lsm_set_mag_odr(lsm_drv_param.lsm.mag_odr, 1, i);
  378. if(ret != 0)return -1;
  379. ob_fml_imu.config_flow++;
  380. }
  381. }
  382. else
  383. {
  384. for(int i=1; i <= lsm_mag_flow; i++)
  385. {
  386. ret = drv_lsm_set_mag_odr(lsm_drv_param.lsm.mag_odr, 0, i);
  387. if(ret != 0)return -1;
  388. ob_fml_imu.config_flow++;
  389. }
  390. }
  391. //配置lsm的加速度工作模式
  392. ret = drv_lsm_set_acc_power_mode(lsm_drv_param.lsm.acc_power_mode);
  393. if(ret != 0)return -1;
  394. ob_fml_imu.config_flow++;
  395. //配置lsm的陀螺仪工作模式
  396. ret = drv_lsm_set_gry_power_mode(lsm_drv_param.lsm.gry_power_mode);
  397. if(ret != 0)return -1;
  398. ob_fml_imu.config_flow++;
  399. //配置lsm的加速度量程
  400. ret = drv_lsm_set_acc_fs(lsm_drv_param.lsm.acc_fs);
  401. if(ret != 0)return -1;
  402. ob_fml_imu.config_flow++;
  403. //配置lsm的陀螺仪量程
  404. ret = drv_lsm_set_gry_fs(lsm_drv_param.lsm.gry_fs);
  405. if(ret != 0)return -1;
  406. ob_fml_imu.config_flow++;
  407. //配置lsm的时间戳精度
  408. ret = drv_lsm_set_timestamp_resolution(lsm_drv_param.lsm.timestamp_resolution);
  409. if(ret != 0)return -1;
  410. ob_fml_imu.config_flow++;
  411. //配置lsm的时间戳开关
  412. ret = drv_lsm_set_timestamp_switch(lsm_drv_param.lsm.timestamp_switch);
  413. if(ret != 0)return -1;
  414. ob_fml_imu.config_flow++;
  415. //配置lsm的FIFO采样频率(必须以上都配置成功,才能配置FIFO)
  416. ret = drv_lsm_set_fifo_odr(lsm_drv_param.lsm.fifo_odr, \
  417. lsm_drv_param.lsm.acc_odr, \
  418. lsm_drv_param.lsm.gry_odr, \
  419. lsm_drv_param.lsm.mag_odr, \
  420. lsm_drv_param.lsm.timestamp_switch);
  421. if(ret != 0)return -1;
  422. ob_fml_imu.config_flow++;
  423. //配置lsm的加速度采样频率
  424. ret = drv_lsm_set_acc_odr(lsm_drv_param.lsm.acc_odr);
  425. if(ret != 0)return -1;
  426. ob_fml_imu.config_flow++;
  427. //配置lsm的陀螺仪采样频率
  428. ret = drv_lsm_set_gry_odr(lsm_drv_param.lsm.gry_odr);
  429. if(ret != 0)return -1;
  430. ob_fml_imu.config_flow++;
  431. //配置qmc的地磁计采样频率
  432. for(int i = 1; i <=qmc_mag_flow; i++)
  433. {
  434. ret = drv_qmc6310_set_mag_odr(qmc_drv_param.qmc.mag_odr,i);
  435. if(ret != 0)return -1;
  436. ob_fml_imu.config_flow++;
  437. }
  438. //配置完成!
  439. ob_fml_imu.config_flow = FML_IMU_CONFIG_FLOW_DONE;
  440. break;
  441. }
  442. return 0;
  443. }
  444. static int fml_imu_read_data_lsm(void)
  445. {
  446. int i;
  447. int fifo_group_num;
  448. lsm_data_t temp_lsm_data;
  449. //读取FIFO数据
  450. if(ob_fml_imu.config_param[FML_IMU_DIR_FRONT].fifo_odr != FML_IMU_FIFO_ODR_OFF)
  451. {
  452. //获取当前fifo里存在多少组数据。
  453. fifo_group_num = drv_lsm_get_fifo_group_num();
  454. fifo_group_num = fifo_group_num <= FML_IMU_DATA_GROUP_NUM_MAX ? fifo_group_num : FML_IMU_DATA_GROUP_NUM_MAX;
  455. if(fifo_group_num > 0)
  456. {
  457. for(i=0; i<fifo_group_num; i++)
  458. {
  459. if(drv_lsm_get_fifo_data(&temp_lsm_data) != -1)
  460. {
  461. ob_fml_imu.cur_data[FML_IMU_DIR_FRONT][i].acc[0] = temp_lsm_data.acc[0];
  462. ob_fml_imu.cur_data[FML_IMU_DIR_FRONT][i].acc[1] = temp_lsm_data.acc[1];
  463. ob_fml_imu.cur_data[FML_IMU_DIR_FRONT][i].acc[2] = temp_lsm_data.acc[2];
  464. ob_fml_imu.cur_data[FML_IMU_DIR_FRONT][i].gry[0] = temp_lsm_data.gry[0];
  465. ob_fml_imu.cur_data[FML_IMU_DIR_FRONT][i].gry[1] = temp_lsm_data.gry[1];
  466. ob_fml_imu.cur_data[FML_IMU_DIR_FRONT][i].gry[2] = temp_lsm_data.gry[2];
  467. ob_fml_imu.cur_data[FML_IMU_DIR_FRONT][i].mag[0] = temp_lsm_data.mag[0];
  468. ob_fml_imu.cur_data[FML_IMU_DIR_FRONT][i].mag[1] = temp_lsm_data.mag[1];
  469. ob_fml_imu.cur_data[FML_IMU_DIR_FRONT][i].mag[2] = temp_lsm_data.mag[2];
  470. ob_fml_imu.cur_data[FML_IMU_DIR_FRONT][i].fifo_timestamp = temp_lsm_data.fifo_timestamp;
  471. }
  472. else
  473. {
  474. break;
  475. }
  476. }
  477. }
  478. else
  479. {
  480. return 0;
  481. }
  482. return i;
  483. }
  484. //只读取ACC数据
  485. else if(ob_fml_imu.config_param[FML_IMU_DIR_FRONT].acc_odr != FML_IMU_ACC_ODR_OFF && \
  486. ob_fml_imu.config_param[FML_IMU_DIR_FRONT].gry_odr == FML_IMU_GRY_ODR_OFF && \
  487. ob_fml_imu.config_param[FML_IMU_DIR_FRONT].mag_odr == FML_IMU_MAG_ODR_OFF \
  488. )
  489. {
  490. if(drv_lsm_get_acc_data(&temp_lsm_data) != -1)
  491. {
  492. ob_fml_imu.cur_data[FML_IMU_DIR_FRONT][0].acc[0] = temp_lsm_data.acc[0];
  493. ob_fml_imu.cur_data[FML_IMU_DIR_FRONT][0].acc[1] = temp_lsm_data.acc[1];
  494. ob_fml_imu.cur_data[FML_IMU_DIR_FRONT][0].acc[2] = temp_lsm_data.acc[2];
  495. return 1;
  496. }
  497. }
  498. return 0;
  499. }
  500. static int fml_imu_read_data_qmc(void)
  501. {
  502. qmc_data_t temp_qmc_data;
  503. if(ob_fml_imu.config_param[FML_IMU_DIR_BACK].mag_odr != FML_IMU_MAG_ODR_OFF)
  504. {
  505. if(drv_qmc6310_get_mag_data(&temp_qmc_data) != -1)
  506. {
  507. ob_fml_imu.cur_data[FML_IMU_DIR_BACK][0].mag[0] = temp_qmc_data.mag[0];
  508. ob_fml_imu.cur_data[FML_IMU_DIR_BACK][0].mag[1] = temp_qmc_data.mag[1];
  509. ob_fml_imu.cur_data[FML_IMU_DIR_BACK][0].mag[2] = temp_qmc_data.mag[2];
  510. return 1;
  511. }
  512. }
  513. return 0;
  514. }
  515. static int hal_ser_imu_read_data(FML_IMU_DIR_e dir)
  516. {
  517. int ret = 0;
  518. switch(dir)
  519. {
  520. case FML_IMU_DIR_FRONT:
  521. ret = fml_imu_read_data_lsm();
  522. break;
  523. case FML_IMU_DIR_BACK:
  524. ret = fml_imu_read_data_qmc();
  525. break;
  526. default:
  527. break;
  528. }
  529. return ret;
  530. }
  531. static void monitor_sensor_data(int16_t *f_acc, int16_t *f_gry, int16_t *f_mag, int16_t *b_mag)
  532. {
  533. /*前脚加速度*/
  534. if(f_acc != NULL)
  535. {
  536. if(
  537. ob_fml_imu.last_f_acc[0] == f_acc[0] && \
  538. ob_fml_imu.last_f_acc[1] == f_acc[1] && \
  539. ob_fml_imu.last_f_acc[2] == f_acc[2]
  540. )
  541. {
  542. ob_fml_imu.last_f_acc_err_sum++;
  543. if(ob_fml_imu.last_f_acc_err_sum >= FML_IMU_MONITOR_DATA_ERR_SUM_MAX){
  544. Except_SetExceptype(EXCEPT_DATA_FRONT_ACC);
  545. }
  546. }else{
  547. ob_fml_imu.last_f_acc_err_sum = 0;
  548. }
  549. ob_fml_imu.last_f_acc[0] = f_acc[0];
  550. ob_fml_imu.last_f_acc[1] = f_acc[1];
  551. ob_fml_imu.last_f_acc[2] = f_acc[2];
  552. }
  553. /*前脚陀螺仪*/
  554. if(f_gry != NULL)
  555. {
  556. if(
  557. ob_fml_imu.last_f_gry[0] == f_gry[0] && \
  558. ob_fml_imu.last_f_gry[1] == f_gry[1] && \
  559. ob_fml_imu.last_f_gry[2] == f_gry[2]
  560. )
  561. {
  562. ob_fml_imu.last_f_gry_err_sum++;
  563. if(ob_fml_imu.last_f_gry_err_sum >= FML_IMU_MONITOR_DATA_ERR_SUM_MAX){
  564. Except_SetExceptype(EXCEPT_DATA_FRONT_GRY);
  565. }
  566. }else{
  567. ob_fml_imu.last_f_gry_err_sum = 0;
  568. }
  569. ob_fml_imu.last_f_gry[0] = f_gry[0];
  570. ob_fml_imu.last_f_gry[1] = f_gry[1];
  571. ob_fml_imu.last_f_gry[2] = f_gry[2];
  572. }
  573. /*前脚地磁计*/
  574. if(f_mag != NULL)
  575. {
  576. if(
  577. ob_fml_imu.last_f_mag[0] == f_mag[0] && \
  578. ob_fml_imu.last_f_mag[1] == f_mag[1] && \
  579. ob_fml_imu.last_f_mag[2] == f_mag[2]
  580. )
  581. {
  582. ob_fml_imu.last_f_mag_err_sum++;
  583. if(ob_fml_imu.last_f_mag_err_sum >= FML_IMU_MONITOR_DATA_ERR_SUM_MAX){
  584. Except_SetExceptype(EXCEPT_DATA_FRONT_MAG);
  585. }
  586. }else{
  587. ob_fml_imu.last_f_mag_err_sum = 0;
  588. }
  589. ob_fml_imu.last_f_mag[0] = f_mag[0];
  590. ob_fml_imu.last_f_mag[1] = f_mag[1];
  591. ob_fml_imu.last_f_mag[2] = f_mag[2];
  592. }
  593. /*后脚地磁计*/
  594. if(b_mag != NULL)
  595. {
  596. if(
  597. ob_fml_imu.last_b_mag[0] == b_mag[0] && \
  598. ob_fml_imu.last_b_mag[1] == b_mag[1] && \
  599. ob_fml_imu.last_b_mag[2] == b_mag[2]
  600. )
  601. {
  602. ob_fml_imu.last_b_mag_err_sum++;
  603. if(ob_fml_imu.last_b_mag_err_sum >= FML_IMU_MONITOR_DATA_ERR_SUM_MAX){
  604. Except_SetExceptype(EXCEPT_DATA_BACK_MAG);
  605. }
  606. }else{
  607. ob_fml_imu.last_b_mag_err_sum = 0;
  608. }
  609. ob_fml_imu.last_b_mag[0] = b_mag[0];
  610. ob_fml_imu.last_b_mag[1] = b_mag[1];
  611. ob_fml_imu.last_b_mag[2] = b_mag[2];
  612. }
  613. //监测到前脚数据异常
  614. if(Except_IsError(EXCEPT_DATA_FRONT_ACC) || Except_IsError(EXCEPT_DATA_FRONT_GRY) || Except_IsError(EXCEPT_DATA_FRONT_MAG))
  615. {
  616. ob_fml_imu.except_data_occur_sum[FML_IMU_DIR_FRONT]++;
  617. if(ob_fml_imu.except_data_occur_sum[FML_IMU_DIR_FRONT] > 1)
  618. {
  619. if(Except_IsError(EXCEPT_DATA_FRONT_ACC)){Except_TxError(EXCEPT_DATA_FRONT_ACC,"front_acc_data_error");DEBUG_LOG("front_acc_data_error\n");}
  620. if(Except_IsError(EXCEPT_DATA_FRONT_GRY)){Except_TxError(EXCEPT_DATA_FRONT_GRY,"front_gry_data_error");DEBUG_LOG("front_gry_data_error\n");}
  621. if(Except_IsError(EXCEPT_DATA_FRONT_MAG)){Except_TxError(EXCEPT_DATA_FRONT_MAG,"front_mag_data_error");DEBUG_LOG("front_mag_data_error\n");}
  622. }
  623. //重启前脚LSM(主要是复位结构体)
  624. drv_lsm_power_off();
  625. drv_lsm_power_on();
  626. //重新开始配置
  627. fml_imu_start_config();
  628. //清除异常
  629. Except_ClearExceptype(EXCEPT_DATA_FRONT_ACC);
  630. Except_ClearExceptype(EXCEPT_DATA_FRONT_GRY);
  631. Except_ClearExceptype(EXCEPT_DATA_FRONT_MAG);
  632. }
  633. else
  634. {
  635. ob_fml_imu.except_data_occur_sum[FML_IMU_DIR_FRONT] = 0;
  636. //清除异常
  637. Except_ClearExceptype(EXCEPT_DATA_FRONT_ACC);
  638. Except_ClearExceptype(EXCEPT_DATA_FRONT_GRY);
  639. Except_ClearExceptype(EXCEPT_DATA_FRONT_MAG);
  640. }
  641. //监测到后脚数据异常
  642. if(Except_IsError(EXCEPT_DATA_BACK_MAG))
  643. {
  644. ob_fml_imu.except_data_occur_sum[FML_IMU_DIR_BACK]++;
  645. if(ob_fml_imu.except_data_occur_sum[FML_IMU_DIR_BACK] > 1)
  646. {
  647. if(Except_IsError(EXCEPT_DATA_BACK_MAG))Except_TxError(EXCEPT_DATA_BACK_MAG,"back_mag_data_error");
  648. }
  649. //重启后脚QMC(主要是复位结构体)
  650. drv_qmc6310_power_off();
  651. drv_qmc6310_power_on();
  652. //重新开始配置
  653. fml_imu_start_config();
  654. //清除异常
  655. Except_ClearExceptype(EXCEPT_DATA_BACK_MAG);
  656. }
  657. else
  658. {
  659. ob_fml_imu.except_data_occur_sum[FML_IMU_DIR_BACK] = 0;
  660. //清除异常
  661. Except_ClearExceptype(EXCEPT_DATA_BACK_MAG);
  662. }
  663. }
  664. static void fml_imu_monitor_sensor_data_process(void)
  665. {
  666. int i = 0;
  667. int16_t group_num = 0;
  668. static int16_t f_acc[3];
  669. static int16_t f_gry[3];
  670. static int16_t f_mag[3];
  671. static int16_t b_mag[3];
  672. fml_imu_data_t data;
  673. static uint32_t last_tim = 0;
  674. if(
  675. ob_fml_imu.stage == FML_IMU_CONFIG_STAGE_DONE && \
  676. ob_fml_imu.drv_param[FML_IMU_DIR_FRONT].lsm.acc_odr == LSM_ACC_ODR_104HZ && \
  677. ob_fml_imu.drv_param[FML_IMU_DIR_FRONT].lsm.gry_odr == LSM_GRY_ODR_104HZ && \
  678. (ob_fml_imu.drv_param[FML_IMU_DIR_FRONT].lsm.mag_odr == LSM_MAG_ODR_200HZ || ob_fml_imu.drv_param[FML_IMU_DIR_FRONT].lsm.mag_odr == LSM_MAG_ODR_100HZ)
  679. )
  680. {
  681. if(TIME_GetTicks()-last_tim >= FullPower_Interval) //监测前脚传感器数据(acc + gry + mag)+ 监测后脚传感器数据(mag)
  682. {
  683. last_tim = TIME_GetTicks();
  684. group_num = fml_imu_get_data_num(FML_IMU_DIR_FRONT);
  685. if(group_num > 0)
  686. {
  687. for(i=0;i<group_num;i++)
  688. {
  689. fml_imu_get_data(FML_IMU_DIR_FRONT, i, &data);
  690. f_gry[0] = data.gry[0];f_gry[1] = data.gry[1];f_gry[2] = data.gry[2];
  691. f_acc[0] = data.acc[0];f_acc[1] = data.acc[1];f_acc[2] = data.acc[2];
  692. f_mag[0] = data.mag[0];f_mag[1] = data.mag[1];f_mag[2] = data.mag[2];
  693. monitor_sensor_data(f_acc, f_gry, f_mag, NULL);
  694. }
  695. }
  696. if(fml_imu_get_data_num(FML_IMU_DIR_BACK) >= 1){
  697. fml_imu_get_data(FML_IMU_DIR_BACK, 0, &data);
  698. b_mag[0] = data.mag[0];b_mag[1] = data.mag[1];b_mag[2] = data.mag[2];
  699. monitor_sensor_data(NULL, NULL, NULL, b_mag);
  700. }
  701. }
  702. }
  703. else if(
  704. ob_fml_imu.stage == FML_IMU_CONFIG_STAGE_DONE && \
  705. ob_fml_imu.drv_param[FML_IMU_DIR_FRONT].lsm.acc_odr == LSM_ACC_ODR_104HZ && \
  706. (ob_fml_imu.drv_param[FML_IMU_DIR_FRONT].lsm.mag_odr == LSM_MAG_ODR_200HZ || ob_fml_imu.drv_param[FML_IMU_DIR_FRONT].lsm.mag_odr == LSM_MAG_ODR_100HZ)
  707. )
  708. {
  709. if(TIME_GetTicks()-last_tim >= LowPower_Interval) //监测前脚传感器数据(acc + mag)
  710. {
  711. last_tim = TIME_GetTicks();
  712. group_num = fml_imu_get_data_num(FML_IMU_DIR_FRONT);
  713. if(group_num > 0)
  714. {
  715. for(i=0;i<group_num;i++)
  716. {
  717. fml_imu_get_data(FML_IMU_DIR_FRONT, i, &data);
  718. f_acc[0] = data.acc[0];f_acc[1] = data.acc[1];f_acc[2] = data.acc[2];
  719. f_mag[0] = data.mag[0];f_mag[1] = data.mag[1];f_mag[2] = data.mag[2];
  720. monitor_sensor_data(f_acc, NULL, f_mag, NULL);
  721. }
  722. }
  723. }
  724. }
  725. else if(ob_fml_imu.stage == FML_IMU_CONFIG_STAGE_DONE && ob_fml_imu.drv_param[FML_IMU_DIR_FRONT].lsm.acc_odr == LSM_ACC_ODR_12HZ5)
  726. {
  727. if(TIME_GetTicks()-last_tim >= StandByPower_Interval) //监测前脚传感器数据(acc)
  728. {
  729. last_tim = TIME_GetTicks();
  730. group_num = fml_imu_get_data_num(FML_IMU_DIR_FRONT);
  731. if(group_num > 0)
  732. {
  733. for(i=0;i<group_num;i++)
  734. {
  735. fml_imu_get_data(FML_IMU_DIR_FRONT, i, &data);
  736. f_acc[0] = data.acc[0];f_acc[1] = data.acc[1];f_acc[2] = data.acc[2];
  737. monitor_sensor_data(f_acc, NULL, NULL, NULL);
  738. }
  739. }
  740. }
  741. }
  742. }
  743. static void fml_imu_monitor_sensor_no_data_process(int cur_front_data_num, int cur_back_data_num)
  744. {
  745. static uint32_t front_no_data_tim = 0;
  746. static uint32_t back_no_data_tim = 0;
  747. static char buf[255];
  748. //数据异常检测——前脚没有数据
  749. if(cur_front_data_num == 0)
  750. {
  751. if(ob_fml_imu.drv_param[FML_IMU_DIR_FRONT].lsm.acc_odr != LSM_ACC_ODR_OFF || \
  752. ob_fml_imu.drv_param[FML_IMU_DIR_FRONT].lsm.gry_odr != LSM_GRY_ODR_OFF || \
  753. ob_fml_imu.drv_param[FML_IMU_DIR_FRONT].lsm.mag_odr != LSM_MAG_ODR_OFF)
  754. {
  755. //持续1000ms没数据
  756. if(front_no_data_tim == 0){
  757. front_no_data_tim = TIME_GetTicks();
  758. }else if(TIME_GetTicks()-front_no_data_tim>=1000){
  759. sprintf(buf,"Front Sensor short circuit\r\n");
  760. Except_TxError(EXCEPT_IMU_SUSPEND_OVERFLOW,buf);
  761. front_no_data_tim = 0;
  762. //重启前脚LSM(主要是复位结构体)
  763. drv_lsm_power_off();
  764. drv_lsm_power_on();
  765. //重新开始配置
  766. fml_imu_start_config();
  767. }
  768. }
  769. }
  770. else front_no_data_tim = 0;
  771. //数据异常检测——后脚没有数据
  772. if(cur_back_data_num == 0)
  773. {
  774. if(ob_fml_imu.drv_param[FML_IMU_DIR_BACK].qmc.mag_odr != QMC_MAG_ODR_OFF)
  775. {
  776. //持续1000ms没数据
  777. if(back_no_data_tim == 0){
  778. back_no_data_tim = TIME_GetTicks();
  779. }else if(TIME_GetTicks()-back_no_data_tim>=1000){
  780. sprintf(buf,"Back Sensor short circuit\r\n");
  781. Except_TxError(EXCEPT_IMU_SUSPEND_OVERFLOW,buf);
  782. back_no_data_tim = 0;
  783. //重启后脚QMC(主要是复位结构体)
  784. drv_qmc6310_power_off();
  785. drv_qmc6310_power_on();
  786. //重新开始配置
  787. fml_imu_start_config();
  788. }
  789. }
  790. }
  791. else back_no_data_tim = 0;
  792. }
  793. static void fml_imu_Process(void)
  794. {
  795. int data_num[FML_IMU_DIR_NUM];
  796. uint32_t dir_bit;
  797. static char buf[255];
  798. static uint32_t config_err_report_tim = 0;
  799. switch(ob_fml_imu.stage)
  800. {
  801. case FML_IMU_CONFIG_STAGE_DONE: //配置完成阶段
  802. dir_bit = 0;
  803. //读取前脚IMU数据 + 更新前脚IMU数据量
  804. data_num[FML_IMU_DIR_FRONT] = hal_ser_imu_read_data(FML_IMU_DIR_FRONT);
  805. if(data_num[FML_IMU_DIR_FRONT] > 0){
  806. ob_fml_imu.cur_data_num[FML_IMU_DIR_FRONT] = data_num[FML_IMU_DIR_FRONT];
  807. dir_bit |= (1 << FML_IMU_DIR_FRONT);
  808. }
  809. //读取后脚IMU数据 + 更新后脚IMU数据量
  810. data_num[FML_IMU_DIR_BACK] = hal_ser_imu_read_data(FML_IMU_DIR_BACK);
  811. if(data_num[FML_IMU_DIR_BACK] > 0){
  812. ob_fml_imu.cur_data_num[FML_IMU_DIR_BACK] = data_num[FML_IMU_DIR_BACK];
  813. dir_bit |= (1 << FML_IMU_DIR_BACK);
  814. }
  815. //数据异常检测——没有数据
  816. fml_imu_monitor_sensor_no_data_process(data_num[FML_IMU_DIR_FRONT], data_num[FML_IMU_DIR_BACK]);
  817. //数据异常检测——重复数据
  818. fml_imu_monitor_sensor_data_process();
  819. //数据回调通知
  820. if(dir_bit != 0)ob_fml_imu.data_notify_cb(dir_bit);
  821. break;
  822. case FML_IMU_CONFIG_STAGE_IN_PROGRESS: //配置进行阶段
  823. if(fml_imu_intergrated_setting() == -1)
  824. {
  825. //配置失败
  826. ob_fml_imu.stage = FML_IMU_CONFIG_STAGE_FAIL;
  827. //断电前后脚
  828. fml_imu_close(FML_IMU_DIR_FRONT);
  829. fml_imu_close(FML_IMU_DIR_BACK);
  830. //通知配置失败
  831. ob_fml_imu.config_cb(ob_fml_imu.config_flow);
  832. //上报异常
  833. sprintf(buf,"front_imu_suspend_overflow:0x%x\r\n",ob_fml_imu.config_flow);
  834. Except_TxError(EXCEPT_IMU_SUSPEND_OVERFLOW,buf);
  835. //解除全功率运行
  836. Process_SetHoldOn(fml_imu_Process,0);
  837. }
  838. else
  839. {
  840. if(ob_fml_imu.config_flow == FML_IMU_CONFIG_FLOW_DONE)
  841. {
  842. //配置成功
  843. ob_fml_imu.stage = FML_IMU_CONFIG_STAGE_DONE;
  844. //通知配置成功
  845. ob_fml_imu.config_cb(ob_fml_imu.config_flow);
  846. //更新驱动LSM配置参数
  847. drv_lsm_get_config_param(&ob_fml_imu.drv_param[FML_IMU_DIR_FRONT].lsm);
  848. //更新驱动LSM配置参数
  849. drv_qmc6310_get_config_param(&ob_fml_imu.drv_param[FML_IMU_DIR_BACK].qmc);
  850. //解除全功率运行
  851. Process_SetHoldOn(fml_imu_Process,0);
  852. }
  853. }
  854. break;
  855. default:
  856. //间隔1000ms上报配置失败错误
  857. if(config_err_report_tim == 0){
  858. config_err_report_tim = TIME_GetTicks();
  859. }else if(TIME_GetTicks()-config_err_report_tim>=1000){
  860. sprintf(buf,"front_imu_suspend_overflow:0x%x\r\n",ob_fml_imu.config_flow);
  861. Except_TxError(EXCEPT_IMU_SUSPEND_OVERFLOW,buf);
  862. config_err_report_tim = 0;
  863. }
  864. break;
  865. }
  866. }
  867. /*API ------------------------------------------------------------------------------------------------------------------------------------*/
  868. /**
  869. @brief 初始化IMU功能模块
  870. @param 无
  871. @return 错误代码 - [out] -1失败,0成功
  872. */
  873. int fml_imu_Init(void)
  874. {
  875. int ret;
  876. /***************************************驱动层初始化***************************************************/
  877. //重置结构体
  878. memset(&ob_fml_imu,0,sizeof(ob_fml_imu));
  879. //初始化驱动LSM
  880. ret = drv_lsm_Init();
  881. //初始化驱动QMC
  882. ret += drv_qmc6310_Init();
  883. //获取驱动LSM配置参数
  884. drv_lsm_get_config_param(&ob_fml_imu.drv_param[FML_IMU_DIR_FRONT].lsm);
  885. //获取驱动LSM配置参数
  886. drv_qmc6310_get_config_param(&ob_fml_imu.drv_param[FML_IMU_DIR_BACK].qmc);
  887. /***************************************业务逻辑层初始化***************************************************/
  888. //初始化IMU服务结构体
  889. ob_fml_imu.stage = FML_IMU_CONFIG_STAGE_DONE;
  890. //设置驱动配置线程
  891. Process_Start(0,"fml_imu_Process",fml_imu_Process);
  892. if(ret < 0)return -1;
  893. return 0;
  894. }
  895. /**
  896. @brief 设置需要配置IMU的参数
  897. @param param - [in] 设置IMU的参数
  898. @return 错误代码 - [out] -1失败,0成功
  899. */
  900. int fml_imu_config_param(FML_IMU_DIR_e dir, fml_imu_param_t *param)
  901. {
  902. if(param == NULL || dir >= FML_IMU_DIR_NUM)return -1;
  903. memcpy(&ob_fml_imu.config_param[dir],param,sizeof(fml_imu_param_t));
  904. return 0;
  905. }
  906. /**
  907. @brief 注册IMU配置结果回调函数
  908. @param cb - [in] 回调函数
  909. @return 错误代码 - [out] -1失败,0成功
  910. */
  911. int fml_imu_register_config_callback(fml_imu_config_cb cb)
  912. {
  913. if(cb == NULL)return -1;
  914. ob_fml_imu.config_cb = cb;
  915. return 0;
  916. }
  917. /**
  918. @brief 注册IMU数据通知回调函数
  919. @param cb - [in] 回调函数
  920. @return 错误代码 - [out] -1失败,0成功
  921. */
  922. int fml_imu_register_data_notify_callback(fml_imu_data_notify_cb cb)
  923. {
  924. if(cb == NULL)return -1;
  925. ob_fml_imu.data_notify_cb = cb;
  926. return 0;
  927. }
  928. /**
  929. @brief 开始配置IMU
  930. @param param 无
  931. @return 错误代码 - [out] -1失败,0成功
  932. */
  933. int fml_imu_start_config(void)
  934. {
  935. drv_param_u lsm_drv_param;
  936. drv_param_u qmc_drv_param;
  937. //获取驱动LSM配置参数
  938. drv_lsm_get_config_param(&ob_fml_imu.drv_param[FML_IMU_DIR_FRONT].lsm);
  939. //获取驱动LSM配置参数
  940. drv_qmc6310_get_config_param(&ob_fml_imu.drv_param[FML_IMU_DIR_BACK].qmc);
  941. //转换参数
  942. fml_imu_macro_conversion(FML_IMU_DIR_FRONT, &ob_fml_imu.config_param[FML_IMU_DIR_FRONT], &lsm_drv_param);
  943. fml_imu_macro_conversion(FML_IMU_DIR_BACK, &ob_fml_imu.config_param[FML_IMU_DIR_BACK], &qmc_drv_param);
  944. //进行对比
  945. if((memcmp(&ob_fml_imu.drv_param[FML_IMU_DIR_FRONT].lsm,&lsm_drv_param.lsm,sizeof(lsm_drv_param.lsm)) != 0) || \
  946. (memcmp(&ob_fml_imu.drv_param[FML_IMU_DIR_BACK].qmc,&qmc_drv_param.qmc,sizeof(qmc_drv_param.qmc)) != 0)
  947. )
  948. {
  949. ob_fml_imu.stage = FML_IMU_CONFIG_STAGE_IN_PROGRESS;
  950. ob_fml_imu.config_flow = 0;
  951. //全功率运行进行配置
  952. Process_SetHoldOn(fml_imu_Process,1);
  953. DEBUG_LOG("------------------------------------------------------------->enter fml_imu_start_config\n");
  954. nrf_delay_ms(3);
  955. }
  956. return 0;
  957. }
  958. /**
  959. @brief 读取当前IMU数据的数量
  960. @param dir - [in] 方向
  961. @return 返回当前IMU数据的数量
  962. */
  963. int fml_imu_get_data_num(FML_IMU_DIR_e dir)
  964. {
  965. if(dir >= FML_IMU_DIR_NUM)return 0;
  966. return ob_fml_imu.cur_data_num[dir];
  967. }
  968. /**
  969. @brief 获取当前IMU数据
  970. @param dir - [in] 方向
  971. @param index - [in] 数据索引
  972. @param pdata - [out] 返回的IMU数据指针
  973. @return 错误代码 - [out] -1失败,0成功
  974. */
  975. int fml_imu_get_data(FML_IMU_DIR_e dir, int index, fml_imu_data_t *pdata)
  976. {
  977. if(index < 0 || pdata == NULL || dir >= FML_IMU_DIR_NUM)return -1;
  978. *pdata = ob_fml_imu.cur_data[dir][index];
  979. return 0;
  980. }
  981. /**
  982. @brief 关闭IMU
  983. @param dir - [in] 方向
  984. @return 错误代码 - [out] -1失败,0成功
  985. */
  986. int fml_imu_close(FML_IMU_DIR_e dir)
  987. {
  988. if(dir >= FML_IMU_DIR_NUM)return -1;
  989. switch(dir)
  990. {
  991. case FML_IMU_DIR_FRONT:
  992. drv_lsm_power_on(); //主要是复位结构体
  993. drv_lsm_power_off();
  994. break;
  995. case FML_IMU_DIR_BACK:
  996. drv_qmc6310_power_on(); //主要是复位结构体
  997. drv_qmc6310_power_off();
  998. break;
  999. default:
  1000. break;
  1001. }
  1002. return 0;
  1003. }