Rosanswers logo


I am using an arduino Mega to publish LaserScan message. But I have a weird result, when i run this code:

#include <ros.h>
#include <sensor_msgs/LaserScan.h>

ros::NodeHandle nh;

// Laser Scan
sensor_msgs::LaserScan lidar_msg;
ros::Publisher lidar_pub("/laser_scan", &lidar_msg);
float ranges[10] = {0};
float intensities[10] = {0};

void setup()
  // Initialize ROS node handle, advertise and subscribe the topics
  // Set LaserScan Definition
  lidar_msg.header.frame_id = "lidar";
  lidar_msg.angle_min = 0.0;               // start angle of the scan [rad]
  lidar_msg.angle_max = 3.14*2;            // end angle of the scan [rad]
  lidar_msg.angle_increment = 3.14*2/360;  // angular distance between measurements [rad]
  lidar_msg.range_min = 0.3;               // minimum range value [m]
  lidar_msg.range_max = 50.0;                // maximum range value [m]

void loop(){
  // simple loop to generate values
  for (int i=0 ; i<10 ; ++i){
    ranges[i] = 1.0*i;
  lidar_msg.ranges = ranges;
  lidar_msg.header.stamp = nh.now();

Then, I launch rosserial node to start serial communication:

$ rosrun rosserial_python serial_node.py /dev/ttyUSB2
[INFO] [1519508591.451838]: ROS Serial Python Node
[INFO] [1519508591.468653]: Connecting to /dev/ttyUSB2 at 57600 baud
[INFO] [1519508593.624826]: Note: publish buffer size is 512 bytes
[INFO] [1519508593.625218]: Setup publisher on /laser_scan [sensor_msgs/LaserScan]

But when, i echo the topic, I didn't get any values in ranges:

  seq: 112
    secs: 1519508602
    nsecs:  77765016
  frame_id: "lidar"
angle_min: 0.0
angle_max: 6.28000020981
angle_increment: 0.0174444448203
time_increment: 0.0
scan_time: 0.0
range_min: 0.300000011921
range_max: 50.0
ranges: []
intensities: []

Any idea ?


Originally posted by mattMGN on ROS Answers with karma: 78 on 2018-02-24

Post score: 1


1 Answer 1


Rosanswers logo

Did you perhaps forget to set the ranges_length and intensities_length fields to the nr of elements in the arrays?

That is required with rosserial. See #q265037 and #q229621 for related questions.

According to [wiki/rosserial/Overview/Messages](http://wiki.ros.org/rosserial/Overview - Messages):

Also be aware that the arrays in the messages are not defined as vector objects. Thus you have to predefine the array and then pass it as pointer to the message. To determine the end of the array each one has an auto-generated integer companion with the same name and the suffix _length.

And also wiki/rosserial/Overview - Limitations - Arrays.

Originally posted by gvdhoorn with karma: 86574 on 2018-02-25

This answer was ACCEPTED on the original site

Post score: 1

Original comments

Comment by mattMGN on 2018-02-25:
ranges_length and intensities_length were the point ! Thanks you

Comment by kartiksoni on 2022-09-02:
hey can u show me where and how exactly did u add the ranges_length and intensities_length? Thank you.


Your Answer

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