JETSON TK1 ~ 基于eclipse下开发ROS
此文档是在PC端开发后移植到TK1,并非在TK1上安装eclipse
官方使用IDE开发的文档: http://wiki.ros.org/IDEs
一:安装eclipse
1、下载eclipse安装包: eclipse
2、下载JDK安装包: JDK
3、按照下面步骤执行
1)把下载的文件移动到opt目录下
sudo mv eclipse-SDK-4.2.1-linux-gtk.tar.gz /opt/
2)解压安装包
cd /opt/ sudo tar xzvf eclipse-SDK-4.2.1-linux-gtk.tar.gz
3)在/usr/share/applications目录创建桌面快捷方式文件
sudo gedit /usr/share/applications/eclipse.desktop
4)在文件中填入如下内容
[Desktop Entry]
Name=Eclipse
Type=Application
Exec=bash -i -c "/opt/eclipse/eclipse"
Terminal=false
Icon=/opt/eclipse/icon.xpm
Comment=Integrated Development Environment
NoDisplay=false
Categories=Development;IDE
Name[en]=eclipse.desktop
5)将/usr/share/applications/下的 eclipse.desktop拖拽(或使用指令复制)到桌面。
4、安装Jre到eclipse目录
1)解压下载的JDK
sudo tar xzvf jdk-8u131-linux-x64.tar.gz
2)进入解压后目录 jdk1.8.0_131 复制jre到eclipse目录下
cd jdk1.8.0_131/
sudo cp -r jre/ /opt/eclipse
5、双击桌面图标即可启动eclipse
二:生成eclipse项目文件
1、创建工作空间,终端中按下面步骤执行
mkdir -p catkin_ws/src
cd catkin_ws/src
catkin_init_workspace
cd catkin_ws
catkin_make
2、创建一个程序包
cd catkin_ws/src
catkin_create_pkg first_pkg std_msgs rospy roscpp
3、到catkin的工作空间,利用catkin_make命令,建立一个eclipse的项目
cd catkin_ws catkin_make --force-cmake -G"Eclipse CDT4 - Unix Makefiles"
项目的文件声称在目录 catkin_ws/build里
三:在eclipse导入项目
1、启动eclipse,选择catkin_ws作为工作空间
2、选择File --> Import --> General --> Existing Projects into Workspace
3、从文件夹~/catkin_ws/build中导入项目
4、点击Finish 后把工程导入eclipse中,文件结构如下图
四:配置eclipse工作环境
1、添加预编译头文件的路径
–到 Project Properties --> C/C++ General --> Preprocessor Include Paths, Macros, etc. --> Providers 选项卡
–勾选CDT GCC Built-in Compiler Settings [Shared]
2、添加项目文件
项目的源文件在文件夹 "Source directory"中,右键src选择NEW --> Source File, 创建文件 main.cpp
3、添加代码
双击main.cpp 复制如下代码
/* * main.cpp * * Created on: 2017年5月25日 * Author: kopu */ #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()) // Keep spinning loop until user presses Ctrl+C { 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; }
4、修改CMakeLists.txt文件
在编译前,需要在main.cpp文件目录下的CMakeLists.txt文件中添加新的编译内容(红色部分)
cmake_minimum_required(VERSION 2.8.3) project(first_pkg) ## Add support for C++11, supported in ROS Kinetic and newer # add_definitions(-std=c++11) ## Find catkin macros and libraries ## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz) ## is used, also find other catkin packages find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs ) ################################### ## catkin specific configuration ## ################################### ## The catkin_package macro generates cmake config files for your package ## Declare things to be passed to dependent projects ## INCLUDE_DIRS: uncomment this if you package contains header files ## LIBRARIES: libraries you create in this project that dependent projects also need ## CATKIN_DEPENDS: catkin_packages dependent projects also need ## DEPENDS: system dependencies of this project that dependent projects also need catkin_package( # INCLUDE_DIRS include # LIBRARIES first_pkg # CATKIN_DEPENDS roscpp rospy std_msgs # DEPENDS system_lib ) ########### ## Build ## ########### ## Specify additional locations of header files ## Your package locations should be listed before other locations # include_directories(include) include_directories( ${catkin_INCLUDE_DIRS} ) ## Declare a cpp executable add_executable(RosProj src/main.cpp) ## Specify libraries to link a library or executable target against target_link_libraries(RosProj ${catkin_LIBRARIES}) ## Declare a C++ library # add_library(${PROJECT_NAME} # src/${PROJECT_NAME}/first_pkg.cpp # )
5、编译项目生成可执行文件
快捷键Ctrl-B,编译项目,在/home/kopu/sw_Proj/catkin_ws/devel/lib/first_pkg目录下会生成二进制文件RosProj
6:在eclipse中运行代码
创建新的启动配置,点击 Run --> Run configurations... --> C/C++ Application (double click or click on New)。
选择相应的二进制执行程序 (use the Browse… button)
确保roscore已经在终端中运行,点击Run,运行程序。
7:在eclipse中调试代码
为了调试,需要将编译器设置为调试模式,终端中,到catkin_ws/build目录执行如下代码:
cmake ../src -DCMAKE_BUILD_TYPE=Debug
重启Eclipse,这样就可以利用Eclipse的调试工具进行调试了,进入调试界面,如下图所示: