eclipse 搭建 ROS 工作空间
程序员文章站
2024-03-15 14:19:11
...
文章目录
目的
在 Eclipse 上搭建 Ros 开发环境
环境
ubuntu18.04
Eclipse IDE for C/C++ Developers (includes Incubating components) Version: 2020-09 (4.17.0)
ROS2 melodic
步骤:
创建工作空间
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
cd ~/catkin_ws
catkin_make
创建工作包
cd ~/catkin_ws/src
catkin_create_pkg base_controller std_msgs rospy roscpp
利用catkin_make命令,建立一个eclipse的项目
cd ~/catkin_ws
catkin_make --force-cmake -G"Eclipse CDT4 - Unix Makefiles"
awk -f $(rospack find mk)/eclipse.awk build/.project > build/.project_with_env && mv build/.project_with_env build/.project
导入项目
启动软件 Eclipse,选择工作空间->Launch,选择File --> Import --> General --> Existing Projects into Workspace->next->Select root directory: ->Finish
创建代码文件
右键上图 src->new->source file->Source file: 填入hello.cpp
#include "ros/ros.h"
int main(int argc, char **argv) {
ros::init(argc, argv, "hello");
ros::NodeHandle nh;
ros::Rate loop_rate(10);
int count = 0;
while (ros::ok()) {
ROS_INFO_STREAM("hello world" << count);
ros::spinOnce(); // Allow ROS to process incoming messages
loop_rate.sleep(); // Sleep for the rest of the cycle
count++;
}
return 0;
}
# 在 CMakeLists.txt 中的 ## Bulid ## 部分添加
## Declare a cpp executable
add_executable(hello src/hello.cpp)
## Specify libraries to link a library or executable target against
target_link_libraries(hello ${catkin_LIBRARIES})
加入环境变量
终端中输入:source ~/catkin_ws/devel/setup.bash
可借助 echo $ROS_PACKAGE_PATH 查看环境变量中是否存在:/home/fei/catkin_ws/src
编译节点
快捷键:ctrl+b
在Eclipse中运行节点
Run --> Run configurations --> 双击:C/C++ Application->进入 bulid Configuration->C/C++Application: 键入 /home/fei/catkin_ws/devel/lib/base_controller/hello
进入 Environment 页面->Add-> 终端中键入:echo $LD_LIBRARY_PATH,得到路径 -> 将路径输入如下图中的环境变量(古月居书中让输入环境变量:ROS_ROOT、ROS_MASTER_URI,但是发现没有也没影响) -> Apply -> 终端中键入 roscore -> Run -> 程序开始运行:如下图
Console 窗口中显示乱码( ROS输出编码和Eclipse控制窗口编码不同)
安装ANSI escape sequences插件:
help->Install New Software->点击 Add -> 输入:ANSI escape sequences;http://www.mihai-nita.net/eclipse ->Finish
调试程序
参考
https://jingyan.baidu.com/article/08b6a59199a3a714a80922ae.html
《ROS机器人开发实践(机器人设计与制作)》- 古月居