(訳注:最新の情報は[[ROS/Tutorials/CreatingMsgAndSrv|原文]]を参照してください.) #################################### ##FILL ME IN #################################### ## links to any required tutorials ## note.0= [[ja/ROS/Tutorials/UsingRosEd|rosedを使ってファイルを編集する]] ## descriptive title for the tutorial ## title = ROSのmsgとsrvを作る ## multi-line description to be displayed in search ## description = このチュートリアルでは、どのようにmsgやsrvファイルを作りビルドするかを[[rosmsg]], rossrv や roscpなどのコマンドの使い方とともに学びます ## the next tutorial description (optional) ## next = シンプルなパブリッシャとサブスクライバを書く ## links to next tutorial (optional) ## next.0.link= [[ja/ROS/Tutorials/WritingPublisherSubscriber(python)|(python)]] ## next.1.link= [[ja/ROS/Tutorials/WritingPublisherSubscriber(c++)|(c++)]] ## what level user is this tutorial for ## level= BeginnerCategory #################################### <> <> <> == ROS メッセージとサービスの作成 == === メッセージとサービスの概要 === * [[ja/msg|msg]]: メッセージ(msg)ファイルは ROS メッセージの型を説明する単純なテキストファイルです.これはメッセージのソース コードを異なる言語で作成するのに使います. * [[ja/srv|srv]]: サービス(srv)ファイルはサービスを記述します.これはリクエストとレスポンスという2つの部分で構成されます. メッセージ ファイルはパッケージの `msg`ディレクトリに保管されており、サービス ファイルは `srv` ディレクトリに保管されています. メッセージは一行ごとに型と名前が記載されている,ただの単純なテキスト ファイルです.型には以下のものが使用できます: * int8, int16, int32, int64 (plus uint*) * float32, float64 * string * time, duration * other msg files * variable-length array[] and fixed-length array[C] ROS 固有の型もあります.`Header` はタイムスタンプと ROS でよく使用される座標フレームの情報が含まれています.メッセージ ファイルの最初の行には `Header header` と書かれています. 以下は Header と文字列型と他の2つのメッセージを用いたメッセージの例です: {{{ Header header string child_frame_id geometry_msgs/PoseWithCovariance pose geometry_msgs/TwistWithCovariance twist }}} サービス ファイルはリクエストとレスポンスの部分があることを除けば,メッセージ ファイルによく似ています.2つの部分は '---' の行で区切られています.以下がサービス ファイルの例です. {{{ int64 A int64 B --- int64 Sum }}} 上の例では {{{A}}} と {{{B}}} はリクエストで,{{{Sum}}} がレスポンスです. == メッセージ(msg)を使う == === メッセージの作成 === 先ほどのチュートリアルで作成したパッケージに,新しいメッセージを定義してみましょう. {{{{#!wiki buildsystem catkin {{{ $ cd ~/catkin_ws/src/beginner_tutorials $ mkdir msg $ echo "int64 num" > msg/Num.msg }}} 上の例は内容が1行だけの最もシンプルな.msgファイルです。もちろん、以下のような複数の要素を含んだより複雑なmsgファイルも作成できます: {{{ string first_name string last_name uint8 age uint32 score }}} }}}} ##There's one more step, though. We need to make sure that the msg files are turned into source code for C++, Python, and other languages: ##Open `package.xml`, and make sure these two lines are in it and [[http://www.htmlhelp.com/reference/wilbur/misc/comment.html|uncommented]]: もう1つステップがありますが,ここでメッセージ ファイルが C++ や Python や他の言語のソースコードへ変換できることを確認しましょう: `package.xml`を開き、 package.xmlの中に以下の2行があることを確認して、[[http://www.htmlhelp.com/reference/wilbur/misc/comment.html|コメントタグを外して]]ください: {{{ message_generation message_runtime }}} ##Note that at build time, we need "message_generation", while at runtime, we only need "message_runtime". メッセージビルド時には"message_generation"が有効になっていることが必要で、メッセージ実行時には、"message_runtime"の有効化のみ必要です。 ##Open `CMakeLists.txt` in your favorite text editor ([[ROS/Tutorials/UsingRosEd|rosed]] from the previous tutorial is a good option). エディタで`CMakeLists.txt`を開きましょう。(前項のチュートリアルで出てきた[[ja/ROS/Tutorials/UsingRosEd|rosed]] が使えますね) ##Add the `message_generation` dependency to the `find_package` call which already exists in your `CMakeLists.txt` so that you can generate messages. You can do this by simply adding `message_generation` to the list of `COMPONENTS` such that it looks like this: `CMakeLists.txt`の中にすでにある`find_package`の呼び出しに、`message_generation`の依存項目を付け加えると、メッセージを生成できます。 これは以下にあるような`COMPONENTS`のリストに、`message_generation`を追記するだけですみます。 ## Do not just add this to your CMakeLists.txt, modify the existing text to add message_generation before the closing parenthesis {{{ # 自分のCMakeLists.txtにこれ全体を単に追加しないこと。既にあるfind_packageの項を探し、終わりの括弧の前にmessage_generationを追加すること。 find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs message_generation ) }}} ##You may notice that sometimes your project builds fine even if you did not call `find_package` with all dependencies. This is because catkin combines all your projects into one, so if an earlier project calls `find_package`, yours is configured with the same values. But forgetting the call means your project can easily break when built in isolation. すべての依存を`find_package`で呼び出さなくても、しばしば自分のプロジェクトがきちんとビルドできていることに気づくかもしれません。これはcatkinがすべてのプロジェクトを一つにまとめているからで、元のプロジェクトが`find_package`を呼び出しているなら、自分のプロジェクトはそれと同じ値で構成されます。しかし、`find_package`の呼び出しを忘れると、自分のプロジェクトの単独ビルド時に、プロジェクトが壊れかねません。 ##Also make sure you export the message runtime dependency. 同様にメッセージの実行時依存も有効になっていることを確認しましょう。 {{{ catkin_package( ... CATKIN_DEPENDS message_runtime ... ...) }}} ##Find the following block of code: CMakeLists.txt内のコードにある以下のブロックを見つけてください: {{{ # add_message_files( # FILES # Message1.msg # Message2.msg # ) }}} ## Uncomment it by removing the `#` symbols and then replace the stand in `Message*.msg` files with your `.msg` file, such that it looks like this: 行頭の`#`記号を外してコメント化を解除し、`Message*.msg`と書かれている部分を自分の`.msg`ファイルに置き換えてください。以下のようになります: {{{ add_message_files( FILES Num.msg ) }}} ##By adding the .msg files manually, we make sure that CMake knows when it has to reconfigure the project after you add other .msg files. 他の.msgファイルを追加した後にプロジェクトを再構成しなければならないことを、手動で.msgファイルをadd_message_filesに追加することで、必ずCMakeに認識させましょう。 ##Now we must ensure the `generate_messages()` function is called. 続いて、メッセージ生成`generate_messages()`の呼び出しについても、有効にしておかなければいけません。 ##''For ROS Hydro and later,'' you need to uncomment these lines: ROS Hydro以降のバージョンでは、以下のブロックのコメント化を解除が必要で、: {{{ # generate_messages( # DEPENDENCIES # std_msgs # ) }}} ## so it looks like: . `#`記号を外すと、このようになります。 {{{ generate_messages( DEPENDENCIES std_msgs ) }}} ##''In earlier versions,'' you may just need to uncomment one line: これ以前のバージョンでは、1行をコメント解除するだけです: {{{ generate_messages() }}} {{{{#!wiki buildsystem rosbuild {{{ $ roscd beginner_tutorials $ mkdir msg $ echo "int64 num" > msg/Num.msg }}} 上の例は内容が1行だけの最もシンプルな.msgファイルです。もちろん、以下のような複数の要素を含んだより複雑なmsgファイルも作成できます: {{{ string first_name string last_name uint8 age uint32 score }}} ##There's one more step, though. We need to make sure that the msg files are turned into source code for C++, Python, and other languages. Open `CMakeLists.txt` in your favorite text editor ([[ROS/Tutorials/UsingRosEd|rosed]] from the previous tutorial is a good option) and remove `#` to uncomment the following line: もう1つステップがありますが,ここでメッセージ ファイルが C++ や Python や他の言語のソースコードへ変換できることを確認しましょう。`CMakeLists.txt`を好みのエディタで開き、 CMakeLists.txtを好みのエディタで開きましょう(前項のチュートリアルで出てきた[[ja/ROS/Tutorials/UsingRosEd|rosed]]が使えますね)そして以下の行の`#` のコメントを外してください: {{{ # rosbuild_genmsg() }}} }}}} {{{#!wiki buildsystem catkin ##Now you're ready to generate source files from your msg definition. If you want to do so right now, skip next sections to [[ROS/Tutorials/CreatingMsgAndSrv#Common_step_for_msg_and_srv|Common step for msg and srv]]. これで自分のメッセージ定義からソースファイルを生成する準備ができました。もし今すぐ試したければ、次の項をスキップして、msgとsrvの共通手順の項([[ja/ROS/Tutorials/CreatingMsgAndSrv#msgとsrvの共通手順|msgとsrvの共通手順]])でメッセージのソースファイルを生成できます。 }}} === rosmsg を使う === ##That's all you need to do to create a msg. Let's make sure that ROS can see it using the `rosmsg show` command. 前項でmsgを作るのに必要な作業がわかりました。では`rosmsg show`コマンドを使って、ROSでメッセージを確認していきましょう. 使い方: {{{ $ rosmsg show [message type] }}} 例: {{{ $ rosmsg show beginner_tutorials/Num }}} このように表示されるはずです: . {{{ int64 num }}} 先ほどの例で,メッセージには2つの部分が含まれていました: * {{{beginner_tutorials}}} -- メッセージが定義されているパッケージ * {{{Num}}} -- {{{Num}}} メッセージの名前 どのパッケージにメッセージが入っているのか覚えていなければ,パッケージ名を省略できます.やってみましょう: {{{ $ rosmsg show Num }}} このように表示されるはずです: . {{{ [beginner_tutorials/Num]: int64 num }}} == サービス(srv)を使う == === サービスの作成 === ##Let's use the package we just created to create a srv: 作成したパッケージを使用してサービスを作成しましょう: {{{ $ roscd beginner_tutorials $ mkdir srv }}} ##Instead of creating a new srv definition by hand, we will copy an existing one from another package. 手作業で新しくサービスを定義するのではなく,別のパッケージをコピーします. ##For that, `roscp` is a useful commandline tool for copying files from one package to another. `roscp`は 1つのパッケージから他のパッケージへファイルをコピーするのに便利なコマンドライン-ツールです. 使い方: {{{ $ roscp [package_name] [file_to_copy_path] [copy_path] }}} [[rospy_tutorials]]パッケージからサービスをコピーできるようになりました: {{{ $ roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv }}} もう1つステップがありますが,ここでサービス ファイルが C++ や Python や他の言語のソースコードへ変換できることを確認しましょう: {{{{#!wiki buildsystem catkin ##Unless you have done so already for messages earlier, add the `message_generation` dependency to generate messages in `CMakeLists.txt`: まだmsgの項で処理を終えていないのであれば、メッセージ生成のために、`CMakeLists.txt`に`message_generation`の依存を追加してください: {{{ #以下の行を自分のCMakeLists.txtに直接追加せずに、既存の該当行を編集すること find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs message_generation) }}} ##(Despite its name, `message_generation` works for both `msg` and `srv`.) (`message_generation`という名称になっていますが、`msg` と `srv`の両方の生成に働きかけます.) ##Also you need the same changes to package.xml for services as for messages, so look above for the additional dependencies required. 前項のmsgの作成で見たように、srvについてもCMakeLists.txtのサービスのメッセージのブロックに依存解決の記述を追記します。 ##Remove `#` to uncomment the following lines: 以下のブロックのコメント記号`#`を外し: {{{ # add_service_files( # FILES # Service1.srv # Service2.srv # ) }}} ##And replace the placeholder `Service*.srv` files for your service files: そして`Service*.srv`と書かれている部分を自分のサービスファイルに置き換えます。 {{{ add_service_files( FILES AddTwoInts.srv ) }}} }}}} {{{{#!wiki buildsystem rosbuild ##Once again, open CMakeLists.txt and remove `#` to uncomment the following line: もう一度、`CMakeLists.txt` をエディタで開いてください.そして以下の行の `#` のコメントを外してください: {{{ # rosbuild_gensrv() }}} }}}} {{{#!wiki buildsystem catkin ##Now you're ready to generate source files from your service definition. If you want to do so right now, skip next sections to [[ROS/Tutorials/CreatingMsgAndSrv#Common_step_for_msg_and_srv|Common step for msg and srv]]. これで自分のサービス定義からソースファイルを生成する準備ができました。もし今すぐ試したければ、次の項をスキップして、msgとsrvの共通手順の項([[ja/ROS/Tutorials/CreatingMsgAndSrv#msgとsrvの共通手順|msgとsrvの共通手順]])でサービスのソースファイルを生成できます。 }}} === rossrvを使う === ##That's all you need to do to create a srv. Let's make sure that ROS can see it using the `rossrv show` command. サービスを作成するためのすべての準備が整ったところで、`rossrv show` コマンドを使ってサービスの定義を表示して、内容を確認してみましょう。 使い方: {{{ $ rossrv show }}} 例: {{{ $ rossrv show beginner_tutorials/AddTwoInts }}} ##You will see: このように表示されます: . {{{ int64 a int64 b --- int64 sum }}} ##Similar to `rosmsg`, you can find service files like this without specifying package name: `rosmsg`と同様、パッケージ名を特定しなくても、以下のようにサービスファイルを見つけることが出来ます。 {{{ $ rossrv show AddTwoInts [beginner_tutorials/AddTwoInts]: int64 a int64 b --- int64 sum [rospy_tutorials/AddTwoInts]: int64 a int64 b --- int64 sum }}} ##== Common step for msg and srv == == msgとsrvの共通手順 == {{{{#!wiki buildsystem catkin ##Next find this section in `CMakeLists.txt`: では、`CMakeLists.txt`の中で次のセクションを見つけて下さい: {{{ # generate_messages( # DEPENDENCIES # # std_msgs # Or other packages containing msgs # ) }}} ## Uncomment it and add any packages you depend on which contain `.msg` files that your messages use (in this case `std_msgs`), such that it looks like this: このセクションのコメントを外し、以下のように、自分のメッセージが使う`.msg`ファイルを含む依存パッケージ(この場合は`std_msgs`)を追記します: {{{ generate_messages( DEPENDENCIES std_msgs ) }}} ##Now that we have made some new messages we need to make our package again: これでいくつか新しいメッセージを作成したので、自分のパッケージを再度makeする必要があります: {{{ # catkinワークスペースの中で $ cd ../.. $ catkin_make $ cd - }}} }}}} {{{{#!wiki buildsystem rosbuild ##Now that we have made some new messages we need to make our package again: 新しくメッセージを作成したので,もう一度パッケージをmakeする必要があります: {{{ $ rosmake beginner_tutorials }}} }}}} ##Any .msg file in the msg directory will generate code for use in all supported languages. The C++ message header file will be generated in `~/catkin_ws/devel/include/beginner_tutorials/`. The Python script will be created in `~/catkin_ws/devel/lib/python2.7/dist-packages/beginner_tutorials/msg`. The lisp file appears in `~/catkin_ws/devel/share/common-lisp/ros/beginner_tutorials/msg/`. ##The full specification for the message format is available at the [[ROS/Message_Description_Language|Message Description Language]] page. メッセージ形式のための完全な仕様書は[[ROS/Message_Description_Language|Message Description Language]]のページを利用できます. == コマンドラインでのHelpの取得 == 私たちはまだほんの少しの ROS ツールについてしか見てきていません.それぞれのコマンドが要求する引数を覚えておくことは難しいかもしれません.幸運なことに,ほとんどの ROS ツールは各々がヘルプを提供しています. やってみましょう: {{{ $ rosmsg -h }}} . 違う {{{rosmsg}}} サブコマンドの種類を見ることができます. {{{ Commands: rosmsg show Show message description rosmsg users Find files that use message rosmsg md5 Display message md5sum rosmsg package List messages in a package rosmsg packages List packages that contain messages }}} サブコマンドについてのヘルプも見ることができます. {{{ $ rosmsg show -h }}} . 以下は `rosmsg show` で必要な引数を表示しています: {{{ Usage: rosmsg show [options] Options: -h, --help show this help message and exit -r, --raw show raw message text, including comments }}} == 復習 == これまで使ってきたコマンドを並べてみましょう: {{{#!wiki buildsystem catkin * rospack = ros+pack(age) : ROS パッケージに関連した情報を提供します * roscd = ros+cd : ROS パッケージがスタックのディレクトリへ移動(cd=changes directory)します * rosls = ros+ls : ROS パッケージ内のファイルを表示(ls=list segments)します * roscp = ros+cp : ROS パッケージから、またはパッケージへファイルをコピー(cp=copy)します * rosmsg = ros+msg : ROS メッセージ定義に関連した情報を提供します * rossrv = ros+srv : ROS サービス定義に関連した情報を提供します * catkin_make = catkin+make : ROSパッケージを作成(コンパイル)します }}} {{{#!wiki buildsystem rosbuild * rospack = ros+pack(age) : ROS パッケージに関連した情報を提供します * rosstack = ros+stack : ROS スタックに関連した情報を提供します * roscd = ros+cd : ROS パッケージがスタックのディレクトリへ移動します * rosls = ros+ls : ROS パッケージ内のファイルを表示します * roscp = ros+cp : ROS パッケージから/へファイルをコピーします * rosmsg = ros+msg : ROS メッセージ定義に関連した情報を提供します * rossrv = ros+srv : ROS サービス定義に関連した情報を提供します * rosmake = ros+make : ROSパッケージを作成(コンパイル)します }}} == 次のチュートリアル == ここでは ROS メッセージとサービスを作成しました.次は[[ja/ROS/Tutorials/WritingPublisherSubscriber(python)|(python)]] [[ja/ROS/Tutorials/WritingPublisherSubscriber(c++)|(c++)]]を見てみましょう. ## AUTOGENERATED DO NOT DELETE ## TutorialCategory ## ROSTutorialCategory