Error run simple publisher by main function

asked 2014-04-14 00:38:38 -0500

pico gravatar image

updated 2016-10-24 09:07:12 -0500

ngrennan gravatar image

Hello, I'm trying to set up a simple publisher using a main function in rosjava. This is the code of my main:

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import org.ros.exception.RosRuntimeException;
import org.ros.internal.loader.CommandLineLoader;
import org.ros.node.DefaultNodeMainExecutor;
import org.ros.node.NodeConfiguration;
import org.ros.node.NodeMain;
import org.ros.node.NodeMainExecutor;
import java.net.URI;

public class RosjavaPubSub {
   public static void main(String[] args) {
   Talker pubNodeMain = new Talker();

  // Set up the executor for both of the nodes
  NodeMainExecutor nodeMainExecutor = DefaultNodeMainExecutor.newDefault();

   // Load the publisher(talker)
   NodeConfiguration pubNodeConfiguration = NodeConfiguration.newPrivate();
  //Check if Talker class correctly instantiated
  Preconditions.checkState(pubNodeMain != null);
  //execute the nodelet talker (this will run the method onStart of Talker.java)
  nodeMainExecutor.execute(pubNodeMain, pubNodeConfiguration);
  }
}

And this is the simple publisher:

import org.ros.concurrent.CancellableLoop;
import org.ros.namespace.GraphName;
import org.ros.node.AbstractNodeMain;
import org.ros.node.ConnectedNode;
import org.ros.node.topic.Publisher;

public class Talker extends AbstractNodeMain {

    @Override
      public GraphName getDefaultNodeName() {
        return GraphName.of("rosjava/talker");
      }

      @Override
      public void onStart(final ConnectedNode connectedNode) {
        final Publisher<std_msgs.String> publisher =
            connectedNode.newPublisher("chatter", std_msgs.String._TYPE);
        // This CancellableLoop will be canceled automatically when the node shuts
        // down.
        connectedNode.executeCancellableLoop(new CancellableLoop() {
          private int sequenceNumber;

          @Override
          protected void setup() {
            sequenceNumber = 0;
          }

          @Override
          protected void loop() throws InterruptedException {
            std_msgs.String str = publisher.newMessage();
            str.setData("Hello world! " + sequenceNumber);
            publisher.publish(str);
            sequenceNumber++;
            Thread.sleep(1000);
          }
        });
      } 
}

When i run the main, it works and it creates the topic in fact, if i write on a new shell 'rostopic list', i can see my chatter; but after few moments, i have this kind of exception:

Apr 13, 2014 3:45:55 PM org.ros.internal.node.client.Registrar <init>
INFO: MasterXmlRpcEndpoint URI: localhost:11311
Apr 13, 2014 3:45:55 PM org.ros.internal.node.client.Registrar onPublisherAdded
INFO: Registering publisher: Publisher<PublisherDefinition<PublisherIdentifier<NodeIdentifier</rosjava_tutorial_pubsub/talker, localhost:53391/>, TopicIdentifier</rosout>>, Topic<TopicIdentifier</rosout>, TopicDescription<rosgraph_msgs/Log, acffd30cd6b6de30f120938c17c593fb>>>>
Apr 13, 2014 3:45:56 PM org.ros.internal.node.client.Registrar callMaster
INFO: Response<Success, Registered [/rosjava_tutorial_pubsub/talker] as publisher of [/rosout], [ubuntu:46978/]>
Apr 13, 2014 3:45:56 PM org.ros.internal.node.topic.DefaultPublisher$1 onMasterRegistrationSuccess
INFO: Publisher registered: Publisher<PublisherDefinition<PublisherIdentifier<NodeIdentifier</rosjava_tutorial_pubsub/talker, localhost:53391/>, TopicIdentifier</rosout>>, Topic<TopicIdentifier</rosout>, TopicDescription<rosgraph_msgs/Log, acffd30cd6b6de30f120938c17c593fb>>>>
Apr 13, 2014 3:45:56 PM org.apache.xmlrpc.server.XmlRpcStreamServer execute
SEVERE: execute: Error while performing request
org.apache.xmlrpc.server.XmlRpcNoSuchHandlerException: No such handler: requestTopic
    at org.apache.xmlrpc.server.AbstractReflectiveHandlerMapping.getHandler(AbstractReflectiveHandlerMapping.java:195)
    at org.apache.xmlrpc.server.XmlRpcServerWorker.execute(XmlRpcServerWorker.java:42)
    at org.apache.xmlrpc.server.XmlRpcServer.execute(XmlRpcServer.java:83)
    at org.apache.xmlrpc.server.XmlRpcStreamServer.execute(XmlRpcStreamServer.java:182)
    at org.apache.xmlrpc.webserver.Connection.run(Connection.java:175)
    at org.apache.xmlrpc.util.ThreadPool$MyThread.runTask(ThreadPool.java:71)
    at org.apache.xmlrpc.util.ThreadPool$MyThread.run(ThreadPool.java:87)

Any idea what might be going on?

Thanks

edit retag flag offensive close merge delete

Comments

I copied and pasted your RosjavaPubSub class and it works fine, including launching from within Android Studio. Have you checked that rosjava installed correctly?

Lorenzo Riano gravatar imageLorenzo Riano ( 2014-11-25 14:19:55 -0500 )edit

I use gradelw installApp to generate the classpath, but I could not find any way to run the main. I used java package.className to run but it always give an error Exception in thread "main" java.lang.NoClassDefFoundError: org/ros/node/NodeMain

Do you have any idea how to solve this?

Anis gravatar imageAnis ( 2015-01-23 15:34:58 -0500 )edit