Get the state of publishing

Hi,
Is there a way to achieve that, when a topic is publishing, a variable shows 1, when the topic stops publishing, the variable become 0. And turns back to 1 when the topic is publishing again.
Thank you in advance for any help,
mingcheng

Publishing topics is not a continuous process. When publishing in a loop, it might be publishing many times a second, but it is still individual messages. Many ROS messages have a header, which includes the ROS time of the publish and a sequential number. So you could check the time of the last publish and set your variable to 0, if it is older than a certain threshold. What exactly are you trying to do?

Hi @zhangmingcheng28,

The big question here is: what are you trying to do? In theory, a topic does not publish; rather something (a publisher) publishes into it. Your publisher can be created in code or on the command line.

Usually, when you publish to a topic successfully, the robot continues to act on the last message it gets until it gets another one that asks it to do something else.

Let’s say you publish a “move” Twist message to a robot on its /cmd_vel topic, the robot continues to move until it receives a “stop moving” Twist message on the /cmd_vel topic. And it remains stopped until…

So, what exactly do you want to do?

Hi,
Thank you for reply, I am trying to get the state of the publisher. I need to know when the publisher stops publish, and when does the publish starts to publish again. Is there a way to do this?
Thank you,
MingCheng

Hi @zhangmingcheng28,

The publisher publishes only when it’s told to do so - it does not act on its own. And when you tell it to publish, it does so once and then stops until it get another instruction.

If you are writing the code, you just determine when and what you want the publisher to publish under certain conditions. If someone else wrote the code, you just need to check the conditions under which the publisher publishes.

It will be helpful to provide a code sample or at least tell us the exact problem you are trying to solve.