How to subscriber for a topic in the same callback function of topic?

asked 2019-11-19 00:22:00 -0600

Harish_More gravatar image

updated 2019-11-19 02:31:22 -0600

gvdhoorn gravatar image

Hi,

I want to get updated new sensor value in callback function of subscribed topic. And the the topic I want to subscribe in callback function is same topic.

for ex.

def left_dist(data):
      print(data.data)

def callback(data):
      rospy.Subscriber("DistSensor", String,left_dist)

if __name__ == '__main__':
      rospy.Subscriber("DistSensor", String, callback)

Thank you in avanced.

edit retag flag offensive close merge delete

Comments

My first response would be: don't do that.

Perhaps if you could explain why you believe you need / want to do this, we can avoid an xy-problem and help you solve the real problem.

gvdhoorn gravatar imagegvdhoorn ( 2019-11-19 02:32:00 -0600 )edit

What is your use-case that requires this behaviour? When you get new data on "DistSensor" that is when the callback function is executed. So anything inside that callback function uses the data you just received. I think that if you manage to implement this what will happen is:

  1. wait on data "DistSensor"
  2. data is received (data0) -> enter def callback() -> wait on "DistSensor"
  3. data is received again (data1) -> enter def left_dist() -> print (data1.data)
  4. return inside the body of def callback() -> continue processing with data0 or data1 depending on whether you use a pointer or a copy of the data?

Either I am misunderstanding the implementation you have in mind or I'm predicting the behaviour wrong, but this seems like a weird system to me. What is the benefit of this over just having

def callback(data):
    print(data.data)
    do other stuff...
MCornelis gravatar imageMCornelis ( 2019-11-19 02:44:32 -0600 )edit

Hello Sorry for silly questions and mistakes I want to get forward distance from my distance Sensor after that if the distance is less than threshold, then change heading of vehicle to left(which will in callback function). After changing heading Again get the new distance with ref to left side (to check left side occupancy of vehicle). So to get new distance (left side), will I have to again subscribe the same topic or not?

Else, how should I deal with this problem?

Thank you

Harish_More gravatar imageHarish_More ( 2019-11-19 03:04:57 -0600 )edit

You could not do anything on the first run except for saving the distance information as old_data. Then the next time you receive data you compare the new data to old_data and at the end of the callback you do old_data = new_data. This way you always have the data from your previous step.

MCornelis gravatar imageMCornelis ( 2019-11-19 03:06:47 -0600 )edit

Pseudo code of callback function

init = 0

def callback(data):
    if init ==1
        compare old_data to data
            do some stuff ...
    init = 1
    old_data = data
  1. you enter the callback (init = 0) -> if-statement is false -> set init = 1 -> store data in old_data
  2. you enter the callback (init = 1) -> if-statement is true -> you can now compare data to old_data from the previous step and do things

This is a very simple example, you can do it prettier probably, if you want to store more data-points you might want to look into buffering, but I think this will suffice for what you are trying to do.

If I misunderstood you and you just need to pass the data around then you can ignore the above suggestion. You can simply share the data in the callback to different functions. It will execute all functions with the received data

def callback(data)
    func_1(data)
    func_2(data)
MCornelis gravatar imageMCornelis ( 2019-11-19 03:22:23 -0600 )edit

Thank you MCornelis. It worked for me.

Harish_More gravatar imageHarish_More ( 2019-11-21 06:02:51 -0600 )edit