Example 3: Get Sensor Status and Sampling Rate, with Running Average

examples/Example3_StatusandRate.py
  1#-----------------------------------------------------------------------
  2# VL53L1X - Example 3
  3#-----------------------------------------------------------------------
  4#
  5# Ported by  SparkFun Electronics, October 2019
  6# Author: Nathan Seidle
  7# Ported: Wes Furuya
  8# SparkFun Electronics
  9# 
 10# License: This code is public domain but you buy me a beer if you use
 11# this and we meet someday (Beerware license).
 12#
 13# Compatibility: https://www.sparkfun.com/products/14722
 14# 
 15# Do you like this library? Help support SparkFun. Buy a board!
 16# For more information on VL53L1x (ToF), check out the product page
 17# linked above.
 18#
 19# This program is distributed in the hope that it will be useful, but
 20# WITHOUT ANY WARRANTY without even the implied warranty of
 21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 22# General Public License for more details.
 23#
 24# You should have received a copy of the GNU General Public License
 25# along with this program.  If not, see <http:www.gnu.org/licenses/>.
 26#
 27#=======================================================================
 28# Copyright (c) 2019 SparkFun Electronics
 29#
 30# Permission is hereby granted, free of charge, to any person obtaining
 31# a copy of this software and associated documentation files (the
 32# "Software"), to deal in the Software without restriction, including
 33# without limitation the rights to use, copy, modify, merge, publish,
 34# distribute, sublicense, and/or sell copies of the Software, and to
 35# permit persons to whom the Software is furnished to do so, subject to
 36# the following conditions:
 37#
 38# The above copyright notice and this permission notice shall be
 39# included in all copies or substantial portions of the Software.
 40#
 41# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 42# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 43# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 44# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 45# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 46# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 47# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 48#=======================================================================
 49
 50"""
 51	Reading distance from the laser based VL53L1X
 52
 53	This example configures the sensor to short distance mode and then
 54	prints the distance to an object. The output also includes a
 55	running average of the last 10 readings along with some statistics,
 56	like signal rate and frequency (of the measurements).
 57	
 58	If you are getting weird readings, be sure the vacuum tape has been
 59	removed from the sensor.
 60"""
 61
 62import qwiic
 63import time, statistics
 64
 65print("VL53L1X Qwiic Test\n")
 66ToF = qwiic.QwiicVL53L1X()
 67
 68if (ToF.sensor_init() == None):					 # Begin returns 0 on a good init
 69	print("Sensor online!\n")
 70
 71ToF.set_distance_mode(1)	# Sets Distance Mode Short (Long- Change value to 2)
 72
 73distance = [] # Initialize list
 74
 75while True:
 76	start = time.time()
 77
 78	try:
 79		ToF.start_ranging()									# Write configuration bytes to initiate measurement
 80		time.sleep(.005)
 81		distance.append(ToF.get_distance())	# Get the result of the measurement from the sensor
 82		time.sleep(.005)
 83		ToF.stop_ranging()
 84
 85	except Exception as e:
 86		print(e)
 87
 88	end = time.time()
 89
 90	distanceInches = distance[len(distance)-1] / 25.4
 91	distanceFeet = distanceInches / 12.0
 92	
 93	if len(distance) < 10:
 94		avgdistance = statistics.mean(distance)
 95	else:
 96		distance.remove(distance[0])
 97		avgdistance = statistics.mean(distance[len(distance)-10:len(distance)+1]) # Running average of last 10 measurements
 98	
 99	signalrate = ToF.get_signal_rate()
100	rangestatus = ToF.get_range_status()
101	
102	#print("Distance(mm): %s avgDistance(mm): %s Distance(ft): %.3f Signal Rate: %s Range Status: %s Hz: %.5f" % (distance[len(distance)-1], avgdistance, distanceFeet, signalrate, rangestatus,(end-start)))
103	print("Distance(mm): %s avgDistance(mm): %.2f Signal Rate: %s Range Status: %s Hz: %.5f" % (distance[len(distance)-1], avgdistance, signalrate, rangestatus,(end-start)))
104
105
106	#if rangestatus == 0:
107		#print("Good ")
108	#elif rangestatus == 1:
109		#print("Signal Fail ")
110	#elif rangestatus == 2:
111		#print("Sigma Fail ")
112	#elif rangestatus == 7:
113		#print("Wrapped Target Fail ")
114	#else:
115		#print("Unknown (code: %s) ", rangestatus)