Lesson: What is a Program?

Overview: 
Explore the general ideas behind programming and how it applies to robots.
Objectives: 

Understand the general idea of what the programming activity is, how we do it and how a program is the result. Understand how these ideas apply to a robot.

Content: 

Simply put, a program is a set of instructions that describe the actions we want a computer to take to reach the desired output (result) from a set of input(s). That set of actions, taken as a whole, is also called an algorithm. The instructions to making a peanut butter and jelly sandwich qualify as an algorithm:

Take two slices of bread
Open jelly jar
Open peanut butter jar
Spread jelly on one side of one slice
Spread peanut butter on one side of the other slice
Place the two slices together so that the covered faces touch

Keep in mind that just like regular instructions, a program is written sequentially, and will run sequentially. In this silly example, I am assuming some pre-built instructions are in place that provide the small details of how you open a jar of peanut butter, and that you should use a knife when doing the action of “spreading”. This is a good example of the fact that we will not have to give every tiny instruction at the lowest level, but will be able to use some pre-built functions that are provided by the programming language or a robotics support library (these functions would be an API).

While the particular statements or program design we might use will vary with each language, generally the steps to achieve the output are the same. We can express these steps in English using normal language to define the steps and create an initial design for a program, without worrying about how a specific programming platform wants us to express a particular instruction. In effect, we write a program in English. This is called using pseudo code, that is, a made up computer language.

For example, lets say we want a robot to drive in a square pattern, for 3 seconds on each side. We could write this program in pseudo code as follows:

drive.forward
wait.3seconds
drive.stop
turn.right.90degrees
drive.forward
wait.3seconds
drive.stop
turn.right.90degrees
drive.forward
wait.3seconds
drive.stop
turn.right.90degrees
drive.forward
wait.3seconds
drive.stop

Notice that we just made up this "language", but it does express the idea of what the program has tell the robot to do to achieve the desired result. The function that we need in order to move forward may not be exactly “drive.forward”, but writing this pseudo code gave us a framework to think about what we are doing without worrying about the specifics of the language.

Lets do another example. Lets say we want a robot to drive forward until it hits an obstacle, using a touch sensor to detect the obstacle. When we contact an obstacle we want to back up a bit and stop. The touch sensor is on the front of the robot.

drive.forward
label:keepdriving
if touchsensor.istouching
    drive.stop
    drive.backward
    wait.1second
    drive.stop
otherwise
    go to keepdriving
end of question

We can use pseudo code to think through how we can produce the desired result of the program and design the steps or algorithm needed to achieve the result. Then we can use that pseudo code to as a guide to write the actual program in a real programming language.

Note that block or visual programming tools are based on this idea of designing programs in simplified terms and then these tools write the actual program for you.

Another powerful tool you can use to design a program is a Flowchart. We are not going discuss them here but flowcharts are very useful in designing algorithms to solve problems. You can create flowcharts online at draw.io.

There other techniques and tools available to help in program design but once the algorithm is designed, you will express those steps in an actual programming language to create your program.

As an exercise, write the pseudocode instructions to get from your classroom to your school's front office. What are the functions in the API that you used to write your pseudocode?

 

Navigation: