Simple, Yet Accurate, Dual Servo Driver (No Assembly Required)
JonnyMac
Posts: 9,208
From time-to-time I see new Spin programmers struggling with driving servos, usually trying to adopt methods learned from their BASIC Stamp (or other micro) experience. For those that want to control a few servos without using a PASM object, this little demo shows how. It does in fact launch a method (called servos) into its own cog so that the servos are properly refreshed every 20ms.
The code is in Spin and I did some analysis to account for the instruction overhead so that the output, expressed in microseconds, is as intended -- I have verified with a 'scope. The demo has two servos, but this can easily be modified (copy, pasted, edit) to up to eight -- don't go beyond eight else you might overrun the 20ms refresh window.
This may seem a little advanced to Spin newcomers, but it's really not that bad. Take a few minutes to study it and you'll find that it's quite straightforward.
[noparse][[/noparse]Edit] Fixed my own copy-paste error as identified by Tim (see below).
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon McPhalen
Hollywood, CA
Post Edited (JonnyMac) : 6/23/2010 8:42:07 PM GMT
The code is in Spin and I did some analysis to account for the instruction overhead so that the output, expressed in microseconds, is as intended -- I have verified with a 'scope. The demo has two servos, but this can easily be modified (copy, pasted, edit) to up to eight -- don't go beyond eight else you might overrun the 20ms refresh window.
This may seem a little advanced to Spin newcomers, but it's really not that bad. Take a few minutes to study it and you'll find that it's quite straightforward.
[noparse][[/noparse]Edit] Fixed my own copy-paste error as identified by Tim (see below).
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon McPhalen
Hollywood, CA
Post Edited (JonnyMac) : 6/23/2010 8:42:07 PM GMT
Comments
Tim
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon McPhalen
Hollywood, CA
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon McPhalen
Hollywood, CA
In my opinion, every numeric constant, with the exception of 0 and 1 and a few other rare exceptions, should be declared as a named constant. This is just good programming practice.
This "magic numbers" rule is a silly rule in my mind, and not one that adds value to an embedded program. For example, I have a pause() method that creates a delay in milliseconds. Personally, I don't find pause(1000) any less useful than pause(ONE_SECOND). But then, I take for granted that anyone using my code knows what they're doing. I refuse to write for those that don't -- maybe this is my problem.
Again, this is just my opinion. I did comment that the 989 accounts for overhead in the code; that should be quite enough, espeically for code that has been tuned and required no additional modification.
What makes me smile is that I've been accused of overwriting code; of being too verbose -- you're suggesting I'm not verbose enough. [noparse];)[/noparse] And let's be honest... there are far more egregious violations of "good programming practice" is posted code than a couple magic numbers from time-to-time.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon McPhalen
Hollywood, CA
You do contradict yourself in your posts, however. In your initial post you say "I see new Spin programmers struggling with driving servos", which is the audience you seem to be addressing with your code. Then later you say "In this particular case the cycles count is not going to change, even with a clock change and, therefore, does not need to be "newbie friendly."
It seems to me that your code is directed at new Spin programmers who are struggling to understand things, yet you then say that it doesn't need to be newbie friendly. Your code comment says "adjust for overhead", and I know what you mean, but will a struggling newbie?
I wrote the code in Spin because many newcomers are afraid of PASM, and I've seen a lot of mis-steps generating proper servo pulses in Spin. The initiated (i.e., not lazy) programmer will find more than enough information to learn from; the lazy can use it as is. I mean, really, how many of us in this forum would take the time to measure cycle count by specific instructions to make the code accurate (as I did)? Five, tops; others don't want to be bothered with this level of detail, they just wanted to be handed code that works. I did that.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon McPhalen
Hollywood, CA
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon McPhalen
Hollywood, CA
John Abshier
BTW, lest anyone think otherwise, I am not proclaiming this a *better* way to do anything, just *another* way, and perhaps something for others to expand on if they choose.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon McPhalen
Hollywood, CA
There is a version 7 now which address a few minor fixes that are different than version 5:
Version 6 - (07-18-2009) - Fixed slight timing skew in ZoneLoop and fixed overhead timing latency in ZoneCore
Version 7 - (08-18-2009) - Fixed servo jitter in ramping function when servo reached it's target position.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe
IC Layout Engineer
Parallax, Inc.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon McPhalen
Hollywood, CA