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

This commit is contained in:
winterhalderp 2021-04-20 11:52:00 +02:00
parent 02b00f8981
commit 67d83c47e7

View File

@ -163,17 +163,38 @@ exchanging _package_name_ with the source package of the custom message type (he
<exec_depend>pubsub_msg</exec_depend>
```
### 4. Build Package
### 4. Configure setup.py
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
```python
def main():
# 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
entry_points={
'console_scripts': [
'talker = pubsub.talker:main',
'listener = pubsub.listener:main'
],
},
```
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`.
### 5. Build Package
Now you can build the Python package.
* Move to your workspace's root: `cd ~/<workspace_path>`
* Build workspace: `colcon build --symlink-install`
### 5. Source newly built workspace
### 6. Source newly built workspace
* Run: `source ~/<workspace_path>/install/local_setup.bash`
* If you already [updated your .bashrc file](https://github.com/patrickw135/pubsub/blob/master/bashrc_addons.txt) you can close all open consoles and start a new console (Ctrl+Alt+T). This will source your workspace automatically, as .bashrc is run every time you start a console.
__Important__: If you use multiple workspaces make sure you have the wanted workspace defined in .bashrc! Otherwise the changes introduced when building will not be available.
### 6. Run scripts
### 7. Run scripts
* Run Talker: `ros2 run pubsub talker`
* Run Listener: `ros2 run pubsub listener`