-
Notifications
You must be signed in to change notification settings - Fork 45
/
sample_capture_with_settings_from_file.py
executable file
·86 lines (64 loc) · 2.56 KB
/
sample_capture_with_settings_from_file.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
#!/usr/bin/env python
import sys
from ament_index_python.packages import get_package_share_directory
from rcl_interfaces.srv import SetParameters
import rclpy
from rclpy.executors import ExternalShutdownException
from rclpy.node import Node
from rclpy.parameter import Parameter
from sensor_msgs.msg import PointCloud2
from std_srvs.srv import Trigger
class Sample(Node):
def __init__(self):
super().__init__('sample_capture_with_settings_from_file_py')
self.capture_service = self.create_client(Trigger, 'capture')
while not self.capture_service.wait_for_service(timeout_sec=3.0):
self.get_logger().info('capture service not available, waiting again...')
self._set_settings()
self.subscription = self.create_subscription(
PointCloud2, 'points/xyzrgba', self.on_points, 10
)
def _set_settings(self):
path_to_settings_yml = (
get_package_share_directory('zivid_samples')
+ '/settings/camera_settings.yml'
)
self.get_logger().info(
'Setting parameter `settings_file_path` to: ' + path_to_settings_yml
)
settings_parameter = Parameter(
'settings_file_path',
Parameter.Type.STRING,
path_to_settings_yml,
).to_parameter_msg()
param_client = self.create_client(SetParameters, 'zivid_camera/set_parameters')
while not param_client.wait_for_service(timeout_sec=3):
self.get_logger().info('Parameter service not available, waiting again...')
future = param_client.call_async(
SetParameters.Request(parameters=[settings_parameter])
)
rclpy.spin_until_future_complete(self, future, timeout_sec=30)
if not future.result():
raise RuntimeError('Failed to set parameters')
def capture(self):
self.get_logger().info('Calling capture service')
return self.capture_service.call_async(Trigger.Request())
def on_points(self, msg):
self.get_logger().info(
f'Received point cloud of size {msg.width} x {msg.height}'
)
def main(args=None):
rclpy.init(args=args)
try:
sample = Sample()
future = sample.capture()
rclpy.spin_until_future_complete(sample, future)
sample.get_logger().info('Capture complete')
sample.get_logger().info('Spinning node.. Press Ctrl+C to abort.')
rclpy.spin(sample)
except KeyboardInterrupt:
pass
except ExternalShutdownException:
sys.exit(1)
if __name__ == '__main__':
main()