0
$\begingroup$

Rosanswers logo

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;

import java.io.IOException;

public class Listener extends AbstractNodeMain {

@Override

public GraphName getDefaultNodeName() {

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

}

@Override

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>() {
  @Override
  public void onNewMessage(std_msgs.String message) {
  java.lang.String da = daClassifer.classify(message.getData());
  
  log.info("Classifier's input : \"" + message.getData() + "\"");
  log.info("Classifier's output: \"" + da + "\"");

    }

});

} catch (IOException e) {

    }

} }


Originally posted by uzair on ROS Answers with karma: 87 on 2014-01-29

Post score: 0


Original comments

Comment by uzair on 2014-01-29:
i dont know why these </std_msgs.string></std_msgs.string> tags appear. They are not there in the code.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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  log.info("Classifier's output: \"" + da + "\"");
    std_msgs.String str = publisher.newMessage();
    str.setData(da);
    publisher.publish(str);

Originally posted by uzair with karma: 87 on 2014-01-31

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.