Any sample PIC code (or pseudo code) for a servo slow?

RCMF

Welcome to RCMF

The Uk's Premier Model Flying Forum

Putting the Community back in to Radio Control


Welcome, Guest. Please login or register.
May 23, 2012, 09:24:50 AM

Login with username, password and session length

Pages: [1]   Go Down

Author Topic: Any sample PIC code (or pseudo code) for a servo slow?  (Read 721 times)

0 Members and 1 Guest are viewing this topic.

Offline aesmith wrote Any sample PIC code (or pseudo code) for a servo slow? on November 16, 2010, 18:00:51 PM
Hi,

I want to get back into electronics using PICs.   A possible first project will be a special servo controller, and I want to include a "slow" function into that.   I've looked at various examples of servo driving code, but I've not seen any "slow"  examples.

Can anyone point me to anything?   It doesn't need to be bomb proof code as I need to build it into the rest of my project, I'm really just looking for ideas for the logic to use (hence pseudo code would help as well).  My actual code will be assembler.

It will be a low end PIC, and I was thinking maybe only 8 bit precision for servo position.

Thanks,  Tony S


Reply #1
Offline rbp28668 wrote Re: Any sample PIC code (or pseudo code) for a servo slow? on November 17, 2010, 08:17:09 AM
You've got 3 logical blocks: measuring the input pulse, limiting the rate of the servo, generating the output pulse.
Pulse measurement and generation can use simple timing loops (providing interrupts are turned off) but see later.
limiting the rate -you need to keep a current position of where the servo should be.  If your measured pulse width is called measured then you've got something like:
pos = measurePulse()  // startup else servo will jump

for ever
  measured = measurePulse()
  if measured > pos then
    step = N
    pos = pos + N
    if pos > measured then pos = measured
  else
    step = -N
    pos = pos -N
    if pos < measured then pos = measured
  endif
  outputPulse(pos)
endFor

where N determines the slew rate of the servo.

If you can program the timer1 and CCP registers of (some?) pics then you can do the pulse capture and generation in hardware with help from the CCP interrupt:
To capture set timer1 in timer mode and set CCP to look for the rising edge of the servo pulse.  When the pulse starts the ccp captures the current value of timer 1.  In the CCP interrupt you look at which edge has been captured and store the CCP capture values.  If it's the rising edge you reprogram it to look for the falling edge.  If it's the falling edge then you've now got times at which the rising and falling edges of the servo pulse occur so can get the pulse width as falling-rising.  If your interrupt routine then sets the flag your main loop can wait on that flag and when triggered, do something creative with the measured servo pulse (like going through the slow algorithm and triggering the output pulse).

Bruce

Bruce Porteous

Reply #2
Offline aesmith wrote Re: Any sample PIC code (or pseudo code) for a servo slow? on November 17, 2010, 10:27:58 AM
Thanks.  Something to think about in all that.   

The simplest logic is to wait for the input pulse, measure it, decide what to output, create output pulse, then back to waiting.   All one loop where the whole process is "clocked" by the input.   For a series of events that always happen in the same order I didn't envisage the need for interrupts.

However I would need some sanity checking to make sure it didn't blindly follow rubbish on the input, some sort of check on the interval between input pulses as well as their length.


Reply #3
Offline sydc wrote Re: Any sample PIC code (or pseudo code) for a servo slow? on January 20, 2011, 18:35:48 PM
Hi,
I have some info for PICAXE's for slow servo control, PM me if your still interested.

sydc.


Reply #4
Offline scottieb wrote Re: Any sample PIC code (or pseudo code) for a servo slow? on January 23, 2011, 21:29:14 PM
Hi,

I'm not a PIC man at all but some projects for the AVR you could use as a staring piont.

Regards,

Scott


Reply #5
Offline Phil_G wrote Re: Any sample PIC code (or pseudo code) for a servo slow? on February 09, 2011, 14:50:10 PM
My pic-based reeds emulation encoder uses simultaneous servo slow on all channels. In a nutshell, all you do is set a 'target'  that the servo intends to reach. Every frame, you compare the current position with the target. If its the same, the servo has reached the target. If its smaller you increment the current position. If its greater, you decrement the current position.
This is repeated every frame loop. Setting targets is done by reading the incoming pulse widths. Easy peasy.
Cheers
Phil

Pages: [1]   Go Up