Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

@agkhalil what you need in this case is a publisher "on demand".

First of all, save the publisher object in a member variable in the node in the onStart method, so that you can use it later. You have to do this in onStart because the ConnectedNode provides you the publisher.

class Talker extends AbstractNodeMain {
Publisher<std_msgs.String> publisher;
(...)

@Override
public void onStart(final ConnectedNode connectedNode) {
    publisher = connectedNode.newPublisher("chatter", std_msgs.String._TYPE);
    (...)
}
}

Second, add a publish method to your node that you can call whenever you want:

// Note that you shouldn't call this before onStart is called; check that publisher is not null before using it!
public void publish(String message) {
    std_msgs.String toPublish = publisher.newMessage();
    toPublish.setData(message);
    publisher.publish(toPublish);
}

Then, just store your Talker node somewhere you can access from the callback button, and call publish from there. That should cover your use case. Remember that there is a period of time between the moment you create the node and the moment onStart is called; that's why you should be careful with the state of publisher member variable.

Bonus: Note that you can also make your publisher latched; if you do so, the publisher will publish the message to each new subscriber and you won't need to do anything else. To do so, after creating the publisher, do publisher.setLatchMode(true).

Hope it helps!

@agkhalil what you need in this case is a publisher "on demand".

First of all, save the publisher object in a member variable in the node in the onStart method, so that you can use it later. You have to do this in onStart because the ConnectedNode provides you the publisher.

class Talker extends AbstractNodeMain {
Publisher<std_msgs.String> publisher;
(...)

@Override
public void onStart(final ConnectedNode connectedNode) {
    publisher = connectedNode.newPublisher("chatter", std_msgs.String._TYPE);
    (...)
}
}

Second, add a publish method to your node that you can call whenever you want:

// Note that you shouldn't call this before onStart is called; 
// check that publisher is not null before using it!
public void publish(String message) {
    std_msgs.String toPublish = publisher.newMessage();
    toPublish.setData(message);
    publisher.publish(toPublish);
}

Then, just store your Talker node somewhere you can access from the callback button, and call publish from there. That should cover your use case. Remember that there is a period of time between the moment you create the node and the moment onStart is called; that's why you should be careful with the state of publisher member variable.

Bonus: Note that you can also make your publisher latched; if you do so, the publisher will publish the message to each new subscriber and you won't need to do anything else. To do so, after creating the publisher, do publisher.setLatchMode(true).

Hope it helps!