在 PyQt4 GUI 中获取 bash 文件

Sourcing bash file inside PyQt4 GUI

我正在开发一个使用 PyQt4 和 Python 2.7 构建的 GUI,它可以为 Clearpath Husky running ROS Indigo. The GUI switches between launching navigation demos 启动各种演示并可视化我的物理机器人。为此,它必须在本地 ROS 和我的 Husky 上的 ROS 上启动演示之间切换。在两个不同的 ROS 实例之间切换时,我需要能够 "source" 每个 OS 的 devel/setup.bash 以便正确构建包并在 Rviz 中可视化 Husky 并没有中断(TF 帧错误 "No tf data. Actual error: Fixed Frame [odom] does not exist" 和 RobotModel "URDF Model failed to parse")。在我的 .bashrc 中,如果我获取 Husky 的 setup.bash,可视化工作正常,直到我尝试 运行 本地演示。反之亦然;虽然采购本地 setup.bash 将 运行 本地演示很好,但 Husky 的可视化中断了。

有没有一种方法可以使用 python 的子进程(或其他替代方法)在 GUI 的实例中获取适当的 devel/setup.bash,这样可视化就不会休息?

是的,在执行您想要的 ROS 命令之前获取安装脚本应该就足够了,例如:

#!/usr/bin/env python

from subprocess import Popen, PIPE

shell_setup_script = "/some/path/to/workspace/devel/setup.sh"
command = "echo $ROS_PACKAGE_PATH"
cmd = ". %s; %s" % (shell_setup_script, command)

output = Popen(cmd, stdout=PIPE, shell=True).communicate()[0]
print(output)

如你所见,使用了shell=True,这将在subshell中执行cmd。然后,cmd 包含 . <shell_setup_script>; <command>,它在执行命令之前获取安装脚本。请注意,.sh 文件是源文件而不是 .bash,因为通用 POSIX shell 很可能被 Popen 使用。