Skip to content
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

Open
wants to merge 5 commits into
base: rolling
Choose a base branch
from

Conversation

threeal
Copy link

@threeal threeal commented Jul 3, 2021

Separate rclcpp's minimal client and minimal service examples each into lambda, member function, and not composable.

@threeal threeal force-pushed the update-services-examples branch from f6173ae to 9d79427 Compare July 3, 2021 14:46
Copy link
Contributor

@ralph-lange ralph-lange left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for improving and extending the ROS 2 examples. I deem this to be important work!

I found an issue in case that the client node is canceled/stopped (e.g., by ctrl+c) while waiting for the service.

@@ -0,0 +1,67 @@
// Copyright 2016 Open Source Robotics Foundation, Inc.
Copy link
Contributor

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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Copyright 2016 Open Source Robotics Foundation, Inc.
// Copyright 2024 Open Source Robotics Foundation, Inc.

Happy new year 🆕 can be applied to other new files.

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.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prevent an infinite loop, add return (which is valid in a ctor) to leave the loop, cf. the return 1; in not_composable.cpp. However, note that the spin(node) in the main function must not be called if rclcpp::ok() is already false since this will cause an the given context is not valid error. Probably it is better to move the waiting for the service into a one-shot timer as in the examples_rclcpp_minimal_action_client example.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

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.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my above comment about leaving the loop by a return.


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.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
RCLCPP_ERROR(node->get_logger(), "client interrupted while waiting for service to appear.");
RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "client interrupted while waiting for service to appear.");

With the node's logger, the publisher for rosout will occasionally raise a publisher's context is invalid exception on this line.

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.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the return issue is solved, it is probably also necessary to change the logger to rclcpp::get_logger("rclcpp") as in not_composable.cpp.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? this log is not obviously for rclcpp? so getting logger from this(Node) is fine? (saying current code looks fine.)

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.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the return issue is solved, it is probably also necessary to change the logger to rclcpp::get_logger("rclcpp") as in not_composable.cpp.

@fujitatomoya
Copy link
Collaborator

@threeal this's been 3 years. still working on this? if you are, could you please rebase this?

@threeal
Copy link
Author

threeal commented Jan 2, 2024

I'm sorry, i almost forgot i have this PR, i'll try to work on this when i have time.

Copy link
Collaborator

@fujitatomoya fujitatomoya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@threeal a couple of comments. thank you for the contribution.

@@ -0,0 +1,67 @@
// Copyright 2016 Open Source Robotics Foundation, Inc.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Copyright 2016 Open Source Robotics Foundation, Inc.
// Copyright 2024 Open Source Robotics Foundation, Inc.

Happy new year 🆕 can be applied to other new files.

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.");
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

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.");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? this log is not obviously for rclcpp? so getting logger from this(Node) is fine? (saying current code looks fine.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants