You are on page 1of 8

Robotics

Robots and similar automation work primarily off of the same topics you have been
studying: patterns and change. At their
simplest essence, they perform a pattern
until they detect some change. Typically
the detection
itself is part of
a pattern, too.
This could be
fairly simple,
such as
streetlights
going through
their pattern
until they
detect the
button for a pedestrian has been pressed, at which point
they alter their pattern for a short bit to let people cross
before returning to their prior pattern. Or it could be
more complex, as is needed for a humanoid robot like
Atlas to navigate an obstacle course. Regardless of the
complexity, this all boils down to the same fundamentals:
patterns and change.

In this unit you will learn to program robots to follow patterns and to look for
changes with various sensors. You will do this with drag-and-drop programming,
moving about blocks of code which seem a little like puzzle pieces so that you can
see what fits where. In many cases there may be several different ways to program a
robot to accomplish the same thing. Be creative, try things your way and adapt.

Engineering Design Cycle

In engineering, the process is far


more cyclical than linear. Once
you know what you are trying to
accomplish and the limits, you
enter the process. That process
is to design, implement, test,
evaluate, and then repeat all this
until you are finished. Your first
implementation will rarely work.
But by trying it out and
examining what works and what does not, you can go back and redesign. Then you
do the same with your new design, implementing it, testing it, and evaluating it. Do
this enough and you can accomplish a lot.
Robot Components

While they can do many things in many ways, the majority of robotics is based off of
motors, several types of sensors, and a computer.

 Brain
The robot’s “brain” is the computer that holds the code and
communicates between the various motors and sensors. They
also have timers built into them, allowing you to control or
measure how long actions last.

 Motor
Motors rotate shafts or axles connected to them. “Smart motors,”
such as the ones you will use, have encoders (see below) built
into them as well as feedback control to let them handle better.

 Remote Control
Many, though far from all, robots have some sort of manual
control. The one you will be using communicates wirelessly
with the robot’s brain. It has both sticks and buttons, which are
read in different ways.

 Encoder
Encoders measure how far an axle has turned. Your encoders are
built into the motors, so they measure how far the motor has
turned its axle. You can figure out how far a robot has traveled or
how much a joint has rotated.

 Gyroscope
A gyroscope measures how much something is turning. You can
use this to determine how much a robot has rotated, though that
could also be determined with encoders. When using
gyroscopes, be careful to pay attention to which direction is
positive.

 Color Sensor
Color sensors measure the light reflected from a nearby object.
Some only detect brightness. Some have their own little
flashlights. Yours only uses ambient light, but it can identify
different colors.
 Bumper Switch
Switches are commonly used to identify when the robot touches
something or when a device within it has reached some limit.

 Ultrasonic Distance Sensor


Ultrasonic rangefinders or distance sensors emit high frequency
sound and listen for the echo. Based on the time it takes the echo
to return, they determine how far away an object is.

 Touch LED
LED’s are light emitting diodes. They are electronic one-way
valves that give off specific colors of light depending on the
materials used to build them. Yours are sensitive to human
touch and can handle many different colors by using more than
just one LED.

Programming a Robot

Programming a robot is essentially the same as programming any other computer.


The program’s structure holds the key to the patterns and identifying when to
change the patterns.

Programming Loops

Though there are many variants on them, there are essentially two types of loops in
computer programming. The first is the finite loop, a loop that repeats a certain
number of times. For example, you might want to repeatedly add all the integers
from 0 to 10, so you add a new value exactly 10 times. The second is the indefinite
loop, a loop that repeats an undetermined number of times until something
happens. For example, you might want a program to keep running until the user
tells it to quit, but you don’t know when that will be. In some circumstances we
make the indefinite loop an infinite loop by never giving it an end condition. Let’s
take a look at some of these loops.
 Finite Loops

What if you want to have your robot drive


forward 2 seconds, wait for 1 second, and repeat
that whole process five times? You could write it
directly this way:

A couple times might not be so bad, but even five


