-
Notifications
You must be signed in to change notification settings - Fork 45
/
sample_capture_2d.py
executable file
·86 lines (66 loc) · 2.45 KB
/
sample_capture_2d.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 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 Image
from std_srvs.srv import Trigger
class Sample(Node):
def __init__(self):
super().__init__('sample_capture_2d_py')
self.capture_2d_service = self.create_client(Trigger, 'capture_2d')
while not self.capture_2d_service.wait_for_service(timeout_sec=3.0):
self.get_logger().info('capture_2d service not available, waiting again...')
self._set_settings_2d()
self.subscription = self.create_subscription(
Image, 'color/image_color', self.on_image_color, 10
)
def _set_settings_2d(self):
self.get_logger().info('Setting parameter `settings_2d_yaml`')
settings_parameter = Parameter(
'settings_2d_yaml',
Parameter.Type.STRING,
"""
__version__:
serializer: 1
data: 3
Settings2D:
Acquisitions:
- Acquisition:
Aperture: 2.83
Brightness: 1.0
ExposureTime: 10000
Gain: 2.5
""",
).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_2d service')
return self.capture_2d_service.call_async(Trigger.Request())
def on_image_color(self, msg):
self.get_logger().info(f'Received image of size {msg.width} x {msg.height}')
def main(args=None):
rclpy.init(args=args)
try:
sample = Sample()
sample.get_logger().info('Spinning node.. Press Ctrl+C to abort.')
while rclpy.ok():
future = sample.capture()
rclpy.spin_until_future_complete(sample, future)
sample.get_logger().info('Capture complete')
except KeyboardInterrupt:
pass
except ExternalShutdownException:
sys.exit(1)
if __name__ == '__main__':
main()