hal_ser_imu.c 47 KB

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