SlaveApiTestNode.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros;
00018 
00019 import org.ros.concurrent.CancellableLoop;
00020 import org.ros.message.MessageFactory;
00021 import org.ros.message.MessageListener;
00022 import org.ros.namespace.GraphName;
00023 import org.ros.node.AbstractNodeMain;
00024 import org.ros.node.ConnectedNode;
00025 import org.ros.node.topic.Publisher;
00026 import org.ros.node.topic.Subscriber;
00027 
00033 public class SlaveApiTestNode extends AbstractNodeMain {
00034 
00035   @Override
00036   public GraphName getDefaultNodeName() {
00037     return GraphName.of("rosjava/slave_api_test_node");
00038   }
00039 
00040   @Override
00041   public void onStart(final ConnectedNode connectedNode) {
00042     // Basic chatter in/out test.
00043     final Publisher<std_msgs.String> pub_string =
00044         connectedNode.newPublisher("chatter_out", std_msgs.String._TYPE);
00045     MessageListener<std_msgs.String> chatter_cb = new MessageListener<std_msgs.String>() {
00046       @Override
00047       public void onNewMessage(std_msgs.String m) {
00048         System.out.println("String: " + m.getData());
00049       }
00050     };
00051 
00052     Subscriber<std_msgs.String> stringSubscriber =
00053         connectedNode.newSubscriber("chatter_in", std_msgs.String._TYPE);
00054     stringSubscriber.addMessageListener(chatter_cb);
00055 
00056     // Have at least one case of dual pub/sub on the same topic.
00057     final Publisher<std_msgs.Int64> pub_int64_pubsub =
00058         connectedNode.newPublisher("int64", std_msgs.Int64._TYPE);
00059     MessageListener<std_msgs.Int64> int64_cb = new MessageListener<std_msgs.Int64>() {
00060       @Override
00061       public void onNewMessage(std_msgs.Int64 m) {
00062       }
00063     };
00064 
00065     Subscriber<std_msgs.Int64> int64Subscriber =
00066         connectedNode.newSubscriber("int64", "std_msgs/std_msgs.Int64");
00067     int64Subscriber.addMessageListener(int64_cb);
00068 
00069     // Don't do any performance optimizations here. We want to make sure that
00070     // GC, etc. is working.
00071     connectedNode.executeCancellableLoop(new CancellableLoop() {
00072       @Override
00073       protected void loop() throws InterruptedException {
00074         MessageFactory topicMessageFactory = connectedNode.getTopicMessageFactory();
00075         std_msgs.String chatter = topicMessageFactory.newFromType(std_msgs.String._TYPE);
00076         chatter.setData("hello " + System.currentTimeMillis());
00077         pub_string.publish(chatter);
00078 
00079         std_msgs.Int64 num = topicMessageFactory.newFromType(std_msgs.Int64._TYPE);
00080         num.setData(1);
00081         pub_int64_pubsub.publish(num);
00082         Thread.sleep(100);
00083       }
00084     });
00085   }
00086 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49