130 likes | 265 Views
ENGR 101: Robotics Lecture 5 – Subprograms. Outline Subprograms The Infrared Sensor System References http://csserver.evansville.edu/~richardson/ PBASIC Programming Guide: Setting Up PBASIC Programming Guide: Writing Programs BASIC Stamp Syntax and Reference Manual. 1.
E N D
ENGR 101: RoboticsLecture 5 – Subprograms • Outline • Subprograms • The Infrared Sensor System • References • http://csserver.evansville.edu/~richardson/ • PBASIC Programming Guide: Setting Up • PBASIC Programming Guide: Writing Programs • BASIC Stamp Syntax and Reference Manual 1
Lecture 5 – SubprogramsWhy Subprograms? • If you have a common block of code that appears several times at different locations in your program, you may want to move that block of code into a subprogram (also known as subroutines, functions or methods). • Splitting your program into subprograms usually results in better organized code that is much easier to maintain. It may result in smaller programs that use less memory (and require less typing) too. 2
Lecture 5 – SubprogramsPBASIC Subprograms • An example that uses a subprogram: LOW RM ' Initialization LOW LM PAUSE 100 GOSUB GoForward PAUSE 1000 GOSUB StopMotors END GoForward: 'Drive Forward PULSOUT RM, RMFull PULSOUT LM, LMFull RETURN 3
Lecture 5 – SubprogramsPBASIC Subprograms • A GOSUB statement causes program execution to branch to the program label that follows the GOSUB keyword. A subprogram should end with a RETURN statement, this causes program execution to continue at the statement following the GOSUB. • Another advantage of using subprograms is obvious here, to change the way in which the Scribbler goes straight we need only make changes to the GoForward subprogram. 4
Lecture 5 – SubprogramsPBASIC Subprograms • For easy code maintenance, a common rule-of-thumb is that any section of code that is longer than about 50 lines should be moved into a subprogram. Subprograms should not be longer than 50 lines either. Subprograms may call (using GOSUB) other subprograms. 5
Lecture 5 – SubprogramsObject Avoidance • There are two IR emitters and an IR receiver on the front of the Scribbler. 6
Lecture 5 – SubprogramsObject Avoidance • The IR detector has a filter that allows it to see IR light flashing at around 38,500 Hz. We use FREQOUT to generate the proper frequency IR at one of the emitters: ObsTxRight PIN 14 FREQOUT ObsTxRight, 1, 38500 • A short duration (1 ms) is used so that only a short IR burst of light is emitted. 7
Lecture 5 – SubprogramsObject Avoidance • The program on the following slides alternately emits a short IR pulse on the right and left emitters. After each pulse is sent we look for a reflection back to the detector. • The detector sends a low signal to PIN 6 (binary 0) if there is reflected IR. A high signal (1) is sent if there is no reflected IR. • Download (02_Eye_Test.bs2) from the web site. Run and then test it by holding a piece of paper about 6” in front of the robot. 8
Lecture 5 – SubprogramsObject Avoidance ' I/O Pin Definitions ObsRx PIN 6 ' IR detector LedRight PIN 8 LedLeft PIN 10 ObsTxRight PIN 14 ' RT IR emitter ObsTxLeft PIN 15 ' LT IR emitter ' Variable Declarations eyeRight VAR Bit eyeLeft VAR Bit 9
Lecture 5 – SubprogramsObject Avoidance ' Main Program DO GOSUB CheckRightIR 'Look RT GOSUB TestRightIR 'Set LED GOSUB CheckLeftIR 'Look LT GOSUB TestLeftIR 'Set LED LOOP END 10
Lecture 5 – SubprogramsObject Avoidance CheckRightIR: FREQOUT ObsTxRight, 1, 38500 eyeRight = ObsRx RETURN TestRightIR: IF (eyeRight = 0) THEN HIGH LedRight ELSE LOW LedRight ENDIF RETURN 11
Lecture 5 – SubprogramsObject Avoidance CheckLeftIR: FREQOUT ObsTxLeft, 1, 38500 eyeLeft = ObsRx RETURN TestLeftIR: IF (eyeLeft = 0) THEN HIGH LedLeft ELSE LOW LedLeft ENDIF RETURN 12
Lecture 5 – SubprogramsAssignment • Modify the test program so that the Scribbler turns to the left if there is an object on the right and turns to the right if there is an object on the right. Otherwise the Scribbler should move forward. Also check for a stalled condition and take appropriate action. • Add bells and whistles as desired ... 13