Close the loop

In the last two parts of the tutorial we have seen how to change Outputs and retrieve Inputs state. It's now time to close the loop and control Output directly from Input.

One of the main goal of pyluos is to let you do that as easily as it sounds. We will illustrate this by building two simple demos:

  • the first one where we turn on a LED when we press the button,
  • the second one where we control which LED to turn on with the potentiometer.

Turn the light on and off

So, first we need a way to continuously check for the button state. Well, the easiest way to do that is a simple infinite loop. In Python, this will look like this:

while True:
    if robot.btn_gpios.p8.is_high():
        # The button is pressed
    else:
        # The button is released

Pretty straightforward right?

Then, we simply have to replace our comment and send the right command.

while True:
    if robot.btn_gpios.p8.is_high():
        robot.led_gpios.p2.set_high()
    else:
        robot.led_gpios.p2.set_low()

And that's it... It's a simple as it should be. You can try and run this code.

Note: as we use an infinite loop the program will run forever. To stop it, you can simply press Ctrl-C.

Make our life even easier: aliasing

Even if the code above is already quite simple, we can still remove quite a bit of the boilerplate thanks to aliasing.

What we would like is to define is a name for the specific pins that we use. Python lets you do that really easily.

button = robot.btn_gpios.p8
blue_led = robot.led_gpios.p2

And now we can rewrite our infinite loop like this:

while True:
    if button.is_high():
        blue_led.set_high()
    else:
        blue_led.set_low()

You can find the entire example here.

Note: any oneliner fans around here? Obviously, you can also write it like this:

while True:
   blue_led.set_high() if button.is_high() else blue_led.set_low()

A word on performance issue: active polling

Here we are doing what is called active polling meaning we poll the input state as fast as we can. This is not problematic as the communication is done asynchronously and the is_high, set_high and set_low calls are asynchronously.

Yet, we could write an event based loop where our program is only awaken when a change in the Input pin occurs. pyluos provides tools to do that simply. But this will make the code a bit more complex to read and we will thus not show any example here, it will be introduced in another dedicated tutorial.

Just keep in mind that before going to production, this may be a good topic to look into. Our planet is already heating enough :)

Which light to turn on?

Now the second demo. We will use the potentiometer to choose which led to turn on. This is a slightly more complex example. First, let's define all our leds:

blue_led = robot.led_gpios.p2
green_led = robot.led_gpios.p3
red_led = robot.led_gpios.p4

and our potentiometer:

potentiometer = robot.btn_gpios.p9

We will now write a simple function that turn on one of the led and turn the other one off. This will make our selector code much simpler. You can write this function in various ways but a simple version could look like this:

def turn_on(selected_led):
    for led in (blue_led, green_led, red_led):
        if led == selected_led:
            led.set_high()
        else:
            led.set_low()

You can test that our function works well interactively:

import time

turn_on(red_led)
time.sleep(1)
turn_on(green_led)

And finally we need to get the potentiometer value. We will split its range in three and check if it's in the first, the second or the last part. As our potentiometer can go from 0 to 4096 our threshold will be defined as:

threshold = 4096 / 3

And now we can check the position of the potentiometer and turn on the led accordingly:

while True:
    if potentiometer.read() < threshold:
        turn_on(blue_led)
    elif potentiometer.read() < 2 * threshold:
        turn_on(green_led)
    else:
        turn_on(red_led)

You can now run the whole program and you should have a working setup like this one!

Led selector demo

Going further

The code that you have been writing is actually slightly different from the one you see on the animation above. In the example, you see that you first have to press the button and then only you can choose which led to turn on. You could also only turn on the led when the button is pressed.

Another alternative could be to control the blink frequency of the led using the potentiometer.

It's time for you to dig in the code and explore what you can do! It's the best way to get familiar with the Python API.

Wrap it up

We are done with this tutorial. We have seen quite a lot actually:

  • We have seen different drivers for the L0 to specify a board for a dedicated task (USB gate, GPIO).
  • We have seen how to connect together multiple L0 boards to create a network.
  • Finally, we have also seen how to program a Luos network from your computer using a Python high-level API.

Do not hesitate to give us feedback, comment or contribute to this tutorial on the luos forum!

results matching ""

    No results matching ""