How the Wireless Controlled Bot Script Works

Like the transmitter script, the wireless receiver script’s import and radio calls are also similar to countdown_receiver.py.  The radio.config call's length is set to 64 to accommodate longer strings if needed.  More importantly, since it has to actually control the cyber:bot, from microbit import * was changed to from cyberbot import *.

# terminal_controlled_bot_wireless

from cyberbot import *
import radio

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

sleep(1000)

print("Ready...\n")

Inside the main loop, the script rapidly and repeatedly checks the radio.receive() method.  If the micro:bit has not received a radio message, it returns None.  If the micro:bit receives a packet (because you finished typing all three values and pressed Enter), then radio.receive returns a string that contains the dictionary.  That gets stored in a variable named packet.

while True:
    packet = radio.receive()

When radio.receive returns None, the rest of the script gets skipped and the while(True) loop repeats.  When radio.receive returns a string (with characters that represent a dictionary), the statements under if packet is not non are executed, starting with printing the packet.  (You can only see that if you have the cyber:bot tethered and connected to a terminal.)

    if packet is not None:
        print("Receive: ", packet)

This statement converts a string containing a dictionary into an actual dictionary.  The result is named dictionary.

        dictionary = eval(packet)

Assuming you typed 25, -25, 1000 in response to the prompts, the dictionary will be something like {'vL': 25, 'vR': -25, 'ms': 1000}.  (Again, the order of the key-value pairs doesn’t matter since you use the key to find its corresponding value.)  These statements use the 'vL', 'vR', and 'ms' keys to fetch the 25, -25, and 1000 values.

        vL = dictionary['vL']
        vR = dictionary['vR']
        ms = dictionary['ms']

With the variables named vL, vR, and ms now storing the correct values, all that’s left is to make the left and right wheels turn at 25 and -25. And, keep that maneuver going for 1000 ms with sleep before stopping the servos again.

        bot(18).servo_speed(vL)
        bot(19).servo_speed(-vR)
        sleep(ms)
        bot(18).servo_speed(None)
        bot(19).servo_speed(None)