This full version of the tilt control app has exception handling [1] added to the previous script radio_tilt_bot_fb_lr_with_stop_range.
The advantage of using this one is that if another micro:bit is transmitting different messages on the same radio channel, it will be less likely to halt your cyber:bot due to an exception from a packet that doesn’t contain tilt information.
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.
# 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)
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 [2] 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 [1].
Load this application into the micro:bit (or cyber:bot) you want to use as your tilt controller for your cyber:bot that’s running the radio_tilt_controlled_cyberbot script.
The radio_tilt_controller script is just radio_tilt_controller_w_stop_range from Receiver Full Tilt Control & Stop Range [3]. It is repeated here for folks who skip the majority of the tutorial and go directly to running the application.
# radio_tilt_controller from microbit import * import math import radio radio.on() radio.config(channel=7, queue=1, length=64) while True: x = accelerometer.get_x() y = accelerometer.get_y() angle = round( math.degrees( math.atan2(y, x) ) ) needle = ( angle + 90 + 15 ) // 30 if abs(y) > 80: display.show(Image.ALL_CLOCKS[needle]) else: display.show(Image.DIAMOND_SMALL) dictionary = { } dictionary['x'] = x dictionary['y'] = y dictionary['needle'] = needle packet = str(dictionary) radio.send(packet) sleep(50)
Links
[1] https://learn.parallax.com/tutorials/robot/cyberbot/exception-handling-primer
[2] https://learn.parallax.com/tutorials/cybersecurity-radio-tilt-control/add-leftright-tilt-control
[3] https://learn.parallax.com/tutorials/robot/cyberbot/cybersecurity-radio-tilt-control/adding-stop-range/receiver-full-tilt