Shop OBEX P1 Docs P2 Docs Learn Events
having trouble, need a push — Parallax Forums

having trouble, need a push

science_geekscience_geek Posts: 247
edited 2008-08-22 06:03 in Propeller 1
i am having troubles getting servos to work with my propellor, its not a hardware problem, its a software problem, i have servos working,· but only with the help of the object exchange, i have tried various versions to get a servo to go from 0 to 180 and then changing code to get it back to 0, i have the clock set to its highest setting of 80_000_000 and just want a little help with the code to make a servo work, i tried this, and it would go a certain distance and then put out a high pitch tone, here is the code, dont ask me where i got the numbers, i was working on this with my uncle and this is the last we got to before i had to go


CON
··
· _clkmode = xtal1 + pll16x··········· ' Set up clkfreq = 80 MHz.
· _xinfreq = 5_000_000
PUB TestFrequency
· 'Configure ctra module
· ctra[noparse][[/noparse]30..26] := %00100·············· ' Set ctra for "NCO single-ended"
· ctra[noparse][[/noparse]5..0] := 27···················· ' Set APIN to P27
· frqa := 1_000_000····················· ' Set frqa for 2093 Hz (C7 note) using:
······································ ' frqa/b = frequency × (232 ÷ clkfreq)
· 'Broadcast the signal for 1 s
· dira[noparse][[/noparse]27]~~·························· ' Set P27 to output
· waitcnt(clkfreq + cnt /40)

·

Comments

  • AleksAleks Posts: 52
    edited 2008-08-20 13:14
    My first question is are you certain that you have a 5 MHz external crystal on your XI and XO pins? If not you need to adjust your _xinfreq value to match the Hertz value of your crystal. Then change your pll16x to match your _xinfreq, for instance I have a 10 MHz crystal so to reach 80 MHz I use pll8x instead. Just a morsel of thought.

    If that is correct (which it probably is) then i have to ask why your waitcnt line reads "waitcnt(clkfreq + cnt/40)"? If you want to wait for a full second your code should read "waitcnt(clkfreq + cnt)". Using your mathematics, you should have probably written "waitcnt(clkfreq/40 + cnt)". Again, just advice.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ~Some men see things as they are and ask "why?"
    I dream of things that never were and ask "why not?"~
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2008-08-20 15:30
    servos need pulses with lengths between 1us and 2us, why do you think this code should do anything to make them operate?

    Graham
  • DgswanerDgswaner Posts: 795
    edited 2008-08-20 15:38
    why not use the Servo Object?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "A complex design is the sign of an inferior designer." - Jamie Hyneman, Myth Buster

    DGSwaner
  • science_geekscience_geek Posts: 247
    edited 2008-08-21 00:49
    i dont want to use the servo object because i was trying to do this on my own so i can more understand it written in spin and not assembly, yes im sure i have a 5 Mhz crystal, i dont have a chance to try waitcnt(clkfreq/40 + cnt) because im at my aunts tonight, but will try it, and i dont know why it would work, if you have a better code that i could use just for testing and understanding by all means let me see it, but i was just experimenting with what i know from experience
  • AleksAleks Posts: 52
    edited 2008-08-21 12:17
    The only reason I mentioned changing the code to "waitcnt(clkfreq/40 + cnt) " is because the cnt is the value of the internal count register, which is incremented with each cycle (i think with every cog toggle). clkfreq was the actual value of the delay, and you were adding it to cnt to inform the propeller that you wish to wait until the counter reaches that number, hence "waitcnt(...)". I'm sure this is just a review, but the problem i frequently faced with waitcnt is that i would set the internal delay time to a number to small to be picked up on. It takes 381 cnt pulses to process the waitnct(...) command. When i set the delay to 100 (waitcnt(100+cnt)) what i wound up with was a wait period where the command wasn't processed until after the counter had already passed the command it was waiting for. Thats the only reason I tossed it out there. I don't remember a lot about servos, so unfortunately thats all the advice I can offer you.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ~Some men see things as they are and ask "why?"
    I dream of things that never were and ask "why not?"~
  • SeariderSearider Posts: 290
    edited 2008-08-21 17:43
    Please forgive me if the following explanation is too simple as I may be statng the obvious.


    As Graham mentioned. First consider exactly what signal a servo needs in order to operate.
    Then design an highlevel approach to providing that signal
    Then build the code to produce the signal that is needed. Often is small steps that build on each other.

    Here is one typical specification but you should find the actual specifications for the servo you are using.

    1. A Servo requires a square wave pulse from 1ms to 2ms long with the actual lenght based on where you want to possition it
    2. The pulse should be repeated no more often than once every 20ms and typcally every 20ms

    pseudocode

    Loop every 20 mS
    · Start Delay
    ··· Output high
    ··· Time for 1.5ms
    ··· output low
    · end Delay
    End loop

    This would center the servo with a 1.5 ms duration.

    Develop the code to loop every 20 ms. Once you have that working then move on to adde the next complexity.

    Think about how to modify this to move the servo from one stop (1ms) to the other stop (2ms)

    Then what actual spin code would be needed to accomplish this.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

    Searider
  • science_geekscience_geek Posts: 247
    edited 2008-08-21 23:39
    ok, i got it now, one last question though, one problem i had tonight, was trying to figure out how long a pulse is, is there a way/equation to easily figure how many clock cycles to·set·the waitcnt(...) for, im starting to get it, i just havent coded in about 3 months and that was "low level" basic stamp code, i also havnt converted any on my psuedo code or old programs yet, and thanx a million for the help
    ·
  • Mike GreenMike Green Posts: 23,101
    edited 2008-08-21 23:49
    The CLKFREQ function tells you how many clock cycles are in one second.

    CLKFREQ / 1000 tells you how many are in a millisecond.· You can multiply this value to get pretty much any time you want.· If you're dividing up the 1-2ms range for a servo into 100 units, you'd have

    (CLKFREQ / 100000) * X + (CLKFREQ / 1000) where X is a number between 0 and 99.· The result is a clock cycle count for 1ms to 2ms.
  • JerryAJerryA Posts: 11
    edited 2008-08-22 06:03
    Even though you don't want to use the Servo object, giving it a glance will show you 1 approach to the problem that is successful.
    It is well structured and can give you ideas to try out. In any case it never hurts to take a peek, after all it's there to be used and
    no one would call it cheating (well I wouldn't anyway, can't speak for others ) rolleyes.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    jac {^[noparse]:)[/noparse]}

    Never underestimate the power of human stupidity! (It bites me all the time!)
Sign In or Register to comment.