Skip to content

Instantly share code, notes, and snippets.

@atotto
Last active February 20, 2023 21:40
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save atotto/c47bc69a48ed38e86947b5506b8e0e61 to your computer and use it in GitHub Desktop.
Save atotto/c47bc69a48ed38e86947b5506b8e0e61 to your computer and use it in GitHub Desktop.
Publishing Sensor Streams Over ROS (python)
#!/usr/bin/env python
import rospy
from sensor_msgs.msg import LaserScan
rospy.init_node('laser_scan_publisher')
scan_pub = rospy.Publisher('scan', LaserScan, queue_size=50)
num_readings = 100
laser_frequency = 40
count = 0
r = rospy.Rate(1.0)
while not rospy.is_shutdown():
current_time = rospy.Time.now()
scan = LaserScan()
scan.header.stamp = current_time
scan.header.frame_id = 'laser_frame'
scan.angle_min = -1.57
scan.angle_max = 1.57
scan.angle_increment = 3.14 / num_readings
scan.time_increment = (1.0 / laser_frequency) / (num_readings)
scan.range_min = 0.0
scan.range_max = 100.0
scan.ranges = []
scan.intensities = []
for i in range(0, num_readings):
scan.ranges.append(1.0 * count) # fake data
scan.intensities.append(1) # fake data
scan_pub.publish(scan)
count += 1
r.sleep()
@atotto
Copy link
Author

atotto commented Sep 27, 2016

see http://wiki.ros.org/navigation/Tutorials/RobotSetup/Sensors

terminal1:

$ roscore

terminal2:

$ rosrun tf static_transform_publisher 0.0 0.0 0.0 0.0 0.0 0.0 /base_frame /laser_frame 1000

terminal3:

$ rviz

terminal4:

$ python ros_laser_scanner_example.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment