
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/network/1_network_basics.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_network_1_network_basics.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_network_1_network_basics.py:


Network
=======

Illustrative example of how to set up a simple Aseba network
from Python.

.. GENERATED FROM PYTHON SOURCE LINES 10-12

The network will be reachable at target tcp:port=10000
from the local network.

.. GENERATED FROM PYTHON SOURCE LINES 12-18

.. code-block:: Python


    from pyaseba.network import Network, Node

    network = Network(port=10000)
    network





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    Network(address='0.0.0.0', port='10000)



.. GENERATED FROM PYTHON SOURCE LINES 19-22

Nodes specialize the :py:class:pyaseba.network.Node`
base class, by adding events, variables, functions, and (periodic)
control step callbacks.

.. GENERATED FROM PYTHON SOURCE LINES 22-63

.. code-block:: Python



    class SimpleNode(Node):

        events = {
            "event": "emitted at each control step after incrementing counter"
        }
        variables = {"counter": 1}
        functions = {
            "duplicate": ("duplicates the input", [("input", 1), ("result", 1)])
        }

        def __init__(self, node_id: int):
            super().__init__(node_id, name="SimpleNode", default_functions=False)

        def init(self) -> None:
            self.counter = 0
            self.set("_productId", [0])

        @property
        def counter(self) -> int:
            vs = self.get("counter")
            if vs:
                return vs[0]
            return 0

        @counter.setter
        def counter(self, value: int) -> None:
            self.set("counter", [value])

        def tick(self, time_step: float) -> None:
            self.counter += 1
            self.emit("event")

        def reset(self) -> None:
            self.counter = 0

        def duplicate(self, xs: list[int]) -> list[int]:
            return [x * 2 for x in xs]









.. GENERATED FROM PYTHON SOURCE LINES 64-65

Nodes need to be added to a network to perform any work

.. GENERATED FROM PYTHON SOURCE LINES 65-68

.. code-block:: Python

    node = SimpleNode(0)
    network.add_node(node)








.. GENERATED FROM PYTHON SOURCE LINES 69-71

.. code-block:: Python

    print(node.description)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Description(name='SimpleNode', protocol_version=9, variables={'_productId': (34, 1), 'args': (2, 32), 'counter': (36, 1), 'id': (0, 1), 'source': (1, 1)}, local_events={'event': 'emitted at each control step after incrementing counter'}, functions={'duplicate': ('duplicates the input', [('input', 1), ('result', 1)])})




.. GENERATED FROM PYTHON SOURCE LINES 72-73

When the network spins

.. GENERATED FROM PYTHON SOURCE LINES 73-75

.. code-block:: Python

    network.spin(time_step=0.1, duration=1)








.. GENERATED FROM PYTHON SOURCE LINES 76-86

While the network spins, we could connect to the network
with a client running in a different process.
The client may load an Aseba script that uses the defined events, variables, and
functions, like ::

  onevent event
  emit user_event_1 counter

  onevent user_event_2
  call duplicate args[1:2] args[2:3]


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 1.008 seconds)


.. _sphx_glr_download_gallery_network_1_network_basics.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: 1_network_basics.ipynb <1_network_basics.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: 1_network_basics.py <1_network_basics.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: 1_network_basics.zip <1_network_basics.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
