ROS 最简单地输出helloworld
文章目录
1.构建工作区
1.1创建一个文件夹rosTest,文件夹内创建一个src文件夹。
(注意第二个文件夹必须名字要是src)
1.2进入rosTest文件夹,执行catkin_make
执行完后会在目录下生成build和devel文件夹
1.3进入src目录
执行
catkin_create_pkg xxxxx roscpp rospy std_msgs
其中xxxxx是你自己更改地名字,比如我这里用helloworld
src目录下就会生成对应的cmake和helloworld
到这基础工作区就构建完了
2.编写文件
2.1 编写helloworld.cpp
在helloworld/src目录下新建一个helloworld.cpp,编写以下内容
#include "ros/ros.h"
int main(int argc,char *argv[])
{
ros::init(argc,argv,"hello_1");
ROS_INFO("HELLO WORLD");
return 0;
}
2.2 更改cmake内容
同样在helloworld目录下找到CMakeLists.txt文件
加入以下内容(cmake知识不做讲解)
add_executable(myhello src/helloworld.cpp)
target_link_libraries(myhello
${catkin_LIBRARIES}
)
3.编译
以上弄完之后,回到最初地rosTest目录
执行命令
catkin_make
没有问题的话会成功编译
出现错误就修改
4.运行
4.1 开启ros核心
新开一个终端,执行命令
roscore
4.2 执行代码
仍在工作目录下执行命令,下面这句话是固定的
source ./devel/setup.bash
再执行
rosrun helloworld myhello
其中helloworld是src目录下的那个文件夹,myhello是cmake生成的
就可以看到输出了
这部分完整截图如下