how to subscribe to laser /scan topic and publish a customized scan topic back
hi, i want to consider any 72 points from the 'scan' topic for my other application. Currently the lidar i am using gives around 2000 points. I want to take only 72 points out of it and publish it as a new topic 'revised_scan'. I tried with a small script in python:
#! /usr/bin/env python
import rospy
from sensor_msgs.msg import LaserScan
import sensor_msgs.msg
pub = rospy.Publisher('/revised_scan', LaserScan, queue_size = 10)
rev_scan = LaserScan()
def callback(msg):
#print(len(msg.ranges)) len is 2019 from 0-360
req_range = rev_scan.msg.ranges[0:72]
pub.publish(req_range)
def listener():
rospy.init_node('revised_scan', anonymous=True)
sub = rospy.Subscriber('/scan', LaserScan, callback)
rospy.spin()
if __name__ == '__main__':
listener()
however, it gives me an error saying
[ERROR] [1613466169.831151]: bad callback: <function callback at 0x7f99a8d350>
Traceback (most recent call last):
File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
cb(msg)
File "rev_scan.py", line 12, in callback
req_range = rev_scan.msg.ranges[0:72]
AttributeError: 'LaserScan' object has no attribute 'msg'
Though i can change directly in the lidars source file to publish two topics(scan & revised_scan) but I want to make this as a standard irrespective of the type of lidar I will use in the future.
I appreciate if someone can help with this.