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

.. only:: html

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

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

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

.. _sphx_glr_gallery_client_6_variables.py:


Variables
=========

Shows how to get and set variables
of a (remote) Aseba node.

.. GENERATED FROM PYTHON SOURCE LINES 8-17

.. code-block:: Python


    from pyaseba.client import Client
    import time

    client = Client()
    client.connect("tcp", port=33333)
    node_id, connection = client.wait_node()
    node_id, connection





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

 .. code-block:: none


    (0, 1)



.. GENERATED FROM PYTHON SOURCE LINES 18-19

Let us inspect which variables are defined by the node

.. GENERATED FROM PYTHON SOURCE LINES 19-24

.. code-block:: Python


    desc = client.get_description(node_id=node_id)
    assert desc
    list(desc.variables)





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

 .. code-block:: none


    ['_productId', 'args', 'counter', 'id', 'source', 'value']



.. GENERATED FROM PYTHON SOURCE LINES 25-26

We get the current value of all variables one by one

.. GENERATED FROM PYTHON SOURCE LINES 26-34

.. code-block:: Python


    client.cmd_reset(node_id)

    for name in desc.variables:
        value = client.get_variable(node_id=node_id, name=name)
        print(f"{name} = {value}")






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

 .. code-block:: none

    _productId = [0]
    args = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    counter = [72]
    id = [0]
    source = [0]
    value = [0]




.. GENERATED FROM PYTHON SOURCE LINES 35-36

We reset the value to a uniform 1.

.. GENERATED FROM PYTHON SOURCE LINES 36-39

.. code-block:: Python

    for name, (index, size) in desc.variables.items():
        client.set_variable(node_id=node_id, name=name, value=[1] * size)








.. GENERATED FROM PYTHON SOURCE LINES 40-42

As the node will need a bit of time to set the variable, we
wait patiently

.. GENERATED FROM PYTHON SOURCE LINES 42-44

.. code-block:: Python

    time.sleep(1)








.. GENERATED FROM PYTHON SOURCE LINES 45-46

before checking the current values, getting them all at once.

.. GENERATED FROM PYTHON SOURCE LINES 46-49

.. code-block:: Python

    for name, value in client.get_all_variables(node_id=node_id).items():
        print(f"{name} = {value}")





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

 .. code-block:: none

    _productId = [1]
    args = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
    counter = [11]
    id = [1]
    source = [0]
    value = [1]




.. GENERATED FROM PYTHON SOURCE LINES 50-53

.. note::

   Not all values are 1 because in the meantime the node has changed some of them.

.. GENERATED FROM PYTHON SOURCE LINES 55-56

.. code-block:: Python

    client.close()








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

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


.. _sphx_glr_download_gallery_client_6_variables.py:

.. only:: html

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

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

      :download:`Download Jupyter notebook: 6_variables.ipynb <6_variables.ipynb>`

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

      :download:`Download Python source code: 6_variables.py <6_variables.py>`

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

      :download:`Download zipped: 6_variables.zip <6_variables.zip>`


.. only:: html

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

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