Repository navigation
Expand file tree
/
Copy pathsample_capture_with_settings_from_file.cpp
More file actions
122 lines (103 loc) · 4.55 KB
/
Copy pathsample_capture_with_settings_from_file.cpp
File metadata and controls
122 lines (103 loc) · 4.55 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
// get_package_share_path was added in ament_index_cpp 1.13.2; on older
// versions fall back to the get_package_share_directory. Note that the
// latter was deprecated in 1.13.0, so if compiling with
// 1.13.0 <= version < 1.13.2, then the compiler will raise a deprecation
// warning. However, this should not happen on any of the ROS distros we
// support.
// TODO: remove this when dropping support for jazzy
#if __has_include(<ament_index_cpp/get_package_share_path.hpp>)
#define HAVE_AMENT_INDEX_CPP_PATH_API
#include <ament_index_cpp/get_package_share_path.hpp>
#else
#include <ament_index_cpp/get_package_share_directory.hpp>
#endif
#include <rclcpp/rclcpp.hpp>
#include <sensor_msgs/msg/image.hpp>
#include <sensor_msgs/msg/point_cloud2.hpp>
#include <std_srvs/srv/trigger.hpp>
#include <stdexcept>
/*
* This sample shows how to set the settings_file_path parameter of the zivid node, subscribe for
* the points/xyzrgba topic, and invoke the capture service. When a point cloud is received, a new
* capture is triggered.
*/
void fatal_error(const rclcpp::Logger & logger, const std::string & message)
{
RCLCPP_ERROR_STREAM(logger, message);
throw std::runtime_error(message);
}
void set_settings(const std::shared_ptr<rclcpp::Node> & node)
{
#ifdef HAVE_AMENT_INDEX_CPP_PATH_API
const auto share_directory = ament_index_cpp::get_package_share_path("zivid_samples");
const auto path_to_settings_yml = share_directory / "settings/camera_settings.yml";
#else
const auto share_directory = ament_index_cpp::get_package_share_directory("zivid_samples");
const auto path_to_settings_yml = share_directory + "/settings/camera_settings.yml";
#endif
RCLCPP_INFO_STREAM(
node->get_logger(),
"Setting parameter `settings_file_path` to '" << path_to_settings_yml << "'");
auto param_client = std::make_shared<rclcpp::AsyncParametersClient>(node, "zivid_camera");
while (!param_client->wait_for_service(std::chrono::seconds(3))) {
if (!rclcpp::ok()) {
fatal_error(node->get_logger(), "Client interrupted while waiting for service to appear.");
}
RCLCPP_INFO(node->get_logger(), "Waiting for the parameters client to appear...");
}
auto result =
param_client->set_parameters({rclcpp::Parameter("settings_file_path", path_to_settings_yml)});
if (
rclcpp::spin_until_future_complete(node, result, std::chrono::seconds(30)) !=
rclcpp::FutureReturnCode::SUCCESS) {
fatal_error(node->get_logger(), "Failed to set `settings_file_path` parameter");
}
}
void set_srgb(const std::shared_ptr<rclcpp::Node> & node)
{
auto param_client = std::make_shared<rclcpp::AsyncParametersClient>(node, "zivid_camera");
while (!param_client->wait_for_service(std::chrono::seconds(3))) {
if (!rclcpp::ok()) {
fatal_error(node->get_logger(), "Client interrupted while waiting for service to appear.");
}
RCLCPP_INFO(node->get_logger(), "Waiting for the parameters client to appear...");
}
auto result = param_client->set_parameters({rclcpp::Parameter("color_space", "srgb")});
if (
rclcpp::spin_until_future_complete(node, result, std::chrono::seconds(30)) !=
rclcpp::FutureReturnCode::SUCCESS) {
fatal_error(node->get_logger(), "Failed to set `color_space` parameter");
}
}
auto create_capture_client(std::shared_ptr<rclcpp::Node> & node)
{
auto client = node->create_client<std_srvs::srv::Trigger>("capture");
while (!client->wait_for_service(std::chrono::seconds(3))) {
if (!rclcpp::ok()) {
fatal_error(node->get_logger(), "Client interrupted while waiting for service to appear.");
}
RCLCPP_INFO(node->get_logger(), "Waiting for the capture service to appear...");
}
RCLCPP_INFO(node->get_logger(), "Service is available");
return client;
}
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
auto node = rclcpp::Node::make_shared("sample_capture_with_settings_from_file");
RCLCPP_INFO(node->get_logger(), "Started the sample node");
set_srgb(node);
auto points_xyzrgba_subscription = node->create_subscription<sensor_msgs::msg::PointCloud2>(
"points/xyzrgba", 10, [&](sensor_msgs::msg::PointCloud2::ConstSharedPtr msg) -> void {
RCLCPP_INFO(
node->get_logger(), "Received point cloud of size %d x %d", msg->width, msg->height);
});
set_settings(node);
auto client = create_capture_client(node);
RCLCPP_INFO(node->get_logger(), "Triggering capture");
client->async_send_request(std::make_shared<std_srvs::srv::Trigger::Request>());
RCLCPP_INFO(node->get_logger(), "Spinning node.. Press Ctrl+C to abort.");
rclcpp::spin(node);
rclcpp::shutdown();
return EXIT_SUCCESS;
}