Programming – Touch Sensors

Download Report

Transcript Programming – Touch Sensors

Programming – Touch
Sensors
Intro to Robotics
The Limit Switch
When designing robotic arms there is always the
chance the arm will move too far up or too far down.
These problems can be stopped if there was
something to PREVENT the arm from moving.
Operator Assist – Touch Sensor
You should see the following code above. Add the code below.
Robotic Arm?
Robotic Arm – Approach #2
Problem!!!
The robot does NOT drive
properly while handling the
arm controls. Each time an
arm control is activated the
system will stick as it
updates. In other words, the
program gets trapped inside
the “arm” while loops.
Solution? Not a loop! The “if” statement
The “if” conditional staement works similarly to
a while loop. Whereas a while ( ) loops runs
{body} commands over and over while a
(condition) is true, an “if” statement runs a
{body} of code ONCE if the (condition) is true,
and skips past it altogether if the (condition)
is false.
There is no loop so there is no risk of getting
stuck!
“If” statement
Change, check,
and save the code
with “arm” in the
title for later use.
The program already uses if-statements to check the value of the operator
inputs. We need to add a second check – on the limit switch – to determine
whether it is really appropriate to run the motors.
If you need to find out more than one thing from someone, you ask
several questions in order. Depending on an earlier answer, you
may need to ask a follow-up question. This is the basic principle
behind a nested conditional statement.
???
PROBLEM! The program
doesn’t work right
Operator Assist – Limiting the Arm
The program is partly working, but we clearly left something
out. We told the robot what to do when the Transmitter
button was held and the Limit Switch was not being
pressed. But we didn’t tell it what to do when the Limit
Switch was pressed! The arm simply continued to run at
the last given motor power because it was not told to stop.
The if-else statement is simply an if-statement with an
additional else block added on that describes a second
{body} of code to run if the (condition) is false.
If-Else
Pay careful attention to which if each else belongs to. The else blocks belong
to the inner if-statements (starting on lines 19 and 30 below). These are the
statements that check the Limit Switches, not the ones that check the
Transmitter buttons. They tell the robot what to do if you answer “no” to the
followup question, not the opener — you had to answer “yes” to the opener
for the followup to even be asked.
When you press the button on an elevator, you only need to
press it once. When you press a button on your
Transmitter, however, you have to hold it down, or its effect
stops. The elevator remembers that its button was pressed,
but your robot does not. It would certainly be convenient if
your robot could also use its memory to keep track of
important commands, so that you don’t need to hold down
a button to remind it.
When you really need to remember something, it can be
helpful to write it down. By storing that information
somewhere permanent, you ensure that you can come
back to it later when it’s needed. The robot works in a
similar way. If it needs to keep track of something, it will
need to store that piece of information somewhere.
Robots store information in variables. Variables function
as containers, or storage, for values. As such, they can
hold concrete values, such as the -127, 0, or 127 sent by
the Transmitter buttons. They can also store abstract
values that a program uses to represent different things
(perhaps 1 means up, and -1 means down).
Variables and State Names
It is this last type of value, an abstract representation, that we
will be using to make the robot arm remember which way it
should be going, even after the Transmitter button stops
being pressed. Since the robot doesn’t directly understand
“up” or “down”, we will use one of the types of values that it
does understand to represent those directions. That value
will be stored in a variable for the robot to remember.
In order to create (or declare) a variable, the programmer
must identify two key pieces of information: the type of
value it will hold, and a name for the variable. The naming
rules for sensors that you learned earlier also apply to
variables. Give a variable a name that makes it easy to
remember what kind of information is stored in the variable.
This makes it easier to debug your program. Here are
some of the most common variable types used in
ROBOTC:
Variables
For the robotic arm, either letters or simple numbers
make sense as a way to represent the direction
that the arm should move. We will use numbers,
with the following values representing arm
directions:
The first step in the program is to create (or declare) the
variable. Declaring a variable requires you to specify its
type and name. We will use an integer (int) variable and
call it “armDirection”. It will store the -1, 0, or 1 representing
the direction of arm travel.
Change the existing motor-setting code to set the powers of the
motors based on the desired direction (value of the “armDirection”
variable) rather than the button being pushed. Continue to check
whether the Limit Switches are pressed in.
Summary and checking for
understanding
After checking your arm directions, apply your
own comments to the code and turn it in for
grading.