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")