目标:从0-1配置一个ROS项目,实现hello,world打印,在此基础上进行功能开发。
步骤1:创建工作空间:
mkdir -p ros_workspace/src
cd ros_workspace
对工作空间进行初始化:
catkin_make
source devel/setup.bash
步骤2:创建新的ROS包:
导航到src目录:
cd src
创建package包,依赖roscpp和std_msgs:
catkin_create_pkg hello_world std_msgs roscpp
切换回工作空间的根目录并编辑:
cd ..
catkin_make
步骤3:创建一个c++脚本
在hello_world包下创建talker文件:
cd src/hello_world/src
touch talker.cpp
加入talker.cpp的内容:
#include "ros/ros.h"
#include "std_msgs/String.h"int main(int argc, char **argv)
{ros::init(argc, argv, "talker");ros::NodeHandle n;ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);ros::Rate loop_rate(1);while (ros::ok()){std_msgs::String msg;std::stringstream ss;ss << "hello, world " << ros::Time::now();msg.data = ss.str();ROS_INFO("%s", msg.data.c_str());chatter_pub.publish(msg);ros::spinOnce();loop_rate.sleep();}return 0;
}
步骤4:更新CMakeLists.txt
cmake_minimum_required(VERSION 2.8.3)
project(hello_world)find_package(catkin REQUIRED COMPONENTSroscppstd_msgs
)catkin_package()include_directories(${catkin_INCLUDE_DIRS}
)add_executable(talker src/talker.cpp)target_link_libraries(talker${catkin_LIBRARIES}
)
步骤5:编译包,运行节点
cd ros_workspace
catkin_make
启动
roscore
source ros_workspace/devel/setup.bash
rosrun hello_world talker
输出:
[ INFO] [1621619326.123456]: hello, world 1621619326.123456