-
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
Added services examples #3
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7342666
Added services examples
2f2cbe8
Move request header to a separate structure
fb2c4e6
Refactored to split request messages
6a8b307
Added support for timeouts
513f954
Use ::Request and ::Response
334ae0c
Removed RequestId as a message
dc9ad7a
Pass response object as parameter
c4cbe11
Added support for asynchronous clients
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <algorithm> | ||
#include <chrono> | ||
#include <cmath> | ||
#include <iostream> | ||
#include <string> | ||
#include <sys/time.h> | ||
#include <thread> | ||
|
||
#include <rclcpp/rclcpp.hpp> | ||
|
||
#include <simple_msgs/AllBuiltinTypes.h> | ||
#include <simple_msgs/AllDynamicArrayTypes.h> | ||
#include <simple_msgs/AllPrimitiveTypes.h> | ||
#include <simple_msgs/AllStaticArrayTypes.h> | ||
#include <simple_msgs/Nested.h> | ||
#include <simple_msgs/String.h> | ||
#include <simple_msgs/Uint32.h> | ||
|
||
#include <userland_msgs/AddTwoInts.h> | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
rclcpp::init(argc, argv); | ||
|
||
auto node = rclcpp::Node::make_shared("add_two_ints_client"); | ||
|
||
auto client = node->create_client<userland_msgs::AddTwoInts>("add_two_ints"); | ||
auto request = std::make_shared<userland_msgs::AddTwoInts::Request>(); | ||
auto response = std::make_shared<userland_msgs::AddTwoInts::Response>(); | ||
request->a = 2; | ||
request->b = 3; | ||
|
||
auto f = client->async_send_request(request, response); | ||
|
||
std::future_status status; | ||
do | ||
{ | ||
rclcpp::spin_some(node); | ||
status = f.wait_for(std::chrono::milliseconds(100)); | ||
} while(status != std::future_status::ready && rclcpp::ok()); | ||
|
||
if(std::future_status::ready == status) | ||
{ | ||
std::cout << "FUTURE READY" << std::endl; | ||
std::cout << f.get()->sum << std::endl; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <algorithm> | ||
#include <chrono> | ||
#include <cmath> | ||
#include <iostream> | ||
#include <string> | ||
#include <sys/time.h> | ||
#include <thread> | ||
|
||
#include <rclcpp/rclcpp.hpp> | ||
|
||
#include <simple_msgs/AllBuiltinTypes.h> | ||
#include <simple_msgs/AllDynamicArrayTypes.h> | ||
#include <simple_msgs/AllPrimitiveTypes.h> | ||
#include <simple_msgs/AllStaticArrayTypes.h> | ||
#include <simple_msgs/Nested.h> | ||
#include <simple_msgs/String.h> | ||
#include <simple_msgs/Uint32.h> | ||
|
||
#include <userland_msgs/AddTwoInts.h> | ||
|
||
void add(const std::shared_ptr<userland_msgs::AddTwoInts::Request> req, | ||
std::shared_ptr<userland_msgs::AddTwoInts::Response> res) | ||
{ | ||
std::cout << "Incoming request" << std::endl; | ||
std::cout << "a: " << req->a << " b: " << req->b << std::endl; | ||
res->sum = req->a + req->b; | ||
} | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
rclcpp::init(argc, argv); | ||
|
||
auto node = rclcpp::Node::make_shared("add_two_ints_server"); | ||
|
||
node->create_service<userland_msgs::AddTwoInts>("add_two_ints", add); | ||
|
||
rclcpp::spin(node); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
cmake_minimum_required(VERSION 2.8.3) | ||
|
||
project(userland_msgs) | ||
|
||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") | ||
|
||
find_package(ament_cmake REQUIRED) | ||
find_package(builtin_msgs REQUIRED) | ||
find_package(rosidl_default_generators REQUIRED) | ||
|
||
include_directories(${rosidl_default_generators_INCLUDE_DIRS}) | ||
|
||
rosidl_generate_interfaces(${PROJECT_NAME} | ||
"srv/AddTwoInts.srv" | ||
DEPENDENCIES builtin_msgs | ||
) | ||
|
||
ament_package() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0"?> | ||
<package format="2"> | ||
<name>userland_msgs</name> | ||
<version>0.0.0</version> | ||
<description>A package containing simple message definitions.</description> | ||
<maintainer email="[email protected]">Dirk Thomas</maintainer> | ||
<license>Apache License 2.0</license> | ||
|
||
<buildtool_depend>ament_cmake</buildtool_depend> | ||
|
||
<buildtool_depend>rosidl_default_generators</buildtool_depend> | ||
|
||
<build_depend>builtin_msgs</build_depend> | ||
<build_depend>simple_msgs</build_depend> | ||
|
||
<exec_depend>builtin_msgs</exec_depend> | ||
<exec_depend>simple_msgs</exec_depend> | ||
|
||
<exec_depend>rosidl_default_runtime</exec_depend> | ||
</package> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
int64 a | ||
int64 b | ||
--- | ||
int64 sum |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Adding an asynchronous example (beside this synchronous one) would be good in the future.
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.
I modified the example to work asynchronously. Unfortunately, I can't hide the do { ... } loop because I can't spin the node from inside the client.
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.
I was expecting something like this for the asynchronous case:
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.
I think the future method supports other use cases as well though, we may need something similar to both.
Even in the future method as you have here I would have expected the spin to be elsewhere, though not necessarily.
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.
I expected to be able to spin inside the client, but alas the API doesn't allow that, or I couldn't figure out how. Where else did you expect to spin?
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.
We talked about the issues of spinning inside of a client, you'll need a recursive lock and a way to deal with multithreaded execution...
You can spin in different threads, for example you could create thread to call spin in or you could create the async request in a callback for subscription or you could spin in the main thread and then do the async request in a user created thread. There are lots of options.
The callback pattern that I described above has the advantage that you don't have to poll the future, so its more like request it and forget it. The future pattern has the advantage that you can use one thread to do work while periodically checking the future. But in both cases you need to be spinning in the mean time. In your example you just happen to use the single thread for polling the future and spinning.
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.
Either way this looks like a good proof of concept, we can certainly implement what I described above with the existing functionality. I think we should get a consensus on the API before we spend more time iterating on more API features. To that end I think we should get these branches merged and then we should have a meeting to sync up on the API's, I'll try to setup a meeting today.