07-robotbasic
TRANSCRIPT
-
8/11/2019 07-RobotBASIC
1/35
GET130 Intro to Engineering Technology
Fall 2013
-
8/11/2019 07-RobotBASIC
2/35
Programming is FUN!
in addition to being a software package user, engineersand ETs can often benefit from being able to programcomputers for particular tasks solving special / newly discovered problems
simplify your job function / responsibilities promotions!
to do so, we need to learn a programming language rules, reserved words, syntax
MANY computer programming languages / environmentsexist general purpose / special purpose
mathematical/scientific, database/e-commerce, process control,artificial intelligence
BASIC, C, C++, Java, COBOL, Matlab, LabVIEW, PLCs
2
-
8/11/2019 07-RobotBASIC
3/35
BASIC
BeginnersAll-purpose Symbolic Instruction Code
very easy to learn & use carefree formatting, data typing, syntax
created in 1964 at Dartmouth College a family of general-purpose, high-level programming
languages over 300 dialects (http://en.wikipedia.org/wiki/List_of_BASIC_dialects)
has evolved through many incarnations over the years: Tiny-BASIC, Apple BASIC, Micro-Soft BASIC, IBM QuickBASIC,
Microsoft Visual Basic, VB.NET
and now:
Parallax PBASIC, RobotBASIC
3
http://en.wikipedia.org/wiki/List_of_BASIC_dialectshttp://en.wikipedia.org/wiki/List_of_BASIC_dialects -
8/11/2019 07-RobotBASIC
4/35
LANGUAGE SUMMARY
4
-
8/11/2019 07-RobotBASIC
5/35
BASIC Programs
BASIC is a line-oriented language
Program= series of statements
StatementStatement
Statement
statements may contain various language elements
5
-
8/11/2019 07-RobotBASIC
6/35
Language Elements
numbers
variables
operators
expressions constants
strings
functions
6
covered on the following slides
-
8/11/2019 07-RobotBASIC
7/35
Numbers
numeric values for computation, etc
four types in RobotBASIC:: whole numbers
between -2,147,483,648 +2,147,483,647 (32 bit representation)
include a decimal point
2.23E-308 1.79E+308
(32 bit) integers: start with 0%
ex: n=0%110101 // same as n=53
(base 16) integers: start with 0x ex: n=0x1F // same as n=31
7
number of unique binary patterns with n bits = 2n
-
8/11/2019 07-RobotBASIC
8/35
Variables
are programmer-defined names of storage locations
variable naming rules
must start with a letter, can then have any combination of letters &numbers; are case sensitive
cannot be same as a command, function name, constant name orlabel
RobotBASIC allows variables up to 255 characters
RobotBASIC variables are typeless and do not requiredeclaration before use
8
goals = 2 / / goal s i s t he var i abl e name
-
8/11/2019 07-RobotBASIC
9/35
Operators
quite similar to calculatoroperators
including precedence rules
RobotBASIC operatorsummary (in precedenceorder) shown to right
note: some operators areRobotBASIC-specific
see Helpfile for examples
9
( ) overrides other precedence rules
- unary negation
^ exponentiation
*, / multiply, divide
# modulus (remainder)
% numeric percentage
+, - add, subtract
$ substring test
less, less or equal, greater or equal, greater
=, == equality
, >>,
-
8/11/2019 07-RobotBASIC
10/35
Expressions
a formula comprised of numbers, variables, strings,function calls, operators, and other expressions that yieldsa result
used in variable assignments as well as parameters tocommands and function calls
notice precedence rules in that last statement
10
I nput "Radi us: ", radiusci r cumf er ence = 2*pi()*radiuspr i nt "Ci r cumf er ence = " , circumferencepr i nt "Ar ea = " , pi() * radius^2
function callnumeric constant
variable referenceoperatorsoperators
-
8/11/2019 07-RobotBASIC
11/35
Constants
RobotBASIC provides many predefined names for certainnumeric values
always more meaningful compared to using number
instead of:
also useful in measure conversions:
cc = conversion constant
11
Ci r cl e 10, 10, 30, 30, Red
Ci r cl e 10, 10, 30, 30, 4
pr i nt Conver t ( 212, cc_DFtoDC)
-
8/11/2019 07-RobotBASIC
12/35
Strings
textual content, arrays of characters
delimited with quotation marks
can apply certain operators to strings
+ performs string concatenation $ performs substring testing
12
msg = "al i ve! "Pr i nt "Hel l o Wor l d, "i f " l i ve" $ msg t hen
Pr i nt " I m " + msg + " ! "
-
8/11/2019 07-RobotBASIC
13/35
Functions
use to call a built-in or programmer-defined subprogramunit that returns a result
can be used in expressions like pi() in previous example
format:FunctionName(parameter1,parameter2)
parameters separated by commas, may be optional
note use of parentheses; Command statements do not use them
13
Pr i nt " l og( 1000) = " , log(1000)
-
8/11/2019 07-RobotBASIC
14/35
RobotBASIC Functions
there are MANY functions built in to RobotBASIC
categories: trig, logarithmic, exponential, coordinate, financial
string manipulation
scaling, weight & measure, numberstring
& I/O
, , chrono/calendar, system information
refer to Helpfile for details
14
-
8/11/2019 07-RobotBASIC
15/35
Statement Types
Assignment: assign a value to a variable
Command: built-in features that perform various tasks
Flow Control: control the sequence of program execution
Comment: provides documentation not actually statements
Label: provides a name for a program location
15
-
8/11/2019 07-RobotBASIC
16/35
Comments
Linecomments from // or to end of line
Blockcomments everything between /* and */
16
/ / t hi s i s a l i ne comment' so i s t hi s
count = 0 ' i ni t i al i ze count t o 0
/ *************************t hi s i s an exampl eof a bl ock comment
**************************/
17
-
8/11/2019 07-RobotBASIC
17/35
Assignment Statements assigns value to a variable or array element
value can be a constantor result of an expression
formats:Var = expression
Var[] = expression[ ] denotes index to an array variable
17
r adi us = 3. 5di amet er = 2*r adi us
Speed[ 5] = 12
18
-
8/11/2019 07-RobotBASIC
18/35
Command Statements
perform built-in tasks input, output, drawing, robot actions, etc.
format:CommandName parameter1,parameter2
commands are not case sensitive
parameters separated by commas, may be optional
18
Circle 20, 20, 40, 40, r ed r adi us=10, cent er =30, 30Print "Ci r cl e i s dr awn"End
19
-
8/11/2019 07-RobotBASIC
19/35
Labels
is a marker (think bookmark) for a particular location in the program
use by flow control statements to transfer execution to other than thenext line
includes a colon : where defined
RobotBASIC: can be numeric or alphanumeric style
19
I nput "How ol d ar e you? " , agei f age
-
8/11/2019 07-RobotBASIC
20/35
Flow Control Statements
control flow of program execution
unconditional
conditional tests & program branching
/ / /
iteration / repetition/
/
//
invoke subroutines / subprograms/
20
21
-
8/11/2019 07-RobotBASIC
21/35
Conditional: IF evaluates a numeric expression
and conditionally executesstatement(s) based on result
2 formats:IF expressionNTHEN statement
IF expressionNstatement
{ELSEIF expressionN}statement
{ELSE}statement
ENDIF
21
a=5If ( a- 3) >0 Then Pr i nt "ok"
' check cof f ee t emper at ur e
t = t empsensor ( )If t >190Pr i nt "t oo hi gh"
ElseIf t
-
8/11/2019 07-RobotBASIC
22/35
Iteration: FOR
used when number of loopsneeded is already known
format:
FOR var=expr1NTO expr2N
statement
NEXT
may include optional "STEP"
clause expression2 is generally >
expression1; however, canalso go in decreasing direction
22
For i =1 To 5Pr i nt " i =" , I
Next
For i =5 To 20 Step 5Pr i nt iNext
For i =100 To 0 Step - 1Pr i nt i , " bot t l es of beer "
Next' act ual l y, t hi s STEP i s opt i onal
23
-
8/11/2019 07-RobotBASIC
23/35
Iteration: REPEAT
used to repeat a loop until acondition is met
format:
REPEATstatement
UNTIL expressionN
statement(s) execute at leastonce
23
Pr i nt "guessi ng game"n=Random( 100)Repeat
I nput "guess?" , g
I f gn
pr i nt "t oo hi gh"El se
pr i nt " that ' s i t ! "
EndI fUntil g=n
24
-
8/11/2019 07-RobotBASIC
24/35
Iteration: WHILE
used to repeat a loop as long asa condition is true
format:
WHILE expressionNstatement
WEND
statement(s) may execute zerotimes!
24
' advance r obot car ef ul l y
' get i ni t i al di st anced=sensor_di st ance( )
While d > 5r For war d 5' get new r eadi ngd=sensor _di st ance( )
Wend
25
-
8/11/2019 07-RobotBASIC
25/35
Iteration: Alternative Loop Control
used to handle special circumstances
BREAK:
abandons enclosing loop and continues with statement followingloop
CONTINUE: forces next iteration of enclosing loop
25
26
-
8/11/2019 07-RobotBASIC
26/35
Subroutines: Gosub-Return
used to organize a codesequence into a module,typically reusable
formats:
GOSUBlabel
GOSUB expressionNstatement
code at labelis executed until aRETURN is reached
26
I nput "ent er N: " , nGosub Summat i onPr i nt "summat i on=", t ot alEnd
Summat i on:t ot al =0For i =1 t o n
t ot al = t ot al + iNext
Return
27
-
8/11/2019 07-RobotBASIC
27/35
Subroutines: Call-Return
a more advanced subroutinemechanism that supportsparametersand local variables
parameters are passed by valueor by reference
format:
CALL label{(parameter list)}
code at labelis executed until a
RETURN is reached
27
I nput "ent er N: " , nCall Summat i on( n, r esul t )Pr i nt "summat i on=", r esul tEnd
Sub Summat i on( x, &t ot al )t ot al =0For i =1 t o x
t ot al = t ot al + iNext
Return
28
-
8/11/2019 07-RobotBASIC
28/35
ROBOT SIMULATOR
28
29
-
8/11/2019 07-RobotBASIC
29/35
Robot Simulator
RobotBASIC includes a neat simulated robot environmentwith a collection of associated commands and functions
some features: obstacle detection and abort
robot sensors to detect & avoid obstacles
full color support
robot "tail" that can draw (if "down")
invisible colors are not detected as obstacles
simulated robot battery and charge state heading and position tracking via simulated compass & "GPS"
simulated "slip" factor for motions
29
30
-
8/11/2019 07-RobotBASIC
30/35
Robot Simulator Commands Summary
creates & locates robot
sets colors that will not be consideredobstacles
defines floor color (default is WHITE) moves forward/backward some number of
pixels
rotates robot CW (+) or CCW (-)
sets robot's heading (0-359 degrees) sets a robot "slowdown" factor
sets robot's pen UP or DOWN
sets a motor slip factor
30
31
-
8/11/2019 07-RobotBASIC
31/35
Heading Details
and are based on directional compassmeasure as opposed to mathematical angle measures
31
0
180
90270
90
270
0180
135
135
-
8/11/2019 07-RobotBASIC
32/35
33
-
8/11/2019 07-RobotBASIC
33/35
rBumper() details
the robot has 4 bump sensors (switches)
front, rear, left, right
each switch has an assignedbit position in the return value(bottom 4 bits):
each bit is 1 if bump sensor is active (object within 2 pixels) or 0 if not
ex: return value of 4 (01002) means front bumper is active
use bitwise Boolean operators to evaluate result ex:
left front right rear
bit: 3 2 1 0
I f ( rBumper()&4) = 0 t hen ' f r ont bumper i nact i ve
I f ( rBumper()&4) ! = 0 t hen ' f r ont bumper act i ve
-
8/11/2019 07-RobotBASIC
34/35
35
-
8/11/2019 07-RobotBASIC
35/35
rGround() details
the robot has 3 down-facing sensors
center, 10
each sensor has an assigned sensor number, one of which must bepassed to rGround as an argument 3=10 left, 2=center, 1=10 right
rGround() returns color seen directly under specified sensor and does not ignore any colors, even if invisible
ex: move forward until robot is over something yellow
+10-100
Repeatr For war d 1
Unt i l rGround(2)= YELLOW