0
$\begingroup$

Rosanswers logo

https://wiki.ros.org/tf2/Tutorials/Writing%20a%20tf2%20static%20broadcaster%20%28Python%29

Hi, running through this tutorial and its not clear whether I should be creating my broadcaster source code file in learning_tf2/src/nodes or learning_tf2/nodes.

In section 2.1 it says just make a directory called nodes, whereas in 3 that nodes directory now lives under src.

Would be good to fix inconsistency. Also, does it matter where this file is located within the package? I've seen other places where Python files are in a dir called scripts as opposed to src.


Originally posted by shdblowers on ROS Answers with karma: 3 on 2020-04-11

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

I think the nodes directory should be directly under the package rather than src folder. According to REP8-Style Guide for Python Code,

"The recommended layout is to put all of your package's importable Python code in the src folder of your package and in a "Python package" with the same name as your package. In this case a "Python package" is a folder which contains a __init__.py file, indicating that the folder contains additional Python packages and modules."

The src folder thus should store Python packages and modules if you want to make them importable into other ROS packages.

Also, does it matter where this file is located within the package? I've seen other places where Python files are in a dir called scripts as opposed to src.

As far as I know, technically, it does not matter where the file is located within the package since rosrun is able to find the location of the executable for a package recursively (rosrun-source code). So as long as your executable file- python script is in the directory of your ROS package (under src in the catkin workspace, since catkin_make command only looks for packages under src dir) and your local workspace is properly sourced, rosrun should be able to find that executable.

However, conventionally, according to an excerpt from REP8-Style Guide for Python Code,

"We distinguish between "nodes" and "scripts" for clarity to users. Nodes are executable Python files that conform to the ROS node API. Scripts are executable Python files that do not conform to the ROS node API."

Aslo from Installing Python scripts and modules

“Standard ROS practice is to place all executable Python programs in a package subdirectory named nodes/ or scripts/. Their usage is the same, the two names distinguish ROS nodes from other executable Python scripts.”

So I think, conventionally, an executable python script that contains ROS nodes should go to nodes folder.

I guess distinctions are the convention and good practices to make your package structure more clear.

I think the rationale for ROS users putting python scripts into scripts folder is based on the common practice for python packages. In Python, people usually put top-level executable scripts in the scripts/ (or bin/) directory and place python packages and modules (used by scripts) into the src/ directory. As for the distinction between nodes and scripts in ROS, I don't really know if it's a horrible practice to not separate nodes and scripts. Tbh, personally, I've seldom seen nodes folder. My guess is perhaps ROS users generally consider nodes/ as a subset of scripts/, since scripts are any executable python scripts for any purpose.


Originally posted by karenchiang with karma: 241 on 2020-04-12

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by shdblowers on 2020-04-12:
hi, thanks for the detailed answer. very informative.

Comment by karenchiang on 2020-04-13:
No problem!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.