【ROS学习】ROS中的Message


# ROS学习:ROS中的message使用方法 [代码]([Copy2000/ROS_msg (github.com)](https://github.com/Copy2000/ROS_msg))csdn上面的文章或多或少有一点毛病,不是catkin_package有问题就是CMakeLists配置出现问题。这里我们给出csdn上[最多人看的一篇]([ROS教程3 ROS自定义msg类型及使用 - MKT-porter - 博客园 (cnblogs.com)](https://www.cnblogs.com/gooutlook/p/7401590.html)). 我看过之后真正说明白的只有[here]([机器人操作系统ROS:从入门到放弃(一) 发布接收消息 - 简书 (jianshu.com)](https://www.jianshu.com/p/06e0e40cf6da)). -------- 创建的方法: ``` cd ~/catkin_ws/src catkin_create_pkg {package} std_msgs rospy roscpp cd .. catkin_make ``` 实现: * publisher: * subscriber: # geometry_msgs ## [geometry_msgs::PoseStamped](http://docs.ros.org/en/api/geometry_msgs/html/msg/PoseStamped.html) ``` Raw Message Definition: # A Pose with reference coordinate frame and timestamp Header header Pose pose Compact Message Definition: std_msgs/Header header geometry_msgs/Pose pose ``` ### [std_msgs](http://docs.ros.org/en/api/std_msgs/html/index-msg.html)/Header Message 引用:[here](https://blog.csdn.net/qq_18676517/article/details/109270525) ``` # Standard metadata for higher-level stamped data types. # This is generally used to communicate timestamped data # in a particular coordinate frame. # # sequence ID: consecutively increasing ID uint32 seq #Two-integer timestamp that is expressed as: # * stamp.sec: seconds (stamp_secs) since epoch (in Python the variable is called 'secs') # * stamp.nsec: nanoseconds since stamp_secs (in Python the variable is called 'nsecs') # time-handling sugar is provided by the client library time stamp #Frame this data is associated with string frame_id ``` ### [geometry_msgs](http://docs.ros.org/en/api/geometry_msgs/html/index-msg.html)/Pose Message ``` Raw Message Definition # A representation of pose in free space, composed of position and orientation. Point position Quaternion orientation Compact Message Definition geometry_msgs/Point position geometry_msgs/Quaternion orientation ``` #### [geometry_msgs](http://docs.ros.org/en/api/geometry_msgs/html/index-msg.html)/Point Message ``` # This contains the position of a point in free space float64 x float64 y float64 z ``` #### [geometry_msgs](http://docs.ros.org/en/api/geometry_msgs/html/index-msg.html)/Quaternion Message ``` # This represents an orientation in free space in quaternion form. float64 x float64 y float64 z float64 w ``` ## [geometry_msgs](http://docs.ros.org/en/melodic/api/geometry_msgs/html/index-msg.html)/Twist Message cite:[here](https://blog.csdn.net/ktigerhero3/article/details/80740920) ``` # This expresses velocity in free space broken into its linear and angular parts. Vector3 linear Vector3 angular ``` ### [geometry_msgs](http://docs.ros.org/en/melodic/api/geometry_msgs/html/index-msg.html)/Vector3 Message ``` # This represents a vector in free space. # It is only meant to represent a direction. Therefore, it does not # make sense to apply a translation to it (e.g., when applying a # generic rigid transformation to a Vector3, tf2 will only apply the # rotation). If you want your data to be translatable too, use the # geometry_msgs/Point message instead. float64 x float64 y float64 z ``` 使用方法: ```c++ int main(int argc,char** argv) { ros::init(argc,argv,"init"); ros::NodeHandle nh; ros::Publisher Pub = nh.advertise("/hector1/cmd_vel",10) // 初始化geometry_msgs::Twist类型的消息 geometry_msgs::Twist vel_msg; vel_msg.linear.x=0.2; vel_msg.angular.z=0.2; // 设置循环的频率 ros::Rate loop_rate(10); while(ros::ok) { ros::spinonce(); Pub.publish(Twist); ROS_INFO("Publsh velocity command[%0.2f m/s, %0.2f rad/s]", vel_msg.linear.x, vel_msg.angular.z); loop_rate.sleep(); } return 0; } ``` linear.x指向机器人前方,linear.y指向左方,linear.z垂直向上满足右手系,平面移动机器人常常linear.y和linear.z均为0; angular.z代表平面机器人的角速度,因为此时z轴为旋转轴 ## sensor_msgs ![sensor_msgs](【ROS学习】ROS中的Message/image-20220405153718538.png) * [(71条消息) ROS下sensor_msgs::ImagePtr到sensor_msgs::Image之间的转换_Tom Hardy的博客-CSDN博客](https://blog.csdn.net/qq_29462849/article/details/88795250):ROS下面好像没办法直接定义sensor_msgs/ImagePtr,而是sensor_msgs/Image。然而在从Mat格式转到ROS下的图像格式时,一般都是ImagePtr,这样在发布信息的时候就需要一次转换啦,其实也很简单! ```c++ sensor_msgs::ImagePtr msg= cv_bridge::CvImage(std_msgs::Header(), "bgr8", src).toImageMsg(); detect_msg.image_raw=*msg; 这里自定义的detect_msg.image_raw就是sensor_msgs/Image格式。 ```

文章作者: copy
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 copy !
  目录