-
Notifications
You must be signed in to change notification settings - Fork 316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update services examples #317
base: rolling
Are you sure you want to change the base?
Changes from all commits
5de6121
fcc2137
32cef87
45817c7
9d79427
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2016 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <chrono> | ||
#include <cinttypes> | ||
#include <memory> | ||
|
||
#include "example_interfaces/srv/add_two_ints.hpp" | ||
#include "rclcpp/rclcpp.hpp" | ||
|
||
using namespace std::chrono_literals; | ||
using example_interfaces::srv::AddTwoInts; | ||
|
||
/* This example creates a subclass of Node and uses std::bind() to register a | ||
* member function as a callback from the client. */ | ||
|
||
class MinimalClient : public rclcpp::Node | ||
{ | ||
public: | ||
MinimalClient() | ||
: Node("minimal_client") | ||
{ | ||
client_ = this->create_client<AddTwoInts>("add_two_ints"); | ||
while (!client_->wait_for_service(1s)) { | ||
if (!rclcpp::ok()) { | ||
RCLCPP_ERROR(this->get_logger(), "client interrupted while waiting for service to appear."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To prevent an infinite loop, add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think @threeal tries to keep the current behavior here, i am okay with current behavior for this example so that user does not need to issue service 1st. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? this log is not obviously for |
||
} | ||
RCLCPP_INFO(this->get_logger(), "waiting for service to appear..."); | ||
} | ||
auto request = std::make_shared<AddTwoInts::Request>(); | ||
request->a = 41; | ||
request->b = 1; | ||
auto result_future = client_->async_send_request( | ||
request, | ||
[this](rclcpp::Client<AddTwoInts>::SharedFutureWithRequest result_future) { | ||
auto result = result_future.get(); | ||
auto request = result.first; | ||
auto response = result.second; | ||
RCLCPP_INFO( | ||
this->get_logger(), "result of %" PRId64 " + %" PRId64 " = %" PRId64, | ||
request->a, request->b, response->sum); | ||
rclcpp::shutdown(); | ||
}); | ||
} | ||
|
||
private: | ||
rclcpp::Client<AddTwoInts>::SharedPtr client_; | ||
}; | ||
|
||
int main(int argc, char * argv[]) | ||
{ | ||
rclcpp::init(argc, argv); | ||
rclcpp::spin(std::make_shared<MinimalClient>()); | ||
rclcpp::shutdown(); | ||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2016 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <chrono> | ||
#include <cinttypes> | ||
#include <memory> | ||
|
||
#include "example_interfaces/srv/add_two_ints.hpp" | ||
#include "rclcpp/rclcpp.hpp" | ||
|
||
using namespace std::chrono_literals; | ||
using std::placeholders::_1; | ||
using example_interfaces::srv::AddTwoInts; | ||
|
||
/* This example creates a subclass of Node and uses std::bind() to register a | ||
* member function as a callback from the client. */ | ||
|
||
class MinimalClient : public rclcpp::Node | ||
{ | ||
public: | ||
MinimalClient() | ||
: Node("minimal_client") | ||
{ | ||
client_ = this->create_client<AddTwoInts>("add_two_ints"); | ||
while (!client_->wait_for_service(1s)) { | ||
if (!rclcpp::ok()) { | ||
RCLCPP_ERROR(this->get_logger(), "client interrupted while waiting for service to appear."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my above comment about leaving the loop by a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once the |
||
} | ||
RCLCPP_INFO(this->get_logger(), "waiting for service to appear..."); | ||
} | ||
auto request = std::make_shared<AddTwoInts::Request>(); | ||
request->a = 41; | ||
request->b = 1; | ||
auto result_future = client_->async_send_request( | ||
request, std::bind(&MinimalClient::service_callback, this, _1)); | ||
} | ||
|
||
private: | ||
void service_callback(rclcpp::Client<AddTwoInts>::SharedFutureWithRequest result_future) const | ||
{ | ||
auto result = result_future.get(); | ||
auto request = result.first; | ||
auto response = result.second; | ||
RCLCPP_INFO( | ||
this->get_logger(), "result of %" PRId64 " + %" PRId64 " = %" PRId64, | ||
request->a, request->b, response->sum); | ||
rclcpp::shutdown(); | ||
} | ||
rclcpp::Client<AddTwoInts>::SharedPtr client_; | ||
}; | ||
|
||
int main(int argc, char * argv[]) | ||
{ | ||
rclcpp::init(argc, argv); | ||
rclcpp::spin(std::make_shared<MinimalClient>()); | ||
rclcpp::shutdown(); | ||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -19,14 +19,20 @@ | |||||
#include "example_interfaces/srv/add_two_ints.hpp" | ||||||
#include "rclcpp/rclcpp.hpp" | ||||||
|
||||||
using AddTwoInts = example_interfaces::srv::AddTwoInts; | ||||||
using namespace std::chrono_literals; | ||||||
using example_interfaces::srv::AddTwoInts; | ||||||
|
||||||
/* We do not recommend this style anymore, because composition of multiple | ||||||
* nodes in the same executable is not possible. Please see one of the subclass | ||||||
* examples for the "new" recommended styles. This example is only included | ||||||
* for completeness because it is similar to "classic" standalone ROS nodes. */ | ||||||
|
||||||
int main(int argc, char * argv[]) | ||||||
{ | ||||||
rclcpp::init(argc, argv); | ||||||
auto node = rclcpp::Node::make_shared("minimal_client"); | ||||||
auto client = node->create_client<AddTwoInts>("add_two_ints"); | ||||||
while (!client->wait_for_service(std::chrono::seconds(1))) { | ||||||
while (!client->wait_for_service(1s)) { | ||||||
if (!rclcpp::ok()) { | ||||||
RCLCPP_ERROR(node->get_logger(), "client interrupted while waiting for service to appear."); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
With the node's logger, the publisher for rosout will occasionally raise a |
||||||
return 1; | ||||||
|
@@ -43,10 +49,10 @@ int main(int argc, char * argv[]) | |||||
RCLCPP_ERROR(node->get_logger(), "service call failed :("); | ||||||
return 1; | ||||||
} | ||||||
auto result = result_future.get(); | ||||||
auto response = result_future.get(); | ||||||
RCLCPP_INFO( | ||||||
node->get_logger(), "result of %" PRId64 " + %" PRId64 " = %" PRId64, | ||||||
request->a, request->b, result->sum); | ||||||
request->a, request->b, response->sum); | ||||||
rclcpp::shutdown(); | ||||||
return 0; | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2016 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <cinttypes> | ||
#include <memory> | ||
|
||
#include "example_interfaces/srv/add_two_ints.hpp" | ||
#include "rclcpp/rclcpp.hpp" | ||
|
||
using example_interfaces::srv::AddTwoInts; | ||
|
||
/* This example creates a subclass of Node and uses a fancy C++11 lambda | ||
* function to shorten the callback syntax, at the expense of making the | ||
* code somewhat more difficult to understand at first glance. */ | ||
|
||
class MinimalService : public rclcpp::Node | ||
{ | ||
public: | ||
MinimalService() | ||
: Node("minimal_service") | ||
{ | ||
server_ = this->create_service<AddTwoInts>( | ||
"add_two_ints", | ||
[this](const AddTwoInts::Request::SharedPtr request, | ||
AddTwoInts::Response::SharedPtr response) { | ||
RCLCPP_INFO( | ||
this->get_logger(), | ||
"request: %" PRId64 " + %" PRId64, request->a, request->b); | ||
response->sum = request->a + request->b; | ||
}); | ||
} | ||
|
||
private: | ||
rclcpp::Service<AddTwoInts>::SharedPtr server_; | ||
}; | ||
|
||
int main(int argc, char * argv[]) | ||
{ | ||
rclcpp::init(argc, argv); | ||
rclcpp::spin(std::make_shared<MinimalService>()); | ||
rclcpp::shutdown(); | ||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2016 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <cinttypes> | ||
#include <memory> | ||
|
||
#include "example_interfaces/srv/add_two_ints.hpp" | ||
#include "rclcpp/rclcpp.hpp" | ||
|
||
using std::placeholders::_1; | ||
using std::placeholders::_2; | ||
using example_interfaces::srv::AddTwoInts; | ||
|
||
/* This example creates a subclass of Node and uses std::bind() to register a | ||
* member function as a callback from the server. */ | ||
|
||
class MinimalService : public rclcpp::Node | ||
{ | ||
public: | ||
MinimalService() | ||
: Node("minimal_service") | ||
{ | ||
server_ = this->create_service<AddTwoInts>( | ||
"add_two_ints", std::bind(&MinimalService::service_callback, this, _1, _2)); | ||
} | ||
|
||
private: | ||
void service_callback( | ||
const AddTwoInts::Request::SharedPtr request, | ||
AddTwoInts::Response::SharedPtr response) const | ||
{ | ||
RCLCPP_INFO( | ||
this->get_logger(), | ||
"request: %" PRId64 " + %" PRId64, request->a, request->b); | ||
response->sum = request->a + request->b; | ||
} | ||
rclcpp::Service<AddTwoInts>::SharedPtr server_; | ||
}; | ||
|
||
int main(int argc, char * argv[]) | ||
{ | ||
rclcpp::init(argc, argv); | ||
rclcpp::spin(std::make_shared<MinimalService>()); | ||
rclcpp::shutdown(); | ||
return 0; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may want to update the copyright information, at least with the year 2021.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happy new year 🆕 can be applied to other new files.