Variables

Variables#

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

from pyaseba.client import Client
import time

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

Let us inspect which variables are defined by the node

desc = client.get_description(node_id=node_id)
assert desc
list(desc.variables)
['_productId', 'args', 'counter', 'id', 'source', 'value']

We get the current value of all variables one by one

client.cmd_reset(node_id)

for name in desc.variables:
    value = client.get_variable(node_id=node_id, name=name)
    print(f"{name} = {value}")
_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]

We reset the value to a uniform 1.

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

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

time.sleep(1)

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

for name, value in client.get_all_variables(node_id=node_id).items():
    print(f"{name} = {value}")
_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]

Note

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

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

Gallery generated by Sphinx-Gallery