ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

subscribing and publishing at the same time in rosjava

asked 2014-01-29 06:49:42 -0500

uzair gravatar image

updated 2014-01-30 21:37:28 -0500

I hava a subscriber which accepts a string from the pocketsphinx node. I added external jars to my subscriber and I am processing that string. I now need to send the processed string to another node from this node. So I want to subscribe to a string, process it and then publish it. How do I do this in rosjava? I tried to add the import statements for the Cancellable loop and Publisher from the publisher code and also tried to squeeze in the connectedNode.executeCancellableLoop(new CancellableLoop() { }) part of the publisher code. I dont get any build errors but I also dont see the message being published. This is my modified code for the subscriber with the external jars. Where and how do i add the publisher to this node?

package org.ros.rosjava_tutorial_pubsub;

import org.apache.commons.logging.Log;

import org.ros.message.MessageListener;

import org.ros.namespace.GraphName;

import org.ros.node.AbstractNodeMain;

import org.ros.node.ConnectedNode;

import org.ros.node.NodeMain;

import org.ros.node.topic.Subscriber;

import edu.uic.cs.nlp.ros.da.DaClassifier;


public class Listener extends AbstractNodeMain {


public GraphName getDefaultNodeName() {

return GraphName.of("rosjava_tutorial_pubsub/listener");



public void onStart(ConnectedNode connectedNode) {

final Log log = connectedNode.getLog();

try { final edu.uic.cs.nlp.ros.da.DaClassifier daClassifer =DaClassifier.getInstance();

Subscriber<std_msgs.string> subscriber =        
connectedNode.newSubscriber("recognizer/output", std_msgs.String._TYPE);
subscriber.addMessageListener(new MessageListener<std_msgs.string>() {
  public void onNewMessage(std_msgs.String message) {
  java.lang.String da = daClassifer.classify(message.getData());"Classifier's input : \"" + message.getData() + "\"");"Classifier's output: \"" + da + "\"");



} catch (IOException e) {


} }</std_msgs.string></std_msgs.string>

edit retag flag offensive close merge delete


i dont know why these </std_msgs.string></std_msgs.string> tags appear. They are not there in the code.

uzair gravatar image uzair  ( 2014-01-29 07:06:00 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2014-01-30 23:56:08 -0500

uzair gravatar image

updated 2014-01-31 00:41:32 -0500

Solved it. Just added these lines

   //import statement for the publisher
   import org.ros.node.topic.Publisher;

    final Publisher<std_msgs.string> publisher =
    connectedNode.newPublisher("chatter", std_msgs.String._TYPE);//added this line above the try block
    //added the following lines of code below"Classifier's output: \"" + da + "\"");
    std_msgs.String str = publisher.newMessage();


edit flag offensive delete link more

Question Tools

1 follower


Asked: 2014-01-29 06:49:42 -0500

Seen: 930 times

Last updated: Jan 31 '14