Radio Tilt Controlled cyber:bot Code

This is the receiver script for your Radio Tilt Controlled cyber:bot.  You will also need to load the script on the next page into the tilt controller micro:bit (or cyber:bot) you will use to drive this receiver cyber:bot.

  • Set the cyber:bot board's PWR switch to 0.
  • Make sure you are working in the browser with the micro:bit Python Editor that is connected to the radio tilt controlled cyber:bot.
  • Depending on the path you took to get here, your most recent script will either be radio_tilt_receive_test or  radio_tilt_bot_fb_lr_with_stop_range.  If your most recent script is not already open, click Open to load it into the editor.
  • Change the project name to radio_tilt_controlled_cyberbot.
  • Update the script to match the one below.
  • If you are in a classroom, adjust the channel= in the script to your assigned channel.
  • Click Save, and then click Send to micro:bit.  
# radio_tilt_controlled_cyberbot

from cyberbot import *
import radio

radio.on()
radio.config(channel=7, queue=1, length=64)

while True:

    packet = radio.receive()

    if packet:

        try:

            dictionary = eval(packet)
    
            x = dictionary.get('x')
            y = dictionary.get('y')
            needle = dictionary.get('needle')
            
            fb = y / 10
            lr = x / 10

        except Exception as e:

            display.show(Image.SAD)
            print("Exception e:", e)
            print("Type:", type(e))
            continue

        else:

            if abs(fb) > 8:
                display.show(Image.ALL_CLOCKS[needle])
                vL = fb
                vR = -fb

                if(fb < 0): lr = -lr
                vL = vL - lr
                vR = vR - lr

            else:

                display.show(Image.DIAMOND_SMALL)
                vL = None
                vR = None

        finally:

            bot(18).servo_speed(vL)
            bot(19).servo_speed(vR)

 

How it Works

If you came straight here to run the application, this section only explains finishing touches.  You can start at the beginning of this tutorial and read through or actually do the activities that introduce one concept at a time and incrementally build the scripts.

The radio_tilt_controlled_cyberbot script started as radio_tilt_bot_fb_lr_with_stop_range from the Add Right/Left Tilt Control activity.  Along with How It Works sections in some of the earlier activities, it will allow you to take a closer look at the mechanics of the tilt control app.  

As mentioned earlier, exception handling helps prevent other radio messages on the same channel from causing your tilt controlled cyber:bot from throwing an exception in the middle of a maneuver.  The only change to the script is that try…except…else… and finally were added at key points, and the code below each was indented.  For details on how try…except…else…finally works, read or better still, try the activities in the Exception Handling Primer.