Retrieve data from Inputs

Now that we have covered how to toggle Output pins, let's focus on Input ones. While Outputs are used to control effectors, Inputs are used to retrieve data from sensors.

The L0 GPIO driver provides two different types of Input:

  • digital (high/low or on/off)
  • analog value within a range typically [0:4096]

We will now prepare a board showing how to use both of theses types of Input.

Prepare the Inputs board

Ok, let's start with some electronics. We will prepare another board with two Inputs: a push button and a potentiometer.

You will need:

  • the other L0 GPIO board
  • a push button
  • a potentiometer
  • wires
  • another breadboard to make a clean setup

Then, follow this schematic:

schema elec L0 button and potentiometer

And you should have a setup similar to this one:

L0 Inputs board

Now, your Inputs board is ready. Let's connect it to the rest of the setup. First, make sure to unplug the USB from your computer to un-power the whole setup.

Then connect the Input board to any other L0 board. You can actually connect it to either the L0 USB gate or the L0 Led board. As they are all on the same bus it will not make any difference. Once it's done, you can re-power the whole setup by plugging the USB back in your computer.

Connecting the L0 Input board to the gate animation

Back to Python

It's now time to check if everything works as expected. Let's connect to the setup via pyluos again.

Note: When you reset the setup by unpowering/re-powering it, you have to close your Python interpreter and connect again. You can also use the robot.close() method.

We can connect to the robot via the same code we used in the previous part (do not forget to change the usb port with your one):

from pyluos import Robot

robot = Robot('/dev/cu.usbserial-XXXX')

Now, you should have two modules (i.e. boards) connected:

>>> [<"btn_gpios": [<"p1" Input value="2106">, <"p2" Output state="low">, <"p3" Output state="low">, <"p4" Output state="low">, <"p5" Input state="low">, <"p6" Input state="low">, <"p7" Input state="low">, <"p8" Input state="low">, <"p9" Input value="0">]>, <"led_gpios": [<"p1" Input value="2287">, <"p2" Output state="low">, <"p3" Output state="low">, <"p4" Output state="low">, <"p5" Input state="low">, <"p6" Input state="low">, <"p7" Input state="low">, <"p8" Input state="low">, <"p9" Input value="1407">]>]
>>> 2

Ok, everything looks fine! Let's check our Inputs.

Check the button state

If you followed the schematic, the button should be connected to the pin p8 of the Input board. We have already pre-configured the board to use the name btn_gpios.

So you can access it via:

>>> <"p8" Input state="low">

You should see that the pin is in low state. So far, so good. Now press the button and keep it pressed. Run the same command again:

>>> <"p8" Input state="high">

It's now in high state! Cool, it's working.

You can also directly check the state of an Input pin via:

if robot.btn_gpios.p8.is_high():
    # do something when the button is pressed
    # do some other thing when it is released

Get the potentiometer position

Now, the potentiometer. It should be connected to the pin p9.

Let's see if we have a value:

>>> <"p9" Input value="2675">

Good! As you can see, here we receive an integer value as we are using an analog Input. This value represents the voltage applied to the pin p9 on a scale from 0 to 4096. For us, it is a direct reading of the position of the potentiometer, which can move from about 300 degrees.

Let's turn the potentiometer fully in one direction up to its limit position and ask for the value again:

>>> <"p9" Input value="4024">

And on the other side:

>>> <"p9" Input value="0">

As you can see, the entire range is not totally covered. This is quite normal as we are not using an extremely precise hardware.

To directly get the value from an analog pin you can use:

>>> 42

Both of our Inputs are working and we have seen how to get their current value. Before switching to the next part where we link Inputs and Outputs, let's talk a bit about synchronisation in pyluos.

A word on synchronisation

As you have seen throughout this part of the tutorial, the values that you access with pyluos are the current ones. You do not need to send any specific commands to update them.

Explaining the exact process that maintain the hardware and pyluos values synced is out of the scope of this tutorial. But here is a short description of what is happening.

As soon as you create the Robot object in pyluos, a communication starts between the L0 gate and pyluos.

The gate sends the current state of all connected boards to pyluos. It's done at a rather high frequency (about 25Hz in the current version).

To put it in more technical words the read method used to get the value of an analog pin is actually asynchronous. You immediately get the last synced value. You do not send a specific command to the hardware and wait for the answer.

results matching ""

    No results matching ""