概述
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <stdlib.h>
int main ( int argc , char ** argv ) {
ros::init (argc, argv, "publish_velocity") ;
ros::NodeHandle nh ;
ros::Publisher pub = nh.advertise <geometry_msgs::Twist >(
"turtle1/cmd_vel" , 1000 ) ;
srand(time(0));
ROS_INFO_STREAM( "Sending random velocity : ") ;
ros::Rate rate(2) ;
while (ros::ok()) {
geometry_msgs::Twist msg;
msg.linear.x = double (rand()) / double (RAND_MAX);
msg.angular.z = 2*double ( rand())/double (RAND_MAX)-1;
pub.publish ( msg ) ;
// Send a message to rosout with the details .
ROS_INFO_STREAM( "Sending random velocity command : "
<< " linear=" << msg.linear.x
<< " angular=" << msg.angular.z) ;
// Wait untilit's time for another iterationet.
rate.sleep () ;
}
}
ros::Publisher pub = nh.advertise <geometry_msgs::Twist >(
"turtle1/cmd_vel" , 1000 ) ; 创建一个发布者
发布消息:
pub.publish ( msg ) ;
一.CMakeList.txt 中增加如下:
find_package(catkin REQUIRED COMPONENTS roscpp geometry_msgs)
catkin_package()
include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(pubvel pubvel.cpp)
target_link_libraries(pubvel ${catkin_LIBRARIES})
二.package.xml 配置增加如下:
<build_depend>roscpp</build_depend>
<exec_depend>roscpp</exec_depend>
<build_depend>geometry_msgs</build_depend>
<exec_depend>geometry_msgs</exec_depend>
三。运行程序:
最后
以上就是俭朴万宝路为你收集整理的ROS 发布者程序,控制小龟行走的全部内容,希望文章能够帮你解决ROS 发布者程序,控制小龟行走所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复