Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • Cybersecurity: Navigation Control from a Keyboard

Cybersecurity: Navigation Control from a Keyboard

How Tethered Terminal Control Works

Since this script involves cyber:bot motion, it imports the cyberbot module.  The first print statement (after the recommended 1 second startup delay) just reminds the person typing in the terminal that valid speeds are from -100 to 100.  The \n escape characters in the print string add lines before and after the “Speeds are -100 to 100” message.

# terminal_controlled_bot_tethered_intro

from cyberbot import *

sleep(1000)

print("\nSpeeds are -100 to 100\n")

The main loop starts with three input statements getting text from the user that represents values.  As mentioned in Terminal Talk, the input statement returns the character representations of the characters.   Each one is stored in the temporary variable named text.  The first one is text = input(“Enter left speed: “).  Then, vL = int(text) converts the text representation of a number to an actual value an int variable can store.  Remember, once it’s in an int variable, it can be used in calculations.  

while(True):
    text = input("Enter left speed: ")
    vL = int(text)

    text = input("Enter right speed: ")
    vR = int(text)

    text = input("Enter ms to run: ")
    ms = int(text)

In addition to working in calculations, int variables are required by the bot(pin).servo_speed(vL or vR) method as well as the sleep(ms) function call.  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 sleep.  That’s why vL = int(text) stores the int version of the “25” string in the vL variable.  The same applies to vR, and to ms for the sleep call.

Once vL, vR, and ms are all values stored in int variables, the script 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 bot(19).servo_speed(-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!

    bot(18).servo_speed(vL)
    bot(19).servo_speed(-vR)

The ms int variable is used in the sleep function’s argument so that the servos continue running for the ms time you typed into the terminal.  After that, servo_speed calls with the None arguments stop the servos from turning and complete the maneuver.

    sleep(ms)
    bot(18).servo_speed(None)
    bot(19).servo_speed(None)

    print()  

After this, the while(True) loop repeats, and you can type in another maneuver for the cyber:bot to execute.


Did You Know?

Input to int will always be a two step process, but it doesn’t have to take up two lines.  For example,

    text = input("Enter left speed: ")
    vL = int(text)

… can be condensed to:

    vL = int(input("Enter left speed: "))

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 input(“Enter left speed: “).  Then, that result (like the string “25”) is converted into an int with the outer int(…), 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 int(…) function needs something with digits to convert to an int type.  It can convert the string “25” to the value 25, but it cannot convert the string “q5” to any value.  

The Exception Handling Primer addressed a more methodical and comprehensive way of dealing with this.    True, for apps lots of people use, that’s best, but there are lighter treatments for prototyping.  For example, all of the input and servo code can go into one try statement, and an except can just have a helpful message about typing numbers.

(Note this is not a complete script.)

while(True):
    try:
        vL = int(input("Enter left speed: "))
        vR = int(input("Enter right speed: "))
        ms = int(input("Enter ms to run: "))
    
        bot(18).servo_speed(vL)
        bot(19).servo_speed(-vR)
        sleep(ms)
        bot(18).servo_speed(None)
        bot(19).servo_speed(None)

        print()  

    except:
        print("Error in value entered.")
        print("Please try again. \n")

 


Printer-friendly version
Tethered Terminal Control Script
Prev
Try This: Condense the Input
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress