Serial input and PWM

Started by George Graham February 1, 2004

I am stalled in my autopilot project for my homebuilt airplane.

I planned to use the background PWM, however, my servo motors
go crazy. I am confused by the term "percent duty cycle"
used in the documentation.

I sometimes read in serial data from a GPS NEMA source on Com3.
Should I switch to foreground/background tasks?

=====
George Graham
Mazda Rx7ez N4449E
Homepage <http://bfn.org/~ca266>

__________________________________



Duty Cycle is the working portion of a rectangular wave expressed in
percentage of the entire wave. 100% duty cycle is DC; 0% duty cycle is
also DC. Anything between 0% and 100% is a ratio of the "on" time
duration vs the period of the wave.

For example, if in some application "on" is high, a 10Hz 25% duty cycle
signal is high for 25mS and low for 75mS; 25:100 is 25%. It is also
accurate to describe the same signal as a 75% duty cycle signal if "on"
is low. Tom
Tom Becker
--... ...--
www.RighTime.com
The RighTime Clock Company, Inc., Cape Coral, Florida USA
+1239 540 5700




I have never tried to control a servo, but i can tell you that tasks
are great but are not really working a the same time, so this migh be a
problem, you pulse the servos and then stop pulsing them... I don't
really know :)

Are you reading a gyroscope and/or accelerometer of any kind for
Inertial measurement? If yes, would you consider telling me how you are
integrating the gyro and de-drifting it? also a great source of info : autopilot.sf.net

Good Luck. --
Este mensaje ha sido analizado con MailScanner
y se considera que estlimpio.



Many servos use pulse width to control the servo position. The signal
needs to be refreshed periodically, typically every 20 ms. If this applies
to your servos, use PWM with a frequency of about 50 Hz, it's not critical,
and choose the duty cycle to supply the pulse width used by your servos,
typically between one and two milliseconds.

John-

>I am stalled in my autopilot project for my homebuilt airplane.
>
>I planned to use the background PWM, however, my servo motors
>go crazy. I am confused by the term "percent duty cycle"
>used in the documentation.
>
>I sometimes read in serial data from a GPS NEMA source on Com3.
>Should I switch to foreground/background tasks?
>
>=====
>George Graham
>Mazda Rx7ez N4449E
>Homepage <http://bfn.org/~ca266>





--- Francisco Lobo <> wrote:
> Are you reading a gyroscope and/or accelerometer of any kind
> for
> Inertial measurement?

Not yet.

>
> also a great source of info : autopilot.sf.net
>

Thanks, that may help someday.

=====
George Graham
Mazda Rx7ez N4449E
Homepage <http://bfn.org/~ca266>

__________________________________



--- Tom Becker <> wrote:
> Duty Cycle is the working portion of a rectangular wave

Thanks for your reply Tom.

My code which uses the "servo.bas" example, works fine
with a 1.5 ms pulse as centering the servo, then
longer or shorter duration to move it.

However, nothing works right with the "PWMexample.bas"
code. There is a warning not to use the com2 serial
port, as it conflicts with Timer1.

My program uses com3, but it really does not work
without it either. By work, I mean send a steady 1.5ms
pulse.

=====
George Graham
Mazda Rx7ez N4449E
Homepage <http://bfn.org/~ca266>

__________________________________


I did not quite catch if these are model aircraft servos.

Because model a/c servos rely on a consistant pulse width repeated
periodically, but are not critical on variances of the period, the
BX-24 does a nice job of controlling model a/c servos. A background
task on the BX-24 is like a task on any multi-tasking computer. You
do not have precise control on when a task is going to run.

If you need precise control of both the pulse width, and the period,
then you need to use a hardware timer. That is what the PWMExample is
doing. You can run two 8 bit PWM channels on pins 26 and 27. Because
it is using a hardware timer, software functions using that timer are
no longer available.

--- In , George Graham <rx7ez@y...> wrote:
>
> I am stalled in my autopilot project for my homebuilt airplane.
>
> I planned to use the background PWM, however, my servo motors
> go crazy. I am confused by the term "percent duty cycle"
> used in the documentation.
>
> I sometimes read in serial data from a GPS NEMA source on Com3.
> Should I switch to foreground/background tasks?
>
> =====
> George Graham
> Mazda Rx7ez N4449E
> Homepage <http://bfn.org/~ca266>
>
> __________________________________
>




--- andymich2 <> wrote:
> I did not quite catch if these are model aircraft servos.
>
Yes Andrew, I have some very strong model airplane servos.
My hope, was that I might also use the model airplane solid
state gyros as well, since they simply plugin between the
radio and the servo.

> the BX-24 does a nice job of controlling model a/c servos.

In the background ? Mine only went haywire. > If you need precise control of both the pulse width, and the
> period,
> then you need to use a hardware timer. That is what the
> PWMExample is
> doing. You can run two 8 bit PWM channels on pins 26 and 27.
> Because
> it is using a hardware timer, software functions using that
> timer are
> no longer available.

