Ros机器人之(四)发布Publisher消息
从基础学习,发布消息Publisher
首先创建功能包,命令+名称+功能包依赖
catkin_create_pkg learning-topic roscpp rospy std_msgs geometry_msgs turtlesim
src文件夹下创建一个publisher.cpp
touch publisher.cpp
内容如下:
/*
publisher topic
*/
#include<ros/ros.h>
#include<geometry_msgs/Twist.h>
int main(int argc, char**argv){
//初始节点化
ros::init(argc,argv,"publisher");
//创建节点句柄
ros::NodeHandle n;
//创建发布者,发布的话题及队列长度用于缓存
ros::Publisher turtle_vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel",10);
//循环频率
ros::Rate loop_rate(10);
int count=0;
//循环
while(ros::ok())
{
//定义vek_msg 对象
geometry_msgs::Twist vel_msg;
//线速度
vel_msg.linear.x = 0.5;
vel_msg.angular.z = 0.2;
//发布出去
turtle_vel_pub.publish(vel_msg);
//显示消息
ROS_INFO("Publish command[%0.2f,%0.2f]",vel_msg.linear.x,vel_msg.angular.z);
//按照设置的频率循环
loop_rate.sleep();
}
return 0;
}
完成后在Cmakelists中添加
#把cpp编译成publisher
add_executable(publisher src/publisher.cpp)
#将publisher进行接口链接
target_link_libraries(publisher ${catkin_LIBRARIES})
完成后,工作区的根目录下进行编译并设置环境变量。
catkin_make
source devel/setup.bash
完成后运行小乌龟
roscore
rosrun turtlesim turtlesim_node
运行发布节点
rosrun learning_topic publisher
来源:CSDN
作者:爆炒小肥牛
链接:https://blog.csdn.net/weixin_44747240/article/details/104845340