和大多数开发环境一样,理论上,在 ROS 中,只需要记事本就可以编写基本的 ROS 程序,但是工欲善其事必先利其器,为了提高开发效率,可以先安装集成开发工具和使用方便的工具:终端、IDE....
1.安装终端:Terminator
在 ROS 中,需要频繁的使用到终端,且可能需要同时开启多个窗口,推荐一款较为好用的终端:Terminator。
效果如下:
1.1 安装
- sudo apt install terminator
复制代码
1.2 添加到收藏夹
显示应用程序 ---> 搜索 terminator ---> 右击 选择 添加到收藏夹
1.3 Terminator 常用快捷键
在同一个标签内的操作: 在不同标签之间的操作:
2.安装:VS Code
VS Code 全称 Visual Studio Code,是微软出的一款轻量级代码编辑器,免费、开源而且功能强大。它支持几乎所有主流的程序语言的语法高亮、智能代码补全、自定义热键、括号匹配、代码片段、代码对比 Diff、GIT 等特性,支持插件扩展,并针对网页开发和云端应用开发做了优化。软件跨平台支持 Win、Mac 以及 Linux。
2.1 下载
VScode 下载:https://code.visualstudio.com/docs?start=true
历史版本下载链接:https://code.visualstudio.com/updates
2.2 VS Code 安装与卸载
2.2.1 安装
方式1:双击安装即可(或右击选择安装)
方式2: sudo dpkg -i xxxx.deb
2.2.2 卸载
2.3 VS Code 集成 ROS 插件
使用 VS Code 开发 ROS 程序,需要先安装一些插件,常用插件如下:
2.4 vscode 使用_基本配置
2.4.1 创建 ROS 工作空间
- mkdir -p xxx_ws/src # (必须得有 src)
- cd xxx_ws
- catkin_make
复制代码 2.4.2 启动 vscode
进入 xxx_ws 目录,启动 vscode:2.4.3 vscode 中编译 ros
快捷键 ctrl + shift + B 调用编译,选择:catkin_make:build。可以点击配置设置为默认,修改 .vscode/tasks.json 文件:- {
- // 有关 tasks.json 格式的文档,请参见
- // https://go.microsoft.com/fwlink/?LinkId=733558
- "version": "2.0.0",
- "tasks": [
- {
- <launch>
- <node pkg="helloworld" type="demo_hello" name="hello" output="screen" />
- <node pkg="turtlesim" type="turtlesim_node" name="t1"/>
- <node pkg="turtlesim" type="turtle_teleop_key" name="key1" />
- </launch>"label": "catkin_make:debug", //代表提示的描述性信息
- <launch>
- <node pkg="helloworld" type="demo_hello" name="hello" output="screen" />
- <node pkg="turtlesim" type="turtlesim_node" name="t1"/>
- <node pkg="turtlesim" type="turtle_teleop_key" name="key1" />
- </launch>"type": "shell", //可以选择shell或者process,如果是shell代码是在shell里面运行一个命令,如果是process代表作为一个进程来运行
- <launch>
- <node pkg="helloworld" type="demo_hello" name="hello" output="screen" />
- <node pkg="turtlesim" type="turtlesim_node" name="t1"/>
- <node pkg="turtlesim" type="turtle_teleop_key" name="key1" />
- </launch>"command": "catkin_make",//这个是我们需要运行的命令
- <launch>
- <node pkg="helloworld" type="demo_hello" name="hello" output="screen" />
- <node pkg="turtlesim" type="turtlesim_node" name="t1"/>
- <node pkg="turtlesim" type="turtle_teleop_key" name="key1" />
- </launch>"args": [],//如果需要在命令后面加一些后缀,可以写在这里,比如-DCATKIN_WHITELIST_PACKAGES=“pac1;pac2”
- <launch>
- <node pkg="helloworld" type="demo_hello" name="hello" output="screen" />
- <node pkg="turtlesim" type="turtlesim_node" name="t1"/>
- <node pkg="turtlesim" type="turtle_teleop_key" name="key1" />
- </launch>"group": {"kind":"build","isDefault":true},
- <launch>
- <node pkg="helloworld" type="demo_hello" name="hello" output="screen" />
- <node pkg="turtlesim" type="turtlesim_node" name="t1"/>
- <node pkg="turtlesim" type="turtle_teleop_key" name="key1" />
- </launch>"presentation": {
- <launch>
- <node pkg="helloworld" type="demo_hello" name="hello" output="screen" />
- <node pkg="turtlesim" type="turtlesim_node" name="t1"/>
- <node pkg="turtlesim" type="turtle_teleop_key" name="key1" />
- </launch> "reveal": "always"//可选always或者silence,代表是否输出信息
- <launch>
- <node pkg="helloworld" type="demo_hello" name="hello" output="screen" />
- <node pkg="turtlesim" type="turtlesim_node" name="t1"/>
- <node pkg="turtlesim" type="turtle_teleop_key" name="key1" />
- </launch>},
- <launch>
- <node pkg="helloworld" type="demo_hello" name="hello" output="screen" />
- <node pkg="turtlesim" type="turtlesim_node" name="t1"/>
- <node pkg="turtlesim" type="turtle_teleop_key" name="key1" />
- </launch>"problemMatcher": "$msCompile"
- }
- ]
- }
复制代码 2.4.4 创建 ROS 功能包
右键点击 src 目录 ---> create catkin package
设置包名、添加依赖:
☂ 第一个框(package name):填写功能包名称,自定义
☂ 第二个框(dependencies):填写用到的功能包名称 roscpp rospy std_msgs
2.4.5 C++ 实现
在自定义的功能包的 src 目录下新建 xxxx.cpp 文件:- /*
- 控制台输出 HelloVSCode !!!
- */
- #include "ros/ros.h"
- int main(int argc, char *argv[])
- {
- setlocale(LC_ALL,"");
- //执行节点初始化
- ros::init(argc,argv,"HelloVSCode");
- //输出日志
- ROS_INFO("Hello VSCode!!!哈哈哈哈哈哈哈哈哈哈");
- return 0;
- }
复制代码 PS1: 如果没有代码提示,修改 .vscode/c_cpp_properties.json ,设置 "cppStandard": "c++17" 。
PS2: main 函数的参数不可以被 const 修饰
PS3: 当 ROS__INFO 终端输出有中文时,会出现乱码
解决办法:在函数开头加入下面代码的任意一句- setlocale(LC_CTYPE, "zh_CN.utf8");
- setlocale(LC_ALL, "");
复制代码 2.4.6 python 实现
在自定义的功能包下新建 scripts 文件夹,创建 python 文件,并添加可执行权限:- #! /usr/bin/env python
- """
- Python 版本的 HelloVScode,执行在控制台输出 HelloVScode
- 实现:
- 1.导包
- 2.初始化 ROS 节点
- 3.日志输出 HelloWorld
- """
- import rospy # 1.导包
- if __name__ == "__main__":
- rospy.init_node("Hello_Vscode_p") # 2.初始化 ROS 节点
- rospy.loginfo("Hello VScode, 我是 Python ....") #3.日志输出 HelloWorld
复制代码 2.4.7 配置 CMakeLists.txt
C++ 配置:- add_executable(节点名称(自定义)
- src/C++源文件名.cpp
- )
- target_link_libraries(节点名称(同上一致)
- ${catkin_LIBRARIES}
- )
复制代码 Python 配置:- catkin_install_python(PROGRAMS scripts/自定义文件名.py
- DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
- )
复制代码 2.4.8 编译执行
编译: ctrl + shift + B
执行 C++:在 VScode 中添加
终端1,执行: source ./devel/setup.bash
终端2,执行: rosrun 功能包名称 文件名
执行 Python:在 VScode 中添加
终端1,执行: roscore
终端2,执行: source ./devel/setup.bash
终端3,执行: rosrun 功能包名称 文件名
PS: 如果不编译直接执行 python 文件,会抛出异常。
1.第一行解释器声明,可以使用绝对路径定位到 python3 的安装路径 #! /usr/bin/python3 ,但是不建议
2.建议使用 #!/usr/bin/env python ,但是会抛出异常: /usr/bin/env: “python”: 没有那个文件或目录
3.解决1: #!/usr/bin/env python3 ,直接使用 python3,但不兼容之前的 ROS 相关 python 实现
4.解决2: 创建一个链接符号到 python 命令: sudo ln -s /usr/bin/python3 /usr/bin/python
2.5 其他 IDE
ROS 开发可以使用的 IDE 还是比较多的,除了上述的 VScode,还有 Eclipse、QT、PyCharm、Roboware ...,详情可以参考官网介绍:IDEs。
3 launch 文件演示
3.1 需求
一个程序中可能需要启动多个节点,比如:ROS 内置的小乌龟案例,如果要控制乌龟运动,要启动多个窗口,分别启动 roscore、乌龟界面节点、键盘控制节点。如果每次都调用 rosrun 逐一启动,显然效率低下,如何优化?
官方给出的优化策略是使用 launch 文件,可以一次性启动多个 ROS 节点。
launch 文件会默认首先启动 roscore。
3.2 实现
① 选定功能包右击 ---> 添加 launch 文件夹
② 选定 launch 文件夹,右击 ---> 添加 XXX.launch 文件
③ 编辑 launch 文件内容:- <launch>
- <node pkg="helloworld" type="demo_hello" name="hello" output="screen" />
- <node pkg="turtlesim" type="turtlesim_node" name="t1"/>
- <node pkg="turtlesim" type="turtle_teleop_key" name="key1" />
- </launch>
复制代码 ★ node :包含的某个节点
✄ pkg :功能包
✄ type :被运行的节点文件
✄ name :为节点命名
✄ output :设置日志的输出目标
④ 运行 launch 文件- source ./devel/setup.bash
- roslaunch 包名 launch文件名
复制代码 运行结果: 一次性启动了多个节点。
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |