Обзор roscpp / Обзор roscpp

→2. Calling Services

There are two ways of calling a service in roscpp, the "handle" way and the "bare" way.

Bare

For convenience, the ros::service namespace provides a call method, which does not require creation of a NodeHandle:

1 //remember that ros::init(...) must have been called
2 my_package::Foo foo;
3 ...
4 if (ros::service::call("my_service_name", foo))
5 {
6 ...
7 }
The ros::service namespace also provides some convenience functions such as exists() and waitForService(). See the ros::service namespace API docs for more information.
Handle

The handle way works more similar to how the rest of roscpp works, in that you are returned a ros::ServiceClient which is then used to call the service:
ros::ServiceClient client = nh.serviceClient<my_package::Foo>("my_service_name");
my_package::Foo foo;
...
if (client.call(foo))
{
...
}
ros::ServiceClient also has a number of other useful methods suchs as exists() and waitForExistence(). See the ros::ServiceClient API docs for more information.
0 2. Вызов Сервисов

Существует два способа вызова Сервиса в roscpp, "handle" способ и "bare" способ.

"Bare"
Для удобства, ros::service namespace обеспечивает такой способ вызова, при котором нет необходимости создания NodHandle:
1 //помните что необходимо вызвать ros::init(...)
2 my_package::Foo foo;
3 ...
4 if (ros::service::call("my_service_name", foo))
5 {
6 ...
7 }
ros::service namespace также предоставляет некоторые удобные функции, такие как exist() и waitForService(). Смотри ros::service namespace API документацию для более подробной информации.

"Handle"
Работа Handle способа похожа на работу roscpp. Создается ros::ServiceClient который потом используется для вызова сервиса:
ros::ServiceClient client = nh.serviceClient<my_package::Foo>("my_service_name");
my_package::Foo foo;
...
if (client.call(foo))
{
...
}
ros::ServiceClient также предоставляет некоторые полезные функции, такие как exist() и waitFor Existence(). Смотрите ros::ServiceClient API документацию для более детальной информации.
→1. Service definitions, request messages and response messages

ROS Services are defined by srv files, which contains a request message and a response message. These are identical to the messages used with ROS Topics (see roscpp message overview). roscpp converts these srv files into C++ source code and creates three classes that you need to be familiar with: service definitions, request messages, and response messages. The names of these classes come directly from the srv filename:

my_package/srv/Foo.srv →
my_package::Foo
my_package::Foo::Request
my_package::Foo::Response
Generated Structure

The roscpp service generator generates a structure like this:

1 namespace my_package
2 {
3 struct Foo
4 {
5 class Request
6 {
7 ...
8 };
9
10 class Response
11 {
12 ...
13 };
14
15 Request request;
16 Response response;
17 };
18 }
The Request class provides the input to the service. The Response class is returned to the client as the service's output.

Creating Service Request/Responses

There are two versions of any service call method: one which takes, for example, the Foo struct shown above, and one that takes separate Request and Response objects. Filling out either is very simple. To use the Foo struct itself:

1 my_package::Foo foo;
2 foo.request.<var> = <value>;
3 ...
4 <call>(foo);
To use separate objects:

1 my_package::Foo::Request req;
2 my_package::Foo::Response res;
3 req.<var> = <value>;
4 ...
5 <call>(req, res);
0 Сервисы ROS определяются srv файлами, которые состоят из двух частей: сообщение запроса и сообщение ответа. Они идентичны сообщениям используемых в ROS topic, roscpp конвертирует srv файлы в C++ код и создает 3 необходимых нам класса вроде таких: определение сервиса, сообщения запросов, и сообщения ответов. Имена этих классов берутся непосредственно из названий файлов srv.

my_package/srv/Foo.srv →
my_package::Foo
my_package::Foo::Request
my_package::Foo::Response

Генерируемая структура
Генератор служб roscpp создает структуру подобную этой:

1 namespace my_package
2 {
3 struct Foo
4 {
5 class Request
6 {
7 ...
8 };
9
10 class Response
11 {
12 ...
13 };
14
15 Request request;
16 Response response;
17 };
18 }
Класс запроса обеспечивает входные параметры сервиса. Класс ответа возвращается клиенту с выхода сервиса.

Создание Запросов/Ответов сервиса.
Есть два способа для вызова сервиса: один из которых например принимает структуру показанную выше, а другой отдельно принимает объект запроса и объект ответа. Их написание очень простое. Посмотрим на примере структуры Foo.
1 my_package::Foo foo;
2 foo.request.<var> = <value>;
3 ...
4 <call>(foo);

Используем отдельные объекты:
1 my_package::Foo::Request req;
2 my_package::Foo::Response res;
3 req.<var> = <value>;
4 ...
5 <call>(req, res);
→1. Service definitions, request messages and response messages

ROS Services are defined by srv files, which contains a request message and a response message. These are identical to the messages used with ROS Topics (see roscpp message overview). roscpp converts these srv files into C++ source code and creates three classes that you need to be familiar with: service definitions, request messages, and response messages. The names of these classes come directly from the srv filename:

my_package/srv/Foo.srv →
my_package::Foo
my_package::Foo::Request
my_package::Foo::Response
Generated Structure

The roscpp service generator generates a structure like this:

1 namespace my_package
2 {
3 struct Foo
4 {
5 class Request
6 {
7 ...
8 };
9
10 class Response
11 {
12 ...
13 };
14
15 Request request;
16 Response response;
17 };
18 }
The Request class provides the input to the service. The Response class is returned to the client as the service's output.

Creating Service Request/Responses

There are two versions of any service call method: one which takes, for example, the Foo struct shown above, and one that takes separate Request and Response objects. Filling out either is very simple. To use the Foo struct itself:

1 my_package::Foo foo;
2 foo.request.<var> = <value>;
3 ...
4 <call>(foo);
To use separate objects:

1 my_package::Foo::Request req;
2 my_package::Foo::Response res;
3 req.<var> = <value>;
4 ...
5 <call>(req, res);
0 1. Определения сервисов, сообщения запросов и сообщения ответов.
Резюме
Пол:
мужчина
Родной язык:
русский
С нами:
с 12 августа 2012 г. (5181 день)
Не является членом клуба.
Деятельность:
3 версии перевода с общим рейтингом 0
Написать Allovaro личное сообщение