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

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

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

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

Добавлен: 12.06.2025

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

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

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

Page 126 · Advanced Robotics with the Toddler

NEXT

IF Mx < BasicMovements THEN MovementLoop

' exit if submovement table

MovementDone: NEXT

RETURN

DoMovement:

debug " ", DEC Dx, " action",cr

BRANCH Dx,[TiltLeft,TiltCenter,TiltRight,StrideLeft, StrideCenter,StrideRight]

' will fall through if invalid index

RETURN

' ---- Movement routines can be called directly ----

TiltLeft:

NewValue = LeftTilt

GOTO MovementTilt

TiltCenter:

NewValue = CenterTilt

GOTO MovementTilt

TiltRight:

NewValue = RightTilt

MovementTilt:

FOR Pulses = CurrentTilt TO NewValue STEP TiltStep

PULSOUT TiltServo, Pulses

PULSOUT StrideServo, CurrentStride

PAUSE MoveDelay

NEXT

CurrentTilt = NewValue

RETURN

StrideLeft:

NewValue = LeftStride

GOTO MovementStride

StrideCenter:

NewValue = CenterStride

GOTO MovementStride

StrideRight:

NewValue = RightStride

MovementStride:

FOR Pulses = CurrentStride TO NewValue STEP StrideStep

PULSOUT TiltServo, CurrentTilt

Chapter #6: Object Avoidance with Infrared · Page 127

PULSOUT StrideServo, Pulses

PAUSE MoveDelay

NEXT

CurrentStride = NewValue

RETURN

' ----- Move feet to initial center position -----

ResetCC:

CurrentTilt = CenterTilt

CurrentStride = CenterStride

FOR Pulses = 1 TO 100 STEP StrideStep

PULSOUT TiltServo, CenterTilt

PULSOUT StrideServo, CenterStride

PAUSE MoveDelay

NEXT

DoReturn:

RETURN

How IR Roaming by Numbers in Real-Time Works

This Program listing declares the sensors variable, which is one nibble of RAM. Of the four bits in the sensors variable, only the lowest two bits are used. Bit-0 is used to store the left detector’s output, and bit-1 is used to store the right detector’s output.

The main routine starts with the FREQOUT commands used to send the IR signals, but the commands following each freqout command are slightly different from those used in the previous program. Instead of saving the bit value at the input pin to a bit variable, each bit value is stored as a bit in the sensors variable. Bit-0 of sensors is set to the binary value of IN8, and bit-1 of the sensors variable is set to the binary value of IN0. After setting the values of the lower two bits of the sensors variable, it will have a decimal value between “0” and “3.” The BRANCH command uses these numbers to determine to which label it sends the program.

DO

FREQOUT

lEmitter,1,38500

' Send freqout signal - left IRLED.

sensors.bit0

=

lInput

' Store IR detector output in RAM.

'

Detect

object on the

right.

FREQOUT

rEmitter,1,38500

'

Repeat

for the right

IR pair.

sensors.bit1

=

rInput


Page 128 · Advanced Robotics with the Toddler

PAUSE 18

' 18 ms pause(2 ms lost on freqout)

'Loads the IR detector output values into the lower 2 bits of the

'sensors variable, a number btwn 0 and 3 that LOOKUP can use

LOOKUP sensors,[Backward,PivotLeft,PivotRight,Forward],Mx

GOSUB Movement

LOOP

The four possible binary numbers that result are shown in Table 6.1. Also shown is the lookup action that occurs based on the value of the state argument.

Binary Value of

Decimal Value of

What the Value Indicates, Branch Action Based on State

state

State

0000

lInput = 0 and rInput = 0,

0

Both IR detectors detect object, step back.

0001

lInput = 0 and rInput = 1,

1

Left IR detector detects object, turn right.

0010

lInput = 1 and rInput = 0,

2

Right IR detector detects object, turn left.

0011

lInput = 1 and rInput = 1,

3

Neither IR detector detects object, step forward.

Table 6-1: IR Detector States as Binary Numbers

The Mx variable is set to the appropriate movement table index. The Movement routine then performs the appropriate sequence of commands.


Chapter #6: Object Avoidance with Infrared · Page 129

CHALLENGES

You can rearrange the address labels in the LOOKUP command so that the Toddler does different things in response to obstacles. One interesting activity is to try replacing the Backward address with the Forward address. There will be two instances of Forward in the Lookup address list, but this is not a problem. Also, swap the Left_turn and

Right_turn addresses.

Try making the changes just discussed.

The Toddler should now run into your hand. Because of this, one Toddler cannot be programmed to follow another without some way of distance detection. If the one in front stops, the one in back will crash into it. This problem will be fixed as an example in the next chapter.

