hal_ser_imu.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  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. for(i=0; i<fifo_group_num; i++)
  373. {
  374. if(drv_lsm_get_fifo_data(&temp_lsm_data) != -1)
  375. {
  376. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].acc[0] = temp_lsm_data.acc[0];
  377. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].acc[1] = temp_lsm_data.acc[1];
  378. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].acc[2] = temp_lsm_data.acc[2];
  379. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].gry[0] = temp_lsm_data.gry[0];
  380. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].gry[1] = temp_lsm_data.gry[1];
  381. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].gry[2] = temp_lsm_data.gry[2];
  382. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].mag[0] = temp_lsm_data.mag[0];
  383. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].mag[1] = temp_lsm_data.mag[1];
  384. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].mag[2] = temp_lsm_data.mag[2];
  385. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].fifo_timestamp = temp_lsm_data.fifo_timestamp;
  386. // char printfbuff[255]={0};
  387. // static uint32_t tim = 0;
  388. //
  389. // if(TIME_GetTicks()-tim >= 1000)
  390. // {
  391. // sprintf(printfbuff,"i:%d;acc:%d,%d,%d;gry:%d,%d,%d;mag:%d,%d,%d;timestamp:%d;id:%d\r\n",i,ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].acc[0], \
  392. // ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].acc[1], \
  393. // ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].acc[2], \
  394. // ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].gry[0], \
  395. // ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].gry[1], \
  396. // ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].gry[2], \
  397. // ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].mag[0], \
  398. // ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].mag[1], \
  399. // ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].mag[2], \
  400. // ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][i].fifo_timestamp, \
  401. // drv_lsm_get_id());
  402. //
  403. // tim = TIME_GetTicks();
  404. // UART0_PrintfDebug(printfbuff);
  405. // }
  406. }
  407. }
  408. return fifo_group_num;
  409. }
  410. //只读取ACC数据
  411. else if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].acc_odr != SER_IMU_ACC_ODR_OFF && \
  412. ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].gry_odr == SER_IMU_GRY_ODR_OFF && \
  413. ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].mag_odr == SER_IMU_MAG_ODR_OFF \
  414. )
  415. {
  416. if(drv_lsm_get_acc_data(&temp_lsm_data) != -1)
  417. {
  418. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][0].acc[0] = temp_lsm_data.acc[0];
  419. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][0].acc[1] = temp_lsm_data.acc[1];
  420. ob_ser_imu.cur_data[SER_IMU_DIR_FRONT][0].acc[2] = temp_lsm_data.acc[2];
  421. return 1;
  422. }
  423. }
  424. return 0;
  425. }
  426. static int hal_ser_imu_read_data_qmc(void)
  427. {
  428. qmc_data_t temp_qmc_data;
  429. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_BACK].mag_odr != SER_IMU_MAG_ODR_OFF)
  430. {
  431. if(drv_qmc6310_get_mag_data(&temp_qmc_data) != -1)
  432. {
  433. ob_ser_imu.cur_data[SER_IMU_DIR_BACK][0].mag[0] = temp_qmc_data.mag[0];
  434. ob_ser_imu.cur_data[SER_IMU_DIR_BACK][0].mag[1] = temp_qmc_data.mag[1];
  435. ob_ser_imu.cur_data[SER_IMU_DIR_BACK][0].mag[2] = temp_qmc_data.mag[2];
  436. return 1;
  437. }
  438. }
  439. return 0;
  440. }
  441. static int hal_ser_imu_read_data(SER_IMU_DIR_e dir)
  442. {
  443. int ret = 0;
  444. switch(dir)
  445. {
  446. case SER_IMU_DIR_FRONT:
  447. ret = hal_ser_imu_read_data_lsm();
  448. break;
  449. case SER_IMU_DIR_BACK:
  450. ret = hal_ser_imu_read_data_qmc();
  451. break;
  452. default:
  453. break;
  454. }
  455. return ret;
  456. }
  457. static bool hal_ser_imu_is_need_config(void)
  458. {
  459. int i;
  460. //重新获取综合的前脚配置,根据优先级来判定
  461. memset(&ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT],0,sizeof(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT]));
  462. for(i=0; i<SER_IMU_HANDLE_NUM_MAX; i++)
  463. {
  464. if(ob_ser_imu.handle[i].id == VALID_HANDLE)
  465. {
  466. 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]);
  467. }
  468. }
  469. //重新获取驱动LSM配置参数
  470. drv_lsm_get_config_param(&ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm);
  471. //判断综合的前脚配置与当前的驱动配置是否一致
  472. 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)
  473. {
  474. ob_ser_imu.is_need_config[SER_IMU_DIR_FRONT] = true;
  475. }
  476. /************************************************************************************************************************************************/
  477. //重新获取综合的后脚配置,根据优先级来判定
  478. memset(&ob_ser_imu.compre_drv_param[SER_IMU_DIR_BACK],0,sizeof(ob_ser_imu.compre_drv_param[SER_IMU_DIR_BACK]));
  479. for(i=0; i<SER_IMU_HANDLE_NUM_MAX; i++)
  480. {
  481. if(ob_ser_imu.handle[i].id == VALID_HANDLE)
  482. {
  483. 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]);
  484. }
  485. }
  486. //重新获取驱动QMC配置参数
  487. drv_qmc6310_get_config_param(&ob_ser_imu.drv_param[SER_IMU_DIR_BACK].qmc);
  488. //判断综合的前脚配置与当前的驱动配置是否一致
  489. 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)
  490. {
  491. ob_ser_imu.is_need_config[SER_IMU_DIR_BACK] = true;
  492. }
  493. /************************************************************************************************************************************************/
  494. for(i=0; i<SER_IMU_DIR_NUM; i++)
  495. {
  496. if(ob_ser_imu.is_need_config[i] == true){
  497. return true;
  498. }
  499. }
  500. return false;
  501. }
  502. static bool hal_ser_imu_config_dir_front(void)
  503. {
  504. int ret = 0;
  505. static int imu_config_flow_ctl = 0; //配置流程控制
  506. static int imu_mag_cur_flow_ctl;
  507. static int imu_mag_total_flow_ctl;
  508. static uint32_t tim = 0;
  509. // SEGGER_RTT_printf(0,"======= imu_config_flow_ctl:%d ======= \n",imu_config_flow_ctl);
  510. update_cur_front_flow = imu_config_flow_ctl;
  511. switch(imu_config_flow_ctl)
  512. {
  513. case 0:
  514. //配置lsm断电
  515. if(drv_lsm_power_off() == 0){imu_config_flow_ctl = 1;}
  516. break;
  517. case 1:
  518. //等待200ms
  519. if(tim == 0){
  520. tim = TIME_GetTicks();
  521. }else if(TIME_GetTicks()-tim>=200){
  522. tim = 0;
  523. imu_config_flow_ctl = 2;
  524. }
  525. break;
  526. case 2:
  527. //配置lsm上电(默认配置挂起)
  528. if(drv_lsm_power_on() == 0){
  529. imu_config_flow_ctl = 3;
  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. }
  552. else{
  553. imu_mag_cur_flow_ctl++; //进入下一个步骤
  554. tim = 0;
  555. }
  556. }
  557. else{
  558. if(tim == 0){
  559. tim = TIME_GetTicks();
  560. }else if(TIME_GetTicks()-tim>=1000){
  561. tim = 0;
  562. imu_config_flow_ctl = 0; //累计超过1s,重新走流程
  563. }
  564. }
  565. }
  566. else
  567. {
  568. //不使用hub
  569. 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){
  570. if(imu_mag_cur_flow_ctl == imu_mag_total_flow_ctl){
  571. imu_config_flow_ctl = 5; //进入下一个流程
  572. tim = 0;
  573. }
  574. else{
  575. imu_mag_cur_flow_ctl++; //进入下一个步骤
  576. tim = 0;
  577. }
  578. }
  579. else{
  580. if(tim == 0){
  581. tim = TIME_GetTicks();
  582. }else if(TIME_GetTicks()-tim>=1000){
  583. tim = 0;
  584. imu_config_flow_ctl = 0; //累计超过1s,重新走流程
  585. }
  586. }
  587. }
  588. break;
  589. case 5:
  590. //配置lsm的加速度工作模式
  591. 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));
  592. //配置lsm的陀螺仪工作模式
  593. 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));
  594. //配置lsm的加速度量程
  595. 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));
  596. //配置lsm的陀螺仪量程
  597. 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));
  598. //配置lsm的时间戳精度
  599. 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));
  600. //配置lsm的时间戳开关
  601. 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));
  602. if(ret == 0)
  603. {
  604. imu_config_flow_ctl = 6;
  605. tim = 0;
  606. }
  607. else
  608. {
  609. if(tim == 0){
  610. tim = TIME_GetTicks();
  611. }else if(TIME_GetTicks()-tim>=1000){
  612. tim = 0;
  613. imu_config_flow_ctl = 0; //累计超过1s,重新走流程
  614. }
  615. }
  616. break;
  617. case 6:
  618. //配置lsm的FIFO采样频率(必须以上都配置成功,才能配置FIFO)
  619. 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), \
  620. (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), \
  621. (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), \
  622. (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), \
  623. (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));
  624. if(ret == 0)
  625. {
  626. imu_config_flow_ctl = 7;
  627. tim = 0;
  628. }
  629. else
  630. {
  631. if(tim == 0){
  632. tim = TIME_GetTicks();
  633. }else if(TIME_GetTicks()-tim>=1000){
  634. tim = 0;
  635. imu_config_flow_ctl = 0; //累计超过1s,重新走流程
  636. }
  637. }
  638. break;
  639. case 7:
  640. //配置lsm的加速度采样频率
  641. 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));
  642. //配置lsm的陀螺仪采样频率
  643. 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));
  644. if(ret == 0)
  645. {
  646. imu_config_flow_ctl = 8;
  647. tim = 0;
  648. }
  649. else
  650. {
  651. if(tim == 0){
  652. tim = TIME_GetTicks();
  653. }else if(TIME_GetTicks()-tim>=1000){
  654. tim = 0;
  655. imu_config_flow_ctl = 0; //累计超过1s,重新走流程
  656. }
  657. }
  658. break;
  659. case 8:
  660. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].acc_odr != SER_IMU_ACC_ODR_OFF || \
  661. ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].gry_odr != SER_IMU_GRY_ODR_OFF || \
  662. ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].mag_odr != SER_IMU_MAG_ODR_OFF
  663. )
  664. {
  665. if(hal_ser_imu_read_data(SER_IMU_DIR_FRONT) != 0)
  666. {
  667. imu_config_flow_ctl = 0;
  668. return true;
  669. }
  670. else
  671. {
  672. if(tim == 0){
  673. tim = TIME_GetTicks();
  674. }else if(TIME_GetTicks()-tim>=3000){
  675. tim = 0;
  676. imu_config_flow_ctl = 0; //累计超过1s,重新走流程
  677. }
  678. }
  679. }
  680. else
  681. {
  682. imu_config_flow_ctl = 0;
  683. return true;
  684. }
  685. break;
  686. }
  687. return false;
  688. }
  689. static bool hal_ser_imu_config_dir_back(void)
  690. {
  691. int ret = 0;
  692. static int imu_config_flow_ctl = 0; //配置流程控制
  693. static int imu_mag_cur_flow_ctl;
  694. static int imu_mag_total_flow_ctl;
  695. static uint32_t tim = 0;
  696. switch(imu_config_flow_ctl)
  697. {
  698. case 0:
  699. //配置qmc断电
  700. if(drv_qmc6310_power_off() == 0){imu_config_flow_ctl = 1;}
  701. break;
  702. case 1:
  703. //等待200ms
  704. if(tim == 0){
  705. tim = TIME_GetTicks();
  706. }else if(TIME_GetTicks()-tim>=200){
  707. tim = 0;
  708. imu_config_flow_ctl = 2;
  709. }
  710. break;
  711. case 2:
  712. //配置qmc上电(默认配置挂起)
  713. if(drv_qmc6310_power_on() == 0){
  714. imu_config_flow_ctl = 3;
  715. }
  716. break;
  717. case 3:
  718. //等待200ms
  719. if(tim == 0){
  720. tim = TIME_GetTicks();
  721. }else if(TIME_GetTicks()-tim>=200){
  722. tim = 0;
  723. imu_mag_total_flow_ctl = drv_qmc6310_get_mag_odr_flow();
  724. imu_mag_cur_flow_ctl = 1;
  725. imu_config_flow_ctl = 4;
  726. }
  727. break;
  728. case 4:
  729. //配置qmc的地磁计采样频率
  730. 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);
  731. if(ret != -1)
  732. {
  733. if(imu_mag_cur_flow_ctl == imu_mag_total_flow_ctl){
  734. imu_config_flow_ctl = 5;
  735. }
  736. else{
  737. imu_mag_cur_flow_ctl++; //进入下一个步骤
  738. tim = 0;
  739. }
  740. }
  741. else
  742. {
  743. if(tim == 0){
  744. tim = TIME_GetTicks();
  745. }else if(TIME_GetTicks()-tim>=1000){
  746. tim = 0;
  747. imu_config_flow_ctl = 0; //累计超过1s,重新走流程
  748. }
  749. }
  750. break;
  751. case 5:
  752. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_BACK].mag_odr != SER_IMU_MAG_ODR_OFF)
  753. {
  754. if(hal_ser_imu_read_data(SER_IMU_DIR_BACK) != 0)
  755. {
  756. imu_config_flow_ctl = 0;
  757. return true;
  758. }
  759. }
  760. else
  761. {
  762. imu_config_flow_ctl = 0;
  763. return true;
  764. }
  765. break;
  766. }
  767. return false;
  768. }
  769. static bool hal_ser_imu_is_config_done(SER_IMU_DIR_e dir)
  770. {
  771. bool ret = true;
  772. switch(dir)
  773. {
  774. case SER_IMU_DIR_FRONT:
  775. ret = hal_ser_imu_config_dir_front();
  776. break;
  777. case SER_IMU_DIR_BACK:
  778. ret = hal_ser_imu_config_dir_back();
  779. break;
  780. default:
  781. break;
  782. }
  783. return ret;
  784. }
  785. static void monitor_sensor_data(int16_t *f_acc, int16_t *f_gry, int16_t *f_mag, int16_t *b_mag)
  786. {
  787. /*前脚加速度*/
  788. if(f_acc != NULL)
  789. {
  790. if(
  791. ob_ser_imu.last_f_acc[0] == f_acc[0] && \
  792. ob_ser_imu.last_f_acc[1] == f_acc[1] && \
  793. ob_ser_imu.last_f_acc[2] == f_acc[2]
  794. )
  795. {
  796. ob_ser_imu.last_f_acc_err_sum++;
  797. if(ob_ser_imu.last_f_acc_err_sum >= MONITOR_DATA_ERR_SUM_MAX){
  798. Except_SetExceptype(EXCEPT_DATA_FRONT_ACC);
  799. }
  800. }else{
  801. ob_ser_imu.last_f_acc_err_sum = 0;
  802. }
  803. ob_ser_imu.last_f_acc[0] = f_acc[0];
  804. ob_ser_imu.last_f_acc[1] = f_acc[1];
  805. ob_ser_imu.last_f_acc[2] = f_acc[2];
  806. }
  807. /*前脚陀螺仪*/
  808. if(f_gry != NULL)
  809. {
  810. if(
  811. ob_ser_imu.last_f_gry[0] == f_gry[0] && \
  812. ob_ser_imu.last_f_gry[1] == f_gry[1] && \
  813. ob_ser_imu.last_f_gry[2] == f_gry[2]
  814. )
  815. {
  816. ob_ser_imu.last_f_gry_err_sum++;
  817. if(ob_ser_imu.last_f_gry_err_sum >= MONITOR_DATA_ERR_SUM_MAX){
  818. Except_SetExceptype(EXCEPT_DATA_FRONT_GRY);
  819. }
  820. }else{
  821. ob_ser_imu.last_f_gry_err_sum = 0;
  822. }
  823. ob_ser_imu.last_f_gry[0] = f_gry[0];
  824. ob_ser_imu.last_f_gry[1] = f_gry[1];
  825. ob_ser_imu.last_f_gry[2] = f_gry[2];
  826. }
  827. /*前脚地磁计*/
  828. if(f_mag != NULL)
  829. {
  830. if(
  831. ob_ser_imu.last_f_mag[0] == f_mag[0] && \
  832. ob_ser_imu.last_f_mag[1] == f_mag[1] && \
  833. ob_ser_imu.last_f_mag[2] == f_mag[2]
  834. )
  835. {
  836. ob_ser_imu.last_f_mag_err_sum++;
  837. if(ob_ser_imu.last_f_mag_err_sum >= MONITOR_DATA_ERR_SUM_MAX){
  838. Except_SetExceptype(EXCEPT_DATA_FRONT_MAG);
  839. }
  840. }else{
  841. ob_ser_imu.last_f_mag_err_sum = 0;
  842. }
  843. ob_ser_imu.last_f_mag[0] = f_mag[0];
  844. ob_ser_imu.last_f_mag[1] = f_mag[1];
  845. ob_ser_imu.last_f_mag[2] = f_mag[2];
  846. }
  847. /*后脚地磁计*/
  848. if(b_mag != NULL)
  849. {
  850. if(
  851. ob_ser_imu.last_b_mag[0] == b_mag[0] && \
  852. ob_ser_imu.last_b_mag[1] == b_mag[1] && \
  853. ob_ser_imu.last_b_mag[2] == b_mag[2]
  854. )
  855. {
  856. ob_ser_imu.last_b_mag_err_sum++;
  857. if(ob_ser_imu.last_b_mag_err_sum >= MONITOR_DATA_ERR_SUM_MAX){
  858. Except_SetExceptype(EXCEPT_DATA_BACK_MAG);
  859. }
  860. }else{
  861. ob_ser_imu.last_b_mag_err_sum = 0;
  862. }
  863. ob_ser_imu.last_b_mag[0] = b_mag[0];
  864. ob_ser_imu.last_b_mag[1] = b_mag[1];
  865. ob_ser_imu.last_b_mag[2] = b_mag[2];
  866. }
  867. //监测到前脚数据异常
  868. if(Except_IsError(EXCEPT_DATA_FRONT_ACC) || Except_IsError(EXCEPT_DATA_FRONT_GRY) || Except_IsError(EXCEPT_DATA_FRONT_MAG))
  869. {
  870. ob_ser_imu.except_data_occur_sum[SER_IMU_DIR_FRONT]++;
  871. if(ob_ser_imu.except_data_occur_sum[SER_IMU_DIR_FRONT] > 1)
  872. {
  873. if(Except_IsError(EXCEPT_DATA_FRONT_ACC))Except_TxError(EXCEPT_DATA_FRONT_ACC,"front_acc_data_error");
  874. if(Except_IsError(EXCEPT_DATA_FRONT_GRY))Except_TxError(EXCEPT_DATA_FRONT_GRY,"front_gry_data_error");
  875. if(Except_IsError(EXCEPT_DATA_FRONT_MAG))Except_TxError(EXCEPT_DATA_FRONT_MAG,"front_mag_data_error");
  876. }
  877. //重启前脚LSM(主要是复位结构体)
  878. drv_lsm_power_off();
  879. drv_lsm_power_on();
  880. //清除异常
  881. Except_ClearExceptype(EXCEPT_DATA_FRONT_ACC);
  882. Except_ClearExceptype(EXCEPT_DATA_FRONT_GRY);
  883. Except_ClearExceptype(EXCEPT_DATA_FRONT_MAG);
  884. }
  885. else
  886. {
  887. ob_ser_imu.except_data_occur_sum[SER_IMU_DIR_FRONT] = 0;
  888. //清除异常
  889. Except_ClearExceptype(EXCEPT_DATA_FRONT_ACC);
  890. Except_ClearExceptype(EXCEPT_DATA_FRONT_GRY);
  891. Except_ClearExceptype(EXCEPT_DATA_FRONT_MAG);
  892. }
  893. //监测到后脚数据异常
  894. if(Except_IsError(EXCEPT_DATA_BACK_MAG))
  895. {
  896. ob_ser_imu.except_data_occur_sum[SER_IMU_DIR_BACK]++;
  897. if(ob_ser_imu.except_data_occur_sum[SER_IMU_DIR_BACK] > 1)
  898. {
  899. if(Except_IsError(EXCEPT_DATA_BACK_MAG))Except_TxError(EXCEPT_DATA_BACK_MAG,"back_mag_data_error");
  900. }
  901. //重启后脚QMC(主要是复位结构体)
  902. drv_qmc6310_power_off();
  903. drv_qmc6310_power_on();
  904. //清除异常
  905. Except_ClearExceptype(EXCEPT_DATA_BACK_MAG);
  906. }
  907. else
  908. {
  909. ob_ser_imu.except_data_occur_sum[SER_IMU_DIR_BACK] = 0;
  910. //清除异常
  911. Except_ClearExceptype(EXCEPT_DATA_BACK_MAG);
  912. }
  913. }
  914. static void hal_ser_imu_Process(void)
  915. {
  916. int data_num[SER_IMU_DIR_NUM];
  917. switch(ob_ser_imu.stage)
  918. {
  919. case IMU_CONFIG_STAGE_DONE: //配置完成阶段
  920. if(hal_ser_imu_is_need_config()) //判断是否需要配置
  921. {
  922. ob_ser_imu.stage = IMU_CONFIG_STAGE_IN_PROGRESS; //进入配置进行阶段
  923. Process_SetHoldOn(hal_ser_imu_Process,1); //全功率配置
  924. }
  925. else //读数据
  926. {
  927. //读取前脚IMU数据 + 更新前脚IMU数据量
  928. data_num[SER_IMU_DIR_FRONT] = hal_ser_imu_read_data(SER_IMU_DIR_FRONT);
  929. if(data_num[SER_IMU_DIR_FRONT] > 0)ob_ser_imu.cur_data_num[SER_IMU_DIR_FRONT] = data_num[SER_IMU_DIR_FRONT];
  930. //读取后脚IMU数据 + 更新后脚IMU数据量
  931. data_num[SER_IMU_DIR_BACK] = hal_ser_imu_read_data(SER_IMU_DIR_BACK);
  932. if(data_num[SER_IMU_DIR_BACK] > 0)ob_ser_imu.cur_data_num[SER_IMU_DIR_BACK] = data_num[SER_IMU_DIR_BACK];
  933. //回调通知
  934. if(data_num[SER_IMU_DIR_FRONT] > 0 || data_num[SER_IMU_DIR_BACK] > 0)
  935. {
  936. for(int i = 0; i < DATA_UPDATE_NOTIFY_MAX; i++)
  937. {
  938. if(ob_ser_imu.notify_cb[i])
  939. {
  940. ob_ser_imu.notify_cb[i](data_num[SER_IMU_DIR_FRONT],data_num[SER_IMU_DIR_BACK],0-host_get_rssi());
  941. }
  942. }
  943. }
  944. }
  945. break;
  946. case IMU_CONFIG_STAGE_IN_PROGRESS: //配置进行阶段
  947. if(ob_ser_imu.is_need_config[SER_IMU_DIR_FRONT]) //判断前脚是否需要配置
  948. {
  949. if(hal_ser_imu_is_config_done(SER_IMU_DIR_FRONT))
  950. {
  951. ob_ser_imu.is_need_config[SER_IMU_DIR_FRONT] = false;
  952. }
  953. }
  954. else if(ob_ser_imu.is_need_config[SER_IMU_DIR_BACK]) //判断后脚是否需要配置
  955. {
  956. if(hal_ser_imu_is_config_done(SER_IMU_DIR_BACK))
  957. {
  958. ob_ser_imu.is_need_config[SER_IMU_DIR_BACK] = false;
  959. }
  960. }
  961. else
  962. {
  963. if(!hal_ser_imu_is_need_config()) //若是在配置期间,操作的IMU配置参数副本更改,需要重新配置。
  964. {
  965. ob_ser_imu.stage = IMU_CONFIG_STAGE_DONE; //配置完成
  966. //读数据
  967. Process_SetHoldOn(hal_ser_imu_Process,0); //解放线程
  968. }
  969. }
  970. break;
  971. }
  972. }
  973. static void hal_ser_imu_monitor_sensor_data_process(void)
  974. {
  975. int i = 0;
  976. int16_t group_num = 0;
  977. static int16_t f_acc[3];
  978. static int16_t f_gry[3];
  979. static int16_t f_mag[3];
  980. static int16_t b_mag[3];
  981. ser_imu_data_t data;
  982. static uint32_t last_tim = 0;
  983. if(
  984. ob_ser_imu.stage == IMU_CONFIG_STAGE_DONE && \
  985. ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.acc_odr == LSM_ACC_ODR_104HZ && \
  986. ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.gry_odr == LSM_GRY_ODR_104HZ && \
  987. (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)
  988. )
  989. {
  990. if(TIME_GetTicks()-last_tim >= FullPower_Interval) //监测前脚传感器数据(acc + gry + mag)+ 监测后脚传感器数据(mag)
  991. {
  992. last_tim = TIME_GetTicks();
  993. group_num = hal_ser_imu_get_data_num(SER_IMU_DIR_FRONT);
  994. if(group_num > 0)
  995. {
  996. for(i=0;i<group_num;i++)
  997. {
  998. hal_ser_imu_get_data(SER_IMU_DIR_FRONT, i, &data);
  999. f_gry[0] = data.gry[0];f_gry[1] = data.gry[1];f_gry[2] = data.gry[2];
  1000. f_acc[0] = data.acc[0];f_acc[1] = data.acc[1];f_acc[2] = data.acc[2];
  1001. f_mag[0] = data.mag[0];f_mag[1] = data.mag[1];f_mag[2] = data.mag[2];
  1002. monitor_sensor_data(f_acc, f_gry, f_mag, NULL);
  1003. }
  1004. }
  1005. else
  1006. {
  1007. monitor_sensor_data(f_acc, f_gry, f_mag, NULL);
  1008. }
  1009. if(hal_ser_imu_get_data_num(SER_IMU_DIR_BACK) >= 1){
  1010. hal_ser_imu_get_data(SER_IMU_DIR_BACK, 0, &data);
  1011. b_mag[0] = data.mag[0];b_mag[1] = data.mag[1];b_mag[2] = data.mag[2];
  1012. }
  1013. monitor_sensor_data(NULL, NULL, NULL, b_mag);
  1014. }
  1015. }
  1016. else if(
  1017. ob_ser_imu.stage == IMU_CONFIG_STAGE_DONE && \
  1018. ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.acc_odr == LSM_ACC_ODR_104HZ && \
  1019. (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)
  1020. )
  1021. {
  1022. if(TIME_GetTicks()-last_tim >= LowPower_Interval) //监测前脚传感器数据(acc + mag)
  1023. {
  1024. last_tim = TIME_GetTicks();
  1025. group_num = hal_ser_imu_get_data_num(SER_IMU_DIR_FRONT);
  1026. if(group_num > 0)
  1027. {
  1028. for(i=0;i<group_num;i++)
  1029. {
  1030. hal_ser_imu_get_data(SER_IMU_DIR_FRONT, i, &data);
  1031. f_acc[0] = data.acc[0];f_acc[1] = data.acc[1];f_acc[2] = data.acc[2];
  1032. f_mag[0] = data.mag[0];f_mag[1] = data.mag[1];f_mag[2] = data.mag[2];
  1033. monitor_sensor_data(f_acc, NULL, f_mag, NULL);
  1034. }
  1035. }
  1036. else
  1037. {
  1038. monitor_sensor_data(f_acc, NULL, f_mag, NULL);
  1039. }
  1040. }
  1041. }
  1042. 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)
  1043. {
  1044. if(TIME_GetTicks()-last_tim >= StandByPower_Interval) //监测前脚传感器数据(acc)
  1045. {
  1046. last_tim = TIME_GetTicks();
  1047. group_num = hal_ser_imu_get_data_num(SER_IMU_DIR_FRONT);
  1048. if(group_num > 0)
  1049. {
  1050. for(i=0;i<group_num;i++)
  1051. {
  1052. hal_ser_imu_get_data(SER_IMU_DIR_FRONT, i, &data);
  1053. f_acc[0] = data.acc[0];f_acc[1] = data.acc[1];f_acc[2] = data.acc[2];
  1054. monitor_sensor_data(f_acc, NULL, NULL, NULL);
  1055. }
  1056. }
  1057. else
  1058. {
  1059. monitor_sensor_data(f_acc, NULL, NULL, NULL);
  1060. }
  1061. }
  1062. }
  1063. }
  1064. static void hal_ser_imu_monitor_suspend_mode_overflow_process(void)
  1065. {
  1066. char buf[255];
  1067. //监测异常挂起模式是否持续时间超过临界点
  1068. if(ob_ser_imu.is_need_config[SER_IMU_DIR_FRONT] == true || ob_ser_imu.is_need_config[SER_IMU_DIR_BACK] == true)
  1069. {
  1070. ob_ser_imu.except_suspend_occur_sum++;
  1071. if(ob_ser_imu.except_suspend_occur_sum >= MONITOR_SUSPEND_MODE_OVERFLOW_ERR_SUM_MAX){
  1072. Except_SetExceptype(EXCEPT_IMU_SUSPEND_OVERFLOW);
  1073. 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]);
  1074. 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]);
  1075. Except_TxError(EXCEPT_IMU_SUSPEND_OVERFLOW,buf);
  1076. ob_ser_imu.except_suspend_occur_sum = MONITOR_SUSPEND_MODE_OVERFLOW_ERR_SUM_MAX;
  1077. }
  1078. }
  1079. else
  1080. {
  1081. ob_ser_imu.except_suspend_occur_sum = 0;
  1082. Except_ClearExceptype(EXCEPT_IMU_SUSPEND_OVERFLOW);
  1083. }
  1084. }
  1085. static void hal_ser_imu_printf_Process(void)
  1086. {
  1087. SEGGER_RTT_printf(0,"hal_ser_imu----------------------------------------------------------------->\n");
  1088. SEGGER_RTT_printf(0,"stage:0x%x\n",ob_ser_imu.stage);
  1089. SEGGER_RTT_printf(0,"is_need_config[SER_IMU_DIR_FRONT]:0x%x\n",ob_ser_imu.is_need_config[SER_IMU_DIR_FRONT]);
  1090. SEGGER_RTT_printf(0,"is_need_config[SER_IMU_DIR_BACK]:0x%x\n",ob_ser_imu.is_need_config[SER_IMU_DIR_BACK]);
  1091. // SEGGER_RTT_printf(0,"df acc_fs:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.acc_fs);
  1092. SEGGER_RTT_printf(0,"df acc_odr:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.acc_odr);
  1093. // SEGGER_RTT_printf(0,"df acc_power_mode:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.acc_power_mode);
  1094. SEGGER_RTT_printf(0,"df fifo_odr:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.fifo_odr);
  1095. // SEGGER_RTT_printf(0,"df gry_fs:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.gry_fs);
  1096. SEGGER_RTT_printf(0,"df gry_odr:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.gry_odr);
  1097. // SEGGER_RTT_printf(0,"df gry_power_mode:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.gry_power_mode);
  1098. // SEGGER_RTT_printf(0,"df mag_fs:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.mag_fs);
  1099. SEGGER_RTT_printf(0,"df mag_odr:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.mag_odr);
  1100. // SEGGER_RTT_printf(0,"f timestamp_resolution:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.timestamp_resolution);
  1101. // SEGGER_RTT_printf(0,"df timestamp_switch:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm.timestamp_switch);
  1102. SEGGER_RTT_printf(0,"db mag_odr:0x%x\n",ob_ser_imu.drv_param[SER_IMU_DIR_BACK].qmc.mag_odr);
  1103. // SEGGER_RTT_printf(0,"cf acc_fs:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].acc_fs);
  1104. SEGGER_RTT_printf(0,"cf acc_odr:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].acc_odr);
  1105. // SEGGER_RTT_printf(0,"cf acc_power_mode:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].acc_power_mode);
  1106. SEGGER_RTT_printf(0,"cf fifo_odr:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].fifo_odr);
  1107. // SEGGER_RTT_printf(0,"cf gry_fs:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].gry_fs);
  1108. SEGGER_RTT_printf(0,"cf gry_odr:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].gry_odr);
  1109. // SEGGER_RTT_printf(0,"cf gry_power_mode:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].gry_power_mode);
  1110. // SEGGER_RTT_printf(0,"cf mag_fs:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].mag_fs);
  1111. SEGGER_RTT_printf(0,"cf mag_odr:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].mag_odr);
  1112. // SEGGER_RTT_printf(0,"cf timestamp_resolution:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].timestamp_resolution);
  1113. // SEGGER_RTT_printf(0,"cf timestamp_switch:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].timestamp_switch);
  1114. SEGGER_RTT_printf(0,"cb mag_odr:0x%x\n",ob_ser_imu.compre_drv_param[SER_IMU_DIR_BACK].mag_odr);
  1115. 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]);
  1116. 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]);
  1117. SEGGER_RTT_printf(0,"<-----------------------------------------------------------------hal_ser_imu\n");
  1118. }
  1119. /*API ------------------------------------------------------------------------------------------------------------------------------------*/
  1120. /**
  1121. @brief 初始化IMU服务
  1122. @param 无
  1123. @return 错误代码 - [out] -1失败,0成功
  1124. */
  1125. int hal_ser_imu_Init(void)
  1126. {
  1127. int i;
  1128. int ret;
  1129. /***************************************驱动层初始化***************************************************/
  1130. //初始化驱动LSM
  1131. ret = drv_lsm_Init();
  1132. //初始化驱动QMC
  1133. ret += drv_qmc6310_Init();
  1134. //获取驱动LSM配置参数
  1135. drv_lsm_get_config_param(&ob_ser_imu.drv_param[SER_IMU_DIR_FRONT].lsm);
  1136. //获取驱动LSM配置参数
  1137. drv_qmc6310_get_config_param(&ob_ser_imu.drv_param[SER_IMU_DIR_BACK].qmc);
  1138. /***************************************业务逻辑层初始化***************************************************/
  1139. //重置结构体
  1140. memset(&ob_ser_imu,0,sizeof(ob_ser_imu));
  1141. //初始化IMU服务结构体
  1142. ob_ser_imu.stage = IMU_CONFIG_STAGE_DONE;
  1143. for(i=0; i<SER_IMU_HANDLE_NUM_MAX; i++)
  1144. {
  1145. ob_ser_imu.handle[i].id = INVALID_HANDLE;
  1146. ob_ser_imu.handle[i].name = NULL;
  1147. }
  1148. for(i=0; i<SER_IMU_DIR_NUM; i++)
  1149. {
  1150. ob_ser_imu.is_need_config[i] = false;
  1151. }
  1152. //设置驱动配置线程
  1153. Process_Start(0,"hal_ser_imu_Process",hal_ser_imu_Process);
  1154. Process_Start(10,"hal_ser_imu_monitor_sensor_data_process",hal_ser_imu_monitor_sensor_data_process);
  1155. // Process_Start(1000,"hal_ser_imu_printf_Process",hal_ser_imu_printf_Process);
  1156. Process_Start(3000,"hal_ser_imu_monitor_suspend_mode_overflow_process",hal_ser_imu_monitor_suspend_mode_overflow_process);
  1157. if(ret < 0)return -1;
  1158. return 0;
  1159. }
  1160. /**
  1161. @brief 获取IMU服务句柄
  1162. @param p_name - [in] 为服务句柄设置的名字
  1163. @param p_handle_id - [out] 返回的服务句柄ID
  1164. @return 错误代码 - [out] -1失败,0成功
  1165. */
  1166. int hal_ser_imu_get_handle(char *p_name, int *p_handle_id)
  1167. {
  1168. for(int i=0; i<SER_IMU_HANDLE_NUM_MAX; i++)
  1169. {
  1170. //判断是否有空余的句柄
  1171. if(ob_ser_imu.handle[i].id == INVALID_HANDLE)
  1172. {
  1173. //设置该句柄的名字
  1174. ob_ser_imu.handle[i].name = p_name;
  1175. //该句柄使能有效
  1176. ob_ser_imu.handle[i].id = VALID_HANDLE;
  1177. //返回服务句柄ID
  1178. *p_handle_id = i;
  1179. return 0;
  1180. }
  1181. }
  1182. return -1;
  1183. }
  1184. /**
  1185. @brief 设置IMU需要的配置参数
  1186. @param handle - [in] 操作的IMU服务句柄
  1187. @param foot - [in] IMU方向 - 前/后脚
  1188. @param param - [in] IMU配置参数
  1189. @return 错误代码 - [out] -1失败,0成功
  1190. */
  1191. int hal_ser_imu_set_required_param(int handle_id, SER_IMU_DIR_e dir, ser_imu_config_param_t *param)
  1192. {
  1193. //无效句柄返回
  1194. if(handle_id < 0 || ob_ser_imu.handle[handle_id].id == INVALID_HANDLE)return -1;
  1195. //更新操作的配置参数副本
  1196. memcpy(&ob_ser_imu.handle[handle_id].op_param[dir], param ,sizeof(ob_ser_imu.handle[handle_id].op_param[dir]));
  1197. return 0;
  1198. }
  1199. /**
  1200. @brief IMU配置参数是否准备好
  1201. @param foot - [in] IMU方向 - 前/后脚
  1202. @param param - [in] IMU配置参数
  1203. @return 错误代码 - [out] -1失败,0成功
  1204. */
  1205. int hal_ser_imu_is_param_get_ready(SER_IMU_DIR_e dir, ser_imu_config_param_t *param)
  1206. {
  1207. int ret = 0;
  1208. //查看是否需要进行配置
  1209. if(hal_ser_imu_is_need_config())
  1210. {
  1211. return -1;
  1212. }
  1213. //判断当前配置是否满足
  1214. switch(dir)
  1215. {
  1216. case SER_IMU_DIR_FRONT:
  1217. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].acc_fs < param->acc_fs){ret = -1;break;}
  1218. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].acc_odr < param->acc_odr){ret = -1;break;}
  1219. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].acc_power_mode < param->acc_power_mode){ret = -1;break;}
  1220. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].fifo_odr < param->fifo_odr){ret = -1;break;}
  1221. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].gry_fs < param->gry_fs){ret = -1;break;}
  1222. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].gry_odr < param->gry_odr){ret = -1;break;}
  1223. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].gry_power_mode < param->gry_power_mode){ret = -1;break;}
  1224. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].mag_fs < param->mag_fs){ret = -1;break;}
  1225. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].mag_odr < param->mag_odr){ret = -1;break;}
  1226. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].timestamp_resolution < param->timestamp_resolution){ret = -1;break;}
  1227. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_FRONT].timestamp_switch < param->timestamp_switch){ret = -1;break;}
  1228. break;
  1229. case SER_IMU_DIR_BACK:
  1230. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_BACK].mag_fs < param->mag_fs){ret = -1;break;}
  1231. if(ob_ser_imu.compre_drv_param[SER_IMU_DIR_BACK].mag_odr < param->mag_odr){ret = -1;break;}
  1232. break;
  1233. default:
  1234. break;
  1235. }
  1236. return ret;
  1237. }
  1238. /**
  1239. @brief 获取IMU当前数据数量
  1240. @param dir - [in] IMU方向 - 前/后脚
  1241. @return 错误代码 - [out] 返回数据数量
  1242. */
  1243. int hal_ser_imu_get_data_num(SER_IMU_DIR_e dir)
  1244. {
  1245. int ret = 0;
  1246. switch(dir)
  1247. {
  1248. case SER_IMU_DIR_FRONT:
  1249. ret = ob_ser_imu.cur_data_num[SER_IMU_DIR_FRONT];
  1250. break;
  1251. case SER_IMU_DIR_BACK:
  1252. ret = ob_ser_imu.cur_data_num[SER_IMU_DIR_BACK];
  1253. break;
  1254. default:
  1255. break;
  1256. }
  1257. return ret;
  1258. }
  1259. /**
  1260. @brief 获取IMU数据
  1261. @param dir - [in] IMU方向 - 前/后脚
  1262. @param index - [in] 第几组数据
  1263. @param pdata - [out] IMU数据
  1264. @return 错误代码 - [out] -1失败,0成功
  1265. */
  1266. int hal_ser_imu_get_data(SER_IMU_DIR_e dir, int index, ser_imu_data_t *pdata)
  1267. {
  1268. if(index < 0)return -1;
  1269. *pdata = ob_ser_imu.cur_data[dir][index];
  1270. return 0;
  1271. }
  1272. /**
  1273. @brief 数据更新通知
  1274. @param p_cb - [in] 需要回调的函数
  1275. @return 错误代码 - [out] -1失败,0成功,-2已存在
  1276. */
  1277. int hal_ser_imu_data_update_notify(hal_ser_imu_data_update_callback p_cb)
  1278. {
  1279. for(int i=0;i<DATA_UPDATE_NOTIFY_MAX;i++)
  1280. {
  1281. if(ob_ser_imu.notify_cb[i]==p_cb) return -2;
  1282. if(ob_ser_imu.notify_cb[i]==0)
  1283. {
  1284. ob_ser_imu.notify_cb[i] = p_cb; //回调函数
  1285. return 0;
  1286. }
  1287. }
  1288. return -1;
  1289. }