Update 'non-ros-files/Programming_Rules.md'

This commit is contained in:
winterhalderp 2021-04-20 11:57:14 +02:00
parent 67d83c47e7
commit 59b95f5786

View File

@ -29,10 +29,17 @@ The next chapters will describe the content of the files inside a package direct
## setup.py ## setup.py
The file `<colcon_ws>/<package_dir>/setup.py` defines how you can call your scripts using a self defined in the command line. This results in commands such as The file setup.py is used to create entry points, meaning you link your python scripts to names which you can be called through `ros2 run ...`. In order for this to work your python scripts must be written using the following mechanism
`ros2 run pubsub talker` ```python
`ros2 run pubsub listener`. def main():
This is done by linking the correct Python scripts to the names _talker_ and _listener_ using the file _setup.py_, eg. # main code
if __name__ == "__main__":
main()
```
in which you put all of your code inside `main()`. This prevents any code from being run, if this script is not run as main.
Linking your scripts to names is done inside the file _setup.py_ by defining entry points:
```python ```python
entry_points={ entry_points={
'console_scripts': [ 'console_scripts': [
@ -40,5 +47,7 @@ entry_points={
'listener = pubsub.listener:main' 'listener = pubsub.listener:main'
], ],
}, },
``` ```
From this you can see that you need to follow the Python programming style of creating a _main_ procedure, in order for the entry points to work. The _setup.py_-excerp above links the the function `main()` from the scripts _talker.py_ and _listener.py_ from inside `/pubsub` to the names _talker_ and _listener_. This way they can be called using `ros2 run pubsub talker` or `ros2 run pubsub listener`.
From this you can see that you need to follow the Python programming style of creating a _main()_ procedure, in order for the entry points to work.