Book Image

Raspberry Pi Robotics Essentials

By : Richard Grimmett
Book Image

Raspberry Pi Robotics Essentials

By: Richard Grimmett

Overview of this book

Table of Contents (14 chapters)
Raspberry Pi Robotics Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Programming on Raspberry Pi


One last bit of introduction. You'll need some basic programming skills to be successful on your project. This section will touch a little on Python and C programming on the Raspberry Pi.

Creating and running Python programs on the Raspberry Pi

You'll be using Python for two reasons. First, it is a simple language that is intuitive and very easy to use. Second, a lot of open source functionality in the robotics world is available in Python. To work the examples in this section, you'll need a version of Python installed. Fortunately, the basic Raspbian system has a version already installed, so you are ready to begin.

Note

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, go to www.codeacademy.com or http://www.learnpython.org/ and give it a try.

But, to get you started, let's first 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. However, you want to use Python to create programs, so you are going to create Python programs and then run these programs from the command line by invoking Python.

Open an example Python file by typing in emacs example.py. Now, put some code in the file. Start with the lines shown in the following screenshot:

Note

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

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. The raw_input part allows us to do that. The data will be input by the user and stored in 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 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 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.

Once you have created your program, save it (using ctrl-x ctrl-s) and quit emacs (using ctrl-x ctrl-c). Now, from the command line, run your program by typing in python example.py. You should see something similar to the following screenshot:

You can also run the program right from the command line without typing in python example.py by adding one line to the program. Now, the program should look like the following screenshot:

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 in 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 in ./example.py and the program should run, as shown in the following screenshot:

Note that if you simply type in 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.

An introduction to the C/C++ programming language

Now that you've been introduced to a simple programming language in Python, you'll also need a bit of exposure to 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 know 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 into ones that contain programs to machine code that the processor can actually execute. To do this, type in sudo apt-get install build-essential. This will install the programs you need to turn your code into executables for the system.

Now that the tools are installed, let's walk through some simple examples. Here is the first C/C++ code example:

The following is an explanation of the code:

  • #include <iostream>: This is a library that is included so that your program can input data from the keyboard and output information to the screen.

  • int main(): As with Python, we can put functions and classes in the file, but you will always want to start execution at a known point; C defines this as the main function.

  • int a;: This defines a variable named a, of type int. C is what we call a strongly typed language, which means that we need to declare the type of the variable we are defining. The normal types are int, a number that has no decimal points; float, a number that requires decimal points; char, a character of text, and bool, a true or false value. Also note that every line in C ends with the ; character.

  • int b;: This defines a variable named b, of type int.

  • int c;: This defines a variable named c, of type int.

  • std::cout << "Input value: ";: This will display the string "Input value: " on the screen.

  • std::cin >> a;: The input that the user types will go into the variable a.

  • std::cout << "Input second value: ";: This will display the string "Input second value: " on the screen.

  • std::cin >> b;: The input that the user types will go into the variable b.

  • c = a + b: The statement is a simple addition of two values.

  • std::cout << c << std::endl;: The cout command prints out the value of c. The endl command at the end prints out a carriage return so that the next character appears on the next line.

  • return 0;: The main function ends and returns 0.

To run this program, you'll need to run a compile process to turn it into an executable program that you can run. To do this, after you have created the program, type in g++ example2.cpp –o example2. This will then process your program, turning it into a file that the computer can execute. The name of the executable program will be example2 (as specified by the name after the –o option).

If you run an ls on your directory after you have compiled this, you should see the example2 file in your directory, as shown in the following screenshot:

If you run into a problem, the compiler will try to help you figure out the problem. If, for example, you forgot the int before a in the expression int a, you would get the following error when you try to compile:

The error message indicates a problem in the int main() function and tells you that the variable a was not successfully declared. Once you have the file compiled, to run the executable, type in ./example2, and you should be able to create the following result:

Note

If you are interested in learning more about C programming, there are several good tutorials out on the Internet that can help-for example, at http://www.cprogramming.com/tutorial/c-tutorial.html and http://thenewboston.org/list.php?cat=14.

There is one more aspect of C you will need to know about. The compile process that you just encountered seemed fairly straightforward. However, if you have your functionality distributed between a lot of files or need lots of libraries, the command-line approach to executing a compile can get unwieldy.

The C development environment provides a way to automate this process; it is called the make process. When using this, you create a text program named makefile that defines the files you want to include and compile, and then, instead of typing a long command or set of commands, you simply type in make and the system will execute a compile based on the definitions in the makefile program. There are several good tutorials that talk more about this system-for example, http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/ or http://mrbook.org/tutorials/make/.

Now you are equipped to edit and create your own programming files. The next chapters will provide you with lots of opportunities to practice your skills as you translate lines of code into cool robotic capabilities.