Table of contents
1. Implement a code step
1. C++ code implementation steps
Steps to create a function package in catkin_ws/src/
In fact, you can create a workspace arbitrarily. For example, create a new space named catkin_ws_new,
then compile and place code in this workspace.
-
catkin_ws/src/
Open terminal under -
Terminal input
catkin_create_pkg [XXX] roscpp rospy std_msgs...
[XXX]
Define the function package name for yourself, so that you can enter the function package name later and execute the compiled function package.
-
After creating the function package, enter the src of the function package and copy the code into it (note the suffix)
-
Go back
catkin_ws/src/[XXX]/
(that is, return to the CMakeLists.txt of the newly created function package)CMakeLists.txt
and configure the compilation rules of the copied code.
For example:
add_executable(velocity_publisher src/velocity_publisher.cpp) target_link_libraries(velocity_publisher ${catkin_LIBRARIES})
Note here: if the placed code is such as velocity_publisher.cpp, then the two lines of compilation rules corresponding to the above need to correspond one to one. , otherwise a compilation error occurs.
Added to the end of "Build" and before "Install"
There are two main points in configuring code compilation rules:
setting the code to be compiled and the generated executable file;
and setting the link library.
- Compile and run the code.
Go backcatkin_ws/
to the terminal and enter
catkin_make
(6. Set environment variables source devel/setup.bash)
(7. roscore)
2. python code implementation steps
- When
catkin_ws/src/
creating a function package (that is, opening the terminalcatkin_ws/src/
under )
terminal input
catkin_create_pkg [XXX] roscpp rospy std_msgs...
[XXX]
Define the function package name for yourself, so that you can enter the function package name later and execute the compiled function package.
- After creating the function package,
catkin_ws/src/[XXX]/
create a new folder namedscripts
, this folder is dedicated to python code files - Open the terminal under the path
catkin_ws/src/[XXX]/scripts/
and enter
python [XXXXXXX].py
[XXXXXX].py is the python code file to be executed
Question: [pkg name] [py file]
rosrun demo_py hello.py
2. Steps to run compiled code
-
Under terminal
cd ~/catkin_ws
-
Then enter
rosrun [XXXXXXX] [XXXXXXX]
第一个[XXXXXXX]
: The input is catkin_ws/src/
the name of the function package created inside, such as " learning_test
"
第一个[XXXXXXX]
: The input is catkin_ws/src/
the 第一个[XXXXXX]/src/
name of the compiled CPP code inside. If " offb_node.cpp
" code file, then 第二个[XXXXXX]
it is " offb_node
"