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

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

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

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

Добавлен: 12.06.2025

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

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

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

Page 44 · Advanced Robotics with the Toddler

If the Toddler isn’t starting with both feet firmly planted squarely on the ground, or if you would like to experiment with larger step distances you could modify the CenterTilt and StrideCenter values. This would result in a need to also modify the right and left limits for both the tilt and stride.

Your Turn

Modify the code in the example. Try the following:

Adjust the TiltStep and StrideStep values to smaller numbers. Observe how the Toddler behaves. Adjust the same constants to larger numbers and make some observations. Can you envision the servo timing diagram and explain why the Toddler behaves differently?

Decrease the MoveDelay value to a smaller number and make observations. Find the right combination of MoveDelay, TiltStep and StrideStep that makes your Toddler walk the way you want it to. Save these values for future programs.

Chapter #2: Taking Your First Steps · Page 45

ACTIVITY #2: WALKING BACKWARDS: BRUTE FORCE

The Toddler robot can walk backward as well as forward but it is not simply a matter of using the steps in the prior program in reverse order. The Toddler moves in the reverse fashion but the functions necessary to do this will be different. With just over half a dozen routines, the last sample program is relatively simple. Changing it to handling a different direction is not too difficult. Keep in mind that the two other approaches to performing these tasks are presented later in this chapter.

In the prior program, the subroutine for each step was numbered sequentially. In this program, the steps will be slightly different so we can use different routine names. The starting movement is the same as the prior program but the second step will be Movement 9 that matches the M9 routine.

Page 46 · Advanced Robotics with the Toddler

Movement 0:

Movement 1:

Movement 9:

Starting Position

Lean Right from Start

Lean Right; Left Back

Movement 10:

Movement 11:

Movement 12:

Lean Left, Left Back

Lean Left, Right Back

Lean Right, Right Back

Movement 13:

Lean Right, Left Back

Figure 2-6: First Steps – in Reverse

As with the forward walking program, the Toddler starts with Movements 0, 1 and 9. The process of Movements 10, 11, 12 and 13 can repeat to walk in a straight line but

Chapter #2: Taking Your First Steps · Page 47

backwards. An example program is shown is shown below. Adjust the different constants in the program to make your robot walk faster or take bigger steps. The program also cleans up its movement so both feet are centered and flat on the floor.

Note that the routines with the same name have been extracted from the first sample program. A program that used this approach but required more sophisticated actions would need more routines with the potential of requiring all 36.

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

'Toddler Program 2.2 - Walking Backwards.bs2

'Run Movement Patters M9 to M13 to walk backwards

'{$STAMP BS2}

'{$PBASIC 2.5}

'-----[ Declarations ]----------------------------------------------------

TiltStep

CON

5

' TiltServo step size

StrideStep

CON

5

' StrideServo step size

MoveDelay

CON

20

' in micrcoseconds

RightTilt

CON

620

' Tilt limits

CenterTilt

CON

750

LeftTilt

CON

880

RightForward

CON

650

' Stride limits

StrideCenter

CON

750

LeftForward

CON

850

StrideServo

CON

12

' Stride servo on P12

TiltServo

CON

13

' Tilt servo on P13

MoveLoop

VAR

Nib

' Repeat movements

Pulses

VAR

Word

' Pulse variable

'-----[ Initialization ]

'-----[ Main Routine ]------------------------------------------------------------------------------------------------------

Main_Program:

GOSUB

M0

' center servos

GOSUB

M1

' tilt right

GOSUB

M9

' step back

FOR

MoveLoop = 1 to 3

GOSUB M10

' tilt left

GOSUB M11

' step left

GOSUB M12

' tilt right

GOSUB M13

' step right


Page 48 · Advanced Robotics with the Toddler

NEXT

GOSUB M10

' tilt left

GOSUB M14

' center feet

GOSUB M8

' center servos

END

' -----[ Subroutines ]-----------------------------------------------------

M0:

FOR Pulses = 1 TO 100 STEP StrideStep

