-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathlidar_device_control.py
More file actions
229 lines (193 loc) · 8.16 KB
/
Copy pathlidar_device_control.py
File metadata and controls
229 lines (193 loc) · 8.16 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# ******************************************************************************
# pyorbbecsdk LiDAR Example — LiDAR Device Control
#
# What you will learn:
# 1. How to enumerate LiDAR-specific device properties
# 2. How to read and set LiDAR sensor parameters at runtime
# 3. How to enable LiDAR scan, accelerometer, and gyroscope streams together
#
# Device requirement: Orbbec LiDAR devices
#
# Run:
# python examples/lidar_examples/lidar_device_control.py
# ******************************************************************************
import os
import sys
from pyorbbecsdk import Context, OBPermissionType, OBPropertyType # type: ignore
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from utils import is_lidar_device
# Select a device, the name, pid, vid, uid of the device will be printed here,
# and the corresponding device object will be created after selection
def select_device(device_list):
dev_count = device_list.get_count()
print("Device list: ")
for i in range(dev_count):
print(
f"{i}. name: {device_list.get_device_name_by_index(i)}, "
f"vid: 0x{hex(device_list.get_device_vid_by_index(i))}, "
f"pid: 0x{hex(device_list.get_device_pid_by_index(i))}, "
f"uid: {device_list.get_device_uid_by_index(i)}, "
f"sn: {device_list.get_device_serial_number_by_index(i)}"
)
while True:
try:
dev_index = int(input("Select a device index: "))
if 0 <= dev_index < dev_count:
return device_list.get_device_by_index(dev_index)
except ValueError:
pass
print("Invalid selection, please reselect.")
# Convert permission type to string
def permission_type_to_string(permission):
if permission == OBPermissionType.PERMISSION_READ:
return "R/_"
elif permission == OBPermissionType.PERMISSION_WRITE:
return "_/W"
elif permission == OBPermissionType.PERMISSION_READ_WRITE:
return "R/W"
return "_/_"
# Check if the property is a primary type (Int, Float, or Bool)
def is_primary_type_property(property_item):
return property_item.type in [
OBPropertyType.OB_INT_PROPERTY,
OBPropertyType.OB_FLOAT_PROPERTY,
OBPropertyType.OB_BOOL_PROPERTY,
]
# Get property list
def get_property_list(device):
property_vec = []
size = device.get_support_property_count()
for i in range(size):
item = device.get_supported_property(i)
if is_primary_type_property(item) and item.permission != OBPermissionType.PERMISSION_DENY:
property_vec.append(item)
return property_vec
# Print a list of supported properties
def printf_property_list(device, property_list):
print(f"size: {len(property_list)}")
if not property_list:
print("No supported property!")
return
print("\n" + "-" * 72)
for i, item in enumerate(property_list):
str_range = ""
try:
if item.type == OBPropertyType.OB_BOOL_PROPERTY:
# Bool type: fixed string, no range query needed
str_range = "Bool value(min:0, max:1, step:1)"
elif item.type == OBPropertyType.OB_INT_PROPERTY:
# Try to get range (same as 06_control.py)
try:
int_range = device.get_int_property_range(item.id)
str_range = f"Int value(min:{int_range.min}, max:{int_range.max}, step:{int_range.step})"
except Exception:
str_range = "Int value"
elif item.type == OBPropertyType.OB_FLOAT_PROPERTY:
# Try to get range (same as 06_control.py)
try:
float_range = device.get_float_property_range(item.id)
str_range = f"Float value(min:{float_range.min:.2f}, max:{float_range.max:.2f}, step:{float_range.step:.2f})"
except Exception:
str_range = "Float value"
except Exception:
str_range = "Int value" if item.type == OBPropertyType.OB_INT_PROPERTY else "Float value"
print(
f"{i:02d}. {item.name}({int(item.id)}), "
f"permission={permission_type_to_string(item.permission)}, "
f"range={str_range}"
)
print("-" * 72 + "\n")
# Get property value
def get_property_value(device, item):
try:
val = None
if item.type == OBPropertyType.OB_BOOL_PROPERTY:
val = device.get_bool_property(item.id)
elif item.type == OBPropertyType.OB_INT_PROPERTY:
val = device.get_int_property(item.id)
elif item.type == OBPropertyType.OB_FLOAT_PROPERTY:
val = device.get_float_property(item.id)
print(f"property name: {item.name}, get value: {val}")
except Exception as e:
print(f"get property failed: {item.name}, error: {e}")
# Set properties
def set_property_value(device, item, str_value):
try:
val = None
if item.type == OBPropertyType.OB_BOOL_PROPERTY:
val = int(str_value)
device.set_bool_property(item.id, bool(val))
elif item.type == OBPropertyType.OB_INT_PROPERTY:
val = int(str_value)
device.set_int_property(item.id, val)
elif item.type == OBPropertyType.OB_FLOAT_PROPERTY:
val = float(str_value)
device.set_float_property(item.id, val)
print(f"property name: {item.name}, set value: {val} success")
except Exception as e:
print(f"set property failed: {item.name}, error: {e}")
def main():
try:
# Create a Context.
ctx = Context()
# Query the list of connected devices
device_list = ctx.query_devices()
# Found no device
if device_list.get_count() <= 0:
print("Device Not Found")
return
# If a single device is plugged in, the first one is selected by default
# Otherwise, select a device from the list
device = select_device(device_list) if device_list.get_count() > 1 else device_list.get_device_by_index(0)
# Check LiDAR device
if not is_lidar_device(device):
print("Invalid device, please connect a LiDAR device!")
return
# Get and print device information
info = device.get_device_info()
print("\n" + "-" * 72)
print(
f"Current Device: name: {info.get_name()}, vid: 0x{info.get_vid():x}, pid: 0x{info.get_pid():04x}, uid: {info.get_uid()}"
)
# Enter property control loop
print("Input '?' to get all properties.")
print("Input 'exit' to exit the program.")
# Get property list
property_list = get_property_list(device)
# Sort property list by ID
property_list.sort(key=lambda x: x.id.value)
while True:
choice = input("\n>> ").strip()
if not choice:
continue
# Exit the program
if choice == "exit":
break
# Show all properties
if choice == "?":
printf_property_list(device, property_list)
print("Please select property. (Usage: [index] [set/get] [value])")
continue
# Parse input and check if it matches the input format
parts = choice.split()
try:
idx = int(parts[0])
if idx >= len(property_list):
print("Your selection is out of range, please reselect.")
continue
item = property_list[idx]
cmd = parts[1].lower()
if cmd == "get":
# get property value
get_property_value(device, item)
elif cmd == "set" and len(parts) >= 3:
# set property value
set_property_value(device, item, parts[2])
else:
print("Property control usage: [property index] [set] [property value] or [property index] [get]")
except (ValueError, IndexError):
print("Invalid input format.")
except Exception as e:
print(f"\nAn error occurred: {e}")
if __name__ == "__main__":
main()