ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How to properly use ROS service

asked 2020-07-15 15:22:44 -0600

zhefan gravatar image

updated 2020-07-15 20:17:40 -0600

Right now, I got some array of geometry_msgs::Point (geometry_msgs::Point[]) by C++. Now I want to use some clustering algorithm implemented in python. I think ros service might be a good choice for different programming language communication, So I have included the clustering algorithm in the ROS server.

I am confused by the client node. I don't want to have a main function since I cannot save the result for later. Instead, I want to have a regular c++ function which the input parameters are the data (geometry_msgs::Point[]) and the return is results from clustering node.

Is there a proper way to do this? or any other options?

For example:

// e.g. return the clustering center for the data, this is the function I would like to write
std::vector<geometry_msgs::Point> functionUseClusterServer(std::vector<geometry_msgs::Point> data){
    // call ROS server written by python
    ...
    return result_from_server;
    }

// I want that the functionUseClusterServer can be used in the main: 
int main(int argc, chat** argv){
    ...
    result = functionUseClusterServer(data);
    something_else = otherFunction(result);
    ...
{
edit retag flag offensive close merge delete

Comments

Every node must have a main function. It's a fundamental requirement of writing software in C++.

Your question is not clear. What are you trying to do? What do you mean by "save the result for later", and "have a regular C++ function"?

Geoff gravatar image Geoff  ( 2020-07-15 19:47:55 -0600 )edit

I mean I would like to have a C++ function that returns the result from the "server". I don't want to use directly call the server in the main function. I am not sure if you can understand this. I have included the example in the problem description.

zhefan gravatar image zhefan  ( 2020-07-15 20:19:32 -0600 )edit

You could just create a C++ class on the node side and save the returned value from the server as a class variable.

deepaktalwardt gravatar image deepaktalwardt  ( 2020-07-17 10:18:27 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-07-17 02:49:13 -0600

Geoff gravatar image

If you want to call a service from a function that is not the function where you initialised ROS and created the ServiceClient, you have two choices:

  1. Pass the ServiceClient instance to the function where you call the service as an additional argument. Obviously your function that calls the service would have to accept an additional parameter in addition to the data you want to send to the service.
  2. Create a class that wraps the ServiceClient and has a member function for calling it with data you pass to that function. Construct the ServiceClient in the class's constructor, or construct it in main() and pass it to the constructor, then use it in the member function.
edit flag offensive delete link more

Comments

Thanks for the answer! But I am still not very clear about that. Could you provide some simple example for these two choices? Thanks in advance.

zhefan gravatar image zhefan  ( 2020-07-17 10:23:45 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2020-07-15 15:22:44 -0600

Seen: 355 times

Last updated: Jul 17 '20