PULSOUT TiltServo, CenterTilt

PULSOUT StrideServo, StrideCenter

PAUSE MoveDelay

NEXT

RETURN

M1:

FOR Pulses = CenterTilt TO RightTilt STEP TiltStep

PULSOUT TiltServo, Pulses

PULSOUT StrideServo, StrideCenter

PAUSE MoveDelay

NEXT

RETURN

M8:

FOR Pulses = LeftTilt TO CenterTilt STEP TiltStep

PULSOUT TiltServo,Pulses

PULSOUT StrideServo, StrideCenter

PAUSE MoveDelay

NEXT

RETURN

M9:

FOR Pulses = StrideCenter TO RightForward STEP StrideStep

PULSOUT TiltServo, RightTilt

PULSOUT StrideServo, Pulses

PAUSE MoveDelay

NEXT

RETURN

M10:

FOR Pulses = RightTilt TO LeftTilt STEP TiltStep

PULSOUT TiltServo,Pulses

PULSOUT StrideServo, RightForward

PAUSE MoveDelay

NEXT

RETURN

M11:

FOR Pulses = RightForward TO LeftForward STEP StrideStep


Chapter #2: Taking Your First Steps · Page 49

PULSOUT TiltServo,LeftTilt

PULSOUT StrideServo, Pulses

PAUSE MoveDelay

NEXT

RETURN

M12:

FOR Pulses = LeftTilt TO RightTilt STEP TiltStep

PULSOUT TiltServo,Pulses

PULSOUT StrideServo, LeftForward

PAUSE MoveDelay

NEXT

RETURN

M13:

FOR Pulses = LeftForward TO RightForward STEP StrideStep

PULSOUT TiltServo,RightTilt

PULSOUT StrideServo, Pulses

PAUSE MoveDelay

NEXT

RETURN

M14:

FOR Pulses = RightForward TO StrideCenter STEP StrideStep

PULSOUT TiltServo,LeftTilt

PULSOUT StrideServo, Pulses

PAUSE MoveDelay

NEXT

RETURN

Your Turn

Try the following:

Experiment with the constants in the program by changing them to the following values. Explain why the Toddler’s behavior is different.

TiltStep

CON

2

' TiltServo step size

StrideStep

CON

2

'

StrideServo step

size

MoveDelay

CON

12

'

in micrcoseconds

Write a program where the Toddler walks forward, then backward to the starting position.

Page 50 · Advanced Robotics with the Toddler

ACTIVITY #3: USING A DATA TABLE TO STORE MOVEMENTS

The length of the two prior programs is similar but more complex programs could grow larger simply because more movement routines would be necessary. Moving information in tables is an excellent way of simplifying the programming task. This next sample application employs both fixed size and variable length tables.

Table data is stored using the BASIC Stamp’s DATA command. This command lets you write data to the Stamp’s EEPROM during program download. If used correctly, it can make your PBASIC programs much more efficient and shorter, too.

The DATA command is shown with many examples in the BASIC Stamp Programming Manual, available on www.parallax.com for download or in a printed format. The basics are the following:

DATA can be written in byte (8-bit) and word (16-bit) sizes The BASIC Stamp 2’s EEPROM stores DATA from numeric location 0 and builds towards location 2048. Your PBASIC program is stored from 2048 and writes backwards towards location 0.

The PBASIC editor does not detect occurrences when you overwrite your own code with

DATA.

Once you download and run Toddler Program 2.3 – First Steps Forward Using Tables.bs2 you can choose Run I Memory Map to see how DATA and program is stored in the EEPROM.

This example uses two types of entries to develop walking movements. The following table entries are a list of movements that comprise the given motion. For example, we know from the prior two programs that M1 followed by an M2 movement will start the Toddler off walking. Then, after this is done movements 3, 4, 5, and 6 can repeat themselves so the Toddler can walk straight.

StartForward

DATA

1, 2, 0

WalkForward

DATA

3,

4,

5, 6, 0

