Файл: Advanced Robotics with the Toddler (Paralax, student guide, v1.3, 2004).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 12.06.2025

Просмотров: 1930

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Chapter #7: Staying on the Table · Page 135

Your Turn

Enter and run Program Listing 7.1.

This program makes use of the Debug Terminal, so leave the serial cable connected to the Toddler while Program Listing 7.1 is running.

When the Toddler is placed facing a nearby wall (3 to 5 cm.), the Debug Terminal should display something similar to Figure 7.2. As the Toddler is moved closer to and further from the wall, the numbers displayed by the Debug Terminal should change increase and decrease. Each “1” represents a zone so that when you see five 1’s the object is nearest to the Toddler.

Place the Toddler so that it faces the wall with its IR LEDs about 1 cm. away from the wall. The left and right readings should both be at “4” or “5.” If not, make sure each IR detector is facing in the same direction as its IR LED.

Figure 7-2: Frequency sweep and data in binary and NCD format

Gradually back the Toddler away from the wall. As the Toddler is backed away from the wall, the left and right readings should gradually decrease to “0.”

Page 136 · Advanced Robotics with the Toddler

If either or both sides stay at all zeros or all ones, it indicates a possible mistake in either your wiring or in the program. If this is the case, unplug your battery pack from the Toddler. Then, check your wiring and PBASIC code for errors.

The maximum detection distance is 20 to 30 cm., depending on the reflectivity of the wall. Some tinkering with how far left/right each IR pair is pointing may be required to get the numbers to be the same at a given distance. A high level of precision IS NOT necessary for these activities.

IR circuit not working? Use a wire stripper to unsheathe about 1 cm of insulation from a jumper wire. Slide the insulation up one of the IR LED leads. This will protect the leads from touching each other during adjustment.

How the Distance Gauge Program Works

Counter is a nibble variable that is used to index a FOR...NEXT loop. The FOR...NEXT loop is used for checking the IR detectors at various frequencies. The L_values and R_values variables store the outputs for the left and right IR detectors at the various frequencies used. Each variable stores five binary measurements. Since the IR detector outputs are tested at a variety of frequencies, IR_freq is a variable that can store the value of the frequency that gets sent each time through the frequency testing loop.

The main routine contains two routines, one for frequency sweep and another for displaying the data collected. The first step in the frequency sweep is setting L_values and R_values to zero. This is important since individual bits in each variable are modified. Clearing L_values and R_values starts each variable with a clean slate. Then individual bits can be set to “1” or “0,” depending on what the IR detectors report.

DO

L_values = 0

R_values = 0

The FOR. . . NEXT loop is where the frequency sweep occurs. The lookup command checks the counter value to determine which frequency to copy to the IR_freq variable. When counter is “0,” 37500 gets copied to IR_freq. When counter is “1,” 38250 is copied to IR_freq. As the value of counter is incremented from “0” to “4” by the FOR. .

. NEXT loop, each successive value in the lookup table is copied to IR_freq.

FOR Counter = 0 to 4


Chapter #7: Staying on the Table · Page 137

LOOKUP counter,[37500,38250,39500,40500,41500],IR_freq

Note that the lookup table begins the frequency sweep at 37500 (most sensitive) and ends at 41500 (least sensitive). You might be wondering why the numbers in the LOOKUP table don’t match the frequency values from Figure 7.1. It’s true that if the BASIC Stamp could transmit a 50% duty cycle pulse train (pulses with the same high time and low time) at these frequencies, they would have to match the frequencies specified for the IR detector’s filter. However, the FREQOUT command introduces other factors that affect the amplitude of the harmonics transmitted by the IR LEDs. The math involved in predicting the optimum frequency arguments to use is very advanced and is well outside the scope of this text. Even so, the best frequencies for a given distance can be determined experimentally. The list of values we are using are known to be reliable.

