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のサンプル)を見てみます。
/*
* Copyright (C) 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
import org.ros.concurrent.CancellableLoop;
import org.ros.namespace.GraphName;
import org.ros.node.Node;
import org.ros.node.NodeMain;
import org.ros.node.topic.Publisher;
/**
* A simple {@link Publisher} {@link Node}.
*
* @author damonkohler@google.com (Damon Kohler)
*/
public class Talker implements NodeMain {
@Override
public GraphName getDefaultNodeName() {
return new GraphName("rosjava_tutorial_pubsub/talker");
}
@Override
public void onStart(final Node node) {
final Publisher<org.ros.message.std_msgs.String> publisher =
node.newPublisher("chatter", "std_msgs/String");
// This CancellableLoop will be canceled automatically when the Node shuts
// down.
node.executeCancellableLoop(new CancellableLoop() {
private int sequenceNumber;
@Override
protected void setup() {
sequenceNumber = 0;
}
@Override
protected void loop() throws InterruptedException {
org.ros.message.std_msgs.String str = new org.ros.message.std_msgs.String();
str.data = "Hello world! " + sequenceNumber;
publisher.publish(str);
sequenceNumber++;
Thread.sleep(1000);
}
});
}
@Override
public void onShutdown(Node node) {
}
@Override
public void onShutdownComplete(Node node) {
}
}
NodeMainというインタフェースを実装しています。NodeMainは
public interface NodeMain extends NodeListener {
/**
* @return the name of the {@link Node} that will be used if a name was not
* specified in the {@link Node}'s associated
* {@link NodeConfiguration}
*/
GraphName getDefaultNodeName();
}
となっており、NodeListenerは以下のようになっています。
public interface NodeListener {
void onStart(Node node);
void onShutdown(Node node);
void onShutdownComplete(Node node);
}
ということで、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 件のコメント:
コメントを投稿