View Full Version : Trying to understand PWM
davethebeard
09-19-04, 07:11 AM
I'm a very new ServoPod user. I thought I'd play with PWM driving a servo. If I type PWM commands directly on the NMIterm terminal, they work as expected - in particular the servo continues to be active. (It is a digital type)
If I create a simple word to wrap up the commands eg
: MS ( n --- ) 65500 33 */ PWMA1 PWM-OUT ;
and execute it by typing, say
15 MS
[ <15> here means set pulse width to 1.5 msec, rep rate is 300 Hz]
the servo twitches, but is then not driven. Typing in commands directly restores operation.
Naively I thought that the PWM stuff was autonomous, in that once you set the registers it carried on generating the same signal. Obviously I'm wrong !
RMDumse
09-19-04, 09:33 AM
You're not navie at all about the PWM stuff being autonomous and actually correct. This is one of the best parts of the PWM on the ServoPod(TM) and certainly figures into the naming of the board. Some boards have only software controlled Servo outputs and can do nothing else but run the servos. Some have interrupt driven servos and can run servos in the background, while doing something else in the foreground. While the ServoPod(TM) could do this as well, the ServoPod(TM) goes further than any other servo controller we know, having hardware PWM for up to 26 servos at the same time, and all this hardware runs autonomously, once started, with no processor intervention at all. So it's just an issue of being sure the hardware is set up right.
First let's talk about the rep rate. You say it is a rep rate of 300. How did you establish that rep rate in the hardware? Normally you need to do a PWM-PERIOD to estabish a rep rate for the PWM output to work. You didn't mention doing that in your listing, so that might be the source of the problem.
For normal servos (not necessarily the digital you have) you would start up, if using cpu at full speed, by setting as long as a period as possible to get as close to the 50 to 60Hz update rate the old RC Servos expect (actually about 76Hz, but close enough for all the servos we've tested). To do that, you'd type
32767 PWMA1 PWM-PERIOD
Then you could set the 1.5ms pulse rate by saying
7500 PWMA1 PWM-OUT
(I know that 7500 number, because that is what I worked out yesterday on a program for a walker robot.) Now you can put those together in a definition and it should still work.
: X
32767 PWMA1 PWM-PERIOD
7500 PWMA1 PWM-OUT
;
So now, if we wanted 300Hz instead, we'd have to compute that period. Well, there are 2,500,000 clocks per second in the PWM time base, so if we want a rep rate of 300 per second, doing the division we get 8333. So we'd want to put 8333 into the PWM-PERIOD
8333 PWMA1 PWM-PERIOD ( 300 Hz update rate
Now, looks to me that based on the 7500 for a 1.5mS pulse, we could come up with an easier multiplier than your 65500 33 */ approach. Looks like we could just multiply any tenths of milliseconds value (i.e. 15) by 500 and use that for PWM-OUT. Try this:
8333 PWMA1 PWM-PERIOD ( 300 Hz update rate
15 500 * PWMA1 PWM-OUT
and if that gets you what you want, try it in a definition
: X
8333 PWMA1 PWM-PERIOD ( 300 Hz update rate
15 500 * PWMA1 PWM-OUT
;
You only have to do the PWM-PERIOD once, so it can be in its own definition
: INIT
8333 PWMA1 PWM-PERIOD ( 300 Hz update rate
7500 PWMA1 PWM-OUT
;
and make another word like your MS, although I'm going to put a decimal point in the name to remember you feed it tenths of milliseconds
: M.S 500 * PWMA1 PWM-OUT ;
Now when I look back at the 65500 33 */, I figure you get a multiplier too big, almost 1985. So your result is about 4x too big as I see it. Please check and let me know if what I've shown you works.
davethebeard
09-19-04, 10:49 AM
Thankyou for your reply.
The servo (Multiplex Titan 4 digi) specifies a nominal frame rate of 300 Hz, 1.5ms for center. It actually works at the lower rates (eg 76 Hz) but is less smooth.
I am setting the frame rate to 8333, gives a period of 3.3 ms.
I cannot make your numbers work for the pwm ratio. What works for me is <period * 10 [ms]> * 1985. Thus
< 15 1985 * PWMA1 SET-PWM > positions to center
and now creating a word seems to work as well ! So I don't know what went wrong earlier, but everything seems fine now - so onto greater things ...
IsoMax V0.61
8333 PWMA1 PWM-PERIOD OK
: M.S 1985 * PWMA1 PWM-OUT ; OK
15 M.S OK
10 M.S OK
vBulletin v3.0.7, Copyright ©2000-2012, Jelsoft Enterprises Ltd.