2012年1月15日日曜日

rosjavaのチュートリアル(Publisher)

(この記事は2011/01/19現在のものです。)


2012/07/12の記事はこちら
http://ros-robot.blogspot.jp/2012/07/rosjavatalker.html

以下は古い内容ですので上記記事を参考にしてください。


rosjavaの概要をまとめたページを見つけました。

http://ros.org/wiki/rosjava/Overview

まだ書かれていない項目も多いですが、これは必読です。
javaで書かれたroscoreがあることなどが書いて有りますね。

でもとりあえずこれを無視して、rosjava_tutorial_pubsubパッケージを見ていこうと思います。

今回はとりあえずTalker(Publisherのサンプル)を見てみます。

  1. /*                
  2.  * Copyright (C) 2011 Google Inc.   
  3.  *  
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not                                                                            
  5.  * use this file except in compliance with the License. You may obtain a copy of                                                                          
  6.  * the License at      
  7.  *         
  8.  * http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
  12.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the   
  13.  * License for the specific language governing permissions and limitations under                                                                          
  14.  * the License.     
  15.  */  
  16.   
  17. import org.ros.concurrent.CancellableLoop;  
  18. import org.ros.namespace.GraphName;  
  19. import org.ros.node.Node;  
  20. import org.ros.node.NodeMain;  
  21. import org.ros.node.topic.Publisher;  
  22.   
  23. /** 
  24.  * A simple {@link Publisher} {@link Node}. 
  25.  *  
  26.  * @author damonkohler@google.com (Damon Kohler) 
  27.  */  
  28. public class Talker implements NodeMain {  
  29.   
  30.   @Override  
  31.   public GraphName getDefaultNodeName() {  
  32.     return new GraphName("rosjava_tutorial_pubsub/talker");  
  33.   }  
  34.   
  35.   @Override  
  36.   public void onStart(final Node node) {  
  37.     final Publisher<org.ros.message.std_msgs.String> publisher =  
  38.         node.newPublisher("chatter""std_msgs/String");  
  39.     // This CancellableLoop will be canceled automatically when the Node shuts  
  40.     // down.  
  41.     node.executeCancellableLoop(new CancellableLoop() {  
  42.       private int sequenceNumber;  
  43.   
  44.       @Override  
  45.       protected void setup() {  
  46.         sequenceNumber = 0;  
  47.       }  
  48.   
  49.       @Override  
  50.       protected void loop() throws InterruptedException {  
  51.         org.ros.message.std_msgs.String str = new org.ros.message.std_msgs.String();  
  52.         str.data = "Hello world! " + sequenceNumber;  
  53.         publisher.publish(str);  
  54.         sequenceNumber++;  
  55.         Thread.sleep(1000);  
  56.       }  
  57.     });  
  58.   }  
  59.   
  60.   @Override  
  61.   public void onShutdown(Node node) {  
  62.   }  
  63.   
  64.   @Override  
  65.   public void onShutdownComplete(Node node) {  
  66.   }  
  67. }  

NodeMainというインタフェースを実装しています。NodeMainは

  1. public interface NodeMain extends NodeListener {  
  2. /** 
  3.    * @return the name of the {@link Node} that will be used if a name was not 
  4.    *         specified in the {@link Node}'s associated 
  5.    *         {@link NodeConfiguration} 
  6.    */  
  7.   GraphName getDefaultNodeName();  
  8. }  

となっており、NodeListenerは以下のようになっています。

  1. public interface NodeListener {  
  2.   void onStart(Node node);  
  3.   void onShutdown(Node node);  
  4.   void onShutdownComplete(Node node);  
  5. }  
ということで、Nodeスタート時にonStartが呼ばれ、終了開始時にonShutdownが呼ばれ、終了後にonShutdownCompleteが呼ばれるようです。

まず、getDefaultNodeNameでノードのデフォルトの名前を決めています。

次に、メインとなるのはonStartです。
Publisherの作り方は↓のように書くみたいですね。

Publisher<org.ros.message.std_msgs.String> publisher = node.newPublisher("chatter", "std_msgs/String");

C++とかなり似てますね。

そしてメインループはnode.executeCancellableLoopで作っています。
C-cで止められる仕組みだと思います。
setupが1回実行され、loopが回るみたいですね。そしてC-cで止まると。

Talkerはこれでおしまい。次回はListenerです。

0 件のコメント:

コメントを投稿