ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

TF2 Tutorial Static Broadcaster Python

asked 2020-04-11 07:02:29 -0500

https://wiki.ros.org/tf2/Tutorials/Wr...

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.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
3

answered 2020-04-12 08:41:59 -0500

updated 2020-04-12 10:22:25 -0500

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.

edit flag offensive delete link more

Comments

hi, thanks for the detailed answer. very informative.

shdblowers gravatar image shdblowers  ( 2020-04-12 08:48:48 -0500 )edit

No problem!

karenchiang gravatar image karenchiang  ( 2020-04-13 10:42:10 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2020-04-11 07:02:29 -0500

Seen: 164 times

Last updated: Apr 12 '20