automateprojectscript.py - TomHulme/306-Swarm-Robotics-Project GitHub Wiki
Overview
This python file runs all of the terminal commands needed to run the project. It just saves time not having to manually type in these commands every time you want to run the project. While it is similar to roslaunch in this regard, extra features are added to make to customisable in a friendlier environment than if it was in a .launch file.
Every command is using the subprocess python module and, in particular, the Popen
calls. This emulates commands sent to a terminal and is the core as to how this script works by allowing to not only invoke the ros commands needed to run the project, but also use many of the useful bash features to automate much more of the process.
Execution
It initially runs the World Generator passing in arguments that can be changed in the script. See the worldGenerator.py section for details of what it does and how it works.
It then cleans up CMakeLists.txt file used in previous executions of the project by removing all of the rosbuild_add_executable lines that were added. The bash command sed
is used here to remove these instances. Doing this means no unnecessary files are compiled during rosmake.
The new rosbuild_add_executable lines are then added so that only the needed files are made.
The latter commands involve running the correct commands to get the project running:
rosmake
(script waits until the project has been made, otherwise the script will continue on even though the project has not been built yet)roscore
(previous instances of roscore are killed)rosrun stage stageros world/myworld.world
The last few commands involve running each of the nodes in a new terminal so that it makes it easier to see what messages are being sent by which robot.
A loop is used to iterate through each sheep needed to be created and runs SheepNode & SheepMove
--tab -e 'bash -c
This line opens a single terminal window that contains tabs for each SheepNode & SheepMove that is executed.
Contextual Explanations
\"rosrun se306Project SheepNode
Executes the SheepNode class
__name:=sheep{0} _sheepNum:={0} _fieldX:={1} _fieldY:={2} \"' --title='SheepNode {0}' """.format(str(i), str(field_X), str(field_Y))
Passes in various arguments for the SheepNode class; the current sheep number the script is at and the dimensions of the field. It also sets the title of the tab to be SheepNode X, where X is the sheep number in the iteration A similar thing happens with the SheepMove in the same iteration.
After running all the sheep, the field is executed in a similar way to what happens above.
Farmer, Sheepdog, Truck Creation
Lastly the farmer, sheepdog and truck are all executed. Due to there only being one instance of each, the way these are run is different to what was used above.
Popen(shlex.split("""gnome-terminal -e 'bash -c "rosrun se306Project farmer"' --title='Farmer'"""),stdout=PIPE)
This opens a new gnome terminal titled Farmer. There are no arguments needed to be passed (from within the automate project script) to the farmer, sheep and truck, so that is why the above command is simpler than the rest.