PDA

View Full Version : Trouble Understanding IsoMax Code


stuey
01-30-04, 03:08 PM
Hi,
I m trying to create a servo control program using IsoMax that will work on IsoPod.
I looked at the servo program on your website(Here is the code fragment):

: RC ( COUNT FROM 0 TO SCALE#, THEN 0-11D PWM #

// 0 MAX B MIN 5 OVER U< IF 1A + THEN E06 +
// SWAP
// 0 MAX SCALE# @ MIN HISTOP @ LOSTOP @ - SCALE# @ */
// LOSTOP @ +
// SWAP !

Can anybody help me understand what this above code does and how ?


Thanks

RMDumse
01-30-04, 03:42 PM
I can try,

The first line consists of a ":" followed by a name "RC". The colon function means: Take the following name and make a new function in the language with this name. In this case the new name is RC. So far RC can't do anything, but the colon cunction also switches into compilation mode, so things that follow aren't immediately executed, but compiled for later use, when the definition of RC is finished, and it is invoked. Then these compile functions will run. Everything after the "(" is a comment. The comment is saying that two parameters should be passed to RC. One will be what setting RC is to output (from 0 up to SCALE#) and the other number is which PWM channel to output on.

: RC ( COUNT FROM 0 TO SCALE#, THEN 0-11D PWM #

So the next line is compiled.

0 MAX B MIN 5 OVER U< IF 1A + THEN E06 +

Let's take it a piece at a time.

0 MAX acts on the PWM Channel parameter, and puts a 0 on the stack above it, then MAX takes the bigger of the two and leaves it on the stack. So if we did channel 5 and executed 0 MAX, we'd still have 5. If we did 200 0 MAX we'd still have 200. But if we did -7 0 MAX, we'd have zero. So what we are doing is being sure we have no channel numbers below 0.

In the same way B MIN limits the PWM channel number to only those numbers less than B, or less than 11 decimal. So after 0 MAX B MIN we know the number is safely between 0 and 11 (we are only doing 12 channels of PWM).

Now 5 OVER U< puts a 5 on the stack, copies the second thing down on the stack, which in this case was that PWM channel we just qualified above, and uses U<. The result will be true if 5 is less than the channel number we have. So the IF THEN will happen if channel number is 6 through 11 decimal. Inside the IF THEN is a 1A + which is the offset from the first 6 PWM registers to the second group of 6 PWM registers. The first set of PWM registers is at E06 hex, and the second set start at E26. So! if the PWM channel we pick is 0-5, after this little computation, we will point to hardware register E06,E07,E08,E09,E0A or E0B. If the PWM channel we pick is 6-11 (decimal) we will point to 6,7,8,9,10, or 11 + 1A hex + E06 hex, or finally, E26, E27, E28, E29, E2A, or E2B.

So now, on the stack we have our original setting, pulse an address of where in memory the hardware register to put the setting in is located. But we need to manipulate the setting from a partial of SCALE# into a count value for the PWM hardware. So we need to bring that setting up to work on it. So we swap it on the next line.

SWAP

The two values on the stack are swapped, so address is lower, and setting is on top where we can work on it.

0 MAX SCALE# @ MIN HISTOP @ LOSTOP @ - SCALE# @ */

As before we do a 0 MAX to make sure we didn't get slipped a negative value which would be out of range for our conversions. Then we take the biggest number allowed by our scale, SCALE#, by fetching it to the stack with a @ and do a MIN to be sure we didn't get slipped an over-range value to work with. Now we take the maximum counter number for our servo, and the minim counter number for our servo, and subtract them HISTOP @ LOSTOP @ - . This gives us a maximum range of counts we can do. Then we take the setting we were passed (as limited only if something was wrong with it) and multiply it by the count range we just computed, then divide it by the SCALE#. The result of this computation is the number of cycles the hardware must count to match the position in the scale we chose. But remember, our servo doesn't work from 0. It works from LOSTOP up, so we need to add the LOSTOP to our computed value to make it ready for the hardware.

LOSTOP @ +

Now that the setting is all set up, we reverse the order to bring up the address it goes, the correct order for storing, with SWAP, with revised setting down on stack and address to put it on the top of stack and store it away with the !

SWAP !

And that puts the value we choose in the PWM channel we choose, and makes the hardware count the correct time stored in that register from there out, until power down, reset, or we write it to another value later.

Does that help?

stuey
02-02-04, 11:02 AM
Thanks Randy.. Finaly i understood what that code segement meant after all.