发布运动消息
#include <ros/ros.h> //类似 C 语言的 stdio.h
#include <geometry_msgs/Twist.h> //要用到 msg 中定义的数据类型
#include <stdlib.h>
int main(int argc,char **argv){
ros::init(argc,argv,"go"); //解析参数,命名节点为 go
ros::NodeHandle nh; //创建句柄,相当于一套工具,可以实例化 node,并且对 node 进行操作
ros::Publisher pub = nh.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel",1000);//创建 publisher 对象
ros::Rate loop_rate(1.0); //创建 rate 对象,定义循环发布的频率,1 HZ
while(ros::ok())
{
geometry_msgs::Twist msg;
msg.linear.x = 1.03;
msg.angular.z = 1.01;
ROS_INFO("msg.linear.x = %f,msg.angular.z= %f",msg.linear.x,msg.angular.z); //打印函数,类似 printf()
pub.publish(msg); //发布消息
loop_rate.sleep(); //根据定义的发布频率,sleep
}
return 0;
}
CMakeLists文件中需要修改的
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
geometry_msgs
)
add_executable(go /home/wt/catkin_ws/src/test/src/go.cpp)
target_link_libraries(go ${catkin_LIBRARIES})