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

How to write laser scan node for sharp GP2Y0A02YK0F sensor using C++?

asked 2015-07-23 10:35:11 -0500

I want to write a laser node for scanning an area using sharp GP2Y0A02YK0F IR range finder. Please give me some hints or source codes to work with that Range finder. Thank you!

edit retag flag offensive close merge delete


Perhaps you should add more details about your system, e.g. how do you want to cover an area with IR-Sensors

Humpelstilzchen gravatar image Humpelstilzchen  ( 2015-07-24 02:12:17 -0500 )edit

Do you already have a hardware-interface to the sensor?

NEngelhard gravatar image NEngelhard  ( 2015-07-25 12:48:35 -0500 )edit

@Humpelstizchen Yes the system has on sharp IR sensor .It is connected to servo motor for rotation.

kaviranga gravatar image kaviranga  ( 2015-08-06 01:00:55 -0500 )edit

@NEngelhard yes I'm using beaglebone black hardware interface.

kaviranga gravatar image kaviranga  ( 2016-08-14 06:34:53 -0500 )edit

1 Answer

Sort by » oldest newest most voted

answered 2015-07-25 07:20:31 -0500

You can read the data form your device and send data in the format as below:

To see the structure of the message sensor_msgs/LaserScan . Use the following command:

$ rosmsg show sensor_msgs/LaserScan

std_msgs/Header header
uint32 seq
time stamp
string frame_id
float32 angle_min
float32 angle_max
float32 angle_increment
float32 time_increment
float32 scan_time
float32 range_min
float32 range_max
float32[] ranges
float32[] intensities

Creating the laser node: create a new file:and put the following code in it:

#include <ros/ros.h>
#include <sensor_msgs/LaserScan.h>
int main(int argc, char** argv){
ros::init(argc, argv, "laser_scan_publisher");
ros::NodeHandle n;
ros::Publisher scan_pub = n.advertise<sensor_
msgs::LaserScan>("scan", 50);
unsigned int num_readings = 100;
double laser_frequency = 40;
double ranges[num_readings];
double intensities[num_readings];
int count = 0;
ros::Rate r(1.0);
//generate some fake data for our laser scan
for(unsigned int i = 0; i < num_readings; ++i){
ranges[i] = count;
intensities[i] = 100 + count;
ros::Time scan_time = ros::Time::now();
Navigation Stack – Robot Setups
[ 224  ]
//populate the LaserScan message
sensor_msgs::LaserScan scan;
scan.header.stamp = scan_time;
scan.header.frame_id = "base_link";
scan.angle_min = -1.57;
scan.angle_max = 1.57;
scan.angle_increment = 3.14 / num_readings;
scan.time_increment = (1 / laser_frequency) / (num_readings);
scan.range_min = 0.0;
scan.range_max = 100.0;
for(unsigned int i = 0; i < num_readings; ++i){
scan.ranges[i] = ranges[i];
scan.intensities[i] = intensities[i];

As you can see, we are going to create a new topic with the name scan and the message type sensor_msgs/LaserScan .

edit flag offensive delete link more


Thank you very much for your quick reply . It was great help for me !

kaviranga gravatar image kaviranga  ( 2015-08-06 00:57:18 -0500 )edit

hi, i want to do the same thing and i want to ask you if you have managed to do it. i use IR connected to arduino Uno and rosserial thanks

Emilien gravatar image Emilien  ( 2016-04-29 11:47:57 -0500 )edit

@Emilien I'm using beagle bone black as my hardware. I've capable of directly run the wheels of the robot using beaglebone black using a program written in C++.But ros-serial is better when communicating with sharp IR sensor!

kaviranga gravatar image kaviranga  ( 2016-08-14 06:48:33 -0500 )edit

Question Tools

1 follower


Asked: 2015-07-23 10:35:11 -0500

Seen: 922 times

Last updated: Jul 25 '15