ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Laser scan topic returns many messages

asked 2020-03-15 18:40:44 -0500

John999991 gravatar image

updated 2020-03-16 10:42:25 -0500

I am using a very simple subscription to a /scan topic to get the laser distances that my hokuyo laser scanner has grabbed, using:

scan_msg = rospy.Subscriber("/scan", LaserScan, scan_message, queue_size=1)

but when I print out the returning message I get 3 message packets, not 1 as expected. Why?

Code:

rospy.init_node("node1")
def scan_message(msg):
        laser_range = msg.ranges
        print "Range: ",laser_range


 scan_msg = rospy.Subscriber("/scan", LaserScan, scan_message, queue_size=1)

Result:

Range: (inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, 2.886342763900757, 2.874502420425415, 2.8628132343292236, 2.8512728214263916, 2.839879274368286, 2.828630208969116, 2.817523241043091, 2.8065567016601562, 2.7957284450531006, 2.785036087036133, 2.7744781970977783, 2.764052629470825, 2.7537574768066406, 2.743590831756592, 2.733551025390625, 2.7236363887786865, 2.7138447761535645, 2.7041749954223633, 2.694624900817871, 2.6851933002471924, 2.6758782863616943, 2.6666784286499023, 2.657592296600342, 2.648618221282959, 2.6397545337677, 2.631000280380249, 2.6223537921905518, 2.613813638687134, 2.6053783893585205, 2.5970470905303955, 2.588818073272705, 2.5806901454925537, 2.572661876678467, 2.564732313156128, 2.5569002628326416, 2.549164295196533, 2.5415234565734863, 2.5339765548706055, 2.526522397994995, 2.5191597938537598, 2.511888027191162, 2.5047056674957275, 2.4976117610931396, 2.490605354309082, 2.4836854934692383, 2.476851224899292, 2.4701013565063477, 2.463434934616089, 2.4568514823913574, 2.4503495693206787, 2.4439284801483154, 2.4375874996185303, 2.431325674057007, 2.4251420497894287, 2.4190359115600586, 2.41300630569458, 2.407052516937256, 2.4011738300323486, 2.395369529724121, 2.389638662338257, 2.3839805126190186, 2.378394365310669, 2.3728795051574707, 2.3674352169036865, 2.362061023712158, 2.3567559719085693, 2.3515193462371826, 2.346350908279419, 2.341249465942383, 2.336214780807495, 2.3312461376190186, 2.326343059539795, 2.3289997577667236, 2.3506991863250732, 2.3728525638580322, 2.3954737186431885, 2.418576717376709, 2.4421770572662354, 2.466289758682251, 2.490931749343872, 2.5161192417144775, 2.541870594024658, 2.568204164505005, 2.5951387882232666, 2.622695207595825, 2.6508941650390625, 2.679757833480835, 2.7093093395233154, 2.739572763442993, 2.770573377609253, 2.802337646484375, 2.8348937034606934, 2.8682701587677, 2.902498245239258 ... (more)

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2020-03-16 02:31:23 -0500

updated 2020-03-16 02:39:38 -0500

Please look over ROS tutorials (http://wiki.ros.org/rospy/Overview/Pu...).

A subscriber object is not a message, its declaring a subscription to a topic with some parameters. The callback is what will process incoming messages. So in your example the scan_msg is actually a subscriber object and the scan_message is a callback function signature.

edit flag offensive delete link more

Comments

I have implemented the callback function from where I print out the message I receive. The problem is that I get 3 messages instead of 1.

John999991 gravatar image John999991  ( 2020-03-16 02:51:59 -0500 )edit

Please show that code and the output as well.

pcoenen gravatar image pcoenen  ( 2020-03-16 06:57:05 -0500 )edit

I have added the important portion of my code on the original question. Thank you.

John999991 gravatar image John999991  ( 2020-03-16 09:58:55 -0500 )edit

Not sure what you mean by three message packets - what is leading you to say that is what is happening.

nickw gravatar image nickw  ( 2020-03-16 10:17:19 -0500 )edit

Is any code missing? I don't understand where the I heard is coming from.

pcoenen gravatar image pcoenen  ( 2020-03-16 10:24:46 -0500 )edit

Sorry, I fixed it.

John999991 gravatar image John999991  ( 2020-03-16 10:40:44 -0500 )edit

I get 3 times the Range: {values}, so I suppose I get the Sub to run 3 times...No?

John999991 gravatar image John999991  ( 2020-03-16 10:43:52 -0500 )edit

I am assuming the laser scanner sends messages one after the other - is this just displaying multiple messages that have arrived, each one a separate call when a message is received. In the absence of other code it is impossible to say ?

The values are slightly different, so it is not the same thing that is being printed 3 times

nickw gravatar image nickw  ( 2020-03-16 10:45:37 -0500 )edit
1

answered 2020-03-16 11:54:40 -0500

ahendrix gravatar image

A ROS subscriber runs continuously and gets all messages that are published on a topic until it is shut down. The callback is called once for each message.

In your case it looks like your program runs long enough to receive three messages before it stops, but I suspect that sometimes you will get 1, 2 or maybe 4 messages depending on timing and other CPU usage.

If you want to receive a single message on a topic, you need to create subscriber, wait for callback to be called once, and then unsubscribe; rospy has a function that does this for you: rospy.wait_for_message

I haven't tested it, but this should work:

#!/usr/bin/env python
from __future__ import print_function
import rospy
from sensor_msgs.msg import LaserScan

rospy.init_node("subscribe_once")
message = rospy.wait_for_message("/scan", LaserScan)
print(message)
edit flag offensive delete link more

Comments

This is the answer.

gvdhoorn gravatar image gvdhoorn  ( 2020-03-17 04:27:38 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2020-03-15 18:40:44 -0500

Seen: 607 times

Last updated: Mar 16 '20