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

Revision history [back]

click to hide/show revision 1
initial version

in the onCreate

final Handler handler=new Handler();
final Runnable r = new Runnable() {
        @Override
        public void run() {

            var        =listener.listenervar;
            dosomething(var);

            handler.postDelayed(this,10);

        }
    };

    Thread t = new Thread(){
        @Override
        public void run() {
            try{
                while(true){
                    sleep(1000);
                    handler.post(r);
                }
            }
            catch(InterruptedException e){e.printStackTrace();}
        }
    };
    t.start();

in the listener.java:

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;

public class Listener extends AbstractNodeMain {




public int listenervar=0;


//also possible
public int getlistenervar(){
return this.listenervar;
}


  @Override
  public GraphName getDefaultNodeName() {
    return GraphName.of("HAF_APP/listener_name");
  }



  @Override
  public void onStart(ConnectedNode connectedNode) {

    final Log log = connectedNode.getLog();
    Subscriber<std_msgs.Int32> subscriber = connectedNode.newSubscriber("topic", std_msgs.Int32._TYPE);



    subscriber.addMessageListener(new MessageListener<std_msgs.Int32>() {
      @Override
      public void onNewMessage(std_msgs.Int32 message) {
    listenervar=message.getData();

      }
    });
  }
}