01Robotics - Programming Basics
Transcript of 01Robotics - Programming Basics
-
8/9/2019 01Robotics - Programming Basics
1/21
Programming Basics -RobotC
Introduction to Robotics
-
8/9/2019 01Robotics - Programming Basics
2/21
Thinking aboutProgrammingCreating a successful robot takes a team effort
between humans and machines.
Role of the Robot
The robot follows the instructions
it is given, thereby carrying out the
plan.
-
8/9/2019 01Robotics - Programming Basics
3/21
Human/Machine
Communication
Because humans and robots don’t normally speak the samelanguage, a special language must be used to translate the
necessary instructions from human to robot. These humantorobot languages are called programming languages. Instructionswritten in them are called programs. R!B!TC is "ust one ofmany such programming languages that humans use to talk tomachines.
-
8/9/2019 01Robotics - Programming Basics
4/21
Think about “Behaviors”Behaviors are a convenient way to talk about what a robot is doing
and what it must do. #oving forward, stopping, turning, lookingfor an obstacle$ these are all behaviors.
Complex Behavior
%ome behaviors are big, like
&solve the ma'e.(
Basic or Simple Behavior
%ome behaviors are small, like &go
forward for ) seconds.( Big
behaviors are actually made up of
these smaller ones.
-
8/9/2019 01Robotics - Programming Basics
5/21
Planning the Behaviors
-
8/9/2019 01Robotics - Programming Basics
6/21
-
8/9/2019 01Robotics - Programming Basics
7/21
-
8/9/2019 01Robotics - Programming Basics
8/21
PSU!"C"!
*s the programmer becomes moree+perienced, the organi'ation of the
behaviors in nglish will start to
include important techni-ues from
the programming language itself,
like ifelse statements and loops.
This hybrid language, halfwaybetween nglish and the
programming language, is called
pseudocode.It is an important tool
in helping to keep larger programs
understandable.
-
8/9/2019 01Robotics - Programming Basics
9/21
R"B"TC is te#t base$%Commands to the robot are first written as te+t on the
screen. They are then processed by the R!B!TCcompiler into a machine language file that the robot
can understand. inally, they are loaded onto the
robot, where they can be run.
-
8/9/2019 01Robotics - Programming Basics
10/21
Co$eTe+t written as part of a program is called code. /ou
type code "ust like you type normal te+t. 0eep in mindthat capitali'ation is important to the computer.Replacing a lowercase letter with a capital letter 1or acapital letter with a lowercase letter2 will cause therobot to become confused.
-
8/9/2019 01Robotics - Programming Basics
11/21
Co$e COLOR
*s you type, R!B!TC will try to help you out bycoloring the words it recogni'es. If a word appearsin a different color, it means R!B!TC recogni'es itas an important word in the programming language.
-
8/9/2019 01Robotics - Programming Basics
12/21
Statements%tatements are instructions for the robot. The most
basic kind of statement in R!B!TC simply gives acommand to the robot. The motor[port3] = 127;statement in the sample program you downloaded isa simple statement that gives a command. It
instructs the motor plugged into #otor 3ort ) to turnon at full power.
-
8/9/2019 01Robotics - Programming Basics
13/21
"r$er the Statements%tatements are run in order as -uickly as the robot is
able to reach them. Running this program on therobot turns the motor on, then waits for )444milliseconds 1) seconds2 with the motor still running,and then ends.
-
8/9/2019 01Robotics - Programming Basics
14/21
RobotC RulesHow did ROBOTC know that motor[port3]= 127 and
wait1msec[3000] were two separate commands.5as it because they appeared on two differentlines6
7o. %paces and line breaks in R!B!TC are only used to separate words from
each other in multiword commands. %paces, tabs, and lines don’t affect the way
a program is interpreted by the machine.
-
8/9/2019 01Robotics - Programming Basics
15/21
R"B"TC Rules
%o why *R they on separate lines6 or theprogrammer. 3rogramming languages are designedfor humans and machines to communicate. 8singspaces, tabs, and lines helps human programmers
read the code more easily. #aking good use ofspacing in your program is a very good habit for yourown sake.
-
8/9/2019 01Robotics - Programming Basics
16/21
Punctuation%But what about R!B!TC6 9ow :I: it know where
one statement ended and the other began6 It knewbecause of the semicolon 1;2 at the end of each line.
very statement ends with a semicolon. It’s like the
period at the end of a sentence.
-
8/9/2019 01Robotics - Programming Basics
17/21
Punctuation Pairs3unctuation pairs, like the parentheses and s-uare
brackets in these two statements, are used to markoff special areas of code. very punctuation pairconsists of an opening punctuation mark and aclosing punctuation mark. The punctuation pairdesignates the area between them as having special
meaning to the command that they are part of.
-
8/9/2019 01Robotics - Programming Basics
18/21
Punctuation Pairs
:ifferent commands make use of different kinds of paired punctuation. The
motor command uses s-uare brackets and the wait1Msec command uses
parentheses. This is "ust the way the commands are set up. /ou will have to
remember to use the right punctuation with the right commands or plan.
-
8/9/2019 01Robotics - Programming Basics
19/21
Control StructuresSimple statements do the work in ROBOTC, but
control structures do the thinking. Control structures1or control statements2 are pieces of code thatcontrol the flow of the program’s commands, ratherthan issue direct orders to the robot.
!ne important control structure is task main. very R!B!TC program includes
a special section called task main. This control structure determines which code
the robot will run as part of the main program.
-
8/9/2019 01Robotics - Programming Basics
20/21
Control Structures
-
8/9/2019 01Robotics - Programming Basics
21/21
CommentsComments are te+t that the program ignores. *
comment can contain notes, messages, andsymbols that may help a human, but would bemeaningless to the robot. R!B!TC simply skipsover them. Comments appear in green in R!B!TC.