Build catkin workspace
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
cd ~/catkin_ws/
catkin_make
Configure environment variables
echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc
source ~/.bashrc
Check environment variables
echo $ROS_PACKAGE_PATH
Encountered an error: the above operation does not need to be operated with sudo, but due to beginners, there may be errors caused by improper operation
Error code reported when ros catkin_made:
The specified base path “/home/ubuntu/catkin_ws” contains a CMakeLists.txt but “catkin_make” must be invoked in the root of workspace…..
The reason for this is that when catkin_init_workspace, the following sentence appears:
Creating symlink “/home/ubuntu/catkin_ws/CMakeLists.txt” pointing to “/opt/ros/indigo/share/catkin/cmake/toplevel.cmake”
Link CMakeLists.txt under the current workspace with cmake under /ros/
Workaround:
unlink /home/youpath/catkin_ws/CMakeLists.txt
Publish a topic case:
Create feature pack
cd ~/catkin_ws/src
catkin_create_pkg package1 roscpp rospy std_msgs
Use the command cd ~/ catkin_ws /src to enter the code space and create a function package in the code space
Use catkin_create_pkg to create a function package named package1. The support libraries of the function package are roscpp rospy and std_msgs. The command format for creating a function package is catkin_create_pkg <package_name> [depends1] [depends2] [depends3]. At this time, there will be two files under the ~/ catkin_ws /src path, one is CMakeLists.txt and the other is our function package package, and there will be four files under the package path, which are the include folders (used to store similar h header file), src folder (used to store cpp files), CMakeLists.txt (specify compilation rules), package.xml (support package related information, if subsequent files need to be expanded, add them in this file Relevant libraries), so far the function package has been created, and we need to modify CMakeLists.txt to update our compilation rules, but before that, we need to create a new cpp source code.
cd ~/catkin_ws/src/package1/src
touch pub_node_test.cpp
Enter the ~/ catkin_ws /src/package1/src folder, use the touch command to create a cpp file, and enter the following content in the cpp file.
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
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); //设定话题名称为chatter,并且缓存队列长度为1000,用于平衡硬件发送速度
ros::Rate loop_rate(10); //设置发送波特率为10HZ,与后面的loop_rate.sleep();相互呼应,如果下一帧信息不满足频率要求系统则会延时
int count = 0;
while(ros::ok())
{
std_msgs::String msg;
std::stringstream ss;
ss<<"first example!"<<count;
msg.data = ss.str();
ROS_INFO("%s",msg.data.c_str());
chatter_pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
++count;
}
return 0;
}
The above code creates a node named talker, which publishes messages through the topic "chatter"
Then modify the CMakeList.txt file in the package1 function package. Open the CMakeLists.txt file and enter the following content in it.
add_executable(pub_node src/pub_node_test.cpp)
target_link_libraries(pub_node ${catkin_LIBRARIES})
In this way, an executable file named pub_node can be generated under the devel/lib folder. The general format of these two compiled codes is as follows: add_executable([executable file name] src/[cpp file name]) target_link_libraries([executable file name] execute filename] ${catkin_LIBRARIES})
Go back to the workspace and use catkin_make to compile the entire workspace
cd ~/catkin_ws
catkin_make
Open a new terminal and use the following command to run the executable file just compiled
rosrun package1 pub_node
The display interface is as follows:
Workspace and message created successfully! Using rostopic list, you can see that a new topic named chatter will appear.