Chapter 2. Programming Raspberry Pi

Now that your system is up and running, you'll want Raspberry Pi to start working. Almost always, this requires you to either create your own programs or edit someone else's programs. This chapter will provide a brief introduction to editing a file and programming.

While it is fun to build hardware (and you'll spend a good deal of time designing and building your robots), your robots won't get very far without programming. This chapter will help introduce you to file editing and programming concepts so you'll feel comfortable creating some of the fairly simple programs that we'll discuss in this book. You'll also learn how to change existing programs, making your robot do even more amazing things.

In this chapter, we will cover the following topics:

  • Basic Linux commands and navigating the filesystem on Raspberry Pi
  • Creating, editing, and saving files on Raspberry Pi
  • Creating and running Python programs on Raspberry Pi
  • Some of the basic programming constructs on Raspberry Pi
  • How the C programming language is both similar and different to Python so you can understand when you need to change C code files

We're going to use the basic configuration that you created in Chapter 1, Getting Started with Raspberry Pi. You can accomplish the tasks in this chapter by connecting a keyboard, mouse, and monitor to Raspberry Pi, or remotely logging in to Raspberry Pi using vncserver or SSH. All of these methods will work in executing the examples in this chapter.

Basic Linux commands on Raspberry Pi

After completing the tasks in Chapter 1, Getting Started with Raspberry Pi, you should have a working Raspberry Pi running a version of Linux called Raspbian. We selected the Linux version because it is the most popular version, and thus has the largest range of supported hardware and software. The commands reviewed in this chapter should also work with other versions of Linux, but the examples shown in this chapter use Raspbian.

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

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 LXTerminal application to the left-hand side of the screen as shown in the following screenshot:

Basic Linux commands on Raspberry Pi

If you are using SSH, you should already be at the terminal emulator program. Either way, the terminal should look something as shown in the following screenshot:

Basic Linux commands on Raspberry Pi

Your cursor is at the command prompt. Unlike with Microsoft Windows or Apple's OS, with Linux, most of our work will be done by actually typing commands in the command line. So, let's try a few commands. First, type ls; you should see the result as shown in the following screenshot:

Basic Linux commands on Raspberry Pi

In Linux, the command ls is short for list-short, and it lists all the files and directories in our current directory. You can tell apart the different file types and directories because they are normally in different colors.

You can move around the directory structure by issuing the cd (change directory) command. For example, if you want to see what is in the Desktop directory, type cd ./Desktop. If you issue the ls command now, you should see something as shown in the following screenshot:

Basic Linux commands on Raspberry Pi

This directory mostly has definitions for what should appear on this Desktop folder. Now, I should point out that we used a shortcut when we typed cd ./Desktop. The dot (.) character is a shortcut for the default directory. The command cd is short for change directory. You may also have typed cd /home/pi/Desktop and received the exact same result; this is because you were in the /home/pi directory, which is the directory where you always start when you first log in to the system.

If you ever want to see which directory you are in, simply type pwd, which stands for print working directory. If you do that, you should get the result shown in the following screenshot:

Basic Linux commands on Raspberry Pi

The result of running the pwd command is /home/pi/Desktop. Now, you can use two different shortcuts to navigate back to the default directory. The first is to type cd .. in the terminal; this will take you to the directory just above the current directory in the hierarchy. Then type pwd; you should see the following screenshot as a result:

Basic Linux commands on Raspberry Pi

The other way to get back to the home directory is by typing cd ~, as this will always return you to your home directory. If you were to do this from the Desktop directory and then type pwd, you should see the result as shown in the following screenshot:

Basic Linux commands on Raspberry Pi

Another way to go to a specific file is using its entire pathname. In this case, if you want to go to the /home/Raspbian/Desktop directory from anywhere in the filesystem, simply type cd /home/Raspbian/Desktop.

There are a number of other Linux commands that you might find useful as you program your robot. The following is a table with some of the more useful commands:

Linux command

What it does

ls

List-short: This command lists all the files and directories in the current directory by just their names.

rm filename

Remove: This command deletes the file specified by filename.

mv filename1 filename2

Move: This command renames filename1 to filename2.

cp filename1 filename2

Copy: This command copies filename1 to filename2.

mkdir directoryname

Make directory: This command creates a directory with the name specified by directoryname; this will be made in the current directory unless otherwise specified.

clear

Clear: This command clears the current terminal window.

sudo

Super user: If you type the sudo command at the beginning of any command, it will execute that command as the super user. This may be required if the command or program you are trying to execute needs super user permissions. If, at any point in this book, you type a command or the name of the program you want to run and the result seems to suggest that the command does not exist or permission is denied, try doing it again with sudo at the beginning.

Now you can play around with the commands and look at your system and the files that are available to you. But, be a bit careful! Linux is not like Windows: it will not warn you if you try to delete or replace a file.

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

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