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:
(View full-size: terminal_controlled_bot_wireless.mp4)
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)
- Open countdown_receiver from Send and Receive Packets.
- Paste the three lines you copied over these lines:
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 added to the project.
(See Add modules to your micro:bit). - Set the project name to 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.
- Disconnect your transmitter micro:bit from USB.
- Connect the cyber:bot you will be remote controlling.
- Click Save, then click Send to micro:bit.
- Disconnect your cyber:bot from USB and set it on the floor somewhere nearby.
- Make sure the:
- Batteries are plugged in
- Battery pack's barrel plug is firmly plugged into the cyber:bot board's barrel jack.
- cyber:bot board's PWR 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)