ros三大核心消息包:geometry_msgs.msg、visualization_msgs、action_msgs.msg
2026/4/6 14:08:33 网站建设 项目流程
一. geometry_msgs.msg几何数据用途机器人所有位置、姿态、速度、向量、坐标变换都用它。1基本消息1.1Point—— 3D 坐标点作用表示空间中的一个点x,y,zfloat64 x float64 y float64 z1.2Vector3—— 3D 向量作用表示方向 / 大小平移、速度、缩放float64 x float64 y float64 z和 Point 结构一样但语义不同Vector3 表示方向 / 力 / 速度Point 表示坐标位置1.3Quaternion—— 四元数姿态作用表示 3D 旋转无奇异值ROS 标准姿态格式float64 x float64 y float64 z float64 w必须是单位四元数默认无旋转x0,y0,z0,w199% 的情况不用手动算用tf_transformations或pyquaternion库转换。2高级组合消息2.1Pose—— 位姿位置 姿态Point position Quaternion orientation表示在哪里 朝哪转用途机器人位姿、目标点、物体位姿2.2PoseStamped—— 带时间戳 坐标系的位姿Header header Pose poseHeader 包含std_msgs/Header header uint32 seq time stamp string frame_id # 关键坐标系必须带frame_id如map、base_link、odom导航、SLAM、TF 全都用这个2.3Twist—— 速度线速度 角速度控制小车移动的核心消息Vector3 linear # 线速度 Vector3 angular # 角速度linear.x前进 / 后退angular.z左转 / 右转2.4TwistStamped—— 带时间戳的速度Header header Twist twist2.5Transform—— 坐标变换两个坐标系之间的平移 旋转2.5 Transform —— 坐标变换 两个坐标系之间的平移 旋转2.6TransformStamped—— 带坐标系的变换Header header string child_frame_id Transform transform3.最常用消息作用最常用场景Point3D 点坐标位置Quaternion姿态旋转方向Pose位置 姿态目标点PoseStamped带坐标系位姿导航、SLAMTwist速度小车控制TransformStamped坐标变换TF、雷达 / 相机外参二visualization_msgs是 ROS 2 中专门用于 RViz 可视化的消息包1. 核心消息详解字段类型核心说明取值 / 示例headerstd_msgs/Header坐标系 时间戳必设frame_idmap、stamp当前时间nsstring命名空间分组管理如waypoints、obstaclesidint32唯一 ID同 ns 下唯一0、1、2... 用于区分同组内元素typeint32元素类型核心见下文「类型枚举」actionint32操作指令ADD0新增 / 修改、DELETE2删除单个、DELETEALL3删除整组posegeometry_msgs/Pose元素位姿位置 朝向如position.x1.0、orientation.w1.0无旋转scalegeometry_msgs/Vector3尺寸缩放单位米如scale.x0.3立方体边长 0.3mcolorstd_msgs/ColorRGBA颜色RGBA0~1r1.0,g0.0,b0.0,a1.0红色不透明lifetimebuiltin_interfaces/Duration生命周期0 永久Duration(5,0)5 秒后自动消失frame_lockedbool是否锁定坐标系true随坐标系移动、false固定位置pointsgeometry_msgs/Point[]顶点数组部分类型必用如折线、点云、三角形列表textstring文字内容TEXT 类型专用如目标点mesh_resourcestring3D 模型路径MESH 类型专用如package://my_description/meshes/robot.dae「type」枚举常用 8 类枚举值名称说明必用字段0ARROW箭头表示方向 / 路径pose起点 朝向、scale长度、半径1CUBE立方体pose、scale长宽高2SPHERE球体pose、scale半径3CYLINDER圆柱体pose、scale半径、高度4LINE_STRIP折线连续点连线points顶点列表、scale线宽9TEXT_VIEW_FACING面向相机文字pose、text、scale文字大小10MESH_RESOURCE3D 模型pose、mesh_resource、scale11TRIANGLE_LIST三角形面复杂形状points3 个顶点 / 个三角形1.2MarkerArray批量元素定义Marker[] markers多个 Marker 组成的数组。优势单次发布多个元素减少通信开销适合批量显示如粒子云、多个障碍物。用法创建多个 Marker 加入markers数组一次性发布到/markers话题。2示例2.1 Python 示例RViz 显示红色立方体 文字箭头import rclpy from rclpy.node import Node from visualization_msgs.msg import Marker, MarkerArray from geometry_msgs.msg import Pose, Point, Quaternion class MarkerVisualizer(Node): def __init__(self): super().__init__(marker_visualizer) self.publisher_ self.create_publisher(MarkerArray, /visualization_markers, 10) self.timer self.create_timer(1.0, self.publish_markers) # 每秒发布一次 def publish_markers(self): marker_array MarkerArray() stamp self.get_clock().now().to_msg() # 1. 红色立方体ID0 cube Marker() cube.header.frame_id map # 参考坐标系 cube.header.stamp stamp cube.ns basic_shapes cube.id 0 cube.type Marker.CUBE cube.action Marker.ADD cube.pose Pose(positionPoint(x1.0, y0.0, z0.5), orientationQuaternion(w1.0)) cube.scale.x 0.6 # 边长0.6m cube.scale.y 0.6 cube.scale.z 1.0 cube.color.r 1.0 # 红色 cube.color.g 0.0 cube.color.b 0.0 cube.color.a 1.0 # 不透明 cube.lifetime rclpy.Duration(seconds0) # 永久显示 marker_array.markers.append(cube) # 2. 蓝色箭头ID1指向立方体 arrow Marker() arrow.header.frame_id map arrow.header.stamp stamp arrow.ns basic_shapes arrow.id 1 arrow.type Marker.ARROW arrow.action Marker.ADD arrow.pose Pose(positionPoint(x0.0, y0.0, z0.5), orientationQuaternion(w1.0)) arrow.scale.x 1.0 # 长度1m arrow.scale.y 0.1 # 半径0.1m arrow.scale.z 0.1 arrow.color.r 0.0 arrow.color.g 0.0 arrow.color.b 1.0 arrow.color.a 1.0 marker_array.markers.append(arrow) # 3. 绿色文字ID2标注立方体 text Marker() text.header.frame_id map text.header.stamp stamp text.ns basic_shapes text.id 2 text.type Marker.TEXT_VIEW_FACING text.action Marker.ADD text.pose Pose(positionPoint(x1.0, y0.0, z1.2)) # 立方体上方 text.text 目标物体 text.scale.z 0.2 # 文字大小0.2m text.color.r 0.0 text.color.g 1.0 text.color.b 0.0 text.color.a 1.0 marker_array.markers.append(text) self.publisher_.publish(marker_array) self.get_logger().info(发布3个可视化标记) def main(argsNone): rclpy.init(argsargs) node MarkerVisualizer() rclpy.spin(node) node.destroy_node() rclpy.shutdown() if __name__ __main__: main()2.2 C 示例批量发布点云折线#include rclcpp/rclcpp.hpp #include visualization_msgs/msg/marker_array.hpp #include geometry_msgs/msg/point.hpp using namespace std::chrono_literals; class MarkerArrayPublisher : public rclcpp::Node { public: MarkerArrayPublisher() : Node(marker_array_publisher) { publisher_ this-create_publishervisualization_msgs::msg::MarkerArray(/visualization_markers, 10); timer_ this-create_wall_timer(1s, std::bind(MarkerArrayPublisher::publish_markers, this)); } private: void publish_markers() { auto marker_array visualization_msgs::msg::MarkerArray(); auto stamp this-get_clock()-now(); // 发布折线ID05个点 auto line_strip visualization_msgs::msg::Marker(); line_strip.header.frame_id map; line_strip.header.stamp stamp; line_strip.ns path; line_strip.id 0; line_strip.type visualization_msgs::msg::Marker::LINE_STRIP; line_strip.action visualization_msgs::msg::Marker::ADD; line_strip.pose.orientation.w 1.0; line_strip.scale.x 0.05; // 线宽0.05m line_strip.color.r 1.0; // 黄色 line_strip.color.g 1.0; line_strip.color.b 0.0; line_strip.color.a 1.0; line_strip.lifetime rclcpp::Duration(0); // 添加5个顶点形成矩形路径 std::vectorstd::pairdouble, double points {{0,0}, {2,0}, {2,1}, {0,1}, {0,0}}; for (auto [x, y] : points) { geometry_msgs::msg::Point p; p.x x; p.y y; p.z 0.0; line_strip.points.push_back(p); } marker_array.markers.push_back(line_strip); publisher_-publish(marker_array); RCLCPP_INFO(this-get_logger(), 发布路径折线); } rclcpp::Publishervisualization_msgs::msg::MarkerArray::SharedPtr publisher_; rclcpp::TimerBase::SharedPtr timer_; }; int main(int argc, char * argv[]) { rclcpp::init(argc, argv); rclcpp::spin(std::make_sharedMarkerArrayPublisher()); rclcpp::shutdown(); return 0; }3. 关键技巧与避坑坐标系frame_id必设错设会导致元素 “消失”常见值map全局地图、base_link机器人本体、odom里程计、laser_frame雷达坐标系。颜色必须设 alpha透明度不设a1.0可能导致完全透明看不见。生命周期控制临时标记设lifetime如 5 秒避免 RViz 残留永久显示设0。批量用 MarkerArray多个元素一次性发布效率远高于多次发布单个 Marker。删除元素单个删除设actionDELETEnsid整组删除设actionDELETEALLns。文字大小TEXT_VIEW_FACING仅通过scale.z控制大小x/y无效。三action_msgsaction_msgs不是动作本身而是所有 Action 通信的 “底层规则、状态、ID 管理”。geometry_msgs管位置visualization_msgs管 RViz 显示action_msgs管Action 通信的状态、目标 ID、取消机制它不包含业务数据如目标点、速度、路径只负责给每个任务发一个唯一 ID告诉你任务当前状态等待 / 运行 / 成功 / 失败 / 取消提供取消任务的服务1. 它包含哪些内容官方完整定义action_msgs一共就3 个消息 1 个服务1.1 GoalInfo.msg目标身份证每个 Action 目标都会分配一个唯一 IDunique_identifier_msgs/UUID goal_id # 唯一ID一串随机码 builtin_interfaces/Time stamp # 时间戳1.2 GoalStatus.msg目标状态这是action_msgs 最重要的消息表示任务当前处于什么状态。GoalInfo goal_info int8 status # 状态常量 int8 STATUS_UNKNOWN 0 int8 STATUS_ACCEPTED 1 # 已接受 int8 STATUS_EXECUTING 2 # 执行中 int8 STATUS_CANCELING 3 # 正在取消 int8 STATUS_SUCCEEDED 4 # 成功完成 int8 STATUS_ABORTED 5 # 异常失败 int8 STATUS_CANCELED 6 # 已取消 int8 STATUS_PENDING 7 # 排队等待状态机流程最标准流程PENDING → ACCEPTED → EXECUTING → SUCCEEDED / ABORTED / CANCELED1.3 GoalStatusArray.msg发布多个目标的状态列表GoalStatus[] status_list1.4 CancelGoal.srv取消目标服务请求客户端用来取消正在运行的任务。1.4 CancelGoal.srv取消目标服务 客户端用来取消正在运行的任务。响应bool success GoalInfo[] canceled_goals2. 为什么需要 action_msgs因为 Action 通信有 3 个特点长时任务不是立刻完成可取消有反馈、有结果、有状态这些机制全部由 action_msgs 提供底层支持。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询