vl53l1x.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #include "vl53l1.h"
  2. int32_t distance;
  3. VL53L1_Dev_t VL53;
  4. VL53L1_RangingMeasurementData_t result_data;
  5. mode_data Mode_data[]=
  6. {
  7. {(FixPoint1616_t)(16384), //its uint32_t, uint16_.uint16_t, 0.25*65536
  8. (FixPoint1616_t)(1179648), //18*65536
  9. 33000,
  10. 14,
  11. 10},//default
  12. {(FixPoint1616_t)(16384), //0.25*65536
  13. (FixPoint1616_t)(1179648), //18*65536
  14. 200000,
  15. 14,
  16. 10},//high accuracy
  17. {(FixPoint1616_t)(6554), //0.1*65536
  18. (FixPoint1616_t)(3932160), //60*65536
  19. 33000,
  20. 18,
  21. 14},//long distance
  22. {(FixPoint1616_t)(16384), //0.25*65536
  23. (FixPoint1616_t)(2097152), //32*65536
  24. 20000,
  25. 14,
  26. 10},//high speed
  27. };
  28. VL53L1_Error VL53L1Init(VL53L1_Dev_t* pDev)
  29. {
  30. VL53L1_Error Status = VL53L1_ERROR_NONE;
  31. pDev->I2cDevAddr=0x52;//默认地址
  32. pDev->comms_type=1;//默认通信模式
  33. pDev->comms_speed_khz = 400;//通信速率(可到400hz)
  34. Status = VL53L1_WaitDeviceBooted(pDev);
  35. if(Status!=VL53L1_ERROR_NONE)
  36. {
  37. printf("Wait device Boot failed!\r\n");
  38. return Status;
  39. }
  40. HAL_Delay(2);
  41. Status = VL53L1_DataInit(pDev);//device init
  42. if(Status!=VL53L1_ERROR_NONE)
  43. {
  44. printf("datainit failed!\r\n");
  45. return Status;
  46. }
  47. HAL_Delay(2);
  48. Status = VL53L1_StaticInit(pDev);
  49. if(Status!=VL53L1_ERROR_NONE)
  50. {
  51. printf("static init failed!\r\n");
  52. return Status;
  53. }
  54. HAL_Delay(2);
  55. Status = VL53L1_SetDistanceMode(pDev, VL53L1_DISTANCEMODE_LONG); //short,medium,long
  56. if(Status!=VL53L1_ERROR_NONE)
  57. {
  58. printf("set discance mode failed!\r\n");
  59. return Status;
  60. }
  61. HAL_Delay(2);
  62. return Status;
  63. }
  64. /**
  65. * @brief 传感器进行一个出厂校准
  66. * @param [in] pDev 指定传感器
  67. * @param [in] save 结果存储地址
  68. * @retval VL53L1_Error类型
  69. * @par 日志
  70. *
  71. */
  72. VL53L1_Error VL53Cali(VL53L1_Dev_t* pDev,void * save)
  73. {
  74. VL53L1_Error Status = VL53L1_ERROR_NONE;
  75. Status = VL53L1_StopMeasurement(pDev);
  76. if(Status!=VL53L1_ERROR_NONE)
  77. return Status;
  78. Status = VL53L1_PerformRefSpadManagement(pDev);//perform ref SPAD management
  79. if(Status!=VL53L1_ERROR_NONE)
  80. return Status;
  81. /*
  82. Status = VL53L1_PerformOffsetSimpleCalibration(pDev,140);//14cm的出厂校验值
  83. if(Status!=VL53L1_ERROR_NONE)
  84. return Status;
  85. */
  86. Status = VL53L1_GetCalibrationData(pDev,save);
  87. if(Status!=VL53L1_ERROR_NONE)
  88. return Status;
  89. //全部完成 重新打开测量
  90. Status = VL53L1_StartMeasurement(pDev);
  91. return Status;
  92. }
  93. VL53L1_Error VL53InitParam(VL53L1_Dev_t* pDev,uint8_t mode)
  94. {
  95. VL53L1_Error status = VL53L1_ERROR_NONE;
  96. //4个限制
  97. status = VL53L1_SetLimitCheckEnable(pDev,VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE,1);//sigma--standard deviation, enable SIGMA limit check
  98. if(status!=VL53L1_ERROR_NONE)
  99. return status;
  100. HAL_Delay(2);
  101. status = VL53L1_SetLimitCheckEnable(pDev,VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,1);//signal--amplitude of the signal-
  102. //-reflected. enable signal rate limit check
  103. if(status!=VL53L1_ERROR_NONE)
  104. return status;
  105. HAL_Delay(2);
  106. status = VL53L1_SetLimitCheckValue(pDev,VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE,Mode_data[mode].sigmaLimit);//set SIGMA limit
  107. if(status!=VL53L1_ERROR_NONE)
  108. return status;
  109. HAL_Delay(2);
  110. status = VL53L1_SetLimitCheckValue(pDev,VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,Mode_data[mode].signalLimit);//set signal rate limit
  111. if(status!=VL53L1_ERROR_NONE)
  112. return status;
  113. //4个限制值设置完成
  114. status = VL53L1_SetMeasurementTimingBudgetMicroSeconds(pDev,Mode_data[mode].timingBudget);//set the max interval for a whole diatance test
  115. if(status!=VL53L1_ERROR_NONE)
  116. return status;
  117. HAL_Delay(2);
  118. status = VL53L1_SetInterMeasurementPeriodMilliSeconds(pDev, 300);
  119. if(status!=VL53L1_ERROR_NONE)
  120. {
  121. printf("SetInterMeasurementPeriodMilliSeconds failed!\r\n");
  122. return status;
  123. }
  124. HAL_Delay(2);
  125. status = VL53L1_StartMeasurement(pDev);
  126. if(status!=VL53L1_ERROR_NONE)
  127. {
  128. printf("start measurement failed!\r\n");
  129. return status;
  130. }
  131. return status;
  132. }
  133. int32_t flag=0;
  134. VL53L1_Error getDistance(VL53L1_Dev_t* pDev)
  135. {
  136. VL53L1_Error status = VL53L1_ERROR_NONE;
  137. uint8_t isDataReady=1;
  138. status = VL53L1_WaitMeasurementDataReady(pDev);
  139. //status = VL53L1_GetMeasurementDataReady(pDev,&isDataReady);
  140. if(status!=VL53L1_ERROR_NONE)
  141. {
  142. printf("Wait too long!\r\n");
  143. return status;
  144. }
  145. if(!isDataReady)
  146. {
  147. flag++;
  148. return -7;
  149. }
  150. status = VL53L1_GetRangingMeasurementData(pDev, &result_data);
  151. distance = result_data.RangeMilliMeter;
  152. printf("========>distance:%d\r\n",distance);
  153. status = VL53L1_ClearInterruptAndStartMeasurement(pDev);
  154. return status;
  155. }
  156. //add by lwy
  157. VL53L1_Error VL53L1Init_Roi(VL53L1_Dev_t* pDev,VL53L1_DistanceModes ROI_DistanceMode)
  158. {
  159. VL53L1_Error Status = VL53L1_ERROR_NONE;
  160. pDev->I2cDevAddr=0x52;//默认地址
  161. pDev->comms_type=1;//默认通信模式
  162. pDev->comms_speed_khz = 400;//通信速率(可到400hz)
  163. Status = VL53L1_WaitDeviceBooted(pDev);
  164. if(Status!=VL53L1_ERROR_NONE)
  165. {
  166. printf("Wait device Boot failed!\r\n");
  167. return Status;
  168. }
  169. HAL_Delay(2);
  170. Status = VL53L1_DataInit(pDev);//device init
  171. if(Status!=VL53L1_ERROR_NONE)
  172. {
  173. printf("datainit failed!\r\n");
  174. return Status;
  175. }
  176. HAL_Delay(2);
  177. Status = VL53L1_StaticInit(pDev);
  178. if(Status!=VL53L1_ERROR_NONE)
  179. {
  180. printf("static init failed!\r\n");
  181. return Status;
  182. }
  183. HAL_Delay(2);
  184. Status = VL53L1_SetDistanceMode(pDev, ROI_DistanceMode); //short,medium,long
  185. if(Status!=VL53L1_ERROR_NONE)
  186. {
  187. printf("set discance mode failed!\r\n");
  188. return Status;
  189. }
  190. HAL_Delay(2);
  191. return Status;
  192. }
  193. /*
  194. @brief 初始化roi参数
  195. @param pDev -[in] 指向VL53L1设备的handle
  196. @param roi_zone -[in] 设置roi区域
  197. @return 错误代码
  198. */
  199. VL53L1_Error VL53InitParam_Roi(VL53L1_Dev_t* pDev,uint32_t ROI_TimingBudgetMicroSeconds, uint32_t ROI_InterMeasurementPeriodMilliSeconds, VL53L1_UserRoi_t *roi_zone)
  200. {
  201. uint8_t mode = 2;
  202. VL53L1_Error status = VL53L1_ERROR_NONE;
  203. //4个限制
  204. status = VL53L1_SetLimitCheckEnable(pDev,VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE,1);//sigma--standard deviation, enable SIGMA limit check
  205. if(status!=VL53L1_ERROR_NONE)
  206. return status;
  207. HAL_Delay(2);
  208. status = VL53L1_SetLimitCheckEnable(pDev,VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,1);//signal--amplitude of the signal-
  209. //-reflected. enable signal rate limit check
  210. if(status!=VL53L1_ERROR_NONE)
  211. return status;
  212. HAL_Delay(2);
  213. status = VL53L1_SetLimitCheckValue(pDev,VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE,Mode_data[mode].sigmaLimit);//set SIGMA limit
  214. if(status!=VL53L1_ERROR_NONE)
  215. return status;
  216. HAL_Delay(2);
  217. status = VL53L1_SetLimitCheckValue(pDev,VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,Mode_data[mode].signalLimit);//set signal rate limit
  218. if(status!=VL53L1_ERROR_NONE)
  219. return status;
  220. //4个限制值设置完成
  221. status = VL53L1_SetMeasurementTimingBudgetMicroSeconds(pDev,ROI_TimingBudgetMicroSeconds);//set the max interval for a whole diatance test
  222. if(status!=VL53L1_ERROR_NONE)
  223. return status;
  224. HAL_Delay(2);
  225. status = VL53L1_SetInterMeasurementPeriodMilliSeconds(pDev, ROI_InterMeasurementPeriodMilliSeconds);
  226. if(status!=VL53L1_ERROR_NONE)
  227. {
  228. printf("SetInterMeasurementPeriodMilliSeconds failed!\r\n");
  229. return status;
  230. }
  231. HAL_Delay(2);
  232. status = VL53L1_SetUserROI(pDev,roi_zone);
  233. if(status!=VL53L1_ERROR_NONE)
  234. {
  235. printf("VL53L1_SetUserROI failed!\r\n");
  236. return status;
  237. }
  238. HAL_Delay(2);
  239. status = VL53L1_StartMeasurement(pDev);
  240. if(status!=VL53L1_ERROR_NONE)
  241. {
  242. printf("start measurement failed!\r\n");
  243. return status;
  244. }
  245. return status;
  246. }
  247. int32_t flag2=0;
  248. VL53L1_Error getDistanceByRoi(VL53L1_Dev_t* pDev, VL53L1_UserRoi_t *roi_zone, int32_t *return_distance)
  249. {
  250. VL53L1_Error status = VL53L1_ERROR_NONE;
  251. uint8_t isDataReady=1;
  252. //status = VL53L1_WaitMeasurementDataReady(pDev);
  253. status = VL53L1_GetMeasurementDataReady(pDev,&isDataReady);
  254. if(status!=VL53L1_ERROR_NONE)
  255. {
  256. printf("Wait too long!\r\n");
  257. return status;
  258. }
  259. if(!isDataReady)
  260. {
  261. flag2++;
  262. return -7;
  263. }
  264. flag2 = 0;
  265. status = VL53L1_GetRangingMeasurementData(pDev, &result_data);
  266. *return_distance = result_data.RangeMilliMeter;
  267. status += VL53L1_ClearInterruptAndStartMeasurement(pDev);
  268. /*wait a couple of ms to ensure the setting of the new ROI center for the next ranging*/
  269. status += VL53L1_SetUserROI(pDev,roi_zone);
  270. return status;
  271. }