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.
Originally posted by agkhalil on ROS Answers with karma: 13 on 2017-10-13
Post score: 1