You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/create_python_node_from_scratch.rst
+35-44Lines changed: 35 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,6 +15,29 @@ Creating a Python Node from scratch (for :program:`ament_python`)
15
15
Let us add an additional Node to our :program:`ament_python` package that actually uses ROS2 functionality.
16
16
These are the steps that must be taken, in general, to add a new Node.
17
17
18
+
File structure
19
+
--------------
20
+
21
+
In this section, we will be modifying or creating the following files.
22
+
23
+
.. code-block:: console
24
+
:emphasize-lines: 2,5,10
25
+
26
+
python_package_with_a_node
27
+
|-- package.xml
28
+
|-- python_package_with_a_node
29
+
| |-- __init__.py
30
+
| |-- print_forever_node.py
31
+
| `-- sample_python_node.py
32
+
|-- resource
33
+
| `-- python_package_with_a_node
34
+
|-- setup.cfg
35
+
|-- setup.py
36
+
`-- test
37
+
|-- test_copyright.py
38
+
|-- test_flake8.py
39
+
`-- test_pep257.py
40
+
18
41
.. _Handling dependencies:
19
42
20
43
Handling dependencies (:file:`package.xml`)
@@ -30,69 +53,31 @@ By no coincidence, the :file:`package.xml` has the :code:`.xml` extension, meani
30
53
31
54
Let us add the dependency between the :code:`<license>` and :code:`<test_depend>` tags. This is not a strict requirement but is where it commonly is for standard packages.
Depending on the particulars of the workspace you are (re)building, :program:`PyCharm` will only be able to recognize certain changes if it is restarted from a properly sourced terminal.
46
-
47
-
After you add a new dependency to :file:`package.xml`, nothing really changes in the workspace unless a new build is performed.
48
-
49
-
In addition, when programming with new dependencies, unless you rebuild the workspace, :program:`PyCharm` will not recognize the libraries, and autocomplete will not work.
50
-
51
-
So,
52
-
53
-
#. close :program:`PyCharm`.
54
-
#. Run (in the terminal you used to run :program:`PyCharm` before)
55
-
.. include:: the_canonical_build_command.rst
56
-
#. Re-open pycharm
57
-
.. code:: console
58
-
59
-
pycharm_ros2
60
-
61
-
62
63
Creating the Node
63
64
-----------------
64
65
65
66
In the directory :file:`src/python_package_with_a_node/python_package_with_a_node`, create a new file called :file:`print_forever_node.py`. Copy and paste the following contents into the file.
By now, this should be enough for you to be able to run the node in :program:`PyCharm`. You can right-click it and choose :guilabel:`D&ebug print_forever_node`. This will output
To finish, press the :guilabel:`Stop` button or press :kbd:`CTRL+F2` on :program:`PyCharm`. The node will exit gracefully with
85
-
86
-
.. code :: console
87
-
88
-
Process finished with exit code 0
89
-
90
75
.. _Making rosrun work:
91
76
92
77
Making :command:`ros2 run` work
93
78
-------------------------------
94
79
95
-
Even though you can run the new node in :program:`PyCharm`, we need an additional step to make it deployable in a place where :command:`ros2 run` can find it.
80
+
We need an additional step to make it deployable in a place where :command:`ros2 run` can find it.
96
81
97
82
To do so, we modify the :code:`console_scripts` key in the :code:`entry_points` dictionary defined in :file:`setup.py`, to have our new node, as follows
98
83
@@ -116,19 +101,25 @@ The format is straightforward, as follows
116
101
:code:`main` The function, within the script, that will be called. In general, :code:`main`.
0 commit comments