BASIC Stamp II Programming for Descartes Robots Alexander Stoytchev Mobile Robot Laboratory

advertisement
BASIC Stamp II Programming
for Descartes Robots
Alexander Stoytchev
Mobile Robot Laboratory
Georgia Tech
CS4630: Robotics and Sensing
Descartes Robot
Bumpers
Motors
Wheel Encoders
Wheel Belts
Serial Cable
System Requirements
• IBM PC or compatible computer
• 3.5" floppy disk drive
• serial port
• 128K of RAM
• MS-DOS 2.0 or higher.
• A color video display is recommended.
Onboard Computer
Microcontroller Layout
BASIC Stamp II
• Manufactured by
Parallax Inc.
• Runs Parallax
BASIC (PBASIC)
programs.
BASIC Stamp II
• Processor: Parallax PIC16C57
• EEPROM: 2048 bytes
• RAM: 16 word registers (or 32 bytes!!!)
• Max. Program Length: ~500 instructions
• Clock Speed: 20-MHz
• Execution Speed: 4000 instructions/sec.
• Current Requirements: 7 mA running, 50 uA in sleep modes
• 16 I/O Lines;
• RS-232 input & output (300 - 50K bauds)
• Price $49
Secondary Processor
• Microchip(TM) PIC16C71 RISC, 10MHz
• Automatically executes commands received from
the main processor without intervention
– Motor PWM pulsing for speed control
– encoder monitoring for wheel position and velocity
– A/D conversions
– navigation control
– monitoring tasks
Electrical Diagram
EEPROM Layout
RAM Layout
PBASIC
• Parallax BASIC
• Token interpreter in program memory of
the PIC
• EEPROM as token and data memory
Types
• WORD, BYTE, NIB, BIT
• VAR, CON, DATA
–
–
–
–
–
–
symbol VAR type
symbol VAR size(array)
symbol VAR variable.modifier
symbol CON expression
symbol DATA type
symbol DATA data, data, ...
Operators
Common Arithmetic Problems
• Evaluation is always from left to right
• X = 2 + 3*4
‘= 20
• X = 3*4 + 2
‘= 14
• X = 2 + (3*4)
‘= 14
IF
• IF variable ?? value {AND/OR variable ??
value ...} THEN address
–
–
–
–
?? can be =, <>, =>, =<, >, <
Variable(s) will be compared to Value(s).
Value is a variable/constant.
Address is a label which specifies where to go if
condition is true.
BRANCH
• BRANCH offset,(address0,address1...addressN)
– Branch to address specified by offset (if in range).
– Offset is a variable/constant which specifies which
Address# to use (0-N).
– Addresses are labels which specify where to go.
Jumps & Subroutines
• GOTO label
• GOSUB subname
• RETURN
FOR Loop
• FOR variable = start TO end {STEP {-}increment}
NEXT {variable}
–
–
–
–
Variable will be used as a counter
Start is the initial value of Variable
End is the terminal value of Variable
Increment is an optional value which overrides the
default counter delta of +1.
– If Increment is negative it is subtracted
EEPROM
• Symbol DATA data, data, …
• READ location,variable
– location (0-2047)
• WRITE location,data
– location (0-2047)
• EEPROM {location},(data,data...)
– Preload EEPROM data locations (those not
used by the program).
DEBUG
• DEBUG cls,"Text",cr,var,$var,%var,#var,#$var,#%var
–
–
–
–
–
Display information in the debug window
Text to be printed can be expressed in quotes;
cls - clear screen
cr - carriage return
Simply naming the variables prints their current values
• Decimal is the default,
• $ - can be used for hex
• % - can be used for binary.
• # - (with optional '$' or '%' in front) the variable name
will not be printed, instead only the value will be printed.
Unary Operators
Sin(x) Example
• y=sin(x) , x= 0 to 2*Pi
• Y= 127 * sin (X/256 *2*Pi), X=1 to 255
• The whole circle is described by 256 steps
Sin(x) and Cos(x)
Pausing and Sleeping
• PAUSE milliseconds
• NAP period
– Power consumption is reduced to an extent dependent upon the
length of the period.
– duration 2^period * ~18ms. Period (0 to 7)
• SLEEP seconds
– resolution is ~2.3s, accuracy is ~99.9%
– Power consumption reduced to 1/100th normal
– Seconds is a variable/constant (0-65535).
• STOP - like end but no power consumption
• END
– Sleep terminally until the power cycles (program re-runs) or the
pc connects.
– Power is reduced to an absolute minimum
Miscellaneous
• RANDOM wordvariable
• SOUND pin#,(note0, duration0, note1, duration1, ….)
• SERIN pin,baudmode,(qualifier,qualifier...)
• SEROUT pin,baudmode,({#}data,{#}data...)
Robot Commands: Initialization
• The secondary PIC communicates with the
BS2-IC at 50kbps over a serial link.
• Port 15 is used for communication.
• Port 14 is used for flow control.
• The PIC requires about 80ms to start-up.
• PAUSE 100 - use before every program
• SEROUT 15\14, 0, ["I"] - to reset the PIC
Syntax of PIC Commands
SEROUT 15\14, 0, ["C", VALUE.LOWBYTE, VALUE.HIGHBYTE]
– C is the command character (not case-sensitive).
– VALUE.LOWBYTE is the low byte of the value.
– VALUE.HIGHBYTE is the high byte of the value.
Going Forward and Backward
SEROUT 15\14, 0, ["F", 100]
SEROUT 15\14, 0, ["B", 44, 1]
---------------------------------DISTANCE VAR WORD
DISTANCE = 300
SEROUT 15\14, 0, [“B", DISTANCE.LOWBYTE, DISTANCE.HIGHBYTE]
Requesting Sensor Data
Send a SEROUT request for data followed
IMMEDIATELY by a SERIN command.
--------------------------------‘Read left wheel encoder counts
DISTANCE VAR WORD
DL VAR DISTANCE.LOWBYTE
DH VAR DISTANCE.HIGHBYTE
SEROUT 15\14, 0, ["["]
SERIN 15\14, 0, [DL, DH]
Heading Information
SEROUT 15\14, 0, ["?"]
SERIN 15\14, 0, [HEADING]
– HEADING should be 0-179 (turn resolution = 2 degrees)
----------------------------------HEADING VAR WORD
HEADING = 320
SEROUT 15\14, 0, ["H", HEADING/2]
SEROUT 15\14, 0, ["?"]
SERIN 15\14, 0, [HEADING]
HEADING = HEADING * 2
Making Turns
SEROUT 15\14, 0, [“L“, 20] ‘ turn left 40 degrees
SEROUT 15\14, 0, [“R“, 30] ‘ turn right 60 degrees
– Turn parameter * 2 = turn angle
SEROUT 15\14, 0, [“H“, 45] ‘ Set heading
SEROUT 15\14, 0, [“T“, 45] ‘ Set target heading
Motor Control
SEROUT 15\14, 0, [“M“, %1010]
–
–
–
–
%00
%01
%10
%10
turn off motor
set in backward motion
set in forward motion
set in forward motion
SEROUT 15\14, 0, [“S“, 10] ‘ Set Speed
– Valid speeds 0-32. Automatic speed compensation.
SEROUT 15\14, 0, [“<“, 10] ‘ Set left motor speed
SEROUT 15\14, 0, [“>“, 15] ‘ Set right motor speed
SEROUT 15\14, 0, [“(“]
SEROUT 15\14, 0, [“)“]
SEROUT 15\14, 0, [“#“]
‘ Request left wheel instantaneous velocity
‘ Request right wheel instantaneous velocity
‘ Request Stopped State
SEROUT 15\14, 0, [“C“]
‘ Clear distances traveled
Common Problems
• “Hardware Not found” error message
– Check Serial cable orientation
– Power down and up
• Check arithmetic operations for overflows
• Variable names and values
References
• http://www.divent.com
– File: progrmng.html
• http://www.parallaxinc.com/
• Claus Kuhnel and Klaus Zahnert
“BASIC Stamp” (second edition),
Newness, 2000
Download