第一步:创建 ROS 工作空间 (Workspace)
首先,我们需要一个专门存放代码的地方。
创建文件夹:
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
初始化工作空间:
catkin_init_workspace
cd ~/catkin_ws
catkin_make
若遇到以下错误提示:
ubuntu@ubuntu:~/catkin_ws$ catkin_make Base path: /home/ubuntu/catkin_ws Source space: /home/ubuntu/catkin_ws/src Build space: /home/ubuntu/catkin_ws/build Devel space: /home/ubuntu/catkin_ws/devel Install space: /home/ubuntu/catkin_ws/install #### #### Running command: "cmake /home/ubuntu/catkin_ws/src -DCATKIN_DEVEL_PREFIX=/home/ubuntu/catkin_ws/devel -DCMAKE_INSTALL_PREFIX=/home/ubuntu/catkin_ws/install -G Unix Makefiles" in "/home/ubuntu/catkin_ws/build" #### -- The C compiler identification is GNU 9.4.0 -- The CXX compiler identification is unknown -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done CMake Error at CMakeLists.txt:6 (project): No CMAKE_CXX_COMPILER could be found. Tell CMake where to find the compiler by setting either the environment variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path to the compiler, or to the compiler name if it is in the PATH. -- Configuring incomplete, errors occurred! See also "/home/ubuntu/catkin_ws/build/CMakeFiles/CMakeOutput.log". See also "/home/ubuntu/catkin_ws/build/CMakeFiles/CMakeError.log". Invoking "cmake" failed
解决方法:安装必要的构建工具。
我们只需要运行一行命令把 C++ 编译器和相关的开发工具补齐即可:
sudo apt update
sudo apt install build-essential
catkin_make
创建你的控制包
cd ~/catkin_ws/src
catkin_create_pkg offboard_control roscpp std_msgs mavros_msgs
编写 C++ 代码:
cd offboard_control/src
nano main.cpp
这里你就可以写自己的代码了,可以请求 AI 辅助生成例程,首先确保能跑通即可。
修改 CMakeLists.txt: 别忘了去 ~/catkin_ws/src/offboard_control/CMakeLists.txt 的末尾加上那两行编译规则:
add_executable(offboard_node src/main.cpp)
target_link_libraries(offboard_node ${catkin_LIBRARIES})

