P. 1
Robotics_StudentWorkbook_eng

Robotics_StudentWorkbook_eng

|Views: 580|Likes:
Published by api-3752654

More info:

Published by: api-3752654 on Oct 15, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

05/09/2014

pdf

text

original

All the navigation routines in Activity #5 ended with goto commands. Each goto command sent program
control to the main: label. A similar technique involves the use of subroutines, and several example
programs in this text will make use of them. Since the example program in Activity #7 makes use of
subroutines, let’s take a look at how one works before moving on.

Programming Navigation with Subroutines

A subroutine is a segment of code that does a particular job. To make the subroutine do its job, a command is
used in the main routine that “calls” the subroutine. The command for calling a subroutine is the gosub
command, and it’s similar to the goto command. A goto command tells the program to go to a label, and
then start executing instructions. The gosub command tells the program to go to a label, and start executing
instructions, but come back when finished. A subroutine is finished when the return command is
encountered.

Figure 2.6: Close-up of the detailed EEPROM Map after Display
ASCII box is checked.

Chapter #2: Programming the Boe-Bot to Go Places

Robotics! Version 1.5 • Page 63

Program Listing 2.6 uses a different technique for navigation. It sets variable values for number of pulses
delivered and servo pulse widths. After the variable values have been set, the main program calls a
subroutine that makes use of the information stored in the variables to pulse the servos.

' Robotics! v1.5, Program Listing 2.6: Subroutine Navigation.
' {$Stamp bs2}

' Stamp Directive.

'----- Declarations --------------

loop_count var word

' For...next loop counter.

right_width var word

' Variable stores right pulse width.

left_width var word

' Variable stores left pulse width.

pulse_count var byte

' Used to set number of pulses delivered.

'----- Initialization ------------

output 2

' Set P2 to output.

freqout 2, 2000, 3000

' Signal program is starting/restarting.

low 12

' Set P12 and 13 to output-low.

low 13

'----- Main Routine --------------

main:

' Main routine.

forward:

' Forward routine.

pulse_count = 75

' Set pulse count for 75 pulses.

right_width = 500

' Set right pulse width to 1.0 ms.

left_width = 1000

' Set left pulse width to 2.0 ms.

gosub pulses

' Call the pulses subroutine.

pause 500

' Pause for 0.5 s.

backward:

' Backward routine.

pulse_count = 75

' Set pulse count for 75 pulses.

right_width = 1000

' Set right pulse width to 2.0 ms.

left_width = 500

' Set left pulse width to 1.0 ms.

gosub pulses

' Call the pulses subroutine.

pause 500

' Pause for 0.5 s.

goto main

' Go to main label (infinite loop).

'----- Subroutines ---------------

pulses:

' Pulses subroutine.

for loop_count = 1 to pulse_count

' Use pulse_count for number of pulses.

pulsout 12, right_width

' Use right_width for right pulse width.

Chapter #2: Programming the Boe-Bot to Go Places

Page 64 • Robotics! Version 1.5

pulsout 13, left_width

' Use left_width for left pulse width.

pause 20

' Pause 20 ms.

next
return

' Return from subroutine.

How the Subroutine Navigation Program Works

Look up the gosub and return commands in Appendix C: PBASIC Quick Reference or in the BASIC Stamp
Manual before continuing.

Figure 2.7 shows how the backward routine calls
the pulses subroutine. The commands in the
pulses subroutine are executed until the
program gets to the return command. The
return command sends the program back to
the command just after gosub pulses
command, which is pause 500 in this case. This
same process occurred in the forward routine,
which was executed before the backward
routine shown.

Your Turn

Add routines to the main routine that set
the values for right_turn and left_turn.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->