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

Revision history [back]

I understand how to make nodes, but how can I access to lidar data ...

This sentence is confusing in a sense. Anyway, let's break your problem into pieces and solve them:

  1. detect an object from 1.0[m] to 1.5[m], using a lidar

    To do this task, we need to create a subscriber first, and then inside the callback, we can pick the angles based on our criteria. In this case, the criteria is the distance of the object.

  2. send angle information to another node

    To do this task, we need to create a publisher that can publish the angle data. Then, the published data can be acquired by other nodes easily.

Please see the complete code below:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import rospy
from sensor_msgs.msg import LaserScan
from std_msgs.msg import Float32MultiArray

pub = rospy.Publisher('/detected_object', Float32MultiArray, queue_size=1)


def callback(msg):
    # let's define a range
    distance_min = 1.0
    distance_max = 1.5

    result = Float32MultiArray()
    for i, r in enumerate(msg.ranges):
        # we need object in 1 to 1.5 m range
        if distance_min < r < distance_max:
            angle = msg.angle_min + i * msg.angle_increment
            result.data.append(angle)
    pub.publish(result)


def objectDetector():
    rospy.init_node('object_detector', anonymous=True)
    rospy.Subscriber('/base_scan', LaserScan, callback)
    rospy.spin()


if __name__ == '__main__':
    objectDetector()

I used a recorded bag file file mentioned in this wiki page to run the above code. Please see below an example: Please see below a demonstration:

$ rosrun my_package object_detector.py 
$ rostopic echo /detected_object
layout: 
  dim: []
  data_offset: 0
data: [0.5559560656547546, 0.608904242515564]
---
layout: 
  dim: []
  data_offset: 0
data: [0.5559560656547546, 0.608904242515564]
---
layout: 
  dim: []
  data_offset: 0
data: [0.5030078887939453, 0.5559560656547546]
---

To understand the definition of sensor_msgs/LaserScan message, please see here.