Servo control woes


Closed Thread
Results 1 to 25 of 25
  1. #1
    Join Date
    Feb 2006
    Posts
    11

    Default Servo control woes

    I'm trying to get down the basics of PIC servo control, and not having much luck. I'm using a 16F690 and trying to get some kind of meaningful movement out of my servo. However, the only thing my servo will do is move to one extreme of its range and sit there, perhaps scornfully.

    The offending code is:

    @ DEVICE MCLR_OFF,WDT_OFF,PROTECT_OFF
    DEFINE OSC 8
    servo var PORTC.5
    i var byte

    loop:

    for i = 200 to 400
    low servo
    pulsout servo,i
    pause 18
    next i

    goto loop


    I thought that would get a wiggle out of it, but no. I'm using the internal oscillator there, so I think my timing is correct - the 16F690 is running at 8MHz.

    I've tried many different timings, and I've RTFM and RTFDataSheet and RTF-half-the-contents-of-google. I've tried so many tweaks and variations that I'm worried I'll hit the PIC's write limit soon. I've also tried using different servos (from other manufacturers) and a different 16F690. It just moves in one direction to its extreme and sits, every time.

    What is it I am missing here? Any help would be greatly appreciated.

  2. #2
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Wink a Word about Bytes ...

    [QUOTE=Eriswerks]I'm trying to get down the basics of PIC servo control, and not having much luck. I'm using a 16F690 and trying to get some kind of meaningful movement out of my servo. However, the only thing my servo will do is move to one extreme of its range and sit there, perhaps scornfully.

    The offending code is:

    @ DEVICE MCLR_OFF,WDT_OFF,PROTECT_OFF
    DEFINE OSC 8
    servo var PORTC.5
    i var byte

    loop:

    for i = 200 to 400
    low servo
    pulsout servo,i
    pause 18
    next i

    goto loop


    hi,Eris

    @ 8 Mhz, pulsout resolution is 5µS ( 10µS *4 / F.Mhz)

    so, correct for that.

    for i = 200 to 400 means i should not be a byte ( i max = 255 !!! )

    so, what's happening ??? i value goes from 200 to 255 then ... back to 0 , out of the for - next loop !!! program ends.

    just change i for a WORD !!!

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  3. #3
    Join Date
    Feb 2006
    Posts
    11


    Did you find this post helpful? Yes | No

    Default Still no joy

    Thanks for the reply. I've changed the variable's type from BYTE to WORD, but there must be something else wrong as well - my servo still goes to one extreme and sits. Even if I do away with the loop entirely and just send it one position to center it, I'm still getting this behavior. That suggests to me that it must be timing-related, but I've tried timing some LED pulses without doing a DEFINE OSC and it confirms the 8MHz I see in the datasheet.

    Maybe if I buy the PIC some chocolates?

  4. #4
    Join Date
    Feb 2006
    Posts
    11


    Did you find this post helpful? Yes | No

    Default More thoughts on the subject

    Looking through the 16F690 datasheet for anything I might have missed, I found this little nugget:

    The output of the 8 MHz HFINTOSC and 31 kHz
    LFINTOSC connects to a postscaler and multiplexer
    (see Figure 3-1). The Internal Oscillator Frequency
    select bits, IRCF<2:0> (OSCCON<6:4>), select the
    frequency output of the internal oscillators. One of eight
    frequencies can be selected via software.


    So, it seems like in order to make sure that the PIC is running at 8MHz, I should be telling it to use HFINTOSC and not LFINTOSC, and according to the diagram mentioned I should be setting the frequency select bits to 111.

    How do I go about doing that in PicBasic? Is it actually necessary to do so, or does the "intrc_osc_noclkout" that's in the include file for this chip have me covered already? I've also found mentions of putting "OSCCON = $70" in the code to set 8MHz, but I can't seem to find any real documentation on it. Is this controlling the frequency select bits? If so, do I still need to specify HFINTOSC somewhere?

    Sorry for the deluge of questions...

  5. #5
    Join Date
    Sep 2005
    Location
    Dayton, Ohio
    Posts
    72


    Did you find this post helpful? Yes | No

    Default

    1. Why not take the whole 8MHz issue out of the equation and run it at 4MHz? Once you get it working at 4MHz...THEN change the oscillator speed.

    2. I have some servo code at home. For a 12F629 I think. I'll dig it out and post it later.

    3. From what I remember, sending a single pulse of 1.5mS won't center the servo. You have to KEEP sending pulses to get it to move. So if it's at the extreme end, then you have to keep sending 1.5mS pulses until the servo reaches center. AND you need to keep sending those pulses if you expect it to STAY in the center. Some servos are quite a bit slower than others (check the spec sheet).

    regards
    Jim Robertson
    "MilesTag" DIY Lasertag
    www.lasertagparts.com/mtdesign.htm
    Dayton, Ohio

  6. #6
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default

    Eriswerks, milestag is right, the servo requires updates every 50 milliseconds or so othwise most servos will shutdown and produce no torque.

    Dave Purola,
    N8NTA

  7. #7
    Join Date
    Feb 2006
    Posts
    11


    Did you find this post helpful? Yes | No

    Default Damn you, inanimate object

    Thanks for your suggestions. I've whittled down the code to the bare essentials:

    @ DEVICE MCLR_OFF,WDT_OFF,PROTECT_OFF

    servo var PORTC.5

    loop:

    pulsout servo,150
    pause 18

    goto loop


    That's the entirety of it, no configuration or anything left out. That ought to center it and hold it there so long as the PIC is powered, but once again it moves all the way to one extreme and stays. I tried other pulsout values from 1 to 2000 just for the fun of it, but I keep getting the same result.

    The only thing I can think of is that the internal timer is in its LFINTOSC mode, which is 31kHz and would probably cause any pulse I care to send to look the same to the servo. There must be some language in PicBasic to tell the PIC to which frequency to set the internal oscillator, but I haven't been able to find it.

    What am I (still) doing wrong here?

  8. #8
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Post Datasheet !!!

    Hi,Eris

    Seems you have to read some closer the µChip Datasheet section 3 ...

    Lots ( ! ) of registers to set " by hand " that PbP do not handle !!!

    to confirm that, just program a dusty 16F84 ... will work properly with your example !!!

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  9. #9
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    The internal oscillator doesn't automatically run at 8MHz. You have to
    write to OSCCON to switch to 8MHz.

    The default value of OSCCON frequency select bits is for 4MHz. See the
    datasheet for the default value in this register at power up.

    Also PULSOUT toggles the pin twice to produce your pulse, so the initial
    state of the pin determins the polarity of the pulse.

    You want a high-going pulse for your servo, so clear the pin before the
    PULSOUT command.

    See if this works.
    Code:
    @ DEVICE INTRC_OSC_NOCLKOUT, MCLR_OFF,WDT_OFF,PROTECT_OFF
    DEFINE OSC 8
    servo var PORTC.5
    i var word
    
    OSCCON = %01110000 ' Now it's going to run at 8MHz
    
    PORTC.5 = 0 ' <-- make pin low before PULSOUT (toggles high then low)
    TRISC.5 = 0  ' make pin an output
    
    loop:
    
    for i = 200 to 400
    low servo
    pulsout servo,i
    pause 18
    next i
    
    goto loop
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  10. #10
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Cool

    Hi Eriswerks,

    Try using another servo or at least make sure that your servo is ok.

    Also, as you know, for most of the servos, the pulse range is from 100mS to 200mS.

    For some recent ones, range is from 60mS to 240mS (wider movement).

    Also, below is a code to consider while playing with servos.

    servo var portc.5
    freq var byte
    pulse var byte


    loop:

    pot portc.3,255,pulse
    pot portc.4,255,freq

    if pulse>240 then pulse=240 'Max pulse protection.
    If pulse<60 then pulse=60 'Min pulse protection
    If freq>30 then freq=30 'Frequency protection
    IF freq<5 then freq=5 'Frequency protection

    pulsout servo,pulse
    pause freq


    goto loop
    -----------------------
    If you also use an LCD, you can see at what value(s) your servo does what it does and how it does.



    ------------------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  11. #11
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Talking Servo inside ???

    [QUOTE=sayzer]

    Try using another servo or at least make sure that your servo is ok.

    Also, as you know, for most of the servos, the pulse range is from 100mS to 200mS.

    > That's really new ...


    > Mhhhh, I do not know who's inside ... but he has a lot to learn about R/C Equipments ...

    Alain

    PS: ALL servos accept the 40-50 Hz frame rate ...
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  12. #12
    Join Date
    Sep 2005
    Location
    Dayton, Ohio
    Posts
    72


    Did you find this post helpful? Yes | No

    Default

    ---------------------------------------------
    There must be some language in PicBasic to tell the PIC to which frequency to set the internal oscillator, but I haven't been able to find it.

    What am I (still) doing wrong here?
    ---------------------------------------------

    Yes, there is a PicBasic command... POKE

    Use POKE to write values to ANY register in the datasheet. I usually use binary values so it's easier to see and change individual bits:

    POKE OSCON, %10110001

    (this is a made up value, I don't know what you will need exactly)
    Jim Robertson
    "MilesTag" DIY Lasertag
    www.lasertagparts.com/mtdesign.htm
    Dayton, Ohio

  13. #13
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Cool

    For Acetronics,

    This is not the place for your egoistic posts.

    Why don’t you go to a primary school and show the little kids how much you know about everything you think you know about?

    You can top your ego that way.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  14. #14
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Angry The important thing is to speak ...

    Sayzer,

    If you want to know, I do not do care about my EGO ... no time for that.

    I'd just like you NOT to tell wrong things to those who do not know ...

    It's difficult enough to learn pic's programming ... try not to add your own errors nor misunderstandings ... verify first !!!

    Alain

    Engineer, 30 years of DiY R/C, ... and always doubting ...
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  15. #15
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Cool

    Ok, Acetronics I agree . People make mistake even the ones with 30yrs of experiences.

    Could you pls tell us what happens if we send 250mS - 300mS pulse to an R/C servo at 50Hz?

    Also, 350mS and 400mS at the same freq?

    How would servo respond to these pulse(s) ?

    Pls do not make it so complicated so that we, the learners without 30 yrs of experiences, can learn something from your valuable words. Before you post your answer, make sure that your answer does not conflict with your previous posts;

    ----------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  16. #16
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Angry Stoooooop !!!!

    Hi, sayzer

    So, you've decided to be frankly ridiculous ... your problem !!!

    250ms pulses @ 50 HZ gives .... a firm "1" ( or "0" ) level !!! you've just recreated the resettable monostable mutivibrator !!!

    Who should go back to primary school ??? ( period = 1 / frequency ... no need a highschool for that )

    your servo wil just have the little "jump" for amplifier settling at power up ... and won't move any more.

    you shouldn't have tried .... you're really boring.

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  17. #17
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Cool

    Finally you got it Acetronics,

    That is what I have been saying.

    If you look at the original loop, below, the code is sending pulses from 200mS to 400mS. In this case, the R/C servo would goto extreme edge and stop there at its max input pulse (let say 240mS). However, the code keeps sending larger pulses upto 400mS even though the R/C servo already stopped at 240mS.

    -----Orginal code-----

    loop:

    for i = 200 to 400 'When i=240, servo stops, but loop keeps increasing the pulse.
    low servo
    pulsout servo,i
    pause 18
    next i

    goto loop

    ---------------------


    An engineer like you, age 49, should be in teaching mode rather then an egoistic mode. You should be using more explicit words to express your knowledge and have others understand what you are trying to say rather then using complicated engineering terms (an expression of egoistic unsatisfactory career to show others how much you know and others don’t)

    I will stop here. This is going nowhere.
    Let stop here Acetronics; it has no benefit to any of us.

    -----------------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  18. #18
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Angry

    Eris's pulses are NOT 200 to 400 ms, as you tell,

    but 200 to 400 x 5µS !!! this EQUALS 1 to 2 ms ...

    read your manual more carefully before hoping to give lessons ...


    Straight back to primary school !!!

    Hope not to read you any more about that. CENSORED !!!

    E.O.F.

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  19. #19
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Cool Apology

    Acetronics,

    I would like to apologize for the things I said.

    You were right and I was wrong. I missed some parts and behaved impolite.

    I also sent you a private message to apologize.

    Sayzer

    ---------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  20. #20
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Wink no public apologies needed ...

    [QUOTE=sayzer]Acetronics,

    I would like to apologize for the things I said.

    ...

    Hi, Sayzer

    that's kind from you ,

    the only important thing is you've learnt something useful ... I don' like this teaching method ... but I'm pretty sure you will always remember about PULSOUT resolution.
    a good joke is my favourite method for students to keep things in mind.

    That's the only real important thing....; may be ... life respect too, but it's not the right place to discuss it here.

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  21. #21
    Join Date
    Feb 2006
    Posts
    11


    Did you find this post helpful? Yes | No

    Default Update: absolutely no progress

    I've tried all the tips you guys gave, and have also been trying any and all PicBasic servo control code I could find on the net, but something is still not right. I've tried four different servos that I know to be functional. I've tried a 16F690 and a 16F628A, with the internal oscillator and with an external 4MHz crystal. I've tried changing the pulsewidths and pauses to every reasonable and many unreasonable numbers. I've switched breadboards. I'm shopping around for a goat to sacrifice to the PIC.

    Here's what I'm trying to use now, with a 16F628a:

    trisb = 0
    cont var byte
    cont = 0
    led var PORTB.1

    hither:

    low led
    for cont = 0 to 50
    pulsout PORTB.0,110
    pause 18
    next cont

    goto yon

    yon:

    high led
    for cont = 0 to 50
    pulsout PORTB.0,190
    pause 18
    next cont

    goto hither


    It is a slightly modified version of some code from the rentron site, which was meant for a 16F84. My include file (not the whole thing of course) looks like:

    device pic16F628A, xt_osc, wdt_on, mclr_on, lvp_off, protect_off


    My LED does flash convincingly, but still my servo heads to one extreme and stays there. This is keeping me awake at night now. What mistakes might I still be making? Does anyone know of an example of a working servo controller written in PicBasic, including a circuit diagram and not intended for a 16F84? I just need something functional to build on.

    You're probably sick of this thread by now, but any help would be appreciated.

  22. #22
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Cool

    Try this code.
    Schematic and code are also attached.
    ------------------------------------------------------------------------


    @ DEVICE intrc_osc, MCLR_OFF,WDT_OFF,PROTECT_OFF

    DEFINE LCD_EREG PORTA
    ' Set LCD Enable port

    DEFINE LCD_EBIT 7
    ' Set LCD Enable bit

    TRISA=%00100000
    ' RA5=Input, Rest= Output

    TRISB=%01100001
    ' RB6, RB5, RB0 = Input, Rest=Output

    '--------Assign name to ports---------------
    potvar var portb.0
    'Pot is connected to RB0.

    servo var portb.3
    'Servo is connected to RB3.

    Button1 var portb.5
    'Button1 is connected to Portb.5 and increases the frequency.

    Button2 var portb.6
    'Button2 is connected to Portb.6 and decreases the frequency.


    '---------Variables-------------------------
    pulse var byte
    'Variable to hold Pot value for servo Position

    freq var byte
    'Variable to hold Button1 and Button2 adjustments for Frequency

    '--------Initial Setup----------------------
    Freq=18
    'Initial Frequency

    '--------Main Loop-------------------------

    loop:

    IF button1=0 then freq=freq+1
    'Incr freq

    IF button2=0 then freq=freq-1
    'Decr freq

    IF freq>30 then freq=30
    'Max. Low freq

    IF freq<5 then freq=5
    'Max. High Freq

    pot potvar,255,pulse
    'Read pot and store the value in pulse variable

    If pulse>240 then pulse=240
    'Max. Pulse 240mS

    IF pulse<60 then pulse=60
    'Min. Pulse 60mS.

    pulsout servo,pulse
    'pulsout the stored pot value (pulse) to servo pin.

    pause freq

    lcdout $fe,1,"Pulse:",#pulse
    LCDOUT $fe,$c0,"Freq:",#freq


    goto loop

    --------------------------------------------------------------------------
    Attached Images Attached Images  
    Attached Files Attached Files
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  23. #23
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Question little walkaround ...

    Hi, Eris

    Seem strange ...

    There's one last thing we didn't had a look to : Your supply !!!

    Could you describe it or better send a scheme.

    On my side, I try your code on my 628 R/C testboard ... just to be sure!.

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  24. #24
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Wink Walkaround highly requested ...

    Hi, Eris

    Bad news ... I copied and pasted your code, just changed portB N°s as required here ... put a 6LR1 cell ( there's a regulator on the board !!! ) and a futaba servo.

    Every thing goes as you programmed it : the led blinks and the windscreen wiper does its job ...

    The problem is not ( no more ... humour ) in your code ...

    So, What to do ??? ...

    First verify your servo supply ( good common ground w/Pic ... enough current to feed the servo ...)

    Second verify the signal lead of the servo ... servo behaviour smells no received signal !!!
    In that way I would recommend you !!! SOFORT !!! to add a 1k resistor between Portb.0 and your servo connector.

    I keep the line
    Alain
    Last edited by Acetronics2; - 27th February 2006 at 13:12.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  25. #25
    Join Date
    Feb 2006
    Posts
    11


    Did you find this post helpful? Yes | No

    Default Finally, it works!

    Many thanks to you smart and helpful people - I've finally got this working. I was spending all my time checking and rechecking the code, and it seems that the problem (or the final one, anyway) was that the servo and PIC didn't have a common ground. And here I thought that breadboard with two separate bus strips would be *helpful*.

    I did, at least, pick up a lot of PicBasic tips while working on this, so I should be better equipped for the rest of my project. Thanks again!

Similar Threads

  1. More Servo Woes
    By chrisshortys in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 13th May 2009, 08:40
  2. Replies: 10
    Last Post: - 26th May 2008, 07:00
  3. Help with Servo Control Please!
    By wireman22 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 7th June 2007, 18:15
  4. Control RC servo via Parallax Servo Control
    By cibotsan in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 17th September 2005, 08:18
  5. Servo control with 12F629
    By achilles03 in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 23rd June 2005, 23:34

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts