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

agkhalil's profile - activity

2018-05-23 05:43:40 -0500 received badge  Notable Question (source)
2018-05-23 05:43:40 -0500 received badge  Popular Question (source)
2018-05-23 05:43:40 -0500 received badge  Famous Question (source)
2017-10-13 19:48:27 -0500 commented answer rosjava publishing to a topic based on user input (button)

Thanks a bunch man! It works!

2017-10-13 19:48:12 -0500 marked best answer rosjava publishing to a topic based on user input (button)

I have been playing around with rosjava now for a few days and it is great. I am able to connect my android phone to ros master over usb/wifi and have them publish/subscribe to one another.

What I am trying to do now is add a button on my android app, when the user hits it, the message being published changes. To clarify: I have a publisher node "talkernode" created that starts by publishing a String "Hello" message on a "/chatter" topic. When the user hits a button, I want the String message to change to something of my choice.

Below is the relevant portion of my code. The publishGo() method is my onClick callback function, where I am trying to add code to change the message being published. I have already tested that something happens when the button is hit and it works.

public class MainActivity extends RosActivity{
Button btn;

public MainActivity() {
    super("Example", "Example");
}

@Override
public void startMasterChooser() {
    URI uri;
    try {
        uri = new URI("http://192.168.42.19:11311/");
    } catch (URISyntaxException e) {
        throw new RosRuntimeException(e);
    }

    nodeMainExecutorService.setMasterUri(uri);
    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            MainActivity.this.init(nodeMainExecutorService);
            return null;
        }
    }.execute();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.robot_button);
}

@Override
protected void init(NodeMainExecutor nodeMainExecutor) {
    Talker talkernode = new Talker(this);

    NodeConfiguration nodeConfiguration = NodeConfiguration.newPublic(
            InetAddressFactory.newNonLoopback().getHostAddress());
    nodeConfiguration.setMasterUri(getMasterUri());

    nodeMainExecutor.execute(talkernode, nodeConfiguration);
}

protected void publishGo(View view) {
    // Code here
}
}

Below is the Talker class code. randomCommand is the string being published and the string I want to change when the button is clicked.

class Talker extends AbstractNodeMain {
private String randomCommand = "Hello";
Talker(MainActivity mainActivity) {

}

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

@Override
public void onStart(final ConnectedNode connectedNode) {
    final Log log = connectedNode.getLog();
    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() {
        @Override
        protected void setup() {
        }

        @Override
        protected void loop() throws InterruptedException {
            std_msgs.String str = publisher.newMessage();
            str.setData(randomCommand);
            log.info("I publish: \"" + randomCommand + "\"");
            publisher.publish(str);
            Thread.sleep(1000);
        }
    });

}
}

I apologize if this is an easy question or if it has been answered before, but any help would be immensely appreciated.

2017-10-13 19:48:11 -0500 received badge  Scholar (source)
2017-10-13 19:48:10 -0500 received badge  Supporter (source)
2017-10-13 15:21:11 -0500 received badge  Student (source)
2017-10-13 08:42:30 -0500 asked a question rosjava publishing to a topic based on user input (button)

rosjava publishing to a topic based on user input (button) I have been playing around with rosjava now for a few days an