The left sensor is checked by using FREQOUT to send the current value of IR_freq. Next, the .lowbit() argument is used to address each successive bit in L_values. When counter is “0,” the .lowbit(counter) argument addresses bit-0 of L_values. When counter is “1,” the .lowbit(counter) argument addresses bit-1 of L_values, and so on. Before writing the value of IN8 to L_values.lowbit(counter), the NOT operator (~) is used to invert the bit’s value before it is stored to its bit array location in L_values. The same process is then repeated for R_values. After the fifth time through the FOR...NEXT loop, the IR data bits have all been loaded into L_values and R_values.

FREQOUT LeftIRLED,1, IR_freq

L_values.lowbit(counter) = ~LeftDetector

FREQOUT RightIRLED,1, IR_freq

R_values.lowbit(counter) = ~RightDetector

NEXT

The DEBUG commands use a variety of formatters and text strings to display the L_values and R_values variables. The first row of the display is the text heading indicating which readings correspond the right IR detector and which readings correspond to the left IR detector. Remember that left and right are treated as though you are sitting in the Toddler’s body.

DEBUG HOME, CR,

CR, "Left

readings

Right

Readings", cr

DEBUG

"

",BIN8

L_values,

"

", BIN8

R_values, cr

DEBUG

"

",DEC5

NCD(L_values), "

", DEC5 NCD(R_values),

CR

Page 138 · Advanced Robotics with the Toddler

The second row displays L_values and R_values in binary format. This allows for observation of how the bit values in L_values and R_values change as the apparent distance of an object changes.

The third row displays the NCD value of each variable. The NCD operator returns a value that corresponds to the location of the most significant bit in a variable. If the variable is all zeros, NCD returns a zero. If the least significant bit contains a “1,” and all the rest of the digits are “0,” NCD returns a “1.” If bit-1 contains a “1,” but all the numbers to the left of bit-1 are zeros, NCD returns a “2,” and so on. The NCD operator is a handy way of indicating how many ones have been loaded into the lower bits of L_values and R_values. What’s really handy is that NCD directly tells you in which zone the object has been detected.

When the display routine is finished sending data to the Debug Terminal, program control is returned to the main label.

Your Turn

With Program 7.1 running, place the Toddler facing the wall so that the IR LEDs are about 1.5 cm. from the wall. For best results, tape a white sheet of paper to the wall.

Make a note of the left and right readings.

Start pulling the Toddler away from the wall.

Each time the value of one or the other sensors decreases, make a note of the distance. In this way you can determine the zones for each of your Toddler’s IR pairs.

If the readings on one side are consistently larger than the other, you can point the IR LED on the side reporting the larger readings outward a little further. For example, if the left IR pair continually reports higher readings than the right IR pair, try pointing the left IR LED and detector a little further to the left.


Chapter #7: Staying on the Table · Page 139

ACTIVITY #2: THE DROP-OFF DETECTOR

Figure 7-3: IR LED and Receiver Adjustment for Edge Detection

One application for distance detection is checking for a drop-off. For example, if the Toddler is navigating on a table, it can change direction if it sees the edge of the table. All you have to do is point the IR pairs downward so that they are both pointing at the table right in front of the Toddler. A distance detection program can then be used to detect that the table is close-up. When the Toddler nears the edge of a table, one or both of the distance detectors will start reporting that they no longer see something close-up. That means it’s time to turn away from the abyss. This program works best on a light-colored table. Darker tables will absorb more light and be less useful at reflecting infrared.

Point your IR pairs at the surface directly in front of the Toddler as shown in Figure 7.3. The IR pairs should be pointed downward at least 45° from horizontal and outward 45° from the Toddler’s center line.

Perform the tests below using Program 7.1 before trying Program 7.2.

Record the IR pair outputs when the Toddler is looking straight at the table. If the values of the IR pairs when they are looking at your tabletop are “3” or more, it indicates your detectors are seeing what they are supposed to see.

Page 140 · Advanced Robotics with the Toddler

Record the IR pair outputs when the Toddler is looking off the edge of the table. If these values remain less than “3,” the Toddler is ready to try Program Listing 7.2.

