LEARN.PARALLAX.COM
Published on LEARN.PARALLAX.COM (https://learn.parallax.com)
Home > Cybersecurity: Navigation Control from a Keyboard

Cybersecurity: Navigation Control from a Keyboard

What it’s about

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.

Before you start

You will need:

  • At least one cyber:bot and one additional micro:bit (on or off a cyber:bot)
  • At least one USB A to microB cable

Complete these tutorials first:

  • Navigation with the cyber:bot
  • Computer-micro:bit Talk
  • Dictionary Primer (optional)
  • Cybersecurity: Radio Basics
  • Cybersecurity: Radio Data

After you finish

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!):

  • Cybersecurity: Radio Tilt Control

Terminal Control — Start Tethered

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.

Tethered Terminal Control project

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

  • Make sure the cyber:bot’s batteries are connected, and its 3-position switch is set to 2.
  • Open a browser with makecode.microbit.org and add the cyber:bot extension to your project.
  • Enter this project as terminal_controlled_bot_tethered_intro. 
  • Flash the project into the micro:bit.

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.

  • Open a new browser and navigate to Google Chrome Labs Serial Terminal to open the terminal, press connect, and choose your micro:bit. 
  • Make sure you have the local echo option checked. Click in the terminal and type 25 in response to the Enter right speed: prompt, then press Enter.
  • Type 25 in response to the Enter left speed: prompt, then press Enter.
  • Type 750 in response to the Enter ms to run: prompt, and press Enter.
  • Verify that your cyber:bot rolled forward for about ¾ of a second.
  • Next, try -25, -25, and 750. Did it roll backwards?
  • Now, try 25, -25, 750. Did it pivot to the right?
  • Try 25, 10, 750 for a curving motion.
  • Can you make it turn in place left and right?
  • Try causing an exception by typing non-digits in response to one of the prompts. 

For example, what happens if you type abc and press enter? You will have to press/release the reset button to restart the app. 

How Tethered Terminal Control Works

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.

Did You Know?

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. 

Try This: Condense the Input

Let’s try reducing the input block count from six to three.

  • Duplicate terminal_controlled_bot_tethered_intro as terminal_controlled_bot_tethered_try_this. 
  • Replace these six blocks:

 …with these three blocks:

  • Check your modified project, it should resemble the example below.
  • Flash the modified project, and repeat the tests you did with the intro version of the program. It should run the same modified as it did before.

Example project: terminal_controlled_bot_tethered_try_this

Terminal Control — Go Wireless!

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.

Transmitter Project for Keyboard Inputs

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:

  •  Open the countdown_sender project from Send and Receive Packets.
  • Find these blocks:

  • Replace them with these blocks we made earlier:

  • Then find this block:

  • Replace it with this block:

  • Find these blocks:

Replace it with this block:

  • Make sure your using the radio send long string block:
  • If you are in a classroom and using assigned channels, update your channel. 
  • Duplicate and rename the project terminal_bot_controller_wireless.

Now, your project should be ready. 

  • Verify the changes you made against the example project shown below.
  • Load the code into the micro:bit you plan to use as the transmitter.

Example Project: terminal_bot_controller_wireless

Receiver Project for cyber:bot Output

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:

  • Open countdown_receiver from Send and Receive Packets.
  • Find these blocks:

  • Replace them with these blocks:

  • Make sure the cyberbot extension is loaded on your micro:bit.
  • Rename the project terminal_controlled_bot_wireless.
  • Find these blocks:

  • Replace them with these blocks:

  • Delete these blocks:

  • Find these blocks:

  • Replace them with these blocks:

  • If you are in a classroom and using assigned channels, update your channel. 

Now, your project should be ready. 

  • Verify the changes you made against the example project listed below.
  • Set your project’s name.
  • 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 Project: terminal_controlled_bot_wireless

Test It: Navigate Wirelessly

Now, it’s time to reconnect your transmitter micro:bit and start radio-broadcasting navigation commands for your cyber:bot.

  • Reconnect your micro:bit to USB.
  • Use the makecode.microbit.org editor, use the Connect button to establish the serial connection.
  • Open a terminal in a new browser and connect to the desired micro:bit, make sure the local echo option is checked.
  • If you do not see a user prompt, press and release the micro:bit’s reset button.

Next, repeat the tests you typed into the app when it was tethered:

  • Type 25 in response to the Enter right speed: prompt, then press Enter.
  • Type 25 in response to the Enter left speed: prompt, then press Enter.
  • Type 750 in response to the Enter ms to run: prompt, and press Enter.
  • Verify that your cyber:bot rolled forward for about ¾ of a second.

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.

  • Next, try -25, -25, and 750. Did it roll backwards?
  • Now, try 25, -25, 750. Did it pivot to the right?
  • Try 25, 10, 750 for a curving motion.
  • Can you make it turn in place left and right?

How the Wireless Controller Project Works

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.

How the Wireless Controlled Bot Project Works

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.

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024


Source URL:https://learn.parallax.com/courses/cybersecurity-navigation-control-from-a-keyboard_makecode/
Links