Created
January 28, 2026 12:22
-
-
Save patham9/8c0c5126132a3ea24b89afce0afd64db to your computer and use it in GitHub Desktop.
ros2fix.py (Keeping fixed amounts of laser readings)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import math | |
| import rclpy | |
| from rclpy.node import Node | |
| from sensor_msgs.msg import LaserScan | |
| FIXED_LEN = 447 | |
| class ScanSanitizer(Node): | |
| def __init__(self): | |
| super().__init__('scan_sanitizer') | |
| self.sub = self.create_subscription( | |
| LaserScan, '/scan_raw', self.cb, 10) | |
| self.pub = self.create_publisher( | |
| LaserScan, '/scan', 10) | |
| def cb(self, msg): | |
| safe_max = msg.range_max * 0.999 | |
| # sanitize values | |
| ranges = [ | |
| r if math.isfinite(r) and r <= msg.range_max else safe_max | |
| for r in msg.ranges | |
| ] | |
| # force fixed length = 452 | |
| if len(ranges) >= FIXED_LEN: | |
| ranges = ranges[:FIXED_LEN] | |
| else: | |
| ranges.extend([safe_max] * (FIXED_LEN - len(ranges))) | |
| out = LaserScan() | |
| out.header = msg.header | |
| out.angle_min = msg.angle_min | |
| out.angle_max = msg.angle_max | |
| out.angle_increment = msg.angle_increment | |
| out.time_increment = msg.time_increment | |
| out.scan_time = msg.scan_time | |
| out.range_min = msg.range_min | |
| out.range_max = msg.range_max | |
| out.ranges = ranges | |
| out.intensities = msg.intensities | |
| self.pub.publish(out) | |
| def main(): | |
| rclpy.init() | |
| rclpy.spin(ScanSanitizer()) | |
| rclpy.shutdown() | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment