本文共 2218 字,大约阅读时间需要 7 分钟。
最近在学习无为斋主的《机器人ROS开发实践》中的ROS编程部分时,遇到了编译运行的麻烦。为了便于记录和后续学习,决定详细梳理两种开发ROS程序的方法。
在Ubuntu 18.04 LTS或20.04 LTS系统上安装带有ROS插件的Qt Creator。推荐使用18.04版本,因为20.04尚未正式支持ROS插件。
uGet+aria2下载线下安装文件qtcreator-ros-bionic-latest-offnline-installer.run。mkdir /opt/QtCreator && cd /opt/QtCreatorsudo ./qtcreator-ros-bionic-latest-offnline-installer.run
按照安装界面指引完成安装。
catkin_ws,工作空间路径为~/ros/catkin_ws。src目录,选择“New File”。roscpp、rospy和std_msgs。example_01下的src目录,选择“Add New”。hello.cpp。hello.cpp文件,发布ROS信息。example_01和目标文件hello。roscore,运行rosrun example_01 hello命令。mkdir -p ~/ros/catkin_ws/src && cd ~/ros/catkin_ws/srccatkin_init_workspace
cd ~/ros/catkin_ws/srccatkin_create_pkg example_01 rospy roscpp std_msgs
example_01/src目录下创建hello.cpp文件:#includeint main(int argc, char **argv) { ros::init(argc, argv, "hello"); ros::NodeHandle nh; ROS_INFO("Hello world!"); return 0;}
example_01/CMakeLists.txt:cmake_minimum_required(VERSION 2.8.3)project(example_01)find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs)include_directories(${catkin_INCLUDE_DIRS})add_executable(hello src/hello.cpp)target_link_libraries(hello ${catkin_LIBRARIES}) example_01/package.xml:example_01 0.1.0 Example package simon Apache 2.0 simon catkin roscpp rospy std_msgs roscpp rospy std_msgs
cd ~/ros/catkin_ws/export ROS_PARALLEL_JOBS='-j8 -l8' catkin_make
catkin_make -D CMAKE_INSTALL_PREFIX=/home/simon/melodic install
1.启动节点管理器:
roscore
2.运行节点:
cd ~/ros/catkin_ws/devel/setup.sh && rosrun example_01 hello
两种方法各有优势,Qt Creator提供了更直观的开发体验,而catkin命令则适合脚本化和大规模项目。选择取决于个人的开发环境和需求。
转载地址:http://xkvhz.baihongyu.com/