Chapter #7: Staying on the Table · Page 131

Chapter #7: Staying on the Table

WHAT’S A FREQUENCY SWEEP?

In general, a frequency sweep is what you do when checking your favorite radio stations. Set the station for one frequency, and check the output. If you don’t like the song that’s playing, change the frequency and check the output again.

ACTIVITY #1: TESTING THE FREQUENCY SWEEP

The Toddler can be programmed to send different IR frequencies, and to check for object detection at each frequency. By keeping track of the frequencies for which the IR detector reported an object, its distance can be determined. The left axis of the graph in Figure 7.1 shows how the sensitivity of the IR detector’s electronic filter decreases as it receives frequencies greater than 38.5 kHz. The filter essentially causes the IR detector to become less able to detect IR at these frequencies. Another way to think about it is that you have to move an object closer if you want it to be detected at a less sensitive frequency. Since the detector is less sensitive, it will take brighter IR (or a closer object) to make the detector see the signal.

Page 132 · Advanced Robotics with the Toddler

Figure 7-1: Relative IR

Sensitivity to Frequency

Figure 7-1 compares the left axis of the graph (IR frequency) to the relative sensitivity of the IR detector. The right side of the graph shows how the relative sensitivity of the IR detector relates to distance detection. As detector sensitivity decreases with the increase in frequency, the object must be closer for the IR signal to be detected. Why closer? When the detectors are made less sensitive by sending higher frequencies, it’s like giving them darker and darker lenses to look through. Just as a flashlight beam appears brighter when reflected off an object that’s closer to you, IR reflected off a closer object appears brighter to the IR detectors.

The right axis of Figure 7-1 shows how different frequencies can be used to indicate in which zone a detected object is located. By starting with a frequency of 38.5 kHz, whether or not an object is in Zone 1-5 can be determined. If an object is not yet detected, it must be beyond the detector limit (Zone 0). If an object is detected, by testing again at 39.25 kHz, the first datum about distance is collected. If 38.5 kHz is detected the

Chapter #7: Staying on the Table · Page 133

object but 39.25 kHz did not, the object must be in Zone 1. If the object was detected at both frequencies, but not at 40.5 kHz, we know it’s in Zone 2. If all three frequencies detected the object, but it was not detected at 41.75 kHz, we know it is in Zone 3. If all four frequencies detected the object, but not 42.5 kHz, we know it’s in Zone 4. If all the frequencies detected the object, we know it’s in Zone 5.

The frequency sweep technique used in this chapter works fairly well for the Toddler, and the components are only a fraction of the cost of common IR distance sensors. The trade off is that the accuracy of this method is also only a fraction of the accuracy of common IR distance sensors. For basic Toddler tasks that require some distance perception, such as following another Toddler, this interesting technique does the trick. Along with adding low-resolution distance perception to the Toddler’s senses, it also provides an introduction to the concepts of filters and frequency response.

Parts Required

Use the same IR detection circuit from Chapter 6, shown in Figure 6.4, for this activity.

Programming the IR Distance Gauge

Programming the BASIC Stamp to send different frequencies involves a DO...LOOP. The Counter variable can be used to give the FREQOUT command different frequencies to check. This program introduces the use of arrays. Arrays are used in Program 7.1 to store the IR detector outputs at the different frequencies. For the L_values variable, the Zone 0 output is stored in bit-0 of L_values. The Zone 1 output is stored in bit-1 l_values.bit1, and so on, all the way through Zone 5, which is stored in bit-5 of L_values. The same measurements are taken for R_values.


Page 134 · Advanced Robotics with the Toddler

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

'Toddler Program 7.1: IR Distance Gauge

'Test of infrared sensors to show distance measurement

'{$STAMP BS2}

'{$PBASIC 2.5}

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

LeftIRLED

CON

4

RightIRLED

CON

15

LeftDetector

VAR

IN11

RightDetector

VAR

IN14

' -----

[ Variables ]

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

Counter

VAR

Nib

' Counting variable

L_values

VAR

Byte

' Vars for storing freq

R_values

VAR

Byte

' sweep IR detector outputs

IR_freq

VAR

Word

' Frequency argument

'

-----[ Main Routine ]

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

DO

L_values

=

0

'

Reset L_values and

R_values

=

0

'

R_values to 0

'Load sensor outputs into L_values and R_values using a FOR..NEXT loop

'and a lookup table, and bit addressing

FOR Counter = 0 TO 4

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

FREQOUT LeftIRLED,1, IR_freq

L_values.lowbit(counter) = ~LeftDetector

FREQOUT RightIRLED,1, IR_freq

R_values.lowbit(counter) = ~RightDetector

NEXT

' Display l_values and r_values in binary and ncd format.

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

LOOP