| |||||||||||||||||||||||
| |||||||||||||||||||||||
|
|||||||||||||||||||||||
Emulating the Compurobot on the Calculator Robot II
The Program
To make sure the program was easy to understand, I took some liberties with the programming.
Firstly, I did not include curving code. This would require the servos to be dynamically reconfigured,
so that the left servo was slower than the right when moving forward (to curve left), and vice versa.
This is definitely possible, but would expand the program a fair bit. Similarly, the 3 gear modes
have been omitted. Also, while the code is sufficiently robust, character checks are not performed.
This said, the calculator version is more powerful in other respects. It isn't limited by 48 commands, only by your calculator memory. Additionally, the Compurobot only allowed two numbers to be multiplied together, while the calculator version allows as many as you want. The program is non-interactive, and instead relies upon Str1 to hold the commands. They are inputted as follows:
Example programs include: "F4"->Str1:prgmCOMPROBOT "F2*2"->Str1:prgmCOMPROBOT "FFFF"->Str1:prgmCOMPROBOT "F3LF3LF3LF3"->Str1:prgmCOMPROBOTNote that the first 3 programs to the same thing.
CodeNote: Do not copy and paste this code, since I have had to substitute some symbols here. Use the group file at the end of the article.
1: ClrDraw:AxesOff:LabelOff:CoordOff:ExprOff
2: {-1}->L1
3: RecallPic Pic0
4: For(A,1,250,1)
5: getKey
6: End
7: ClrHome
8: Disp "PARSING"
9: 0->C:0->D:0->L:100->N:1->A
10: Lbl LP
11: While A<=length(Str1)
12: sub(Str1,A,1)->Str2:C->D:A+1->A
13: If Str2="F":Then:122->C:Goto PC:End
14: If Str2="B":Then:100->C:Goto PC:End
15: If Str2="W":Then:111->C:Goto PC:End
16: If Str2="L":Then:112->C:Goto PC:End
17: If Str2="R":Then:121->C:Goto PC:End
18: If Str2<>"*":N*expr(Str2)->N
19: End
20: 0->A:C->D
21: Lbl PC
22: If D<>0:Then
23: augment(L1,{D,N})->L1
24: End
25: 100->N:0->D
26: If A<>0:Goto LP
27: Disp "EXECUTING"
28: For(A,2,dim(L1),2)
29: Send({L1(A),L1(A+1)})
30: Get(Q)
31: End
As with any program in TI-BASIC, it is rather hard to get an idea what the program is
doing, since variables are non-meaningful, nevertheless I'll explain essentially what the program
is doing.
Execution is a two-step procedure: the program parses the string and then sends the commands to the robot. Lines 1-9 simply set everything up and display the splash screen for a second or two. The label on Line 10 stands for LOOP. You can see I use a while loop to parse the whole string, using A as the counter. Line 12 sees me grab the current token and put it into Str2. We then copy the current command to D (more on this in a minute) and then increment our token pointer, A. Now, Lines 13-17 are essentially the same. We check the current token for a particular command, then we store the necessary command in C. We then jump to PC (Process Command). Process Command processes the previous command (currently held in D). This is because Compurobot allows those multipliers after the main command (for example, "F3"). Therefore, the actual full command is not known until the next command (or end of string) is reached. We'll study Process Command itself in a minute. Next, Line 18 processes the numbers. As long as the token isn't the multiplier, we multiply the current number (in N) with the token. expr() will simply take the number and convert it into an integer. Note that N is set to 100 by default (for 100 milliseconds). Line 20 is used to enable us to process the last command once the string has reached the end. Again, this might be hard to grasp, but if you step mentally through the code with a short string (try "3F"), you'll see how it works. Lines 21-26 are our PC routine. Line 22 ensures that we don't accidentally add a command at the beginning of the list before a full command has been processed. Line 23 does the magic - it appends the command {D,N} to L1. Next, we reset our command and number variables, before jumping back to our while loop (unless we've reached the end of our string). We've now completely processed the string and have all the commands in L1. We just need to send them to the robot a pair at a time. Lines 28-31 do just that!
Submitted: 15/07/2003 Article content copyright © James Matthews, 2003.
|
|
||||||||||||||||||||||
All content copyright © 1998-2007, Generation5 unless otherwise noted.
- Privacy Policy - Legal - Terms of Use -