Ask Your Question

Revision history [back]

You're approaching event based programming from the wrong angle.

If you want to repeatedly publish a message on a topic sometimes, and control it using a button then you'll need a timer. The architecture should look something like this.

  1. The initialisation of your program should create the publisher object, a timer object and your button. You will also need a global boolean variable which will control the publishing.

  2. The tick callback for your timer object should check the global boolean and publish a single message if it's true.

  3. The button pressed callback should invert the value of the global boolean.

The timer object is used to do the job of the while loop in this case. Hope this gets you going in the right direction.

You're approaching event based programming from the wrong angle.

If you want to repeatedly publish a message on a topic sometimes, and control it using a button then you'll need a timer. The architecture should look something like this.

  1. The initialisation of your program should create the publisher object, a timer object and your button. You will also need a global boolean variable which will control the publishing.

  2. The tick callback for your timer object should check the global boolean and publish a single message if it's true.

  3. The button pressed callback should invert the value of the global boolean.

The timer object is used to do the job of the while loop in this case. Hope this gets you going in the right direction.

UPDATE:

Thanks for posting you new code. It's getting closer but there are things you need to fix.

1) You don't need any rate objects for this, so get rid of all of them. The publish rate is set by the timer using this method so rate objects are not needed.

2) There shouldn't be a while loops also, we're getting rid of them. Replace the while statement with an if as shown below:

if self.on == True:

Now if your button is controlling the value of self.on then this should start working.