Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sourcing bash file inside PyQt4 GUI

I'm working on a GUI built with PyQt4 and Python 2.7 that launches various demos for a Clearpath Husky running ROS Indigo. The GUI switches between launching navigation demos and visualizing my physical robot. To do this, it has to switch between launching demos on a local ROS and the ROS on my Husky. When switching between the two different ROS instances I need to be able to "source" the devel/setup.bash for each OS so that the packages are built correctly and visualizing the Husky inside Rviz doesn't break (Errors with TF frames like "No tf data. Actual error: Fixed Frame [odom] does not exist" and with RobotModel "URDF Model failed to parse"). In my .bashrc, if I source the Husky's setup.bash, the visualization works fine until I try running a local demo. This also happens vice versa; while sourcing the local setup.bash will run the local demos just fine, the Husky's visualization breaks.

Is there a way to use python's subprocess (or another alternative) to source the appropriate devel/setup.bash within the GUI's instance so that the visualization won't break?

like image 351
dicegame Avatar asked Oct 30 '22 23:10

dicegame


1 Answers

Yes, it should be sufficient to source the setup script just before executing the ROS command you want, for instance:

#!/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)

As you see, shell=True is used, which will execute the cmd in a subshell. Then, cmd contains . <shell_setup_script>; <command> which sources the setup script before executing the command. Please note that the .sh file is sourced instead of .bash since generic POSIX shell is likely to be used by Popen.

like image 182
Andrzej Pronobis Avatar answered Nov 02 '22 10:11

Andrzej Pronobis