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

Added spin_node_until_future_complete #25

Merged
merged 2 commits into from
Apr 28, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions rclcpp/include/rclcpp/executors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
#ifndef RCLCPP_RCLCPP_EXECUTORS_HPP_
#define RCLCPP_RCLCPP_EXECUTORS_HPP_

#include <future>
#include <rclcpp/executors/multi_threaded_executor.hpp>
#include <rclcpp/executors/single_threaded_executor.hpp>
#include <rclcpp/node.hpp>
#include <rclcpp/utilities.hpp>

namespace rclcpp
{
Expand All @@ -26,6 +29,20 @@ namespace executors
using rclcpp::executors::multi_threaded_executor::MultiThreadedExecutor;
using rclcpp::executors::single_threaded_executor::SingleThreadedExecutor;

template<typename FutureT>
std::shared_future<FutureT> &
spin_node_until_future_complete(
rclcpp::executor::Executor & executor, rclcpp::node::Node::SharedPtr & node_ptr,
std::shared_future<FutureT> & future)
{
std::future_status status;
do {
executor.spin_node_some(node_ptr);
Copy link
Member

Choose a reason for hiding this comment

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

It may be worth putting a TODO here mentioning that this won't work recursively right now, i.e. you can't call spin_node_until_future_complete inside a callback executed by an executor.

status = future.wait_for(std::chrono::seconds(0));
} while (status != std::future_status::ready && rclcpp::utilities::ok());
return future;
Copy link
Member

Choose a reason for hiding this comment

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

Does it need to return the passed in future? What would be a use case for that?

Copy link
Member Author

Choose a reason for hiding this comment

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

Doesn't need to, but it's convenient because one can write code like this:

auto value = spin_node_until_future_complete<int64_t>(executor, node, future).get();

Copy link
Member

Choose a reason for hiding this comment

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

For method chaining I find it pretty _un_intuitive that the function returns the future. Especially since it looks like that passing a temporary future does not work.

I don't know if others have an opinion on this. We could always defer this to an API review in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

The function already returns the future, what changes do you suggest?

Copy link
Member

Choose a reason for hiding this comment

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

Not returning anything.

Copy link
Member Author

Choose a reason for hiding this comment

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

For method chaining I find it pretty intuitive that the function returns the future

You meant intuitive or counter intuitive?

Copy link
Member

Choose a reason for hiding this comment

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

I think it is fine as is, the method chaining looks useful and I don't see that it needs to return anything else.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, important spelling error: I meant unintuitive.

The calling code has the future and it looks arbitrary and _un_intuitive to me why the method would return that specific argument for chaining.

Copy link
Contributor

Choose a reason for hiding this comment

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

Supporting chaining operators make usage much more compact. The other option I see is to have an additional or overridden get method which will do the background spinning. But I think this is cleaner and doesn't require modifying builtins.

}

} // namespace executors
} // namespace rclcpp

Expand Down
11 changes: 11 additions & 0 deletions rclcpp/include/rclcpp/rclcpp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ void spin(Node::SharedPtr & node_ptr)
executor.spin();
}

template<typename FutureT>
std::shared_future<FutureT> &
spin_until_future_complete(
Node::SharedPtr & node_ptr, std::shared_future<FutureT> & future)
{
rclcpp::executors::SingleThreadedExecutor executor;
rclcpp::executors::spin_node_until_future_complete<FutureT>(
executor, node_ptr, future);
return future;
}

} /* namespace rclcpp */

#endif /* RCLCPP_RCLCPP_RCLCPP_HPP_ */