Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
BeagleBone Robotic Projects

You're reading from  BeagleBone Robotic Projects

Product type Book
Published in Dec 2013
Publisher Packt
ISBN-13 9781783559329
Pages 244 pages
Edition 1st Edition
Languages
Author (1):
Richard Grimmett Richard Grimmett
Profile icon Richard Grimmett

Table of Contents (18) Chapters

BeagleBone Robotic Projects
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Getting Started with the BeagleBone Black Programming the BeagleBone Black Providing Speech Input and Output Allowing the BeagleBone Black to See Making the Unit Mobile – Controlling Wheeled Movement Making the Unit Very Mobile – Controlling Legged Movement Avoiding Obstacles Using Sensors Going Truly Mobile – Remote Control of Your Robot Using a GPS Receiver to Locate Your Robot System Dynamics By Land, Sea, and Air Index

Chapter 2. Programming the BeagleBone Black

Before you get started with building your robotic projects, let's take a bit of time to either introduce or review how to program the BeagleBone Black.

Mission briefing


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.

Why is it awesome?

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.

Your objectives

In this chapter we will:

  • Introduce some of the basic Linux commands and show how to navigate around the filesystem on the BeagleBone Black

  • Show how to create, edit, and save files on the BeagleBone Black

  • Learn how to create and run Python...

Basic Linux commands and navigating the filesystem


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.

Prepare for lift off

So, power up your BeagleBone Black and log in using the proper username and password. If you are going to log in remotely, go ahead and establish the connection and log in. Now we will take a quick tour of Linux. This will not be extensive, but we will walk through some of the basic commands.

Engage thrusters

Once you have logged in, you should open up a terminal window. If you are logging in using a keyboard, mouse, and monitor, or using vncserver, you'll find the terminal selection by selecting the Applications Menu in the upper...

Creating, editing, and saving files on the BeagleBone Black


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.

Prepare for lift off

If you want to use Emacs, then download and install Emacs by typing sudo apt-get install emacs.

Engage thrusters

Once installed, you can run Emacs simply by typing emacs filename, where filename...

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...

Basic programming constructs on the BeagleBone Black


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.

Prepare for lift off

As with the previous section, once you have the basic system and Emacs, you are ready to start coding.

Engage thrusters

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...

Introduction to the C++ programming language


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.

Prepare for lift off

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.

Engage thrusters

Now that the tools are installed, let's walk through some simple examples...

Mission accomplished


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!

A challenge


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/.

lock icon The rest of the chapter is locked
You have been reading a chapter from
BeagleBone Robotic Projects
Published in: Dec 2013 Publisher: Packt ISBN-13: 9781783559329
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}