{"id":9330,"date":"2023-04-16T11:01:36","date_gmt":"2023-04-16T11:01:36","guid":{"rendered":"https:\/\/www.fambach.net\/?p=9330"},"modified":"2023-04-22T09:43:34","modified_gmt":"2023-04-22T09:43:34","slug":"ros-service-erstellen","status":"publish","type":"post","link":"https:\/\/www.fambach.net\/en\/ros-service-erstellen\/","title":{"rendered":"ROS: Service Erstellen"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_73 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.fambach.net\/en\/ros-catkin\/#Installation\" title=\"Installation\">Installation<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.fambach.net\/en\/ros-catkin\/#Ubuntu_1804\" title=\"Ubuntu 18.04\">Ubuntu 18.04<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.fambach.net\/en\/ros-catkin\/#Abhangigkeiten_auflosen\" title=\"Abh\u00e4ngigkeiten aufl\u00f6sen\">Abh\u00e4ngigkeiten aufl\u00f6sen<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.fambach.net\/en\/ros-catkin\/#Catkin_installieren\" title=\"Catkin installieren\">Catkin installieren<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.fambach.net\/en\/ros-catkin\/#Befehle\" title=\"Befehle\">Befehle<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.fambach.net\/en\/ros-catkin\/#Bauen\" title=\"Bauen\">Bauen<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.fambach.net\/en\/ros-catkin\/#Installieren\" title=\"Installieren\">Installieren<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.fambach.net\/en\/ros-catkin\/#Quellen\" title=\"Quellen\">Quellen<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>Jeder Knoten kann Services anbieten. Ein Service zeichnet sich durch eine Anfrage gefolgt von einer Anwort aus. Services werden verwendet um bestimmt funktionen in Knoten aufzurufen. Zum Beispiel um einen Zustand abzufragen oder um einen Reset durchzuf\u00fchren. Die initative geht hierbei vom Anfragenden aus. Dies unterscheidet sich von den Nachrichten (Messages) die abboniert werden.<\/p>\n\n\n\n<p>Wie das funktioniert soll hier kurz beschrieben werden.  F\u00fcr eine auf\u00fchrliche Anleitung sei auf https:\/\/wiki.ros.org\/ROS\/Tutorials\/CreatingMsgAndSrv verwiesen.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Voraussetzung<\/h1>\n\n\n<div class=\"display-posts-listing\">\n<div class=\"listing-item\"><a class=\"title\" href=\"https:\/\/www.fambach.net\/en\/ros-einfachen-publisher-subscriber-in-c-erstellen\/\">ROS: Einfachen Publisher\/Subscriber in C++ Erstellen<\/a><\/div>\n<div class=\"listing-item\"><a class=\"title\" href=\"https:\/\/www.fambach.net\/en\/ros-service-erstellen\/\">ROS: Service Erstellen<\/a><\/div>\n<div class=\"listing-item\"><a class=\"title\" href=\"https:\/\/www.fambach.net\/en\/ros-nachrichten-erstellen\/\">ROS: Nachrichten Erstellen<\/a><\/div>\n<div class=\"listing-item\"><a class=\"title\" href=\"https:\/\/www.fambach.net\/en\/ros-catkin\/\">ROS: Catkin<\/a><\/div>\n<\/div>\n\n\n\n<h1 class=\"wp-block-heading\">Ordner erstellen<\/h1>\n\n\n\n<p>Services werden im srv Ordner des Paketes abgelegt.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code lang=\"bash\" class=\"language-bash\">mkdir ~\/&lt;Workspace&gt;\/src\/&lt;Paket&gt;\/srv<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Service Datei erstellen<\/h1>\n\n\n\n<p>Eine Service Datei hat die Endung .srv und beinhaltet zwei Teile einen Ein- und einen Ausgang. Diese werden durch drei Minuszeichen von einander getrennt. Auch hier werden nur die Daten der Schnittstelle definiert. Es wird nun eine standard Nachricht kopieren, diese Vorgehensweise ist auch in Praxis hilfreich, da viele Services schon definiert sind. Zum kopieren wird das Programm roscp (Ros Copy) verwendet.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code lang=\"bash\" class=\"language-bash\">roscp [Paket Name] [Dateiname] [Zeilpfad]<\/code><\/pre>\n\n\n\n<p>Hier mein Beispiel, ihr m\u00fcsst euch hierzu im Paketordner befinden.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code lang=\"bash\" class=\"language-bash\">roscp rospy_tutorials AddTwoInts.srv srv\/AddTwoInts.srv<\/code><\/pre>\n\n\n\n<p>So sieht die Datei aus, es werden zwei Ganzzahlen erwartet und eine zur\u00fcckgegeben.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code lang=\"bash\" class=\"language-bash\">int64 a\nint64 b\n---\nint64 sum<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Builddateien anpassen<\/h1>\n\n\n\n<p>Nun m\u00fcssen wir noch sicherstellen, dass die Nachricht auch generiert wird. Hierzu \u00f6ffnen wir die package.xml Datei im eigenen Paket und schauen ob der folgenden Code enthalten ist.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code class=\"\">  &lt;build_depend&gt;message_generation&lt;\/build_depend&gt;\n  &lt;exec_depend&gt;message_runtime&lt;\/exec_depend&gt;<\/code><\/pre>\n\n\n\n<p>Bei mir waren sie schon eindokumentiert.<\/p>\n\n\n\n<p>Zum Schluss passen wir noch die CMakeLists.txt Datei an. Bitte \u00f6ffnen und den folgenden Code vervollst\u00e4ndigen.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code lang=\"xml\" class=\"language-xml\">cmake_minimum_required(VERSION 3.0.2)\nproject(&lt;PacketName&gt;)\n\n\ninclude_directories(\n include\n  ${catkin_INCLUDE_DIRS}ros melodie \n)\n\n\nfind_package(catkin REQUIRED COMPONENTS\n  roscpp\n  rospy\n  std_msgs\n  message_generation\n)\n\nadd_service_files(\n  FILES\n  AddTwoInts.srv\n)\n\ngenerate_messages(\n   DEPENDENCIES std_msgs\n)\n\ncatkin_package(\n  CATKIN_DEPENDS roscpp rospy message_runtime std_msgs\n)\n<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Service erstellen<\/h1>\n\n\n\n<p>Nun catkin_make im Workspace Ordner ausf\u00fchren<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code class=\"\">cd ~\/&lt;Workspace&gt;\ncatkin_make<\/code><\/pre>\n\n\n\n<p>Es werden drei Dateien erstellt.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code lang=\"bash\" class=\"language-bash\">cd ~\/&lt;Workspace&gt;\/devel\/include\/&lt;Paketname&gt; | ls \nAddTwoInts.h  \nAddTwoIntsRequest.h\nAddTwoIntsResponse.h <\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Verwenden von rossrv<\/h1>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code lang=\"bash\" class=\"language-bash\">rossrv show &lt;service type&gt;<\/code><\/pre>\n\n\n\n<p>Bei mir wie folgt<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code lang=\"bash\" class=\"language-bash\">rossrv show beginner_tutorials\/AddTwoInts<\/code><\/pre>\n\n\n\n<p>Ausgabe <\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code lang=\"bash\" class=\"language-bash\">int64 a\nint64 b\n---\nint64 sum<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Hilfe <\/h1>\n\n\n\n<p>Gibts mit -h<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code class=\"\">rossrv -h<\/code><\/pre>\n\n\n\n<p>und zeigt alle Befehle von rossrv<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code class=\"\">rossrv is a command-line tool for displaying information about ROS Service types.\n\nCommands:\n\trossrv show\tShow service description\n\trossrv info\tAlias for rossrv show\n\trossrv list\tList all services\n\trossrv md5\tDisplay service md5sum\n\trossrv package\tList services in a package\n\trossrv packages\tList packages that contain services\n\nType rossrv &lt;command&gt; -h for more detailed usage<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Verwandte Beitr\u00e4ge<\/h1>\n\n\n<div class=\"display-posts-listing\">\n<div class=\"listing-item\"><a class=\"image\" href=\"https:\/\/www.fambach.net\/en\/ros-einfachen-publisher-subscriber-in-c-erstellen\/\"><img loading=\"lazy\" decoding=\"async\" width=\"200\" height=\"58\" src=\"https:\/\/www.fambach.net\/wp-content\/uploads\/ROS_logo_black2.jpg\" class=\"attachment-thumbnail size-thumbnail wp-post-image\" alt=\"\" \/><\/a> <a class=\"title\" href=\"https:\/\/www.fambach.net\/en\/ros-einfachen-publisher-subscriber-in-c-erstellen\/\">ROS: Einfachen Publisher\/Subscriber in C++ Erstellen<\/a><\/div>\n<div class=\"listing-item\"><a class=\"image\" href=\"https:\/\/www.fambach.net\/en\/ros-service-erstellen\/\"><img loading=\"lazy\" decoding=\"async\" width=\"200\" height=\"58\" src=\"https:\/\/www.fambach.net\/wp-content\/uploads\/ROS_logo_black2.jpg\" class=\"attachment-thumbnail size-thumbnail wp-post-image\" alt=\"\" \/><\/a> <a class=\"title\" href=\"https:\/\/www.fambach.net\/en\/ros-service-erstellen\/\">ROS: Service Erstellen<\/a><\/div>\n<div class=\"listing-item\"><a class=\"image\" href=\"https:\/\/www.fambach.net\/en\/ros-nachrichten-erstellen\/\"><img loading=\"lazy\" decoding=\"async\" width=\"200\" height=\"58\" src=\"https:\/\/www.fambach.net\/wp-content\/uploads\/ROS_logo_black2.jpg\" class=\"attachment-thumbnail size-thumbnail wp-post-image\" alt=\"\" \/><\/a> <a class=\"title\" href=\"https:\/\/www.fambach.net\/en\/ros-nachrichten-erstellen\/\">ROS: Nachrichten Erstellen<\/a><\/div>\n<div class=\"listing-item\"><a class=\"image\" href=\"https:\/\/www.fambach.net\/en\/ros-unified-robot-description-format-urdf\/\"><img loading=\"lazy\" decoding=\"async\" width=\"200\" height=\"58\" src=\"https:\/\/www.fambach.net\/wp-content\/uploads\/Screenshot-from-2023-03-26-15-42-59.jpg\" class=\"attachment-thumbnail size-thumbnail wp-post-image\" alt=\"\" \/><\/a> <a class=\"title\" href=\"https:\/\/www.fambach.net\/en\/ros-unified-robot-description-format-urdf\/\">ROS URDF: Projekt aufsetzen<\/a><\/div>\n<div class=\"listing-item\"><a class=\"image\" href=\"https:\/\/www.fambach.net\/en\/ros-catkin\/\"><img loading=\"lazy\" decoding=\"async\" width=\"200\" height=\"58\" src=\"https:\/\/www.fambach.net\/wp-content\/uploads\/ROS_logo_black2.jpg\" class=\"attachment-thumbnail size-thumbnail wp-post-image\" alt=\"\" \/><\/a> <a class=\"title\" href=\"https:\/\/www.fambach.net\/en\/ros-catkin\/\">ROS: Catkin<\/a><\/div>\n<\/div>\n\n\n\n<h1 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Quellen\"><\/span>Quellen<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p>http:\/\/wiki.ros.org\/melodic\/Installation\/Ubuntu<\/p>\n\n\n\n<p>http:\/\/wiki.ros.org\/ROS\/Tutorials\/CreatingMsgAndSrv<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-1 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Jeder Knoten kann Services anbieten. Ein Service zeichnet sich durch eine Anfrage gefolgt von einer Anwort aus. Services werden verwendet um bestimmt funktionen in Knoten aufzurufen. Zum Beispiel um einen Zustand abzufragen oder um einen Reset durchzuf\u00fchren. Die initative geht hierbei vom Anfragenden aus. Dies unterscheidet sich von den Nachrichten (Messages) die abboniert werden. Wie &#8230;<\/p>\n<a href=\"https:\/\/www.fambach.net\/en\/ros-catkin\/\" class=\"read-more-link\">Continue reading <span class=\"screen-reader-text\">\"ROS: Catkin\"<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a>","protected":false},"author":1,"featured_media":9227,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"no","_lmt_disable":"","footnotes":""},"categories":[157,254,699],"tags":[678],"modified_by":"sfambach","_links":{"self":[{"href":"https:\/\/www.fambach.net\/en\/wp-json\/wp\/v2\/posts\/9330"}],"collection":[{"href":"https:\/\/www.fambach.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fambach.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fambach.net\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fambach.net\/en\/wp-json\/wp\/v2\/comments?post=9330"}],"version-history":[{"count":11,"href":"https:\/\/www.fambach.net\/en\/wp-json\/wp\/v2\/posts\/9330\/revisions"}],"predecessor-version":[{"id":9376,"href":"https:\/\/www.fambach.net\/en\/wp-json\/wp\/v2\/posts\/9330\/revisions\/9376"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.fambach.net\/en\/wp-json\/wp\/v2\/media\/9227"}],"wp:attachment":[{"href":"https:\/\/www.fambach.net\/en\/wp-json\/wp\/v2\/media?parent=9330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fambach.net\/en\/wp-json\/wp\/v2\/categories?post=9330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fambach.net\/en\/wp-json\/wp\/v2\/tags?post=9330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}