hal_ser_imu.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  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 "exception.h"
  43. #include "system.h"
  44. #include "hal_ser_imu_mode_manage.h"
  45. #include "hal_ser_imu.h"
  46. #include "bsp_time.h"
  47. #include "nrf_delay.h"
  48. /*Private macro ------------------------------------------------------------------------------------------------------------------------------------*/
  49. #define SER_IMU_HANDLE_NUM_MAX HAL_SER_IMU_MODE_MANAGE_NUM //允许最大的句柄数量
  50. #define INVALID_HANDLE 0x00 //无效句柄
  51. #define VALID_HANDLE 0x01 //有效句柄
  52. #define MAX(a,b) (((a) > (b)) ? (a) : (b)) //对比大小宏
  53. #define IS_EQUAL(a,b) (((a) == (b)) ? true : false) //对比相等宏
  54. #define SER_IMU_DATA_GROUP_NUM_MAX 20 //服务IMU最大数据组数
  55. #define MONITOR_DATA_ERR_SUM_MAX 100 //数据监测错误累计最大值
  56. #define MONITOR_SUSPEND_MODE_OVERFLOW_ERR_SUM_MAX 10 //异常挂起模式持续错误累计最大值
  57. #define DATA_UPDATE_NOTIFY_MAX 32 //数据更新通知回调最大数
  58. /*STRUCTION ------------------------------------------------------------------------------------------------------------------------------------*/
  59. typedef enum {
  60. IMU_CONFIG_STAGE_DONE, //配置完成阶段
  61. IMU_CONFIG_STAGE_IN_PROGRESS, //配置进行阶段
  62. } IMU_CONFIG_STAGE_e;
  63. typedef struct
  64. {
  65. int id; //句柄id
  66. char *name; //句柄名称
  67. ser_imu_config_param_t op_param[SER_IMU_DIR_NUM]; //操作的IMU配置参数副本
  68. } ser_imu_handle_t;
  69. typedef union
  70. {
  71. drv_lsm_config_param_t lsm; //当前的LSM配置参数
  72. drv_qmc_config_param_t qmc; //当前的QMC配置参数
  73. } drv_param_u;
  74. typedef struct ser_imu
  75. {
  76. /*private member*/
  77. IMU_CONFIG_STAGE_e stage; //配置流程状态
  78. bool is_need_config[SER_IMU_DIR_NUM]; //各个方位的IMU是否需要配置
  79. ser_imu_handle_t handle[SER_IMU_HANDLE_NUM_MAX]; //句柄组
  80. drv_param_u drv_param[SER_IMU_DIR_NUM]; //驱动配置参数组
  81. ser_imu_config_param_t compre_drv_param[SER_IMU_DIR_NUM]; //综合的驱动配置参数
  82. int cur_data_num[SER_IMU_DIR_NUM]; //当前的数据量
  83. ser_imu_data_t cur_data[SER_IMU_DIR_NUM][SER_IMU_DATA_GROUP_NUM_MAX]; //当前的IMU数据缓存区
  84. int16_t last_f_acc[3]; //上一次的前脚加速度值
  85. int16_t last_f_gry[3]; //上一次的前脚陀螺仪值
  86. int16_t last_f_mag[3]; //上一次的前脚地磁计值
  87. int16_t last_b_mag[3]; //上一次的后脚地磁计值
  88. int16_t last_f_acc_err_sum; //上一次的前脚加速度值错误累计
  89. int16_t last_f_gry_err_sum; //上一次的前脚陀螺仪值错误累计
  90. int16_t last_f_mag_err_sum; //上一次的前脚地磁计值错误累计
  91. int16_t last_b_mag_err_sum; //上一次的后脚地磁计值错误累计
  92. int16_t except_data_occur_sum[SER_IMU_DIR_NUM]; //异常数据产生次数累计
  93. int16_t except_suspend_occur_sum; //异常挂起产生次数累计
  94. hal_ser_imu_data_update_callback notify_cb[DATA_UPDATE_NOTIFY_MAX]; //数据更新通知回调
  95. } Ser_Imu_t;
  96. /*Local Variable ------------------------------------------------------------------------------------------------------------------------------------*/
  97. static Ser_Imu_t ob_ser_imu;
  98. /*Local Functions ------------------------------------------------------------------------------------------------------------------------------------*/
  99. static int hal_ser_imu_macro_conversion_acc_odr(SER_IMU_DIR_e dir, SER_IMU_ACC_ODR_e acc_odr)
  100. {
  101. int ret = 0;
  102. if(dir == SER_IMU_DIR_FRONT)
  103. {
  104. switch(acc_odr)
  105. {
  106. case SER_IMU_ACC_ODR_OFF:
  107. ret = LSM_ACC_ODR_OFF;
  108. break;
  109. case SER_IMU_ACC_ODR_104HZ:
  110. ret = LSM_ACC_ODR_104HZ;
  111. break;
  112. case SER_IMU_ACC_ODR_12HZ5:
  113. ret = LSM_ACC_ODR_12HZ5;
  114. break;
  115. }
  116. }
  117. return ret;
  118. }
  119. static int hal_ser_imu_macro_conversion_gry_odr(SER_IMU_DIR_e dir, SER_IMU_GRY_ODR_e gry_odr)
  120. {
  121. int ret = 0;
  122. if(dir == SER_IMU_DIR_FRONT)
  123. {
  124. switch(gry_odr)
  125. {
  126. case SER_IMU_GRY_ODR_OFF:
  127. ret = LSM_GRY_ODR_OFF;
  128. break;
  129. case SER_IMU_GRY_ODR_104HZ:
  130. ret = LSM_GRY_ODR_104HZ;
  131. break;
  132. case SER_IMU_GRY_ODR_12HZ5:
  133. ret = LSM_GRY_ODR_12HZ5;
  134. break;
  135. }
  136. }
  137. return ret;
  138. }
  139. static int hal_ser_imu_macro_conversion_mag_odr(SER_IMU_DIR_e dir, SER_IMU_MAG_ODR_e mag_odr)
  140. {
  141. int ret = 0;
  142. if(dir == SER_IMU_DIR_FRONT)
  143. {
  144. switch(mag_odr)
  145. {
  146. case SER_IMU_MAG_ODR_OFF:
  147. ret = LSM_MAG_ODR_OFF;
  148. break;
  149. case SER_IMU_MAG_ODR_10HZ:
  150. ret = LSM_MAG_ODR_10HZ;
  151. break;
  152. case SER_IMU_MAG_ODR_100HZ:
  153. ret = LSM_MAG_ODR_100HZ;
  154. break;
  155. case SER_IMU_MAG_ODR_200HZ:
  156. ret = LSM_MAG_ODR_200HZ;
  157. break;
  158. }
  159. }
  160. if(dir == SER_IMU_DIR_BACK)
  161. {
  162. switch(mag_odr)
  163. {
  164. case SER_IMU_MAG_ODR_OFF:
  165. ret = QMC_MAG_ODR_OFF;
  166. break;
  167. case SER_IMU_MAG_ODR_10HZ:
  168. ret = QMC_MAG_ODR_10HZ;
  169. break;
  170. case SER_IMU_MAG_ODR_100HZ:
  171. ret = QMC_MAG_ODR_100HZ;
  172. break;
  173. case SER_IMU_MAG_ODR_200HZ:
  174. ret = QMC_MAG_ODR_200HZ;
  175. break;
  176. }
  177. }
  178. return ret;
  179. }
  180. static int hal_ser_imu_macro_conversion_fifo_odr(SER_IMU_DIR_e dir, SER_IMU_FIFO_ODR_e fifo_odr)
  181. {
  182. int ret = 0;
  183. if(dir == SER_IMU_DIR_FRONT)
  184. {
  185. switch(fifo_odr)
  186. {
  187. case SER_IMU_FIFO_ODR_OFF:
  188. ret = LSM_FIFO_ODR_OFF;
  189. break;
  190. case SER_IMU_FIFO_ODR_104HZ:
  191. ret = LSM_FIFO_ODR_104HZ;
  192. break;
  193. }
  194. }
  195. return ret;
  196. }
  197. static int hal_ser_imu_macro_conversion_acc_power_mode(SER_IMU_DIR_e dir, SER_IMU_ACC_POWER_MODE_e acc_power_mode)
  198. {
  199. int ret = 0;
  200. if(dir == SER_IMU_DIR_FRONT)
  201. {
  202. switch(acc_power_mode)
  203. {
  204. case SER_IMU_ACC_POWER_MODE_HIGH_PERFORMANCE:
  205. ret = LSM_ACC_POWER_MODE_HIGH_PERFORMANCE;
  206. break;
  207. case SER_IMU_ACC_POWER_MODE_NORMAL:
  208. ret = LSM_ACC_POWER_MODE_NORMAL;
  209. break;
  210. }
  211. }
  212. return ret;
  213. }
  214. static int hal_ser_imu_macro_conversion_gry_power_mode(SER_IMU_DIR_e dir, SER_IMU_GRY_POWER_MODE_e gry_power_mode)
  215. {
  216. int ret = 0;
  217. if(dir == SER_IMU_DIR_FRONT)
  218. {
  219. switch(gry_power_mode)
  220. {
  221. case SER_IMU_GRY_POWER_MODE_HIGH_PERFORMANCE:
  222. ret = LSM_GRY_POWER_MODE_HIGH_PERFORMANCE;
  223. break;
  224. case SER_IMU_GRY_POWER_MODE_NORMAL:
  225. ret = LSM_GRY_POWER_MODE_NORMAL;
  226. break;
  227. }
  228. }
  229. return ret;
  230. }
  231. static int hal_ser_imu_macro_conversion_acc_fs(SER_IMU_DIR_e dir, SER_IMU_ACC_FS_e acc_fs)
  232. {
  233. int ret = 0;
  234. if(dir == SER_IMU_DIR_FRONT)
  235. {
  236. switch(acc_fs)
  237. {
  238. case SER_IMU_ACC_FS_2G:
  239. ret = LSM_ACC_FS_2G;
  240. break;
  241. case SER_IMU_ACC_FS_16G:
  242. ret = LSM_ACC_FS_16G;
  243. break;
  244. }
  245. }
  246. return ret;
  247. }
  248. static int hal_ser_imu_macro_conversion_gry_fs(SER_IMU_DIR_e dir, SER_IMU_GRY_FS_e gry_fs)
  249. {
  250. int ret = 0;
  251. if(dir == SER_IMU_DIR_FRONT)
  252. {
  253. switch(gry_fs)
  254. {
  255. case SER_IMU_GRY_FS_250DPS:
  256. ret = LSM_GRY_FS_250DPS;
  257. break;
  258. case SER_IMU_GRY_FS_2000DPS:
  259. ret = LSM_GRY_FS_2000DPS;
  260. break;
  261. }
  262. }
  263. return ret;
  264. }
  265. static int hal_ser_imu_macro_conversion_mag_fs(SER_IMU_DIR_e dir, SER_IMU_MAG_FS_e mag_fs)
  266. {
  267. int ret = 0;
  268. if(dir == SER_IMU_DIR_FRONT)
  269. {
  270. switch(mag_fs)
  271. {
  272. case SER_IMU_MAG_FS_30GS:
  273. ret = LSM_MAG_FS_30GS;
  274. break;
  275. }
  276. }
  277. if(dir == SER_IMU_DIR_BACK)
  278. {
  279. switch(mag_fs)
  280. {
  281. case SER_IMU_MAG_FS_30GS:
  282. ret = QMC_MAG_FS_30GS;
  283. break;
  284. }
  285. }
  286. return ret;
  287. }
  288. static int hal_ser_imu_macro_conversion_timestamp_resolution(SER_IMU_DIR_e dir, SER_IMU_TIMESTAMP_RESOLUTION_e timestamp_resolution)
  289. {
  290. int ret = 0;
  291. if(dir == SER_IMU_DIR_FRONT)
  292. {
  293. switch(timestamp_resolution)
  294. {
  295. case SER_IMU_TIMESTAMP_6MS4:
  296. ret = LSM_TIMESTAMP_6MS4;
  297. break;
  298. case SER_IMU_TIMESTAMP_25US:
  299. ret = LSM_TIMESTAMP_25US;
  300. break;
  301. }
  302. }
  303. return ret;
  304. }
  305. static int hal_ser_imu_macro_conversion_timestamp_switch(SER_IMU_DIR_e dir, SER_IMU_TIMESTAMP_SWITCH_e timestamp_switch)
  306. {
  307. int ret = 0;
  308. if(dir == SER_IMU_DIR_FRONT)
  309. {
  310. switch(timestamp_switch)
  311. {
  312. case SER_IMU_TIMESTAMP_OFF:
  313. ret = LSM_TIMESTAMP_OFF;
  314. break;
  315. case SER_IMU_TIMESTAMP_ON:
  316. ret = LSM_TIMESTAMP_ON;
  317. break;
  318. }
  319. }
  320. return ret;
  321. }
  322. static void hal_ser_imu_get_compre_param(ser_imu_config_param_t *compre_drv_param, ser_imu_config_param_t *op_param)
  323. {
  324. compre_drv_param->acc_fs = MAX(compre_drv_param->acc_fs, op_param->acc_fs);
  325. compre_drv_param->acc_odr = MAX(compre_drv_param->acc_odr, op_param->acc_odr);
  326. compre_drv_param->acc_power_mode = MAX(compre_drv_param->acc_power_mode, op_param->acc_power_mode);
  327. compre_drv_param->fifo_odr = MAX(compre_drv_param->fifo_odr, op_param->fifo_odr);
  328. compre_drv_param->gry_fs = MAX(compre_drv_param->gry_fs, op_param->gry_fs);
  329. compre_drv_param->gry_odr = MAX(compre_drv_param->gry_odr, op_param->gry_odr);
  330. compre_drv_param->gry_power_mode = MAX(compre_drv_param->gry_power_mode, op_param->gry_power_mode);
  331. compre_drv_param->mag_fs = MAX(compre_drv_param->mag_fs, op_param->mag_fs);
  332. compre_drv_param->mag_odr = MAX(compre_drv_param->mag_odr, op_param->mag_odr);
  333. compre_drv_param->timestamp_resolution = MAX(compre_drv_param->timestamp_resolution, op_param->timestamp_resolution);
  334. compre_drv_param->timestamp_switch = MAX(compre_drv_param->timestamp_switch, op_param->timestamp_switch);
  335. }
  336. static bool hal_ser_imu_param_is_equal_drv_param_lsm(ser_imu_config_param_t *compre_drv_param, drv_param_u *drv_param)
  337. {
  338. if(IS_EQUAL(hal_ser_imu_macro_conversion_acc_fs(SER_IMU_DIR_FRONT, compre_drv_param->acc_fs),drv_param->lsm.acc_fs) == false){return false;}
  339. if(IS_EQUAL(hal_ser_imu_macro_conversion_acc_odr(SER_IMU_DIR_FRONT, compre_drv_param->acc_odr),drv_param->lsm.acc_odr) == false){return false;}
  340. if(IS_EQUAL(hal_ser_imu_macro_conversion_acc_power_mode(SER_IMU_DIR_FRONT, compre_drv_param->acc_power_mode),drv_param->lsm.acc_power_mode) == false){return false;}
  341. if(IS_EQUAL(hal_ser_imu_macro_conversion_fifo_odr(SER_IMU_DIR_FRONT, compre_drv_param->fifo_odr),drv_param->lsm.fifo_odr) == false){return false;}
  342. if(IS_EQUAL(hal_ser_imu_macro_conversion_gry_fs(SER_IMU_DIR_FRONT, compre_drv_param->gry_fs),drv_param->lsm.gry_fs) == false){return false;}
  343. if(IS_EQUAL(hal_ser_imu_macro_conversion_gry_odr(SER_IMU_DIR_FRONT, compre_drv_param->gry_odr),drv_param->lsm.gry_odr) == false){return false;}
  344. if(IS_EQUAL(hal_ser_imu_macro_conversion_gry_power_mode(SER_IMU_DIR_FRONT, compre_drv_param->gry_power_mode),drv_param->lsm.gry_power_mode) == false){return false;}
  345. if(IS_EQUAL(hal_ser_imu_macro_conversion_mag_fs(SER_IMU_DIR_FRONT, compre_drv_param->mag_fs),drv_param->lsm.mag_fs) == false){return false;}
  346. if(IS_EQUAL(hal_ser_imu_macro_conversion_mag_odr(SER_IMU_DIR_FRONT, compre_drv_param->mag_odr),drv_param->lsm.mag_odr) == false){return false;}
  347. if(IS_EQUAL(hal_ser_imu_macro_conversion_timestamp_resolution(SER_IMU_DIR_FRONT, compre_drv_param->timestamp_resolution),drv_param->lsm.timestamp_resolution) == false){return false;}
  348. if(IS_EQUAL(hal_ser_imu_macro_conversion_timestamp_switch(SER_IMU_DIR_FRONT, compre_drv_param->timestamp_switch),drv_param->lsm.timestamp_switch) == false){return false;}
  349. return true;
  350. }
  351. static bool hal_ser_imu_param_is_equal_drv_param_qmc(ser_imu_config_param_t *compre_drv_param, drv_param_u *drv_param)
  352. {
  353. if(IS_EQUAL(hal_ser_imu_macro_conversion_mag_fs(SER_IMU_DIR_BACK, compre_drv_param->mag_fs), drv_param->qmc.mag_fs) == false){return false;}
  354. if(IS_EQUAL(hal_ser_imu_macro_conversion_mag_odr(SER_IMU_DIR_BACK, compre_drv_param->mag_odr), drv_param->qmc.mag_odr) == false){return false;}
  355. return true;
  356. }
  357. static int hal_ser_imu_read_data_lsm(void)
  358. {
  359. int i;
  360. int fifo_group_num;
  361. lsm_data_t temp_lsm_data;
  362. //读取FIFO数据
  363. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].fifo_odr != SER_IMU_FIFO_ODR_OFF)
  364. {
  365. //获取当前fifo里存在多少组数据。
  366. fifo_group_num = drv_lsm_get_fifo_group_num();
  367. fifo_group_num = fifo_group_num <= SER_IMU_DATA_GROUP_NUM_MAX ? fifo_group_num : SER_IMU_DATA_GROUP_NUM_MAX;
  368. for(i=0; i<fifo_group_num; i++)
  369. {
  370. if(drv_lsm_get_fifo_data(&temp_lsm_data) != -1)
  371. {
  372. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].acc[0] = temp_lsm_data.acc[0];
  373. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].acc[1] = temp_lsm_data.acc[1];
  374. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].acc[2] = temp_lsm_data.acc[2];
  375. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].gry[0] = temp_lsm_data.gry[0];
  376. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].gry[1] = temp_lsm_data.gry[1];
  377. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].gry[2] = temp_lsm_data.gry[2];
  378. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].mag[0] = temp_lsm_data.mag[0];
  379. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].mag[1] = temp_lsm_data.mag[1];
  380. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].mag[2] = temp_lsm_data.mag[2];
  381. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].fifo_timestamp = temp_lsm_data.fifo_timestamp;
  382. }
  383. }
  384. return fifo_group_num;
  385. }
  386. //只读取ACC数据
  387. else if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].acc_odr != SER_IMU_ACC_ODR_OFF && \
  388. ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].gry_odr == SER_IMU_GRY_ODR_OFF && \
  389. ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].mag_odr == SER_IMU_MAG_ODR_OFF \
  390. )
  391. {
  392. if(drv_lsm_get_acc_data(&temp_lsm_data) != -1)
  393. {
  394. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][0].acc[0] = temp_lsm_data.acc[0];
  395. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][0].acc[1] = temp_lsm_data.acc[1];
  396. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][0].acc[2] = temp_lsm_data.acc[2];
  397. return 1;
  398. }
  399. }
  400. return 0;
  401. }
  402. static int hal_ser_imu_read_data_qmc(void)
  403. {
  404. qmc_data_t temp_qmc_data;
  405. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_BACK].mag_odr != SER_IMU_MAG_ODR_OFF)
  406. {
  407. if(drv_qmc6310_get_mag_data(&temp_qmc_data) != -1)
  408. {
  409. ob_ser_imu.cur_data[SER_IMU_DIR_BACK][0].mag[0] = temp_qmc_data.mag[0];
  410. ob_ser_imu.cur_data[SER_IMU_DIR_BACK][0].mag[1] = temp_qmc_data.mag[1];
  411. ob_ser_imu.cur_data[SER_IMU_DIR_BACK][0].mag[2] = temp_qmc_data.mag[2];
  412. return 1;
  413. }
  414. }
  415. return 0;
  416. }
  417. static int hal_ser_imu_read_data(SER_IMU_DIR_e dir)
  418. {
  419. int ret = 0;
  420. switch(dir)
  421. {
  422. case SER_IMU_DIR_FRONT:
  423. ret = hal_ser_imu_read_data_lsm();
  424. break;
  425. case SER_IMU_DIR_BACK:
  426. ret = hal_ser_imu_read_data_qmc();
  427. break;
  428. default:
  429. break;
  430. }
  431. return ret;
  432. }
  433. static bool hal_ser_imu_is_need_config(void)
  434. {
  435. int i;
  436. //重新获取综合的前脚配置,根据优先级来判定
  437. memset(&ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT],0,sizeof(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT]));
  438. for(i=0; i<SER_IMU_HANDLE_NUM_MAX; i++)
  439. {
  440. if(ob_ser_imu.handle[i].id == VALID_HANDLE)
  441. {
  442. hal_ser_imu_get_compre_param(&ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT], &ob_ser_imu.handle[i].op_param[SER_IMU_DIR_FRONT]);
  443. }
  444. }
  445. //重新获取驱动LSM配置参数
  446. drv_lsm_get_config_param(&ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm);
  447. //判断综合的前脚配置与当前的驱动配置是否一致
  448. if(hal_ser_imu_param_is_equal_drv_param_lsm(&ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT], &ob_ser_imu.drv_param[SER_IMU_DIR_FRONT]) == false)
  449. {
  450. ob_ser_imu.is_need_config[SER_IMU_DIR_FRONT] = true;
  451. }
  452. /************************************************************************************************************************************************/
  453. //重新获取综合的后脚配置,根据优先级来判定
  454. memset(&ob_ser_imu.compre_drv_param[SER_IMU_DIR_BACK],0,sizeof(ob_ser_imu.compre_drv_param[SER_IMU_DIR_BACK]));
  455. for(i=0; i<SER_IMU_HANDLE_NUM_MAX; i++)
  456. {
  457. if(ob_ser_imu.handle[i].id == VALID_HANDLE)
  458. {
  459. hal_ser_imu_get_compre_param(&ob_ser_imu.compre_drv_param[SER_IMU_DIR_BACK], &ob_ser_imu.handle[i].op_param[SER_IMU_DIR_BACK]);
  460. }
  461. }
  462. //重新获取驱动QMC配置参数
  463. drv_qmc6310_get_config_param(&ob_ser_imu.drv_param[SER_IMU_DIR_BACK].qmc);
  464. //判断综合的前脚配置与当前的驱动配置是否一致
  465. if(hal_ser_imu_param_is_equal_drv_param_qmc(&ob_ser_imu.compre_drv_param[SER_IMU_DIR_BACK], &ob_ser_imu.drv_param[SER_IMU_DIR_BACK]) == false)
  466. {
  467. ob_ser_imu.is_need_config[SER_IMU_DIR_BACK] = true;
  468. }
  469. /************************************************************************************************************************************************/
  470. for(i=0; i<SER_IMU_DIR_NUM; i++)
  471. {
  472. if(ob_ser_imu.is_need_config[i] == true){
  473. return true;
  474. }
  475. }
  476. return false;
  477. }
  478. static bool hal_ser_imu_config_dir_front(void)
  479. {
  480. int ret = 0;
  481. static int imu_config_flow_ctl = 0; //配置流程控制
  482. static int imu_mag_cur_flow_ctl;
  483. static int imu_mag_total_flow_ctl;
  484. static uint32_t tim = 0;
  485. // SEGGER_RTT_printf(0,"======= imu_config_flow_ctl:%d ======= \n",imu_config_flow_ctl);
  486. switch(imu_config_flow_ctl)
  487. {
  488. case 0:
  489. //配置lsm断电
  490. if(drv_lsm_power_off() == 0){imu_config_flow_ctl = 1;}
  491. break;
  492. case 1:
  493. //等待200ms
  494. if(tim == 0){
  495. tim = TIME_GetTicks();
  496. }else if(TIME_GetTicks()-tim>=200){
  497. tim = 0;
  498. imu_config_flow_ctl = 2;
  499. }
  500. break;
  501. case 2:
  502. //配置lsm上电(默认配置挂起)
  503. if(drv_lsm_power_on() == 0){
  504. imu_config_flow_ctl = 3;
  505. }
  506. break;
  507. case 3:
  508. //等待200ms
  509. if(tim == 0){
  510. tim = TIME_GetTicks();
  511. }else if(TIME_GetTicks()-tim>=200){
  512. tim = 0;
  513. imu_mag_total_flow_ctl = drv_lsm_get_mag_odr_flow();
  514. imu_mag_cur_flow_ctl = 1;
  515. imu_config_flow_ctl = 4;
  516. }
  517. break;
  518. case 4:
  519. //配置lsm的地磁计采样频率
  520. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].fifo_odr != SER_IMU_FIFO_ODR_OFF)
  521. {
  522. //使用hub
  523. if(drv_lsm_set_mag_odr((LSM_MAG_ODR_e)hal_ser_imu_macro_conversion_mag_odr(SER_IMU_DIR_FRONT,ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].mag_odr), 1, imu_mag_cur_flow_ctl) != -1){
  524. if(imu_mag_cur_flow_ctl == imu_mag_total_flow_ctl){
  525. imu_config_flow_ctl = 5; //进入下一个流程
  526. }
  527. else{
  528. imu_mag_cur_flow_ctl++; //进入下一个步骤
  529. tim = 0;
  530. }
  531. }
  532. else{
  533. if(tim == 0){
  534. tim = TIME_GetTicks();
  535. }else if(TIME_GetTicks()-tim>=1000){
  536. tim = 0;
  537. imu_config_flow_ctl = 0; //累计超过1s,重新走流程
  538. }
  539. }
  540. }
  541. else
  542. {
  543. //不使用hub
  544. if(drv_lsm_set_mag_odr((LSM_MAG_ODR_e)hal_ser_imu_macro_conversion_mag_odr(SER_IMU_DIR_FRONT,ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].mag_odr), 0, imu_mag_cur_flow_ctl) != -1){
  545. if(imu_mag_cur_flow_ctl == imu_mag_total_flow_ctl){
  546. imu_config_flow_ctl = 5; //进入下一个流程
  547. tim = 0;
  548. }
  549. else{
  550. imu_mag_cur_flow_ctl++; //进入下一个步骤
  551. tim = 0;
  552. }
  553. }
  554. else{
  555. if(tim == 0){
  556. tim = TIME_GetTicks();
  557. }else if(TIME_GetTicks()-tim>=1000){
  558. tim = 0;
  559. imu_config_flow_ctl = 0; //累计超过1s,重新走流程
  560. }
  561. }
  562. }
  563. break;
  564. case 5:
  565. //配置lsm的加速度工作模式
  566. ret += drv_lsm_set_acc_power_mode((LSM_ACC_POWER_MODE_e)hal_ser_imu_macro_conversion_acc_power_mode(SER_IMU_DIR_FRONT,ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].acc_power_mode));
  567. //配置lsm的陀螺仪工作模式
  568. ret += drv_lsm_set_gry_power_mode((LSM_GRY_POWER_MODE_e)hal_ser_imu_macro_conversion_gry_power_mode(SER_IMU_DIR_FRONT,ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].gry_power_mode));
  569. //配置lsm的加速度量程
  570. ret += drv_lsm_set_acc_fs((LSM_ACC_FS_e)hal_ser_imu_macro_conversion_acc_fs(SER_IMU_DIR_FRONT,ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].acc_fs));
  571. //配置lsm的陀螺仪量程
  572. ret += drv_lsm_set_gry_fs((LSM_GRY_FS_e)hal_ser_imu_macro_conversion_gry_fs(SER_IMU_DIR_FRONT,ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].gry_fs));
  573. //配置lsm的时间戳精度
  574. ret += drv_lsm_set_timestamp_resolution((LSM_TIMESTAMP_RESOLUTION_e)hal_ser_imu_macro_conversion_timestamp_resolution(SER_IMU_DIR_FRONT,ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].timestamp_resolution));
  575. //配置lsm的时间戳开关
  576. ret += drv_lsm_set_timestamp_switch((LSM_TIMESTAMP_SWITCH_e)hal_ser_imu_macro_conversion_timestamp_switch(SER_IMU_DIR_FRONT,ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].timestamp_switch));
  577. if(ret != -1)
  578. {
  579. imu_config_flow_ctl = 6;
  580. }
  581. break;
  582. case 6:
  583. //配置lsm的FIFO采样频率(必须以上都配置成功,才能配置FIFO)
  584. ret += drv_lsm_set_fifo_odr((LSM_FIFO_ODR_e)hal_ser_imu_macro_conversion_fifo_odr(SER_IMU_DIR_FRONT,ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].fifo_odr), \
  585. (LSM_ACC_ODR_e)hal_ser_imu_macro_conversion_acc_odr(SER_IMU_DIR_FRONT,ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].acc_odr), \
  586. (LSM_GRY_ODR_e)hal_ser_imu_macro_conversion_gry_odr(SER_IMU_DIR_FRONT,ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].gry_odr), \
  587. (LSM_MAG_ODR_e)hal_ser_imu_macro_conversion_mag_odr(SER_IMU_DIR_FRONT,ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].mag_odr), \
  588. (LSM_TIMESTAMP_SWITCH_e)hal_ser_imu_macro_conversion_timestamp_switch(SER_IMU_DIR_FRONT,ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].timestamp_switch));
  589. if(ret != -1)
  590. {
  591. imu_config_flow_ctl = 7;
  592. }
  593. break;
  594. case 7:
  595. //配置lsm的加速度采样频率
  596. ret += drv_lsm_set_acc_odr((LSM_ACC_ODR_e)hal_ser_imu_macro_conversion_acc_odr(SER_IMU_DIR_FRONT,ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].acc_odr));
  597. //配置lsm的陀螺仪采样频率
  598. ret += drv_lsm_set_gry_odr((LSM_GRY_ODR_e)hal_ser_imu_macro_conversion_gry_odr(SER_IMU_DIR_FRONT,ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].gry_odr));
  599. if(ret != -1)
  600. {
  601. imu_config_flow_ctl = 8;
  602. }
  603. break;
  604. case 8:
  605. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].acc_odr != SER_IMU_ACC_ODR_OFF || \
  606. ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].gry_odr != SER_IMU_GRY_ODR_OFF || \
  607. ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].mag_odr != SER_IMU_MAG_ODR_OFF
  608. )
  609. {
  610. if(hal_ser_imu_read_data(SER_IMU_DIR_FRONT) != 0)
  611. {
  612. imu_config_flow_ctl = 0;
  613. return true;
  614. }
  615. }
  616. else
  617. {
  618. imu_config_flow_ctl = 0;
  619. return true;
  620. }
  621. break;
  622. }
  623. return false;
  624. }
  625. static bool hal_ser_imu_config_dir_back(void)
  626. {
  627. int ret = 0;
  628. static int imu_config_flow_ctl = 0; //配置流程控制
  629. static int imu_mag_cur_flow_ctl;
  630. static int imu_mag_total_flow_ctl;
  631. static uint32_t tim = 0;
  632. switch(imu_config_flow_ctl)
  633. {
  634. case 0:
  635. //配置qmc断电
  636. if(drv_qmc6310_power_off() == 0){imu_config_flow_ctl = 1;}
  637. break;
  638. case 1:
  639. //等待200ms
  640. if(tim == 0){
  641. tim = TIME_GetTicks();
  642. }else if(TIME_GetTicks()-tim>=200){
  643. tim = 0;
  644. imu_config_flow_ctl = 2;
  645. }
  646. break;
  647. case 2:
  648. //配置qmc上电(默认配置挂起)
  649. if(drv_qmc6310_power_on() == 0){
  650. imu_config_flow_ctl = 3;
  651. }
  652. break;
  653. case 3:
  654. //等待200ms
  655. if(tim == 0){
  656. tim = TIME_GetTicks();
  657. }else if(TIME_GetTicks()-tim>=200){
  658. tim = 0;
  659. imu_mag_total_flow_ctl = drv_qmc6310_get_mag_odr_flow();
  660. imu_mag_cur_flow_ctl = 1;
  661. imu_config_flow_ctl = 4;
  662. }
  663. break;
  664. case 4:
  665. //配置qmc的地磁计采样频率
  666. ret = drv_qmc6310_set_mag_odr((QMC_MAG_ODR_e)hal_ser_imu_macro_conversion_mag_odr(SER_IMU_DIR_BACK,ob_ser_imu.compre_drv_param[SER_IMU_DIR_BACK].mag_odr),imu_mag_cur_flow_ctl);
  667. if(ret != -1)
  668. {
  669. if(imu_mag_cur_flow_ctl == imu_mag_total_flow_ctl){
  670. imu_config_flow_ctl = 5;
  671. }
  672. else{
  673. imu_mag_cur_flow_ctl++; //进入下一个步骤
  674. tim = 0;
  675. }
  676. }
  677. else
  678. {
  679. if(tim == 0){
  680. tim = TIME_GetTicks();
  681. }else if(TIME_GetTicks()-tim>=1000){
  682. tim = 0;
  683. imu_config_flow_ctl = 0; //累计超过1s,重新走流程
  684. }
  685. }
  686. break;
  687. case 5:
  688. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_BACK].mag_odr != SER_IMU_MAG_ODR_OFF)
  689. {
  690. if(hal_ser_imu_read_data(SER_IMU_DIR_BACK) != 0)
  691. {
  692. imu_config_flow_ctl = 0;
  693. return true;
  694. }
  695. }
  696. else
  697. {
  698. imu_config_flow_ctl = 0;
  699. return true;
  700. }
  701. break;
  702. }
  703. return false;
  704. }
  705. static bool hal_ser_imu_is_config_done(SER_IMU_DIR_e dir)
  706. {
  707. bool ret = true;
  708. switch(dir)
  709. {
  710. case SER_IMU_DIR_FRONT:
  711. ret = hal_ser_imu_config_dir_front();
  712. break;
  713. case SER_IMU_DIR_BACK:
  714. ret = hal_ser_imu_config_dir_back();
  715. break;
  716. default:
  717. break;
  718. }
  719. return ret;
  720. }
  721. static void monitor_sensor_data(int16_t *f_acc, int16_t *f_gry, int16_t *f_mag, int16_t *b_mag)
  722. {
  723. /*前脚加速度*/
  724. if(f_acc != NULL)
  725. {
  726. if(
  727. ob_ser_imu.last_f_acc[0] == f_acc[0] && \
  728. ob_ser_imu.last_f_acc[1] == f_acc[1] && \
  729. ob_ser_imu.last_f_acc[2] == f_acc[2]
  730. )
  731. {
  732. ob_ser_imu.last_f_acc_err_sum++;
  733. if(ob_ser_imu.last_f_acc_err_sum >= MONITOR_DATA_ERR_SUM_MAX){
  734. Except_SetExceptype(EXCEPT_DATA_FRONT_ACC);
  735. }
  736. }else{
  737. ob_ser_imu.last_f_acc_err_sum = 0;
  738. }
  739. ob_ser_imu.last_f_acc[0] = f_acc[0];
  740. ob_ser_imu.last_f_acc[1] = f_acc[1];
  741. ob_ser_imu.last_f_acc[2] = f_acc[2];
  742. }
  743. /*前脚陀螺仪*/
  744. if(f_gry != NULL)
  745. {
  746. if(
  747. ob_ser_imu.last_f_gry[0] == f_gry[0] && \
  748. ob_ser_imu.last_f_gry[1] == f_gry[1] && \
  749. ob_ser_imu.last_f_gry[2] == f_gry[2]
  750. )
  751. {
  752. ob_ser_imu.last_f_gry_err_sum++;
  753. if(ob_ser_imu.last_f_gry_err_sum >= MONITOR_DATA_ERR_SUM_MAX){
  754. Except_SetExceptype(EXCEPT_DATA_FRONT_GRY);
  755. }
  756. }else{
  757. ob_ser_imu.last_f_gry_err_sum = 0;
  758. }
  759. ob_ser_imu.last_f_gry[0] = f_gry[0];
  760. ob_ser_imu.last_f_gry[1] = f_gry[1];
  761. ob_ser_imu.last_f_gry[2] = f_gry[2];
  762. }
  763. /*前脚地磁计*/
  764. if(f_mag != NULL)
  765. {
  766. if(
  767. ob_ser_imu.last_f_mag[0] == f_mag[0] && \
  768. ob_ser_imu.last_f_mag[1] == f_mag[1] && \
  769. ob_ser_imu.last_f_mag[2] == f_mag[2]
  770. )
  771. {
  772. ob_ser_imu.last_f_mag_err_sum++;
  773. if(ob_ser_imu.last_f_mag_err_sum >= MONITOR_DATA_ERR_SUM_MAX){
  774. Except_SetExceptype(EXCEPT_DATA_FRONT_MAG);
  775. }
  776. }else{
  777. ob_ser_imu.last_f_mag_err_sum = 0;
  778. }
  779. ob_ser_imu.last_f_mag[0] = f_mag[0];
  780. ob_ser_imu.last_f_mag[1] = f_mag[1];
  781. ob_ser_imu.last_f_mag[2] = f_mag[2];
  782. }
  783. /*后脚地磁计*/
  784. if(b_mag != NULL)
  785. {
  786. if(
  787. ob_ser_imu.last_b_mag[0] == b_mag[0] && \
  788. ob_ser_imu.last_b_mag[1] == b_mag[1] && \
  789. ob_ser_imu.last_b_mag[2] == b_mag[2]
  790. )
  791. {
  792. ob_ser_imu.last_b_mag_err_sum++;
  793. if(ob_ser_imu.last_b_mag_err_sum >= MONITOR_DATA_ERR_SUM_MAX){
  794. Except_SetExceptype(EXCEPT_DATA_BACK_MAG);
  795. }
  796. }else{
  797. ob_ser_imu.last_b_mag_err_sum = 0;
  798. }
  799. ob_ser_imu.last_b_mag[0] = b_mag[0];
  800. ob_ser_imu.last_b_mag[1] = b_mag[1];
  801. ob_ser_imu.last_b_mag[2] = b_mag[2];
  802. }
  803. //监测到前脚数据异常
  804. if(Except_IsError(EXCEPT_DATA_FRONT_ACC) || Except_IsError(EXCEPT_DATA_FRONT_GRY) || Except_IsError(EXCEPT_DATA_FRONT_MAG))
  805. {
  806. ob_ser_imu.except_data_occur_sum[SER_IMU_DIR_FRONT]++;
  807. if(ob_ser_imu.except_data_occur_sum[SER_IMU_DIR_FRONT] > 1)
  808. {
  809. if(Except_IsError(EXCEPT_DATA_FRONT_ACC))Except_TxError(EXCEPT_DATA_FRONT_ACC,"front_acc_data_error");
  810. if(Except_IsError(EXCEPT_DATA_FRONT_GRY))Except_TxError(EXCEPT_DATA_FRONT_GRY,"front_gry_data_error");
  811. if(Except_IsError(EXCEPT_DATA_FRONT_MAG))Except_TxError(EXCEPT_DATA_FRONT_MAG,"front_mag_data_error");
  812. }
  813. //重启前脚LSM(主要是复位结构体)
  814. drv_lsm_power_off();
  815. drv_lsm_power_on();
  816. //清除异常
  817. Except_ClearExceptype(EXCEPT_DATA_FRONT_ACC);
  818. Except_ClearExceptype(EXCEPT_DATA_FRONT_GRY);
  819. Except_ClearExceptype(EXCEPT_DATA_FRONT_MAG);
  820. }
  821. else
  822. {
  823. ob_ser_imu.except_data_occur_sum[SER_IMU_DIR_FRONT] = 0;
  824. //清除异常
  825. Except_ClearExceptype(EXCEPT_DATA_FRONT_ACC);
  826. Except_ClearExceptype(EXCEPT_DATA_FRONT_GRY);
  827. Except_ClearExceptype(EXCEPT_DATA_FRONT_MAG);
  828. }
  829. //监测到后脚数据异常
  830. if(Except_IsError(EXCEPT_DATA_BACK_MAG))
  831. {
  832. ob_ser_imu.except_data_occur_sum[SER_IMU_DIR_BACK]++;
  833. if(ob_ser_imu.except_data_occur_sum[SER_IMU_DIR_BACK] > 1)
  834. {
  835. if(Except_IsError(EXCEPT_DATA_BACK_MAG))Except_TxError(EXCEPT_DATA_BACK_MAG,"back_mag_data_error");
  836. }
  837. //重启后脚QMC(主要是复位结构体)
  838. drv_qmc6310_power_off();
  839. drv_qmc6310_power_on();
  840. //清除异常
  841. Except_ClearExceptype(EXCEPT_DATA_BACK_MAG);
  842. }
  843. else
  844. {
  845. ob_ser_imu.except_data_occur_sum[SER_IMU_DIR_BACK] = 0;
  846. //清除异常
  847. Except_ClearExceptype(EXCEPT_DATA_BACK_MAG);
  848. }
  849. }
  850. static void hal_ser_imu_Process(void)
  851. {
  852. int data_num[SER_IMU_DIR_NUM];
  853. switch(ob_ser_imu.stage)
  854. {
  855. case IMU_CONFIG_STAGE_DONE: //配置完成阶段
  856. if(hal_ser_imu_is_need_config()) //判断是否需要配置
  857. {
  858. ob_ser_imu.stage = IMU_CONFIG_STAGE_IN_PROGRESS; //进入配置进行阶段
  859. Process_SetHoldOn(hal_ser_imu_Process,1); //全功率配置
  860. }
  861. else //读数据
  862. {
  863. //读取前脚IMU数据 + 更新前脚IMU数据量
  864. data_num[SER_IMU_DIR_FRONT] = hal_ser_imu_read_data(SER_IMU_DIR_FRONT);
  865. if(data_num[SER_IMU_DIR_FRONT] > 0)ob_ser_imu.cur_data_num[SER_IMU_DIR_FRONT] = data_num[SER_IMU_DIR_FRONT];
  866. //读取后脚IMU数据 + 更新后脚IMU数据量
  867. data_num[SER_IMU_DIR_BACK] = hal_ser_imu_read_data(SER_IMU_DIR_BACK);
  868. if(data_num[SER_IMU_DIR_BACK] > 0)ob_ser_imu.cur_data_num[SER_IMU_DIR_BACK] = data_num[SER_IMU_DIR_BACK];
  869. //回调通知
  870. if(data_num[SER_IMU_DIR_FRONT] > 0 || data_num[SER_IMU_DIR_BACK] > 0)
  871. {
  872. for(int i = 0; i < DATA_UPDATE_NOTIFY_MAX; i++)
  873. {
  874. if(ob_ser_imu.notify_cb[i])
  875. {
  876. ob_ser_imu.notify_cb[i](data_num[SER_IMU_DIR_FRONT],data_num[SER_IMU_DIR_BACK]);
  877. }
  878. }
  879. }
  880. }
  881. break;
  882. case IMU_CONFIG_STAGE_IN_PROGRESS: //配置进行阶段
  883. if(ob_ser_imu.is_need_config[SER_IMU_DIR_FRONT]) //判断前脚是否需要配置
  884. {
  885. if(hal_ser_imu_is_config_done(SER_IMU_DIR_FRONT))
  886. {
  887. ob_ser_imu.is_need_config[SER_IMU_DIR_FRONT] = false;
  888. }
  889. }
  890. else if(ob_ser_imu.is_need_config[SER_IMU_DIR_BACK]) //判断后脚是否需要配置
  891. {
  892. if(hal_ser_imu_is_config_done(SER_IMU_DIR_BACK))
  893. {
  894. ob_ser_imu.is_need_config[SER_IMU_DIR_BACK] = false;
  895. }
  896. }
  897. else
  898. {
  899. if(!hal_ser_imu_is_need_config()) //若是在配置期间,操作的IMU配置参数副本更改,需要重新配置。
  900. {
  901. ob_ser_imu.stage = IMU_CONFIG_STAGE_DONE; //配置完成
  902. //读数据
  903. Process_SetHoldOn(hal_ser_imu_Process,0); //解放线程
  904. }
  905. }
  906. break;
  907. }
  908. }
  909. static void hal_ser_imu_monitor_sensor_data_process(void)
  910. {
  911. int i = 0;
  912. int16_t group_num = 0;
  913. static int16_t f_acc[3];
  914. static int16_t f_gry[3];
  915. static int16_t f_mag[3];
  916. static int16_t b_mag[3];
  917. ser_imu_data_t data;
  918. static uint32_t last_tim = 0;
  919. if(
  920. ob_ser_imu.stage == IMU_CONFIG_STAGE_DONE && \
  921. ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.acc_odr == LSM_ACC_ODR_104HZ && \
  922. ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.gry_odr == LSM_GRY_ODR_104HZ && \
  923. (ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.mag_odr == LSM_MAG_ODR_200HZ || ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.mag_odr == LSM_MAG_ODR_100HZ)
  924. )
  925. {
  926. if(TIME_GetTicks()-last_tim >= FullPower_Interval) //监测前脚传感器数据(acc + gry + mag)+ 监测后脚传感器数据(mag)
  927. {
  928. last_tim = TIME_GetTicks();
  929. group_num = hal_ser_imu_get_data_num(SER_IMU_DIR_FRONT);
  930. if(group_num > 0)
  931. {
  932. for(i=0;i<group_num;i++)
  933. {
  934. hal_ser_imu_get_data(SER_IMU_DIR_FRONT, i, &data);
  935. f_gry[0] = data.gry[0];f_gry[1] = data.gry[1];f_gry[2] = data.gry[2];
  936. f_acc[0] = data.acc[0];f_acc[1] = data.acc[1];f_acc[2] = data.acc[2];
  937. f_mag[0] = data.mag[0];f_mag[1] = data.mag[1];f_mag[2] = data.mag[2];
  938. monitor_sensor_data(f_acc, f_gry, f_mag, NULL);
  939. }
  940. }
  941. else
  942. {
  943. monitor_sensor_data(f_acc, f_gry, f_mag, NULL);
  944. }
  945. if(hal_ser_imu_get_data_num(SER_IMU_DIR_BACK) >= 1){
  946. hal_ser_imu_get_data(SER_IMU_DIR_BACK, 0, &data);
  947. b_mag[0] = data.mag[0];b_mag[1] = data.mag[1];b_mag[2] = data.mag[2];
  948. }
  949. monitor_sensor_data(NULL, NULL, NULL, b_mag);
  950. }
  951. }
  952. else if(
  953. ob_ser_imu.stage == IMU_CONFIG_STAGE_DONE && \
  954. ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.acc_odr == LSM_ACC_ODR_104HZ && \
  955. (ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.mag_odr == LSM_MAG_ODR_200HZ || ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.mag_odr == LSM_MAG_ODR_100HZ)
  956. )
  957. {
  958. if(TIME_GetTicks()-last_tim >= LowPower_Interval) //监测前脚传感器数据(acc + mag)
  959. {
  960. last_tim = TIME_GetTicks();
  961. group_num = hal_ser_imu_get_data_num(SER_IMU_DIR_FRONT);
  962. if(group_num > 0)
  963. {
  964. for(i=0;i<group_num;i++)
  965. {
  966. hal_ser_imu_get_data(SER_IMU_DIR_FRONT, i, &data);
  967. f_acc[0] = data.acc[0];f_acc[1] = data.acc[1];f_acc[2] = data.acc[2];
  968. f_mag[0] = data.mag[0];f_mag[1] = data.mag[1];f_mag[2] = data.mag[2];
  969. monitor_sensor_data(f_acc, NULL, f_mag, NULL);
  970. }
  971. }
  972. else
  973. {
  974. monitor_sensor_data(f_acc, NULL, f_mag, NULL);
  975. }
  976. }
  977. }
  978. else if(ob_ser_imu.stage == IMU_CONFIG_STAGE_DONE && ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.acc_odr == LSM_ACC_ODR_12HZ5)
  979. {
  980. if(TIME_GetTicks()-last_tim >= StandByPower_Interval) //监测前脚传感器数据(acc)
  981. {
  982. last_tim = TIME_GetTicks();
  983. group_num = hal_ser_imu_get_data_num(SER_IMU_DIR_FRONT);
  984. if(group_num > 0)
  985. {
  986. for(i=0;i<group_num;i++)
  987. {
  988. hal_ser_imu_get_data(SER_IMU_DIR_FRONT, i, &data);
  989. f_acc[0] = data.acc[0];f_acc[1] = data.acc[1];f_acc[2] = data.acc[2];
  990. monitor_sensor_data(f_acc, NULL, NULL, NULL);
  991. }
  992. }
  993. else
  994. {
  995. monitor_sensor_data(f_acc, NULL, NULL, NULL);
  996. }
  997. }
  998. }
  999. }
  1000. static void hal_ser_imu_monitor_suspend_mode_overflow_process(void)
  1001. {
  1002. //监测异常挂起模式是否持续时间超过临界点
  1003. if(ob_ser_imu.is_need_config[SER_IMU_DIR_FRONT] == true || ob_ser_imu.is_need_config[SER_IMU_DIR_BACK] == true)
  1004. {
  1005. ob_ser_imu.except_suspend_occur_sum++;
  1006. if(ob_ser_imu.except_suspend_occur_sum >= MONITOR_SUSPEND_MODE_OVERFLOW_ERR_SUM_MAX){
  1007. Except_SetExceptype(EXCEPT_IMU_SUSPEND_OVERFLOW);
  1008. Except_TxError(EXCEPT_IMU_SUSPEND_OVERFLOW,"front_imu_suspend_overflow");
  1009. ob_ser_imu.except_suspend_occur_sum = MONITOR_SUSPEND_MODE_OVERFLOW_ERR_SUM_MAX;
  1010. }
  1011. }
  1012. else
  1013. {
  1014. ob_ser_imu.except_suspend_occur_sum = 0;
  1015. Except_ClearExceptype(EXCEPT_IMU_SUSPEND_OVERFLOW);
  1016. }
  1017. }
  1018. static void hal_ser_imu_printf_Process(void)
  1019. {
  1020. SEGGER_RTT_printf(0,"hal_ser_imu----------------------------------------------------------------->\n");
  1021. SEGGER_RTT_printf(0,"stage:0x%x\n",ob_ser_imu.stage);
  1022. SEGGER_RTT_printf(0,"is_need_config[SER_IMU_DIR_FRONT]:0x%x\n",ob_ser_imu.is_need_config[SER_IMU_DIR_FRONT]);
  1023. SEGGER_RTT_printf(0,"is_need_config[SER_IMU_DIR_BACK]:0x%x\n",ob_ser_imu.is_need_config[SER_IMU_DIR_BACK]);
  1024. // SEGGER_RTT_printf(0,"df acc_fs:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.acc_fs);
  1025. SEGGER_RTT_printf(0,"df acc_odr:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.acc_odr);
  1026. // SEGGER_RTT_printf(0,"df acc_power_mode:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.acc_power_mode);
  1027. SEGGER_RTT_printf(0,"df fifo_odr:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.fifo_odr);
  1028. // SEGGER_RTT_printf(0,"df gry_fs:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.gry_fs);
  1029. SEGGER_RTT_printf(0,"df gry_odr:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.gry_odr);
  1030. // SEGGER_RTT_printf(0,"df gry_power_mode:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.gry_power_mode);
  1031. // SEGGER_RTT_printf(0,"df mag_fs:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.mag_fs);
  1032. SEGGER_RTT_printf(0,"df mag_odr:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.mag_odr);
  1033. // SEGGER_RTT_printf(0,"f timestamp_resolution:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.timestamp_resolution);
  1034. // SEGGER_RTT_printf(0,"df timestamp_switch:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.timestamp_switch);
  1035. SEGGER_RTT_printf(0,"db mag_odr:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_BACK].qmc.mag_odr);
  1036. // SEGGER_RTT_printf(0,"cf acc_fs:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].acc_fs);
  1037. SEGGER_RTT_printf(0,"cf acc_odr:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].acc_odr);
  1038. // SEGGER_RTT_printf(0,"cf acc_power_mode:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].acc_power_mode);
  1039. SEGGER_RTT_printf(0,"cf fifo_odr:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].fifo_odr);
  1040. // SEGGER_RTT_printf(0,"cf gry_fs:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].gry_fs);
  1041. SEGGER_RTT_printf(0,"cf gry_odr:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].gry_odr);
  1042. // SEGGER_RTT_printf(0,"cf gry_power_mode:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].gry_power_mode);
  1043. // SEGGER_RTT_printf(0,"cf mag_fs:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].mag_fs);
  1044. SEGGER_RTT_printf(0,"cf mag_odr:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].mag_odr);
  1045. // SEGGER_RTT_printf(0,"cf timestamp_resolution:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].timestamp_resolution);
  1046. // SEGGER_RTT_printf(0,"cf timestamp_switch:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].timestamp_switch);
  1047. SEGGER_RTT_printf(0,"cb mag_odr:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_BACK].mag_odr);
  1048. SEGGER_RTT_printf(0,"ob_ser_imu.cur_data_num[SER_IMU_DIR_FRONT]:0x%x\n",ob_ser_imu.cur_data_num[SER_IMU_DIR_FRONT]);
  1049. SEGGER_RTT_printf(0,"ob_ser_imu.cur_data_num[SER_IMU_DIR_BACK]:0x%x\n",ob_ser_imu.cur_data_num[SER_IMU_DIR_BACK]);
  1050. SEGGER_RTT_printf(0,"<-----------------------------------------------------------------hal_ser_imu\n");
  1051. }
  1052. /*API ------------------------------------------------------------------------------------------------------------------------------------*/
  1053. /**
  1054. @brief 初始化IMU服务
  1055. @param 无
  1056. @return 错误代码 - [out] -1失败,0成功
  1057. */
  1058. int hal_ser_imu_Init(void)
  1059. {
  1060. int i;
  1061. int ret;
  1062. /***************************************驱动层初始化***************************************************/
  1063. //初始化驱动LSM
  1064. ret = drv_lsm_Init();
  1065. //初始化驱动QMC
  1066. ret += drv_qmc6310_Init();
  1067. //获取驱动LSM配置参数
  1068. drv_lsm_get_config_param(&ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm);
  1069. //获取驱动LSM配置参数
  1070. drv_qmc6310_get_config_param(&ob_ser_imu.drv_param[SER_IMU_DIR_BACK].qmc);
  1071. /***************************************业务逻辑层初始化***************************************************/
  1072. //重置结构体
  1073. memset(&ob_ser_imu,0,sizeof(ob_ser_imu));
  1074. //初始化IMU服务结构体
  1075. ob_ser_imu.stage = IMU_CONFIG_STAGE_DONE;
  1076. for(i=0; i<SER_IMU_HANDLE_NUM_MAX; i++)
  1077. {
  1078. ob_ser_imu.handle[i].id = INVALID_HANDLE;
  1079. ob_ser_imu.handle[i].name = NULL;
  1080. }
  1081. for(i=0; i<SER_IMU_DIR_NUM; i++)
  1082. {
  1083. ob_ser_imu.is_need_config[i] = false;
  1084. }
  1085. //设置驱动配置线程
  1086. Process_Start(0,"hal_ser_imu_Process",hal_ser_imu_Process);
  1087. Process_Start(10,"hal_ser_imu_monitor_sensor_data_process",hal_ser_imu_monitor_sensor_data_process);
  1088. // Process_Start(1000,"hal_ser_imu_printf_Process",hal_ser_imu_printf_Process);
  1089. Process_Start(3000,"hal_ser_imu_monitor_suspend_mode_overflow_process",hal_ser_imu_monitor_suspend_mode_overflow_process);
  1090. if(ret < 0)return -1;
  1091. return 0;
  1092. }
  1093. /**
  1094. @brief 获取IMU服务句柄
  1095. @param p_name - [in] 为服务句柄设置的名字
  1096. @param p_handle_id - [out] 返回的服务句柄ID
  1097. @return 错误代码 - [out] -1失败,0成功
  1098. */
  1099. int hal_ser_imu_get_handle(char *p_name, int *p_handle_id)
  1100. {
  1101. for(int i=0; i<SER_IMU_HANDLE_NUM_MAX; i++)
  1102. {
  1103. //判断是否有空余的句柄
  1104. if(ob_ser_imu.handle[i].id == INVALID_HANDLE)
  1105. {
  1106. //设置该句柄的名字
  1107. ob_ser_imu.handle[i].name = p_name;
  1108. //该句柄使能有效
  1109. ob_ser_imu.handle[i].id = VALID_HANDLE;
  1110. //返回服务句柄ID
  1111. *p_handle_id = i;
  1112. return 0;
  1113. }
  1114. }
  1115. return -1;
  1116. }
  1117. /**
  1118. @brief 设置IMU需要的配置参数
  1119. @param handle - [in] 操作的IMU服务句柄
  1120. @param foot - [in] IMU方向 - 前/后脚
  1121. @param param - [in] IMU配置参数
  1122. @return 错误代码 - [out] -1失败,0成功
  1123. */
  1124. int hal_ser_imu_set_required_param(int handle_id, SER_IMU_DIR_e dir, ser_imu_config_param_t *param)
  1125. {
  1126. //无效句柄返回
  1127. if(handle_id < 0 || ob_ser_imu.handle[handle_id].id == INVALID_HANDLE)return -1;
  1128. //更新操作的配置参数副本
  1129. memcpy(&ob_ser_imu.handle[handle_id].op_param[dir], param ,sizeof(ob_ser_imu.handle[handle_id].op_param[dir]));
  1130. return 0;
  1131. }
  1132. /**
  1133. @brief IMU配置参数是否准备好
  1134. @param foot - [in] IMU方向 - 前/后脚
  1135. @param param - [in] IMU配置参数
  1136. @return 错误代码 - [out] -1失败,0成功
  1137. */
  1138. int hal_ser_imu_is_param_get_ready(SER_IMU_DIR_e dir, ser_imu_config_param_t *param)
  1139. {
  1140. int ret = 0;
  1141. //查看是否需要进行配置
  1142. if(hal_ser_imu_is_need_config())
  1143. {
  1144. return -1;
  1145. }
  1146. //判断当前配置是否满足
  1147. switch(dir)
  1148. {
  1149. case SER_IMU_DIR_FRONT:
  1150. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].acc_fs < param->acc_fs){ret = -1;break;}
  1151. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].acc_odr < param->acc_odr){ret = -1;break;}
  1152. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].acc_power_mode < param->acc_power_mode){ret = -1;break;}
  1153. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].fifo_odr < param->fifo_odr){ret = -1;break;}
  1154. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].gry_fs < param->gry_fs){ret = -1;break;}
  1155. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].gry_odr < param->gry_odr){ret = -1;break;}
  1156. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].gry_power_mode < param->gry_power_mode){ret = -1;break;}
  1157. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].mag_fs < param->mag_fs){ret = -1;break;}
  1158. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].mag_odr < param->mag_odr){ret = -1;break;}
  1159. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].timestamp_resolution < param->timestamp_resolution){ret = -1;break;}
  1160. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].timestamp_switch < param->timestamp_switch){ret = -1;break;}
  1161. break;
  1162. case SER_IMU_DIR_BACK:
  1163. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_BACK].mag_fs < param->mag_fs){ret = -1;break;}
  1164. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_BACK].mag_odr < param->mag_odr){ret = -1;break;}
  1165. break;
  1166. default:
  1167. break;
  1168. }
  1169. return ret;
  1170. }
  1171. /**
  1172. @brief 获取IMU当前数据数量
  1173. @param dir - [in] IMU方向 - 前/后脚
  1174. @return 错误代码 - [out] 返回数据数量
  1175. */
  1176. int hal_ser_imu_get_data_num(SER_IMU_DIR_e dir)
  1177. {
  1178. int ret = 0;
  1179. switch(dir)
  1180. {
  1181. case SER_IMU_DIR_FRONT:
  1182. ret = ob_ser_imu.cur_data_num[SER_IMU_DIR_FRONT];
  1183. break;
  1184. case SER_IMU_DIR_BACK:
  1185. ret = ob_ser_imu.cur_data_num[SER_IMU_DIR_BACK];
  1186. break;
  1187. default:
  1188. break;
  1189. }
  1190. return ret;
  1191. }
  1192. /**
  1193. @brief 获取IMU数据
  1194. @param dir - [in] IMU方向 - 前/后脚
  1195. @param index - [in] 第几组数据
  1196. @param pdata - [out] IMU数据
  1197. @return 错误代码 - [out] -1失败,0成功
  1198. */
  1199. int hal_ser_imu_get_data(SER_IMU_DIR_e dir, int index, ser_imu_data_t *pdata)
  1200. {
  1201. if(index < 0)return -1;
  1202. *pdata = ob_ser_imu.cur_data[dir][index];
  1203. return 0;
  1204. }
  1205. /**
  1206. @brief 数据更新通知
  1207. @param p_cb - [in] 需要回调的函数
  1208. @return 错误代码 - [out] -1失败,0成功,-2已存在
  1209. */
  1210. int hal_ser_imu_data_update_notify(hal_ser_imu_data_update_callback p_cb)
  1211. {
  1212. for(int i=0;i<DATA_UPDATE_NOTIFY_MAX;i++)
  1213. {
  1214. if(ob_ser_imu.notify_cb[i]==p_cb) return -2;
  1215. if(ob_ser_imu.notify_cb[i]==0)
  1216. {
  1217. ob_ser_imu.notify_cb[i] = p_cb; //回调函数
  1218. return 0;
  1219. }
  1220. }
  1221. return -1;
  1222. }