Presentation is loading. Please wait.

Presentation is loading. Please wait.

ENGR 101: Robotics Lecture 5 – Subprograms Outline  Subprograms  The Infrared Sensor System References 

Similar presentations


Presentation on theme: "ENGR 101: Robotics Lecture 5 – Subprograms Outline  Subprograms  The Infrared Sensor System References "— Presentation transcript:

1 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

2 Lecture 5 – Subprograms Why 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

3 Lecture 5 – Subprograms PBASIC 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

4 Lecture 5 – Subprograms PBASIC 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

5 Lecture 5 – Subprograms PBASIC 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

6 Lecture 5 – Subprograms Object Avoidance There are two IR emitters and an IR receiver on the front of the Scribbler. 6

7 Lecture 5 – Subprograms Object 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

8 Lecture 5 – Subprograms Object 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

9 Lecture 5 – Subprograms Object 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

10 Lecture 5 – Subprograms Object Avoidance ' Main Program DO GOSUB CheckRightIR 'Look RT GOSUB TestRightIR 'Set LED GOSUB CheckLeftIR 'Look LT GOSUB TestLeftIR 'Set LED LOOP END 10

11 Lecture 5 – Subprograms Object Avoidance CheckRightIR: FREQOUT ObsTxRight, 1, 38500 eyeRight = ObsRx RETURN TestRightIR: IF (eyeRight = 0) THEN HIGH LedRight ELSE LOW LedRight ENDIF RETURN 11

12 Lecture 5 – Subprograms Object Avoidance CheckLeftIR: FREQOUT ObsTxLeft, 1, 38500 eyeLeft = ObsRx RETURN TestLeftIR: IF (eyeLeft = 0) THEN HIGH LedLeft ELSE LOW LedLeft ENDIF RETURN 12

13 Lecture 5 – Subprograms Assignment 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


Download ppt "ENGR 101: Robotics Lecture 5 – Subprograms Outline  Subprograms  The Infrared Sensor System References "

Similar presentations


Ads by Google