Receiver Script for cyber:bot Output

The receiver script is also adapted from a previous tutorial.

Starting with the countdown_receiver.py from the Send and Receive Packets activity, you can replace its countdown while loop and printed message with the servo_speed and sleep(ms) calls from terminal_controlled_bot_tethered_try_this.  You will also need to update the dictionary parsing so that it gets the vL, vR, and ms values using the 'vL', 'vR', and 'ms' keys.  That’s the most crucial part of incorporating the radio-in to cyber:bot navigation-out part of your tethered app into script that wirelessly receives.  

Here's a GIF showing the modification taking place:



You will also need to make a few other adjustments.  Here they are step-by-step:

  • Open terminal_controlled_bot_tethered_try_this.
  • Copy these lines:
        bot(18).servo_speed(vL)
        bot(19).servo_speed(-vR)
        sleep(ms)
        bot(18).servo_speed(None)
        bot(19).servo_speed(None)
        print("value = ", value)
        print("message = ", message, "\n")
        
        while value >= 0:
            print(value)
            sleep(1000)
            value = value - 1
            
        print(message)
  • Make sure the cyberbot.py module is loaded on your micro:bit.
  • Rename the script terminal_controlled_bot_wireless.
  • Change from microbit import * to from cyberbot import *.
  • Find these two lines:
        value = dictionary['start']
        message = dictionary['after']
  • Replace them with these three lines:
        vL = dictionary['vL']
        vR = dictionary['vR']
        ms = dictionary['ms']
  • Update the comment at the top of the script with the name terminal_controlled_bot_wireless.
  • Delete these two lines:
        print()
        print("Parse: ")
  • Find these two lines:
        print("Countdown App")
        print("micro:bit sender")
  • Replace them with this line:
        print("Ready...\n")
  • Change the 50 in this line to 64:
        radio.config(channel=7,length=50)

It should look like this:

        radio.config(channel=7,length=64)
  • If you are in a classroom and using assigned channels, update your channel.

     

Now, your script should be ready.  

  • Verify the changes you made against the example script listed below.
  • Save your script.
  • Disconnect your transmitter micro:bit from USB, and connect the cyber:bot you will be remote controlling.
  • Load the code into the remote controlled cyber:bot.
  • Disconnect your cyber:bot from USB and set it on the floor somewhere nearby.
  • Make sure batteries are connected, and the 3-position switch is set to 2.

 

Example script: terminal_controlled_bot_wireless

# terminal_controlled_bot_wireless

from cyberbot import *
import radio

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

sleep(1000)

print("Ready...\n")

while True:
    packet = radio.receive()
    if packet is not None:
        print("Receive: ", packet)

        dictionary = eval(packet)

        vL = dictionary['vL']
        vR = dictionary['vR']
        ms = dictionary['ms']
        
        bot(18).servo_speed(vL)
        bot(19).servo_speed(-vR)
        sleep(ms)
        bot(18).servo_speed(None)
        bot(19).servo_speed(None)