# Single scan from a planar laser range-finder
#
# If you have another ranging device with different behavior (e.g. a sonar
# array), please find or create a different message, since applications
# will make fairly laser-specific assumptions about this data
std_msgs/Header header # timestamp in the header is the acquisition time of
# the first ray in the scan.
#
# in frame frame_id, angles are measured around
# the positive Z axis (counterclockwise, if Z is up)
# with zero angle being forward along the x axis
float32 angle_min # start angle of the scan [rad]
float32 angle_max # end angle of the scan [rad]
float32 angle_increment # angular distance between measurements [rad]
float32 time_increment # time between measurements [seconds] - if your scanner
# is moving, this will be used in interpolating position
# of 3d points
float32 scan_time # time between scans [seconds]
float32 range_min # minimum range value [m]
float32 range_max # maximum range value [m]
float32[] ranges # range data [m]
# (Note: values < range_min or > range_max should be discarded)
float32[] intensities # intensity data [device-specific units]. If your
# device does not provide intensities, please leave
# the array empty.
import rclpy
from rclpy.node import Node
from sensor_msgs.msg import LaserScan
class LaserScanPublisher(Node):
def __init__(self):
super().__init__('laser_scan_publisher')
self.publisher_ = self.create_publisher(LaserScan, 'laser_scan', 10)
self.timer_ = self.create_timer(1.0, self.publish_scan)
def publish_scan(self):
scan_msg = LaserScan()
# 设置LaserScan消息的各个字段
scan_msg.header.frame_id = 'laser_frame'
scan_msg.header.stamp = self.get_clock().now().to_msg()
scan_msg.angle_min = -1.57
scan_msg.angle_max = 1.57
scan_msg.angle_increment = 0.01
scan_msg.time_increment = 0.0
scan_msg.scan_time = 0.1
scan_msg.range_min = 0.0
scan_msg.range_max = 10.0
scan_msg.ranges = [1.0, 2.0, 3.0, 4.0, 5.0]
scan_msg.intensities = [100, 200, 300, 400, 500]
self.publisher_.publish(scan_msg)
def main(args=None):
rclpy.init(args=args)
laser_scan_publisher = LaserScanPublisher()
rclpy.spin(laser_scan_publisher)
laser_scan_publisher.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
三、C++程序
#include "rclcpp/rclcpp.hpp"
#include "sensor_msgs/msg/laser_scan.hpp"
using std::placeholders::_1;
class LaserScanPublisher : public rclcpp::Node
{
public:
LaserScanPublisher()
: Node("laser_scan_publisher")
{
publisher_ = this->create_publisher<sensor_msgs::msg::LaserScan>("laser_scan", 10);
timer_ = this->create_wall_timer(std::chrono::milliseconds(1000), std::bind(&LaserScanPublisher::publish_scan, this));
}
private:
void publish_scan()
{
auto scan_msg = std::make_shared<sensor_msgs::msg::LaserScan>();
// 设置LaserScan消息的各个字段
scan_msg->header.frame_id = "laser_frame";
scan_msg->header.stamp = this->now();
scan_msg->angle_min = -1.57;
scan_msg->angle_max = 1.57;
scan_msg->angle_increment = 0.01;
scan_msg->time_increment = 0.0;
scan_msg->scan_time = 0.1;
scan_msg->range_min = 0.0;
scan_msg->range_max = 10.0;
scan_msg->ranges = {1.0, 2.0, 3.0, 4.0, 5.0};
scan_msg->intensities = {100, 200, 300, 400, 500};
publisher_->publish(*scan_msg);
}
rclcpp::Publisher<sensor_msgs::msg::LaserScan>::SharedPtr publisher_;
rclcpp::TimerBase::SharedPtr timer_;
};
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
auto laser_scan_publisher = std::make_shared<LaserScanPublisher>();
rclcpp::spin(laser_scan_publisher);
rclcpp::shutdown();
return 0;
}