times is getting to be a lot of copying and pasting.
But what if you wanted to do it dozens of times, or
hundreds of times?
Fortunately, there are better ways. The same thing
could be written this way:

This is an example of a finite loop. The robot repeats exactly the same pattern a
specified number of times. While there are other ways to write this, they are all
fairly similar.
 Indefinite Loops

What if you want your robot to keep driving forward until it reaches a barrier, such
as a wall? The best way to do things like this is to write them into indefinite loops,
repeating some pattern until some condition to change is met. We could write such
code with a distance sensor, looking for the distance to the barrier to get below
some threshold like 5 cm:

Or we could use the same structure with a bumper, looking for when the bumper
hits the barrier:

Note the in both of these cases we repeated nothing, but in most cases you would
actually want the robot to repeat something.

 Infinite Loops

As a general rule in computer programming, you don’t want to use infinite loops.
But in our case and in others we may want the program to repeat something until
it’s turned off, and we have a method to turn it off that does not rely on this code.
For example, the following code will read one of joystick’s axes forever (until the
program is turned off), repeatedly setting a motor to match its value:
Programming Conditional Statements

While there are multiple ways to program conditional statements in computer


programming, they can all be written as an interpreted as if-then statements. The
computer will check to see if the given condition is true. If it is true, whatever code is
listed there will be performed and any other possible conditions will be skipped. If it
is not true, the next condition will be checked in the same fashion, and onward until
either one of the conditions has been met or there are no more conditions.

For example, what if you want your robot to drive forward only if its color sensor
sees green? You could write it like this:

Or what if you want your robot to drive forward if its color sensor sees green, or
turn right if it sees red, but nothing else? You can add “else if” to place more
conditions after the first one like this:
But what if you want your robot to drive forward if its color sensor sees green, turn
right if it sees red, and drive backward in all other cases? There is no specific
condition to add with another “else if,” though it could be managed. We generally
use a statement about to do otherwise by adding “else” at the end of our if
statement, after the “if” and all of the “else if’s.” Adding an “else” at the end is a
catchall; if none of the conditions listed prior to it have been met, then the code
within it will be run. So we could add “else” to our prior code like this:

Programming Variables

You may have noticed the use of GREEN and RED in the last section to hold the
numbers 7 and 1, respectively. GREEN and RED were variables. Sometimes you
want to put a word with a number so you know what the number means more
readily. That was the case in the last section, where it was easier to read the code to
know what the color sensor was looking for; it makes more sense to see if the color
sensor sees GREEN than if the color sensor sees color id 7. Sometimes we want to
use a value in a number of spots and have them all change together, and it is easier
to change a variable once and have all the various spots use this variable than it is to
track down all the various uses of that value or to write different code for different
values. Sometimes we want to keep track of a value that may be changing. These are
all reasons to use variables.

Variables in computer science are much as they are in mathematics. They are
symbols that represent values. For instance, in math you might have x, and in a
particular problem you might know that x=4, so you put 4 in everywhere x is when
you want to do the calculation. Here “x” is the symbol representing the value “4.”
The main difference in how they are used in computer science is the distinction
between setting the value and reading the value. For instance, in math x=x+2 makes
little sense, while in programming it typically means you are setting “x” equal to
what it used to be plus two. The first “x” is setting the value, while the second “x” is
reading the value. That does mean we need to distinguish between checking if x
equals 2 versus setting x equal to 2, for instance. That comes down to the specifics of
the computer programming language being used.

Here is an example. Try to figure out what number “x” represents in the end.

If you got 24, you’re right! It starts with x=3. Then three times the program adds x to
itself and stores that value as the new x. The first time through the loop results in
3+3=6, so x=6 now. The second time through the loop results in 6+6=12, so x=12
now. The third time through the loop results in 12+12=24, so x=24 now, and the
loop ends there.

While variables commonly represent numbers, there are other possibilities. A


common other possibility is “true” or “false.” We call a variable that holds “true” or
“false” a Boolean. A handy thing about a Boolean is that it can be checked as the
condition for an “if-then” statement without having to compare it to anything else.

You might also like