Now that you are up and running, you'll want your BeagleBone Black to start doing something. This requires you to either create your own programs, or edit an existing program. This chapter will provide a brief introduction into editing a file and programming.
It is fun to build hardware, and you'll spend a good deal of time designing and building your robots, but without programming, your robots won't get very far. This chapter will introduce you to file editing and programming concepts, so you'll feel comfortable creating some of the fairly simple programs that we'll talk about through the book. You'll also know how to change programs that are already available, making your robot do even more amazing things.
After completing Chapter 1, Getting Started with the BeagleBone Black, you should have a working BeagleBone Black running a version of Linux called Ubuntu. We selected this distribution because it is the most popular and has the largest set of supported hardware and software. The commands I am going to review should also work with other versions of Linux, but I'll be showing examples using Ubuntu.
Now that you can log in and move easily between directories and see which files are in your directories, you'll want to be able to edit those files. To do this, you'll need a program that allows you to edit the characters in a file. If you are used to working in Microsoft Windows, you probably have used a program like Microsoft Notepad, Wordpad, or Word to do this. As you might imagine, these are not available in Linux. There are several choices, all of which are free. I am going to show you how to use an editor program called Emacs. Other possibilities are programs like nano, vi, vim, and gedit. Programmers have strong preferences about which editor to use, so if you already have a favorite, you can skip this section.
If you want to use Emacs, then download and install Emacs by typing sudo apt-get install emacs
.
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.
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...
Now that you know how to enter and run a simple Python program on the BeagleBone Black, let's look at some more complex programming constructs. Specifically, we'll cover what to do when we want to decide which instructions to execute and show how to loop our code to do the same thing more than once. I'll give a brief introduction into how to use libraries in the Python code, and how to organize statements into functions. Finally I'll very briefly cover object oriented code organization.
As with the previous section, once you have the basic system and Emacs, you are ready to start coding.
As you have seen, your programs normally start with the first line of code and then continue, executing the next line, until your program runs out of code. This is fine, but what if you want to decide between two different courses of action? We can do this in Python using an if
statement. Here is some example code:
Here...
Now that you've been introduced to a simple programming language in Python, we need to spend a bit of time talking about a more complex but powerful language called C++. C++ is the original language of Linux, and has been around for many decades, but is still widely used by open source developers. It is similar to Python, but is also a bit different, and since you may need to understand and make changes to C++ code, you should be familiar with it and how it is used.
As with Python, you will need to have access to the language capabilities. These come in the form of a compiler and build system, which turns your text files that contain programs to machine code that the processor can actually execute. To do this, type sudo apt-get install build-essential
. This will install the programs you need to turn your code into executables for the system.
It is always a bit difficult to try new things. If this is your first attempt at programming, you might feel a bit uncomfortable as I ask you to create or edit files. However, I will try to give you explicit instructions on what to type so that you can be successful. There is one major challenge with working with computers. They always do exactly what you tell them to do, not necessarily what you wanted them to do. So if you encounter problems, check several times to make sure that your code matches the example exactly. Now, on to some actual coding!
If you are going to do a significant amount of coding you'll want to install an IDE, or Integrated Development Environment. These environments make it much easier to see, edit, compile, and debug your programs. The most popular of these programs in the Linux world is called Eclipse. If you'd like to know more, start with a Google search, or go to http://www.eclipse.org/.