-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_client_template.cpp
More file actions
47 lines (43 loc) · 2 KB
/
service_client_template.cpp
File metadata and controls
47 lines (43 loc) · 2 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
#include "ros2_template_programs/service_client_template.hpp"
ros2_template_programs::ServiceClient::ServiceClient() : Node("service_client_template") {
client_ = this->create_client<ros2_custom_msg::srv::Calculation>("calculate_two_numbers");
while (!client_->wait_for_service(1s)) {
if (!rclcpp::ok()) RCLCPP_ERROR(this->get_logger(), "Interrupted while waiting for the service. Exiting.");
else RCLCPP_INFO(this->get_logger(), "service not available, waiting again...");
}
declare_parameter( "a", 1.0);
declare_parameter( "b", 1.0);
request_ = std::make_shared<ros2_custom_msg::srv::Calculation::Request>();
request_->expression.a = get_parameter("a").as_double();
request_->expression.b = get_parameter("b").as_double();
count_ = 0;
}
void ros2_template_programs::ServiceClient::sendRequest() {
while (rclcpp::ok()) {
if ( count_%4 == 0 ) {
request_->expression.calculate = "+";
} else if ( count_%4 == 1 ) {
request_->expression.calculate = "-";
} else if ( count_%4 == 2 ) {
request_->expression.calculate = "*";
} else {
request_->expression.calculate = "/";
}
auto result = client_->async_send_request(request_);
// Wait for the result.
if (rclcpp::spin_until_future_complete(this->get_node_base_interface(), result) == rclcpp::FutureReturnCode::SUCCESS) {
RCLCPP_INFO(this->get_logger(), "Call the calculate_two_numbers server\nExpression :%.2f %s %.2f\nResult : %.2f",
request_->expression.a, request_->expression.calculate.c_str(), request_->expression.b, result.get()->result );
} else {
RCLCPP_ERROR(this->get_logger(), "Failed to call service calculate_two_numbers");
}
count_++;
}
}
int main(int argc, char * argv[]) {
rclcpp::init(argc, argv);
auto client = std::make_shared<ros2_template_programs::ServiceClient>();
client->sendRequest();
rclcpp::shutdown();
return 0;
}