Creating and running Python programs on the BeagleBone Black

Now that you can get around, and even edit programs, you can begin to use the BeagleBone Black to create programs so you can control your robotic projects.

Prepare for lift off

Now that you are ready to begin programming, you'll need to choose a language. There are many available, C, C++, Java, Python, Perl, and a great deal of other possibilities. I'm going to introduce you to Python for two reasons. First, it is a straightforward language that is intuitive and very easy to use. Second, much of the open source functionality in the robotics world is available in Python. We'll also cover a bit of C in this chapter as well, as some functionality is only available in C. But it makes most sense to start in Python. To work the examples in this section, you'll need a version of Python installed to complete this section. Fortunately the basic Ubuntu system has a version already installed, so you are ready to begin.

We are going to just cover some of the very basic concepts here. If you are new to programming, there are a number of different websites that provide interactive tutorials. If you'd like to practice some of the basic programming concepts in Python using these tools, try www.codeacademy.com or http://www.learnpython.org/ and give it a try. There are also a number of excellent books, for example, A Byte of Python.

Engage thrusters

In this section we'll cover how to create and run a Python file. It turns out that Python is an interactive language, so you could run Python and then type in commands one at a time. But we want to use Python to create programs, so we are going to type our commands using Emacs and then run them from the command line by invoking Python. Let's get started.

Open an example Python file by typing emacs example.py. Now, let's put some code in the file. Start with these five lines:

Engage thrusters

Tip

Note, your code may be color coded. I have removed the color coding here so that it is easier to read.

Here is an explanation of the code.

Let's go through the code to see what is happening:

  1. a = input("Input value: "): One of the basic needs of a program is to get input from the user. raw_input allows us to do that. The data will be input by the user and stored in the variable a. The prompt Input value: will be shown to the user.
  2. b = input("Input second value: "): This data will also be input by the user and stored in the variable b. The prompt Input second value: will be shown to the user.
  3. c = a + b: This is an example of something you can do with the data; in this example you can add the variables a and b.
  4. print c: Another basic need of our program is to print out results. The print command prints out the value of c to the display.

Once you have created your program, save it (using Ctrl + X then Ctrl + sS) and quit Emacs (using Ctrl + X then Ctrl + C). Now from the command line run your program by typing python example.py. You should see something like this:

Engage thrusters

You can also run the program right from the command line without typing python filename by adding one line to the program. Now the program looks like this:

Engage thrusters

Adding #!/usr/bin/python as the first line simply makes this file available for us to execute from the command line. Once you have saved the file and exited Emacs, type chmod +x example.py. This will change the file's execution permissions so the computer will now believe it and execute it. You should be able to simply type ./example.py and the program should run, like this:

Engage thrusters

Notice that if you simply type example.py, the system will not find the executable file. In this case the file has not been registered with the system, so you have to give it a path to the file, in this case ./ is the current directory.

Objective complete – mini debriefing

Now that you know how to create, enter, and run your simple Python programs, let's look at some programming constructs.

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

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