# 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 builtin_interfaces/Time stamp int32 sec uint32 nanosec string frame_id # 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 geometry_msgs.msg import Twist from sensor_msgs.msg import LaserScan from rclpy.qos import qos_profile_sensor_data
defmove(self): msg = Twist() msg.linear.x = 0.0 msg.linear.y = 0.0 msg.linear.z = 0.0 msg.angular.x = 0.0 msg.angular.y = 0.0 msg.angular.z = 0.0 d = self.dist_thresh # 周围没有墙面,则前进 if self.leftfront_dist > d and self.front_dist > d and self.rightfront_dist > d: msg.linear.x = self.forward_speed # 右边有墙,左转 elif self.leftfront_dist > d and self.rightfront_dist < d: msg.angular.z = self.turning_speed # 左边有墙,右转 elif self.leftfront_dist < d and self.rightfront_dist > d: msg.angular.z = -self.turning_speed # 其他情况,左转。适用于饶围墙走 else: msg.angular.z = self.turning_speed
# 发布速度指令 self.publisher_.publish(msg)
defmain():
# Initialize rclpy library rclpy.init(args=args)
# Create the node robot_move = RobotMove()
# Spin the node so the callback function is called # Pull messages from any topics this node is subscribed to # Publish any pending messages to the topics rclpy.spin(robot_move)
# Destroy the node explicitly # (optional - otherwise it will be done automatically # when the garbage collector destroys the node object) robot_move.destroy_node()
# Shutdown the ROS client library for Python rclpy.shutdown()
# Spin the node so the callback function is called # Pull messages from any topics this node is subscribed to # Publish any pending messages to the topics rclpy.spin(robot_move)
# Destroy the node explicitly # (optional - otherwise it will be done automatically # when the garbage collector destroys the node object) robot_move.destroy_node()
# Shutdown the ROS client library for Python rclpy.shutdown()