-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtfluna_test_plot.py
48 lines (45 loc) · 1.54 KB
/
tfluna_test_plot.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
######################################################
# Copyright (c) 2021 Maker Portal LLC
# Author: Joshua Hrisko
# Code refactoring (to classes): Clément Nussbaumer, April 2021
######################################################
#
# TF-Luna Mini LiDAR wired to a Raspberry Pi via UART
# --- test ranging plotter for TF-Luna
#
#
######################################################
#
import time
import numpy as np
import tfluna
#
############################
# Testing the TF-Luna Output
############################
#
with tfluna.TfLuna(baud_speed=115200) as tfluna:
tot_pts = 100 # points for sample rate test
time_array,dist_array = [],[] # for storing values
print('Starting Ranging...')
while len(dist_array)<tot_pts:
try:
distance,strength,temperature = tfluna.read_tfluna_data() # read values
dist_array.append(distance) # append to array
time_array.append(time.time())
except:
continue
print('Sample Rate: {0:2.0f} Hz'.format(len(dist_array)/(time_array[-1]-time_array[0]))) # print sample rate
#
##############################
# Plotting the TF-Luna Output
##############################
#
import matplotlib.pyplot as plt
plt.style.use('ggplot') # figure formatting
fig,ax = plt.subplots(figsize=(12,9)) # figure and axis
ax.plot(np.subtract(time_array,time_array[0]),dist_array,linewidth=3.5) # plot ranging data
ax.set_ylabel('Distance [m]',fontsize=16)
ax.set_xlabel('Time [s]',fontsize=16)
ax.set_title('TF-Luna Ranging Test',fontsize=18)
plt.show() # show figure