The best way to learn ROS is to use it. The ROS official website has a Chinese version of the tutorial . After installing ROS, write the first program "Hello world".
1 Create a workspace
Open a terminal window and enter the following command.
mkdir -p helloworld_ws/src
cd helloworld_ws
catkin_make
The command creates a workspace called "helloworld_ws" with a subdirectory called "src" and compiles it. The compile command can also be replaced by catkin build. Once you start using one, you will need to use it all the time. After the catkin build command is compiled, there will be an additional .catkin_tools hidden folder compared to the catkin_make command.
2. Create a function package
cd src
catkin_create_pkg helloworld roscpp rospy std_msgs
Enter the "src" file, set the package name to "helloworld", and add the "roscpp rospy std_msgs" dependency
3 Edit source files
cd helloworld/src
pluma helloworld_c.cpp
Enter the "src" file in the package, use the pluma editor to create and edit a file named "helloworld_c.cpp". Enter the following code in the file.
#include <ros/ros.h>
int main(int argc,char *argv[]){
ROS_INFO("Hello world!");
return 0;
}
The code includes the "ros.h" header file and then outputs "Hello world!" in the main function.
4 Edit the configuration file
Edit the "CMakeLists.txt" file in the "helloworld" package (helloworld_ws/src/helloword/src/CMakeLists.txt)
Modify line 136 or add the following code below it.
aux_source_directory(./src SRCS)
add_executable(${PROJECT_NAME}_node ${SRCS})
The code creates an executable file named "package name_node" from all the source files in the "/src" directory. The compiled executable file is in "helloworld_ws/devel/lib/hellowrold".
Modify line 149 or add the following code below it.
target_link_libraries(${PROJECT_NAME}_node
${catkin_LIBRARIES}
)
The code links the generated executable file with the catkin library file.
5 Start the kernel
Reopen a terminal and enter.
roscore
This command starts the ROS kernel. In this example, it doesn't matter if it is not started.
6 Compiled
Reopen a terminal and enter.
cd ~/helloworld_ws
catkin_make
Go into the "helloworld_ws" file and compile it.
7 Run
source ./devel/setup.bash
rosrun helloworld helloworld_node
"source devel/setup.bash" makes the environment variable script in the devel directory take effect, otherwise the helloworld package will not be found.
"rosrun helloworld helloworld_node" runs the helloworld_node executable file under the helloworld package.
Finally, you can see the successful output "Hello world!"
question
It is very troublesome to run ROS using operating instructions throughout the process. Is there a good IDE?