工程创建
- 创建并进入catkin文件夹
wilson@ubuntu:~/code$ mkdir catkin
wilson@ubuntu:~/code$ cd catkin/
- 创建并进入src文件夹
wilson@ubuntu:~/code/catkin$ mkdir src
wilson@ubuntu:~/code/catkin$ cd src/
wilson@ubuntu:~/code/catkin/src$
- 在src目录下初始化工作空间
wilson@ubuntu:~/code/catkin/src$ catkin_init_workspace
Creating symlink "/home/wilson/code/catkin/src/CMakeLists.txt" pointing to "/opt/ros/melodic/share/catkin/cmake/toplevel.cmake"
wilson@ubuntu:~/code/catkin/src$ ls
CMakeLists.txt
- 切换回工程目录并执行catkin_make生成开发环境
wilson@ubuntu:~/code/catkin/src$ cd ..
wilson@ubuntu:~/code/catkin$ ls
src
wilson@ubuntu:~/code/catkin$ catkin_make
Base path: /home/wilson/code/catkin
Source space: /home/wilson/code/catkin/src
Build space: /home/wilson/code/catkin/build
Devel space: /home/wilson/code/catkin/devel
Install space: /home/wilson/code/catkin/install
- 将程序激活保证终端其他地方可用
wilson@ubuntu:~/code/catkin$ ls
build devel src
wilson@ubuntu:~/code/catkin$ source devel/setup.bash
- 进入src文件夹并创建hello_demo工程,工程依赖于std_msgs rospy roscpp
wilson@ubuntu:~/code/catkin/src$ catkin_create_pkg hello_demo std_msgs rospy roscpp
Created file hello_demo/package.xml
Created file hello_demo/CMakeLists.txt
Created folder hello_demo/include/hello_demo
Created folder hello_demo/src
Successfully created files in /home/wilson/code/catkin/src/hello_demo. Please adjust the values in package.xml.
编写代码
- 在工程目录src中编写订阅者和发布者代码
wilson@ubuntu:~/code/catkin/src/hello_demo/src$ ls
publisher.cpp subscriber.cpp
publisher.cpp 节点名称为publisher,会话名称为chatter
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
int main(int argc, char **argv)
{
ros::init(argc, argv, "publisher");
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
ros::Rate loop_rate(10);
int count = 0;
while(ros::ok())
{
std_msgs::String msg;
std::stringstream ss;
ss<<"hello, "<<count;
msg.data = ss.str();
ROS_INFO("%s", msg.data.c_str());
chatter_pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
++count;
}
return 0;
}
subscriber.cpp 节点为subscriber,会话为chatter
#include "ros/ros.h"
#include "std_msgs/String.h"
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO("I heard: [%s]", msg->data.c_str());
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "subscriber");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
ros::spin();
return 0;
}
- 修改CMakelist.txt文件
wilson@ubuntu:~/code/catkin/src/hello_demo$ ls
CMakeLists.txt include package.xml src
wilson@ubuntu:~/code/catkin/src/hello_demo$ gedit CMakeLists.txt
添加以下定义
add_executable(publisher src/publisher.cpp)
target_link_libraries(publisher ${catkin_LIBRARIES})
add_dependencies(publisher hello_demo_generate_messages_cpp)
add_executable(subscriber src/subscriber.cpp)
target_link_libraries(subscriber ${catkin_LIBRARIES})
add_dependencies(subscriber hello_demo_generate_messages_cpp)
- 切换回工作目录
wilson@ubuntu:~/code/catkin/src/hello_demo$ cd ..
wilson@ubuntu:~/code/catkin/src$ cd ..
wilson@ubuntu:~/code/catkin$ ls
build devel src
- 编译
wilson@ubuntu:~/code/catkin$ catkin_make
- 将程序激活保证终端其他地方可用
wilson@ubuntu:~/code/catkin$ source devel/setup.bash
测试验证
- 运行核心节点
wilson@ubuntu:~/code/catkin$ roscore - 运行发布者
wilson@ubuntu:~/code/catkin$ rosrun hello_demo publisher
[ INFO] [1579615615.820928731]: hello, 0
[ INFO] [1579615615.921658189]: hello, 1
- 运行订阅者
wilson@ubuntu:~/code/catkin$ rosrun hello_demo subscriber
[ INFO] [1579615616.122224767]: I heard: [hello, 3]
[ INFO] [1579615616.221957346]: I heard: [hello, 4]