android pubsub tutorial: publishing int from app

asked 2014-08-05 08:33:14 -0600

stefan gravatar image

updated 2014-08-06 05:20:49 -0600

Hello,

how can i publish an int with my talker.java (from pubsub tutorial)



i managed to publish an int from my app!

in the app

protected void init(NodeMainExecutor nodeMainExecutor) {
 // talker = new Talker();
talker = new Talker(3);

//talker = new Talker(zu übergebende Infos); - Talker.java anpassen

//listener = new Listener();

NodeConfiguration nodeConfiguration = NodeConfiguration.newPrivate();
// At this point, the user has already been prompted to either enter the URI
// of a master to use or to start a master locally.
nodeConfiguration = NodeConfiguration.newPublic(InetAddressFactory.newNonLoopback().getHostAddress().toString(), getMasterUri());
nodeConfiguration.setMasterUri(getMasterUri());


nodeMainExecutor.execute(talker, nodeConfiguration);

//nodeMainExecutor.execute(listener, nodeConfiguration);
// The RosTextView is also a NodeMain that must be executed in order to
// start displaying incoming messages.
nodeMainExecutor.execute(rosTextView, nodeConfiguration);

in talker.java

public class Talker extends AbstractNodeMain {

  private int x;


//public Talker(){}
public Talker(int i){this.x= i;}

  @Override
  public GraphName getDefaultNodeName() {
    return GraphName.of("rosjava_tutorial_pubsub/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(" Test -  " + x + "  -  "+ sequenceNumber);
    publisher.publish(str);
    sequenceNumber++;
    Thread.sleep(1000);
  }
 });
  }
  }

It is now publishing Test - 3 - XX

but i can't change the value of this int

i want to do

talker = new talker (int_var);

and change int_var while running

so i have to change talker.x value in the loop


Edit:

setting the int x to public makes it possible to write in talker.x in the app

edit retag flag offensive close merge delete