Using the keyboard to control your project

Now the keyboard is connected, let's figure out how to accept commands on the BeagleBone Black.

Prepare for lift off

You can now enter commands wirelessly. The next step is to create a program that can take these commands and then have your project execute them. There are a couple of choices here and I'll give you examples of both. The first is simply to include the command interface in your program. Let's take the example of the program you wrote to move your wheeled robot, robot.py. If you like you can copy that program using cp robot.py remote.py. The following screenshot shows a listing of the current program in the area you want to change:

Prepare for lift off

Engage thrusters

In order to add user control, you need two new programming constructs: the while loop and the if statement. Let's add them to the program, and then I'll explain what they do. The following screenshot shows a listing of the area of the code you are going to change:

Engage thrusters

You will edit your program by making the following changes. Add the following code just below the if __name__="__main__" statement:

  1. var = 'n': This will define a variable named var, which will be of type character, which you will use in your program to get the input from the user.
  2. While var != 'q': This will put your program in a loop. This loop will repeat until you, or the user, enters the letter q.
  3. var – raw_input(">"): This will get the character value from the user.
  4. If var == '<': This checks the value that you got from the user. If it is a < character, the robot will turn left by running the right DC motor for half a second. (You will need to determine how much time should be utilized to run the right DC motor for a left turn. The actual time value, .5 in this case, may need to be higher or lower.)
  5. Type the next few lines of code, which will send a speed command to the motor, wait for 0.5 seconds, and then send a command for the motor to stop.
  6. If var == '>': This checks the value that you got from the user. If it is a > character, the robot will turn right by running the left DC motor for half a second. (You will need to determine for how much time to run the left DC motor for a right turn. The actual time value, .5 in this case, may need to be higher or lower.)
  7. Type the next few lines of code, which will send a speed command to the motor, wait for 0.5 seconds, and then send a command for the motor to stop.
  8. If var == 'f': This checks the value that you got from the user. If it is an f character, the robot will run forward by running the right and left DC motors for half a second. (You will need to determine the speed to set each motor for a straight forward path.)
  9. Type the next few lines of code, which will send a speed command to both the motors, wait for 0.5 seconds, and then send a command for both motors to stop.
  10. If var == 'r': This checks the value that you got from the user. If it is an r character, the robot will run backward by running the right and left DC motors for half a second. (You will need to determine the speed to set each motor for a straight backward path.)
  11. Type the next few lines of code, which will send a speed command to both the motors, wait for 0.5 seconds, and then send a command for both motors to stop.

Once you have edited the program, save it and make it executable by typing chmod +x remote.py. Now you can run the program, but you must run it by typing the command using the wireless keyboard. If you are not yet logged in to the BeagleBone Black directly, make sure you can see the LCD screen and log in using the wireless keyboard. You can now disconnect the LAN cable; you will be able to communicate with the BeagleBone Black via the wireless keyboard. The system should look similar to what is shown in the following image:

Engage thrusters

Using the wireless keyboard and LCD screen, change the directory to the one that holds the remote.py program. In my case this file was in the /home/ubuntu/smc_linux directory, so I used the cd smc_linux command from my home directory. Now you can run the program by typing ./remote.py. The screen will display a prompt, and each time you type the appropriate command (<, >, f, or r) and press Enter, your robot should move. You need to be advised that the range of this technology is at best around 30 feet, so don't let your robot get too far away.

Objective complete – mini debriefing

Now you can move your robot around using the wireless keyboard! You can even take your robot outside. You don't need the LAN cable to run your programs because you can run them using the LCD display and keyboard.

Classified intel

There is one more change you can make so that you don't have to hit the Enter key after each input character is typed. In order to make this work, you'll need to add some import commands to your program and then add one function that can get a single character without pressing the Enter key. The following screenshot shows the first change you'll need to make:

Classified intel

You'll need to add import tty, import sys, and import termios. These are all the libraries that you'll need for your function to work. The following screenshot shows the function itself, and how you're going to use it:

Classified intel

Copy the code from function def getch(): into your program. I'm not going to explain it in detail, just know that it gets a single character without the need of pressing the Enter key after each keystroke. The print statement in the function is optional; I like to use it to map the different keys of my keyboard. Then instead of using var = raw_input(">") to get the character, use var = getch().

Note

The program changes your terminal settings so when you run your program, you can no longer stop the program by typing Ctrl + C. You'll need to type q, and your terminal setting will be restored.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.142.2