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

examples/Example3_StatusandRate.py
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#-----------------------------------------------------------------------
# VL53L1X - Example 3
#-----------------------------------------------------------------------
#
# Ported by  SparkFun Electronics, October 2019
# Author: Nathan Seidle
# Ported: Wes Furuya
# SparkFun Electronics
# 
# License: This code is public domain but you buy me a beer if you use
# this and we meet someday (Beerware license).
#
# Compatibility: https://www.sparkfun.com/products/14722
# 
# Do you like this library? Help support SparkFun. Buy a board!
# For more information on VL53L1x (ToF), check out the product page
# linked above.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http:www.gnu.org/licenses/>.
#
#=======================================================================
# Copyright (c) 2019 SparkFun Electronics
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#=======================================================================

"""
	Reading distance from the laser based VL53L1X

	This example configures the sensor to short distance mode and then
	prints the distance to an object. The output also includes a
	running average of the last 10 readings along with some statistics,
	like signal rate and frequency (of the measurements).
	
	If you are getting weird readings, be sure the vacuum tape has been
	removed from the sensor.
"""

import qwiic
import time, statistics

print("VL53L1X Qwiic Test\n")
ToF = qwiic.QwiicVL53L1X()

if (ToF.sensor_init() == None):					 # Begin returns 0 on a good init
	print("Sensor online!\n")

ToF.set_distance_mode(1)	# Sets Distance Mode Short (Long- Change value to 2)

distance = [] # Initialize list

while True:
	start = time.time()

	try:
		ToF.start_ranging()									# Write configuration bytes to initiate measurement
		time.sleep(.005)
		distance.append(ToF.get_distance())	# Get the result of the measurement from the sensor
		time.sleep(.005)
		ToF.stop_ranging()

	except Exception as e:
		print(e)

	end = time.time()

	distanceInches = distance[len(distance)-1] / 25.4
	distanceFeet = distanceInches / 12.0
	
	if len(distance) < 10:
		avgdistance = statistics.mean(distance)
	else:
		distance.remove(distance[0])
		avgdistance = statistics.mean(distance[len(distance)-10:len(distance)+1]) # Running average of last 10 measurements
	
	signalrate = ToF.get_signal_rate()
	rangestatus = ToF.get_range_status()
	
	#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)))
	print("Distance(mm): %s avgDistance(mm): %.2f Signal Rate: %s Range Status: %s Hz: %.5f" % (distance[len(distance)-1], avgdistance, signalrate, rangestatus,(end-start)))


	#if rangestatus == 0:
		#print("Good ")
	#elif rangestatus == 1:
		#print("Signal Fail ")
	#elif rangestatus == 2:
		#print("Sigma Fail ")
	#elif rangestatus == 7:
		#print("Wrapped Target Fail ")
	#else:
		#print("Unknown (code: %s) ", rangestatus)