工程創建
- 創建並進入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]