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