So, the answer is no, I cannot run the PWM channels as well
as read in serial data on one BX-24?

If that is the case, perhaps I should use two BX-24's, or
try one of the servo controller chips?

=====
George Graham
Mazda Rx7ez N4449E
Homepage <http://bfn.org/~ca266>

__________________________________




George,
YOur final thoughts support what I am doing. While the
BX24 may handle all the tasks I am implementing I opted to
offload servo work to a chipset that does just that
and well. I may later go back and see if the BX24 can indeed
perform all the tasks but an oscillator and another 20=pin
Pic is not much of a load.
The tires carry a vehicle on the road as the wheels are
not efficient enough.
Don Lewis
--- In , George Graham <rx7ez@y...> wrote:
> --- andymich2 <andrew.michalicek@h...> wrote:
> > I did not quite catch if these are model aircraft servos.
> >
> Yes Andrew, I have some very strong model airplane servos.
> My hope, was that I might also use the model airplane solid
> state gyros as well, since they simply plugin between the
> radio and the servo.
>
> > the BX-24 does a nice job of controlling model a/c servos.
>
> In the background ? Mine only went haywire. > > If you need precise control of both the pulse width, and the
> > period,
> > then you need to use a hardware timer. That is what the
> > PWMExample is
> > doing. You can run two 8 bit PWM channels on pins 26 and 27.
> > Because
> > it is using a hardware timer, software functions using that
> > timer are
> > no longer available.
>
> So, the answer is no, I cannot run the PWM channels as well
> as read in serial data on one BX-24?
>
> If that is the case, perhaps I should use two BX-24's, or
> try one of the servo controller chips?
>
> =====
> George Graham
> Mazda Rx7ez N4449E
> Homepage <http://bfn.org/~ca266>
>
> __________________________________
>



Hi Don!

Thought I'd through my two cents in. I have a program controlling 8 servos
pretty easily using only 8 pins. Because of a HD crash, I lost the source
code, but it's really pretty simple.

I setup a task that runs in the background. I use four three global
variables for each servo. One byte variable holds the current position
value, one single holds the minimum position in ms, and the third single
holds the maximum position delta (max - min) in ms. The fourth holds the pin
#.

Here's an outline of the task:

Function Servos()

Dim TotalTime as single

Loop
Loop
For z = 0 to 7
Pos = Min(z) + (Max(z) * (SevPos(z) / 256))
Call Pulseout(Pin(Z),Pos)
TotalTime = TotalTime + Pos
Next z
Call Delay(19ms - TotalTime)
While OkToRun = 1
Call Delay(1 second)
While True

Bear in mind that it's from memory, and something I did about two years ago,
and syntactically it's definately pseudo code....

The only time I see any problems is when all 8 servos are set to near the
max position. (8*2ms = 16 ms)

The reason I used the Min() and Max() arrays is that I found differences
between servos, even the same models. Also, by setting Min() to the actual
max pulse with that the servo could use, and the Max() to a negative number,
I could change the rotation/position direction (i.e. 0 would be at the max
position, 255 at the minimum). Some of my servos actual control values
ranged between 0.8ms and 1.1ms min and between 1.8ms and 2.7ms for maximum
although the spec says it should be between 1ms and 2ms, with center being
1.5ms.

The idea was to have a loop that would execute about every 20ms or so. At
the start of each loop, it sends each servo a pulse, then waits for 20ms
minus the total of the pulse widths. The OkToRun variable is a global that
is set after a delay on program startup. I always add an initial delay of
about 5 seconds so it's easier to gain control of the BX-24 when downloading
a new program.

Most servos will be OK as long as the next pulse comes more that 8ms and
less that about 25ms from the previous pulse. If you were seeing jitter in
the servos, then the pulse were probably either to far apart (servo will
think it lost the signal and try to center), or they were much too fast. It
could also be that the polarity was wrong (i.e low pulse instead of high
pulse or visa versa).

Some of the cheaper servos actually seem to wait at least 3 cycles before
they think the signal went away, so being a little late is better than being
early IMHO.

To control the position of the servos, I had another task that ran,
measuring various inputs, and doing a little math, then setting the global
variable position values. The servo task picked up the new position the next
time it looped.

Hope that helps out!

Sloan ----- Original Message -----
From: "Don Lewis" <>
To: <>
Sent: Friday, February 06, 2004 11:29 AM
Subject: [BasicX] Re: Serial input and PWM > George,
> YOur final thoughts support what I am doing. While the
> BX24 may handle all the tasks I am implementing I opted to
> offload servo work to a chipset that does just that
> and well. I may later go back and see if the BX24 can indeed
> perform all the tasks but an oscillator and another 20=pin
> Pic is not much of a load.
> The tires carry a vehicle on the road as the wheels are
> not efficient enough.
> Don Lewis