Is there an easy way to check how many laser scanner samples are being generated by the ray plugin? I know I can check it in the URDF, XACRO or SDF but I want to confirm it by looking at the messages.

I'm aware that I can create a node that subscribes to the /scan topic and do .size() to the vector of msgs. I wonder if there's a built-in option.


3 Answers 3


If I understood correctly, you are trying to count how many lasers are being used in the ray plugin just checking the topic published with a LaserScan msg.

I think you could use the angle_min and angle_max with angle_increment in order to know the samples used.

In case you want to know the actual samples in each message (perhaps some laserscans are being skipped by inf values or whatever) I find interesting to use the flag --noarr. This flag omits the arrays and just shows the type of the data as well as the length of the arrays (in this case, check ranges). The use of it would be as simple as: rostopic echo /<scan_topic> --noarr

Resulting in:

  seq: 276
    secs: 29
    nsecs: 851000000
  frame_id: "os0_lidar"
angle_min: 0.0
angle_max: 3.14159011841
angle_increment: 0.00870000012219
time_increment: 0.0
scan_time: 0.0500000007451
range_min: 0.10000000149
range_max: 20.0
ranges: "<array type: float32, length: 362>"
intensities: "<array type: float32, length: 0>"```
  • 1
    $\begingroup$ That's exactly what I was looking for! For ROS 2 would be: ros2 topic echo /<scan_topic> --no-arr $\endgroup$
    – andrestoga
    Feb 13 at 17:41
  • $\begingroup$ This doesn't seem to result in the number of laser scans though, which is what the OP asked for. $\endgroup$ Feb 22 at 15:28

You can use ros2 topic echo /scan to view the topic on the command line quickly.

  • $\begingroup$ That doesn't tell you how many sample are being generated right away. You need to count them by hand or using another program. $\endgroup$
    – andrestoga
    Feb 12 at 22:21
  • $\begingroup$ @andrestoga - I'm not familiar with the output format of ros2 topic echo /scan but couldn't you use pipe it into grep (or sed or awk) to get an effective one line per sample and then pipe to wc -l to count the lines, something like this: ros2 topic echo /scan | grep <search term> | wc -l? Obviously replacing <search term> with something that results in one line per sample. $\endgroup$ Feb 12 at 22:31
  • $\begingroup$ I guess so but I was looking for a built-in solution. Asked ChatGPT to do it with pipes as you said but it didn't work lol $\endgroup$
    – andrestoga
    Feb 12 at 23:01
  • $\begingroup$ @andrestoga - That's not surprising, if you used ChatGPT (which just regurgitates nonsense). Note that ChatGPT is rather frowned upon, here on Stack Exchange... I wouldn't even mention it in future. $\endgroup$ Feb 12 at 23:43

I am not familiar with the output of ros2 topic echo /scan, having never used it.

However, after a google, I found this, laser data is not avliable on rviz2 . but i am geting data while using terminal cmd topic echo., which shows an example of the output, like so:

header: stamp: sec: 1675073473 nanosec: 112333930 frame_id: laser angle_min: 0.0 angle_max: 6.2657318115234375 angle_increment: 0.01745329238474369 time_increment: 0.0005592841189354658 scan_time: 0.20134228467941284 range_min: 0.11999999731779099 range_max: 3.5 ranges: - 0.13099999725818634 - 0.12999999523162842 - 0.12999999523162842 - 0.1289999932050705 - 0.1289999932050705 - 0.1289999932050705 - 0.1289999932050705 - 0.1289999932050705 ................

So, assuming that each sample "message" has the text header at the start, then this command should1 count your samples:

ros2 topic echo /scan | grep header | wc -l

1 Unless I've got completely the wrong end of the stick


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.