If the Toddler does not give you steady and consistent readings of “3” or more when the Toddler is looking at the table, try first adjusting the direction the IR pairs are pointing. Also, if the Toddler does not consistently register less than “3” when it’s looking off the edge of the table, some additional adjustment of the IR pairs also is in order.

If the sensors report “3” or more while looking at the table and “2” or less when looking off the edge, the Toddler is ready for Program Listing 7.2.

Make sure to be the spotter for your Toddler when running Program Listing 7.2. Always be ready to pick your Toddler up as it approaches the edge of the table it’s navigating. If the Toddler tries to drive off the edge, pick it up before it takes the plunge. Otherwise, your Toddler might become a Not-Bot!

When spotting your Toddler while it’s avoiding drop-offs, be ready to pick it up from above. Otherwise, the Toddler will see your hands instead of the drop-off and not perform as expected..

Program Listing 7.2 uses modified versions of the forward, right_turn, left_turn and backward routines that have been used and reused in every chapter since Chapter #2. The number of pulses in each routine has been adjusted for better performance along a table edge. The check_sensors subroutine takes distance measurements by recycling code from Program Listing 7.1: IR Distance Gage.

Run and test Program Listing 7.2. Remember, always be ready to pick your Toddler up if it tries to run off the table.

Chapter #7: Staying on the Table · Page 141

'-----[ Title ]-----------------------------------------------------------

'Toddler Program 7.2: Drop-off Detection

'Walking on a table avoiding the edges

'{$STAMP BS2}

'{$PBASIC 2.5}

'-----[ I/O Definitions ]-------------------------------------------------

TiltServo

CON

13

' Tilt servo on P13

StrideServo

CON

12

' Stride servo on P12

' -----

[ Constants ]

-------------------------------------------------------

MoveDelay

CON

25

' in micrcoseconds

TiltStep

CON

10

' TiltServo step size

StrideStep

CON

10

' StrideServo step size

RightTilt

CON

620

' Tilt limits

CenterTilt

CON

750

LeftTilt

CON

880

RightStride

CON

650

' Stride limits

CenterStride

CON

750

LeftStride

CON

850

' -----

[ Variables ]-------------------------------------------------------

FigureLoop

VAR

Nib

MoveLoop

VAR

Byte

' Loop for repeat movements

MoveLoopLimit

VAR

Byte

SubMoveLoop

VAR

Byte

' Loop for repeat submovements

SubMoveLoopLmt

VAR

Byte

Pulses

VAR

Word

' Pulse variable

CurrentTilt

VAR

Word

CurrentStride

VAR

Word

NewValue

VAR

Word

Dx

VAR

Pulses

Mx

VAR

Word

MxCurrent

VAR

Word

Sx

VAR

Word

SxCurrent

VAR

Word


Page 142 · Advanced Robotics with the Toddler

'-----[ EEPROM Data ]-----------------------------------------------------

'The following state tables are lists of movement state numbers.

'A xx indicates the end of a list.

'These are used with the Movement routine.

TL

CON

0

TC

CON

1

TR

CON

2

SL

CON

3

SC

CON

4

SR

CON

5

xx

CON

255

'------ Movement Value Tables ------

'These can be used with the Movement routine.

'The tables can contain Basic Movement Codes.

'Note: ALL movement tables must be in this section

LeftSemicircle

DATA

7, bLeftTurn,

bLeftTurn, bForward,

xx

RightSemicircle

DATA

7, bRightTurn,

bRightTurn, bForward,

xx

WalkForward3

DATA

3,

bForward,

xx

WalkForward8

DATA

8,

bForward,

xx

'------ Basic Movement Codes ------

'Used in Movement tables.

'Referenced below using LOOKUP statement.

bFinish

CON

0

bForward

CON

1

bBackward

CON

2

bLeftTurn

CON

3

bRightTurn

CON

4

bPivotLeft

CON

5

bPivotRight

CON

6

'------ Basic Movement Tables ------

'These tables can contain Movement Support Codes.

BasicMovements CON

Forward