FinishForward

DATA

3,

7,

8, 0

The BASIC Stamp’s EEPROM stores the byte-sized value 1 in location 0, the value 2 in location 1, the value 0 in location 3, the value 3 in location 4 and so on.

The variable length tables are used to store a sequence of movements. This allows complex movements to be of arbitrary length. The tables are accessed using the PBASIC READ command.


Chapter #2: Taking Your First Steps · Page 51

The Movement contains data that is used to tilt or stride the Toddler. Each movement has a starting and ending tilt and stride value. In this DATA table, M1 is actually an address for the storage of CenterTilt. Because the above three lines of code had 12 byte-sized entries, the CenterTilt word-size constant will be stored across EEPROM locations 12 and 13 (the starting EEPROM location is 0, not 1).

The WORD modifier is used because the constants are all greater than 255 – the value of a byte.

M1

DATA

WORD CenterTilt, WORD RightTilt,

WORD StrideCenter, WORD StrideCenter

M2

DATA

WORD RightTilt, WORD RightTilt,

WORD StrideCenter, WORD LeftForward

M3

DATA

WORD RightTilt, WORD LeftTilt,

WORD LeftForward, WORD LeftForward

M4

DATA

WORD LeftTilt, WORD LeftTilt,

WORD LeftForward, WORD RightForward

M5

DATA

WORD LeftTilt, WORD RightTilt,

WORD RightForward, WORD RightForward

M6

DATA

WORD RightTilt, WORD RightTilt,

WORD RightForward, WORD LeftForward

M7

DATA

WORD LeftTilt, WORD LeftTilt,

WORD LeftForward, WORD StrideCenter

M8

DATA

WORD LeftTilt, WORD CenterTilt,

WORD StrideCenter, WORD StrideCenter

Only eight (8) movements are employed in this program but it is easy to see how all 36 movements could be easily added to the table. Storing the movements in EEPROM using DATA requires much less code space than the “brute force” first example.

The program’s management of these routines is handled in the Main_Program: and Movement: routines. The Main_Program: routine starts by calling the M0 routine, which centers the Toddler’s stride and tilt so both feet are place squarely on the surface.

On return, the Mx variable is loaded with the value of StartForward, which is actually address 0 given by the DATA statement. In the Movement: routine the value of EEPROM address is READ and stored in Dx. Dx now equals 1. Mx is incremented by one for the next loop.


Page 52 · Advanced Robotics with the Toddler

The first time through the program the LOOKUP table looks up index 1, storing the address of M1 in the variable Dx. Then, four word values are read from the Dx address and stored consecutively in the TiltStart, TiltEnd, StrideStart and StrideEnd variables. These values are now used in the PULSOUT command to develop a movement.

The line of code:

IF TiltStart = TiltEnd THEN MovementStride

detects an instance when Toddler does not change the tilt, but only moves a leg forward. In this case, the previous TiltStart value is used to hold the tilting servo steady and only a stride occurs.

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

'Toddler Program 2.3: First Steps Forward Using Tables

'Movement routines stored in EEPROM using DATA statement

'{$STAMP BS2}

'{$PBASIC 2.5}

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

'Connect Vs1 and Vs2 to Vss to ground both servos

StrideServo

CON

12

' Stride servo

TiltServo

CON

13

' Tilt servo

' -----

[ Constants ]

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

TiltStep

CON

10

' TiltServo step size

StrideStep

CON

10

' StrideServo step size

MoveDelay

CON

14

' Servo pause (ms)

RightTilt

CON

625

' Tilt limits

CenterTilt

CON

750

LeftTilt

CON

875

RightForward

CON

600

' Stride limits

StrideCenter

CON

750

LeftForward

CON

900

' -----

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

MoveLoop

VAR

Nib

' Loop for repeat movements

Pulses

VAR

Word

' Pulse variable

Dx

VAR

Pulses

' Stores Mx movement index

Mx

VAR

Word

' Movement index

TiltStart

VAR

Word

' Start tilt value