This tutorial focuses on the basics of radio-controlling a cyber:bot from your terminal. You will be able to type in left and right wheel speeds and a number of milliseconds to execute a maneuver. After you’ve entered those three values, the cyber:bot will do the rest.
The first version of this app will be tethered, with all commands going into the cyber:bot through USB. The second version will be wireless. It will work the same as the tethered app, but you won’t have the constraint of a USB cable any more. At that point, it’ll be a great little project for remotely navigating through mazes.
Wireless cyber:bot control through a terminal builds on the radio countdown example in Send and Receive Packets from the Radio Data Tutorial. Instead of an output that counts down and displays a message, the output will be a cyber:bot that executes the maneuver you typed into the terminal.
You will need:
Complete these tutorials first:
You can use this app to make your cyber:bot remotely navigate a maze. Or, use one micro:bit transmitter to control a whole cyber:bot fleet in a synchronized dance routine!
You will also have more practice with a radio application, simplified by packet communication with key-value pairs. The transmitter project will packetize and transmit the speed and maneuver time values you type. The receiver will parse the packet it receives and then execute the maneuver.
You will also be ready to move on to the next tutorial (with more coming soon!):
Most radio applications started as working prototypes connected by wires. At some point in the development process, the wires get replaced by a radio link. When it comes to robots, we refer to that as tethered control. When a radio link replaces the wires, it becomes radio controlled (RC), or sometimes wireless controlled.
One reason many devices are developed with wired connections is that it simplifies troubleshooting. The engineers doing the development do not have to worry about whether the radio is having problems if they are using wires. Once the system behaves well with the wired connection, the next step is to replace the tether with a radio link.
In this tutorial we will do the same thing: start with tethered keyboard control of the cyber:bot, then replace the tether with a radio link.

This is a simple tethered application where you type in left and right wheel speeds, and run time in milliseconds, into a terminal. When you press Enter, your text gets parsed and executed by the micro:bit, which then makes the cyber:bot execute the maneuver.
Example Project: terminal_controlled_bot_tethered_intro


Since your cyber:bot is currently tethered, it’s best to use low speeds and short run times. Otherwise, your cyber:bot might try to roll beyond the tether length. In other words, it’ll try to unplug itself from the USB cable, or maybe roll off the table.
For example, what happens if you type abc and press enter? You will have to press/release the reset button to restart the app.

Since this project involves cyber:bot motion, it uses the cyberbot extension. The first serial write line block (after the recommended 1 second startup delay) just reminds the person typing in the terminal that valid speeds are from -100 to 100. The serial new line block adds a line after the “Speeds are -100 to 100” message.

The main loop starts with three input statements getting text from the user that represents values. The input statement returns the character representations of the characters. The first one is set (text) to (serial read until(carriage return)) which reads the terminal until you press enter in which it will save the things you typed as a string.

In addition to working in calculations, number variables are required by the (Pin…) servo speed (vL or vR) method as well as the pause (ms) blockl. When you typed 25 in response to the Enter left speed: prompt, the “25“ string was stored in text. That won’t work for servo speed or pause. That’s why set (vL) to (parse to number (text)) stores the number version of the “25“ string in the vL variable. The same applies to vR, and to ms for the pause call.
Once vL, vR, and ms are all values stored in number variables, the project uses vL to set the left (P18) servo speed, and vR to set the right (P19) servo speed. Did you notice the negative sign in (Pin19) servo speed (0 – vR)? That makes it so that you can type positive values for left and right servo speeds and the result is forward motion. No more remembering that left is counterclockwise and right is clockwise for forward motion!

The ms number variable is used in the pause block’s argument so that the servos continue running for the ms time you typed into the terminal. After that, the servo stop block stops the servos from turning and completes the maneuver.

After this, the forever loop repeats, and you can type in another maneuver for the cyber:bot to execute.
Input to parse to number will always be a two step process, but it doesn’t have to take up two blocks. For example,

… can be condensed to:

Expressions and statements contained by parentheses are evaluated from the inside toward the outside. So, in this case the result text string result of the digits you typed is returned by serial read until(carriage return). Then, that result (like the string “25”) is converted into a number with the outer parse to number(…), and then stored in vL.
If you accidentally type a character like ‘q’ instead of the number 1, it will cause an exception. That’s because the parse to number(…) block needs something with digits to convert to a number type. It can convert the string “25” to the value 25, but it cannot convert the string “q5” to any value.
Let’s try reducing the input block count from six to three.



…with these three blocks:





Now that you have keyboard control of your cyber:bot working with a USB tether, it’s time to take the app wireless!
The transmitter and receiver projects we will use may be familiar. They are in the Send and Receive Packets activity; it’s the second one in the Cybersecurity: Radio Data tutorial. Each project will get parts of the tethered project added, and some adjustments to make it all work.

Let’s build on experience, and adapt a familiar project from a previous tutorial to use keyboard inputs.
Starting with the countdown_sender project from the Send and Receive Packets activity, you can replace its input statements with the ones from terminal_controlled_bot_tethered_try_this. Then, the dictionary that’s created has to be adjusted to contain the vL, vR, and ms keys and values. That’s the most crucial part of incorporating the terminal-in, radio-out part of your tethered app into a project that wirelessly transmits.
You will also need to make a few other adjustments. Here they are, step-by-step:





Replace it with this block:

Now, your project should be ready.


The receiver project is also adapted from a previous tutorial.
Starting with the countdown_receiver from the Send and Receive Packets activity, you can replace its countdown while loop and printed message with the servo speed and pause (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 the project that wirelessly receives.
You will also need to make a few other adjustments. Here they are step-by-step:







Now, your project should be ready.


Now, it’s time to reconnect your transmitter micro:bit and start radio-broadcasting navigation commands for your cyber:bot.
Next, repeat the tests you typed into the app when it was tethered:
If you take a look at the dictionary that was displayed, it should contain {‘ms’: 750, ‘vR’: 25, ‘vL’: 25}. Remember from the Dictionary Primer, order does not matter for dictionaries because they keep the correct values paired with the correct keys.

The radio call is similar to the countdown_transmitter project from Send and Receive Packets.

As usual, we want at least a 1-second pause before printing anything to make sure the browser terminal is ready. Then, instructions for the range of speeds you’ll use are printed.

The main loop starts with the input statements from terminal_controlled_bot_tethered_your_turn.

Next, the values are stored in a dictionary. This makes it easy because the receiver just has to do the reverse to get the vL, vR, and ms values back out of the dictionary it received. For more background on this, see the Dictionary Primer.

The dictionary is converted to a string named packet. That packet is displayed with a serial write statement and then transmitted with a call to radio send long string.

Like the transmitter project, the wireless receiver project’s radio call is also similar to countdown_receiver.

Inside the on radio received (rLongString) loop, the project waits for the radio to receive a signal. 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 on radio received returns a string that contains the dictionary. That gets stored in a variable named rLongString.

When on radio received returns None, the rest of the project gets skipped and the loop gets ignored. When on radio received returns a string (with characters that represent a dictionary), the statements under if (not ((rLongString) is empty)) are executed, starting with printing the packet. (You can only see that if you have the cyber:bot tethered and connected to a terminal.)

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

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.

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 pause before stopping the servos again.
