Файл: Advanced Robotics with the Toddler (Paralax, student guide, v1.3, 2004).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 12.06.2025
Просмотров: 1914
Скачиваний: 0
Chapter #2: Taking Your First Steps · Page 53 |
||||||
TiltEnd |
VAR |
Word |
' End |
tilt value |
||
StrideStart |
VAR |
Word |
' |
Start stride value |
||
StrideEnd |
VAR |
Word |
' |
End |
stride value |
|
' ----- |
[ EEPROM |
Data ] |
----------------------------------------------------- |
|||
' Indicates which movement routines are executed to comprise steps
' Extend WalkForward |
with 3, |
4, 5, 6, for longer walks |
||
' The number 0 marks |
the end |
of a list |
||
StartForward |
DATA |
1, 2, 0 |
||
WalkForward |
DATA |
3, |
4, 5, 6,3, 4, 5, 6,3, 4, 5, 6,3, 4, 5, 6,3, 4, 5, |
|
6, |
0 |
|||
FinishForward |
DATA |
3, 7, 8, 0 |
||
' Movement routines |
||||
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 |
|||
' |
-----[ Main Routine ] |
---------------------------------------------------- |
Main_Program: |
||
GOSUB M0 |
' Center servos |
|
Mx = StartForward
'DEBUG ? Mx
GOSUB Movement
FOR MoveLoop = 1 to 3
Mx = WalkForward
GOSUB Movement
NEXT
Mx = FinishForward
GOSUB Movement
END
' -----[ Subroutines ]-----------------------------------------------------
Page 54 · Advanced Robotics with the Toddler |
||
Movement: |
||
READ Mx, Dx |
' Read state table number |
|
Mx = Mx + 1 |
||
'DEBUG "Movement routine = ", DEC Mx,cr |
||
IF Dx = 0 THEN DoReturn |
' Skip if no more states |
|
LOOKUP Dx,[M1, M1, M2, M3, M4, M5, M6, M7, M8],Dx |
||
READ Dx, |
WORD TiltStart, WORD TiltEnd, |
|
WORD StrideStart, WORD StrideEnd' Read table entry; store |
||
IF TiltStart = TiltEnd THEN MovementStride
FOR Pulses = TiltStart TO TiltEnd STEP TiltStep
PULSOUT TiltServo, Pulses
PULSOUT StrideServo, StrideStart
PAUSE MoveDelay
NEXT
GOTO Movement
MovementStride:
FOR Pulses = StrideStart TO StrideEnd STEP StrideStep
PULSOUT TiltServo, TiltStart
PULSOUT StrideServo, Pulses
PAUSE MoveDelay
NEXT
GOTO Movement
' ----- M0: Move feet to initial center position -----
M0:
FOR Pulses = 1 TO 100 STEP StrideStep
PULSOUT TiltServo, CenterTilt
PULSOUT StrideServo, StrideCenter
PAUSE MoveDelay
NEXT
DoReturn:
RETURN
Chapter #2: Taking Your First Steps · Page 55
Adding support for walking backward is easier with this program. Three things need to be changed. First, the extra movement entries must be added to the fixed size table and the LOOKUP command. Second, a variable length table must be added for stepping through a backward foot-step. Finally, the table name and a call to the movement routine must be added. This is significantly better than adding more routines to the program.
Your Turn
Try the following:
√Modify the TiltStep, StrideStep and MoveDelay constants so the Toddler walks as smooth as possible, but not necessarily quickly.
√Modify the WalkForward data so that the Toddler walks additional steps.
Page 56 · Advanced Robotics with the Toddler
ACTIVITY #4: USING STATE TRANSITIONS FOR MOVEMENTS
Now we take a look at a completely different way of handling movement control. The prior two approaches essentially required the programming to string together a series of subroutine calls in the proper order. In those cases, the programmer knew what the prior state was and would only use movement routines that were relevant. It was relatively simple for the basic actions examined thus far but it gets very tedious as the actions become more complex.
The state transition approach presented here changes the kinds of routines used from ones that have a known starting and ending state to routines that indicate a desired ending state. The program keeps track of the current state and adjusts accordingly. The reason that this approach simplifies things is that there are really only two kinds of actions, tilt and stride, that the Toddler can perform and only three logical orientations with each of these for a total of nine states. Figure 2-7 shows these states.
Stride |
|||
LL |
LC |
LR |
|
R |
L |
||
Stride |
R |
L |
Stride |
L |
R |
||
Tilt |
Tilt |
Tilt |
Tilt |
Tilt |
||
Pivot |
||||
CL |
CC |
CR |
||
R |
L |
|||
Pivot |
R |
L |
Pivot |
Tilt |
L |
R |
Tilt |
Tilt |
Tilt |
|
RL |
RC |
RR |
|
R |
L |
||
Stride |
R |
L |
Stride |
L |
R |
Stride
Figure 2-7: State Transition Approach
Chapter #2: Taking Your First Steps · Page 57
Each circle represents a state. The center shows the orientation of the Toddler’s legs and body. The arrow indicates the front of the Toddler. The dark background in a rectangle indicates that a leg is on the floor. The white background indicates the leg is in the air. The two letters at the top of the circle provide a representation for the Toddler positioning. The first letter indicates the tilt (T) and the second indicates stride (S) or what foot is in front. The letters can be L, C, or R. C indicates the respect servo motor is centered. For the tilt, C indicates both feet are on the ground. For stride, C indicates that both feet are inline along the center of the body’s axis. The reason for naming each state will become apparent soon.
There are also bi-directional arrows showing valid transitions from one state to another. The label on the line indicates the type of change that occurs. The Pivot designation is the same as Stride but we make the distinction because this will cause the Toddler to pivot.
Nine states, each with four possible transitions yields 36 distinct transitions. If the transition routines are labeled using their starting and ending state names with the format TSxTS then movement M0 is CCxRC as it starts with both legs together in state CC and ends up leaning to the right in state RC. Using this naming convention makes programming easier in the prior examples with the code looking like this.
'-----[ Main Code ]---------------------------------------------------
'Take three full steps.
Main_Program: |
||
GOSUB |
M0 |
' center servos |
GOSUB |
CCxRC |
' tilt right |
GOSUB |
RCxRL |
' step left |
FOR |
MoveLoop = 1 to 3 |
|
GOSUB RLxLL |
' tilt left |
|
GOSUB LLxLR |
' step right |
|
GOSUB LRxRR |
' tilt right |
|
GOSUB RRxRL |
' step left |
|
NEXT |
||
GOSUB |
RLxLL |
' tilt left |
GOSUB |
LLxLC |
' center feet |
GOSUB |
LCxCC |
' center servos |
END |
||
Page 58 · Advanced Robotics with the Toddler
The advantage of the name change is obvious. The last two letters of the prior routine are the starting two letters of the next routine.
Of course, this is still relatively cryptic and without the comments the actions would definitely be confusing. The problem is that this approach requires knowledge of the prior state.
Now take a look at the next sample code snippet. It does away with the comments (not always a good idea but fine for this discussion) and the routines being called indicate what action is being performed.
'-----[ Main Code ]---------------------------------------------------
'Take three full steps.
Main_Program: |
||
GOSUB M0 |
' center servos |
|
GOSUB |
TiltRight |
|
GOSUB |
StrideLeft |
|
FOR MoveLoop = 1 to 3
GOSUB TiltLeft
GOSUB StrideRight
GOSUB TiltRight
GOSUB StrideLeft
NEXT
GOSUB TiltLeft
GOSUB StrideCenter
GOSUB TiltCenter
END
So what happened? You need to take a look at the next program listing to see how these routines were implemented but essentially two variables, CurrentTilt and CurrentStride, were added to keep track of where the feet are. The routine simply applies the designated change.
There are only six routines that need to be used. These can be applied in any order although only four will cause a change at any time.
Why? Because two of the six will cause the Toddler to stay in the state that it is currently in. For example, if the Toddler is tilting to the left then calling the TiltLeft routine will
Chapter #2: Taking Your First Steps · Page 59
leave it in the same position. There will be a delay while it executes the routine but it turns out to be a very short period of time. It is not noticeable when watching the Toddler move.
The following program implements the routines used in the prior code snippet but it retains some of the ideas employed in the last full program listing (Program 2.3) that used tables. They are still worthwhile although they are used in a slightly different fashion here. This program essentially connects the two prior complete program listings in that it lets the programmer connect a series of movements which are read from a DATA table yet it also reads the commands from an EEPROM. You will notice after you modify this program how the State Transition Approach figure applies.
To aid in your understanding of what the different DATA codes mean see Table 2-3.
Table 2-3: State Table Codes
TR |
tilt right |
SL |
stride left |
TL |
tilt left |
SR |
stride right |
TC |
tilt center |
Xx |
end of table |
Page 60 · Advanced Robotics with the Toddler
'-----[ Title ]-----------------------------------------------------------
'Toddler Program 2.4: First Steps Forward with State Transitions
'Keeps track of starting and ending states
'{$STAMP BS2}
'{$PBASIC 2.5}
'-----[ I/O Definitions ]-------------------------------------------------
TiltServo |
CON |
13 |
' Tilt servo on P12 |
|
StrideServo |
CON |
12 |
' Stride servo on P13 |
|
' ----- |
[ Constants ]------------------------------------------------------- |
|||
MoveDelay |
CON |
12 |
' in micrcoseconds |
|
StrideStep |
CON |
5 |
' StrideServo step size |
|
TiltStep |
CON |
5 |
' TiltServo 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 ]------------------------------------------------------- |
|||
MoveLoop |
VAR |
Nib |
' Loop for repeat movements |
|
Pulses |
VAR |
Word |
' Pulse variable |
|
CurrentTilt |
VAR |
Word |
||
CurrentStride |
VAR |
Word |
||
NewValue |
VAR |
Word |
||
Dx |
VAR |
Pulses |
||
Mx |
VAR |
Word |
||
' ----- |
[ EEPROM |
Data ]----------------------------------------------------- |
||
' Take three full steps. |
||||
' |
||||
' 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 |
||