PWM engine for a servo

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Flowcode V4 User
Posts: 115
Joined: Fri Feb 22, 2008 6:38 pm

PWM engine for a servo

Postby chevy6600 » Thu Oct 02, 2008 7:30 pm

>>> Topic follows on from forum posings here: viewtopic.php?f=23&t=4511

Hi all, after struglling for some time trying to spread a 1v span from a sensor to produce a PWM signal
that has a cycle of 17ms and a channel span of 1ms to 2ms. I kind of `back engineered` programs from sean because i was having trouble with the pulse either jumping back a few steps every now and again, and i was only getting around 30 steps spaning the 1ms to 2ms. Although the adc was setup to do my chosen 110 steps, the program ignored the 3rd or 4th step, and also was loosing steps due i believe to be the various rounding in numbers happening in my algarithoms. All which transpired to a 3% or 4% in each step. If you assumed 1ms to 2ms was 100 degrees of servo travel 3% was a 3 degrees each step.

So i decided to put together the program`PWM engine for servo` to get a better understanding on what was going on,
and to get a better resolution for my particular needs. Although it would appear that my prog is setup for 85
steps of resolution, when all is done and dusted, my `PROTEUS` simulator shows that i am getting 94 steps, so
i will be quite happy with that, at the same time i have managed to get a more steady PWM for each of the 94 steps.
This program `PWM engine for servo` should now show pretty close to 1% step or 1 degree step out of a 100 degrees.
While putting together this program i added explanations to what the various parts of the program were doing
together with explanations for pic chip settings that were used.
The program is what`s basicly required to produce a pulse train and move it as required by one potentiometer which
uses data from a `data` list, this list enables `oneself` to make the servo move in a non linear motion, of which could
include a dead band, more movement at the centre, or reversal etc. etc.

So the points of program interest are:

A)shows how to include a data list.
B)Puts together a pulse stream.
C)spreads the value of a potentiometer of which can be changed.
D)As the program stands, it is setup including ONE potentiometer/channel but the program will accept 4 potentiometers without any alteration appart from adding the `adc` components and duplicating some of the code.
E)A total of 8 potentiometers/channels from the current 4, can be used by changing a number in the code (a 2 to a 1).
F) Can span the sensor voltage using +vref and -vref.

The program was designed for my chosen pic a 18F2620, i chose this chip because i wanted the following.
1)Internal oscilator running at 32mhz.
2)Has a hardware multiplier (multiplies in 1 cycle instead of around 64 cycles!).
3)Has a 10 bit`adc` facility.
4)Has 2 Capture/Compare/PWM (i`m using the Capture/Compare).
5)Has 3 timers.
6)Has the abillity to have a +vref and a -vref voltage which i needed for my particular sensor output.
7)Has more inputs than i require.

The `PWM engine for servo1` program can be used i suspect, in many pic chips, so long as it has the same
abillities, even at different speeds, all you need to do i suspect is make sure `registers` byte codes need
changing and the other functions have differnt addresses etc. I have included explanations in my program to enable
an understanding of what the registers were used for so that you can find a similar function in another pic chip.
I would like to add this is the bare bones for a signal, you will need to add your bits of code for it to be usefull, it is a starting
program to get an understanding, because of the `C` code and nothing attached as output you will not see anything happen within
flowcode.....any one fancy having a go adding some program to this :?:

Seeing the interest in this thread and the other threads in using a PWM for a servo, i thought that i might as well
put my program here as it might be of interest to others and may help in understanding it, and wanting to put something
constructive on this forum...hoping that it might even set a trend going :!: :!:

I have also included a `fcd` file for a pic 18F2620 file which has the alterations done to include +vref and -vref.
for those of you who would like to try the 18F2620 family chips.
I hope you like it and hope you thought this interesting being as you have read this far. :mrgreen:
I would also like to thank the guys from the matrix team for getting me this far.

P.S. If i have made a mistake in the prog or some other boo boo please post and i`ll correct it, as i do not profess to be an expert.
PWM engine for servo.fcf
(13.52 KiB) Downloaded 638 times
(6.38 KiB) Downloaded 527 times

Posts: 65
Joined: Fri Nov 23, 2007 11:24 pm

Re: PWM engine for a servo

Postby WalkOver » Sat Apr 25, 2009 7:06 pm

I try to use your program. It isn't finished yet but I just want to thank you for your job and your very complete post and comment :)