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 receiver cyber:bot board PWR switch to 1.
- If you are in a classroom, adjust the channel= in the script to your assigned channel.
- Enter, save, and flash radio_tilt_controlled_cyberbot into the micro:bit that’s in the receiver cyber:bot.
- Disconnect the cyber:bot from the USB cable, and set it on the floor.
- Continue to the Radio Tilt Controller section.
# 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.