1+ #!/usr/bin/env python
2+
3+ #simpleS500Example.py
4+ from brping import definitions
5+ from brping import S500
6+ import time
7+ import argparse
8+
9+ from builtins import input
10+
11+ import signal
12+ import sys
13+ import struct
14+
15+ ##Parse Command line options
16+ ############################
17+
18+ parser = argparse .ArgumentParser (description = "Ping python library example." )
19+ parser .add_argument ('--device' , action = "store" , required = False , type = str , help = "Ping device port. E.g: /dev/ttyUSB0" )
20+ parser .add_argument ('--baudrate' , action = "store" , type = int , default = 115200 , help = "Ping device baudrate. E.g: 115200" )
21+ parser .add_argument ('--udp' , action = "store" , required = False , type = str , help = "Ping UDP server. E.g: 192.168.2.2:9090" )
22+ args = parser .parse_args ()
23+ if args .device is None and args .udp is None :
24+ parser .print_help ()
25+ exit (1 )
26+
27+ # Signal handler to stop pinging on the S500
28+ def signal_handler (sig , frame ):
29+ print ("Stopping pinging on S500..." )
30+ myS500 .control_set_ping_params (
31+ start_mm = 0 ,
32+ length_mm = 0 ,
33+ gain_index = - 1 ,
34+ msec_per_ping = - 1 ,
35+ pulse_len_usec = 0 ,
36+ report_id = 0 ,
37+ reserved = 0 ,
38+ chirp = 1 ,
39+ decimation = 0
40+ )
41+ sys .exit (0 )
42+
43+ signal .signal (signal .SIGINT , signal_handler )
44+
45+ # Make a new S500
46+ myS500 = S500 ()
47+ if args .device is not None :
48+ myS500 .connect_serial (args .device , args .baudrate )
49+ elif args .udp is not None :
50+ (host , port ) = args .udp .split (':' )
51+ myS500 .connect_udp (host , int (port ))
52+
53+ if myS500 .initialize () is False :
54+ print ("Failed to initialize S500!" )
55+ exit (1 )
56+
57+ print ("------------------------------------" )
58+ print ("Starting S500.." )
59+ print ("Press CTRL+C to exit" )
60+ print ("------------------------------------" )
61+
62+ input ("Press Enter to continue..." )
63+
64+ print ("\n -------Distance2-------" )
65+ # Tell S500 to send distance2 data
66+ myS500 .control_set_ping_params (
67+ start_mm = 0 ,
68+ length_mm = 0 ,
69+ gain_index = - 1 ,
70+ msec_per_ping = 0 ,
71+ pulse_len_usec = 0 ,
72+ report_id = definitions .S500_DISTANCE2 ,
73+ reserved = 0 ,
74+ chirp = 1 ,
75+ decimation = 0
76+ )
77+
78+ # Read and print distance2 data
79+ data = myS500 .wait_message ([definitions .S500_DISTANCE2 ])
80+ if data :
81+ print (f"Ping Distance: { data .ping_distance_mm } mm" )
82+ print (f"Confidence: { data .ping_confidence } " )
83+ print (f"Average Distance: { data .averaged_distance_mm } mm" )
84+ print (f"Confidence of Average: { data .average_distance_confidence } " )
85+ print (f"Timestamp: { data .timestamp } " )
86+
87+ print ("\n -------Profile6-------" )
88+ # Tell S500 to send profile6 data
89+ myS500 .control_set_ping_params (
90+ start_mm = 0 ,
91+ length_mm = 0 ,
92+ gain_index = - 1 ,
93+ msec_per_ping = 100 ,
94+ pulse_len_usec = 0 ,
95+ report_id = definitions .S500_PROFILE6_T ,
96+ reserved = 0 ,
97+ chirp = 1 ,
98+ decimation = 0
99+ )
100+
101+ # Read and print profile6 data
102+ data = myS500 .wait_message ([definitions .S500_PROFILE6_T ])
103+ if data :
104+ scaled_result = S500 .scale_power (data )
105+
106+ if (data .num_results > 100 ):
107+ for i in range (5 ):
108+ print (f"{ i } :\t Not Scaled: { data .pwr_results [i ]} | Scaled: { scaled_result [i ]:.2f} dB" )
109+ print (".\n .\n ." )
110+ for i in range (5 , 0 , - 1 ):
111+ print (f"{ data .num_results - i } :\t Not Scaled: { data .pwr_results [data .num_results - i ]} | Scaled: { scaled_result [data .num_results - i ]:.2f} dB" )
112+ else :
113+ for i in range (len (scaled_result )):
114+ print (f"{ i + 1 } :\t Not scaled: { data .pwr_results [i ]} | Scaled: { scaled_result [i ]:.2f} dB" )
115+
116+ print (f"Number of results: { data .num_results } " )
117+ print (f"Min power: { data .min_pwr_db } dB" )
118+ print (f"Max power: { data .max_pwr_db } dB" )
119+ # print(data)
120+
121+ else :
122+ print ("Failed to get profile6 data" )
123+
124+ # Stop pinging
125+ myS500 .control_set_ping_params (
126+ start_mm = 0 ,
127+ length_mm = 0 ,
128+ gain_index = - 1 ,
129+ msec_per_ping = - 1 ,
130+ pulse_len_usec = 0 ,
131+ report_id = 0 ,
132+ reserved = 0 ,
133+ chirp = 1 ,
134+ decimation = 0
135+ )
0 commit comments