Parallax RC Servo Part II: Code Analysis 2/2
So now that we've finished setting the variables, we get into what actually happens.
repeat 100: This asks the program to repeat whatever follows it 100 times.
phsa := -tCw: PHSA stands for counter A phase locked loop registers. This essentially says that we're going to use the clockwise pulse width. Apparently the negative sign is there because that's just how the phsa register works.
t += tC: This funny way of writing the equation is the same as t= t + tC which deliberately gets the clock cycle after the pulse width goes through, and adds 21.5 ms. It also updates what t will be for the next go round.
waitcnt (t): ...we will wait until that new t has been arrived at, then repeat the cycle with the motor pulse.
I wondered why we couldn't just say waitcnt(tC + t), but that's because you haven't redefined tC with the new pulse. At the beginning, the local variable section set what t would be, but you have to deliberately update it - and this is accomplished by making t = t+tC.
The way our good friend Kumi explained the above to me was the following:
Let's walk this out on the macro scale, and talk about minutes, instead of ms. We are starting this program, right now, at 11:54pm, Mountain Time. So we go through all of the CON, and PUB, and all of that stuff, *right up to* the first repeat, but we haven't started it yet.
So, tC says "21.5 minutes", tCtr says "1.5 minutes", down to tCcw says "1.7 minutes", and t says "11:54 Mountain Time" because that's what our System Clock is "right now". So we start the very first repeat.
phsa := -tCw, we pulse the motor.
t +=tC means 11:54 Mountain Time plus 21.5 minutes. t = t + tc ---- t = 11:54 Mountain Time + 21.5 minutes. So t is /now/ 12:15 and 30 seconds Mountain Time, right?
waitcnt(t), Waits until t, which is 12:15 and 30 seconds Mountain Time. So we twiddle our fingers for a while, doing nothing, until 12:15.5 Mountain Time. And then we do our next repeat.
phsa := -tCw, we pulse the motor.
t += tC, t = t + tC, t = 12:15.5 Mountain Time + 21.5 minutes. t = 12:37 Mountain Time
If you don't update t, and you don't update tc, then t + tc will always have the same value. t = 11:54 Mountain Time, tC = 21.5 minutes, t + tC will always be 12:15.5 Mountain Time.
The first time through the repeat, it said "wait until 12:15.5 Mountain Time". This time through the loop, it is saying "wait until 12:37 Mountain Time". Every time you put a new value into t, t has that new value.
Just like when you do phsa := -tCw, phsa gets the value that turns clockwise, and when you do phsa := -tCcw, phsa gets the value that turns counterclockwise. Phsa no longer has -tCw, once you assign -tCcw
And for the next set of commands, also repeated 100 times, the servo is "centered" and it waits for t = t+(tC+200) or essentially 200ms more than in the previous set of commands. And it does this for 100 times.
In the last portion, it goes counterclockwise with tCcw for t = t+(tC-200), or 200ms less than in the first set of instructions.
I wondered why they were waiting 200ms more or less in the subsequent commands:
1.3ms out of 21.5ms
1.5ms out of 21.5 - 200ms
1.7ms out of 21.5 + 200ms
That seems a bit off. And I almost think wrong...as does Kumi. So when the servo gets commands, it should be 1.5ms of pulse, then 20ms from end of pulse to the beginning of next pulse. In this code, when they do the clockwise pulse, there is only 1.3ms of pulse. But by default they have setup their counter to assume 1.5ms (21.5ms)
So they should have subtracted 0.2ms from the 21.5 to get a 1.3ms pulse + 20ms from the end of one pulse to the beginning of the next pulse.
And the 1.5 set of instructions should not have anything subtracted whereas the 1.7ms set of instructions is correct because you need to add 0.2ms to the assumed 1.5ms pulse width.
1.3ms out of 21.5ms - 0.2ms = 1.3ms pulse + 200ms
1.5ms out of 21.5 = 1.5ms pulse + 20ms
1.7ms out of 21.5 + 200ms = 1.7ms pulse + 200ms
It would have been easier to have tC = 20_000 + 1300 or 1500 or 1700.
I think we can do this in an easier way, what do you think?
@atdiy/@tymkrs












