Shop OBEX P1 Docs P2 Docs Learn Events
Need help with Servos and P2 clock speed — Parallax Forums

Need help with Servos and P2 clock speed

I have been too busy to work with the P2 for several months now but I have jumped back in a couple weeks ago !! (Whoot!!)

I’m using TAQOZ in ROM to control servos.

I have 2 different servos:
-A FITEC 180 deg micro servo
-A Parallax Feedback 360 continuous rotation servo

I boot the board and check the sysclock by typing “CLKHZ .”
It outputs “80000000”.
Ok. 80MHz

I calculate the 50 Hz PWM frame using:
Clock / 50 Hz = Frame (in clock cycles)

80,000,000 / 50 = 2,400,000

I want some accuracy in defining the Pulse so I want the Frame to have 1000 clock cycles.
I now need to calculate the Offset Multiplier value:
Frame_at_Full_Speed / Desired_Frame_Size = Offset_Multiplier
2,400,000 / 1000 = 2400

Now I have everything I need to talk to the servo.
Here’s what I now type:

6 PIN —set the pin to use
: S 1000 2400 PWM ; — Define test word “S” to quickly talk to servo
25 S —Send a PULSE of 25 to the servo (25 1000 2400 PWM)

To automate testing the pulse width range, I wrote a little program to go through the whole range:

1000 1 DO I S I . 10000 1 DO I 2 + DROP LOOP LOOP MUTE

Here it is again with some white space:

6 PIN —Set PIN to use
1000 1 DO —Start Loop
I S —Sends pulse to servo
I . —Prints the loop index (pulse) value
10000 1 DO I 2 + DROP LOOP —this is a time wasting loop
LOOP —Return to start of Loop
MUTE —Turns off smart pin PWM mode

This did NOT work !?!

After extensive experimentation I found that assuming the clock speed to be 20 MHz works perfectly.
: S 1000 400 PWM ; —Recalculating the Offset Multiplier to 400/20MHz works.

I have a perfectly responding 180 deg servo now :-)

Moving on to the Feedback 360…
I’m “close” but I still can’t get this to work properly.

Assuming a clock speed of 2MHz (40 as Offset) gets the closest I can get but it is still not correct.
As I run my pulse test program, the servo actually turns the correct direction with small pulse numbers and the opposite direction with large pulse numbers. As it should but it is still very jittery and the speeds vary a lot so it is still not getting a properly aligned 50Hz frame.


Why do I have to adjust my Offset values to something other than 80MHz?
What speed does the board actually run at from boot up?
Is CLKHZ showing me the SysClock?
Are there any other insights that anyone can provide?

Thanks in advance !!
Jason

Comments

  • kwinnkwinn Posts: 8,697
    The clock frequency is 80MHz with a 5MHz crystal, however it takes 4 clocks per instruction so each cog runs at 20 mips (Million Instructions Per Second).
  • Hmmm...
    Somethings not right about that.
    The P1 required 4 clocks per instruction but the P2 should only need 2 clocks per instruction due to multiport RAM.

    j
  • Cluso99Cluso99 Posts: 18,069
    Yes, P2 is 2 clocks for most instructions.
  • Does the ROM assume (and enable) a crystal, or is it still running at RCFAST (~22MHz)?

  • AJL wrote: »
    Does the ROM assume (and enable) a crystal, or is it still running at RCFAST (~22MHz)?

    I think that is the problem, thus assuming 20Mhz works.

    Mike
  • Cluso99Cluso99 Posts: 18,069
    AFAIK the ROM enables RCFAST. It autobauds, and both TAQOZ, SD, and the Monitor/Debugger use the current setting and autobaud.
  • MJBMJB Posts: 1,235
    edited 2019-08-10 12:54
    thej wrote: »
    I have been too busy to work with the P2 for several months now but I have jumped back in a couple weeks ago !! (Whoot!!)

    I’m using TAQOZ in ROM to control servos.

    I have 2 different servos:
    -A FITEC 180 deg micro servo
    -A Parallax Feedback 360 continuous rotation servo

    I boot the board and check the sysclock by typing “CLKHZ .”
    It outputs “80000000”.
    Ok. 80MHz

    I calculate the 50 Hz PWM frame using:
    Clock / 50 Hz = Frame (in clock cycles)

    80,000,000 / 50 = 2,400,000

    I want some accuracy in defining the Pulse so I want the Frame to have 1000 clock cycles.
    I now need to calculate the Offset Multiplier value:
    Frame_at_Full_Speed / Desired_Frame_Size = Offset_Multiplier
    2,400,000 / 1000 = 2400

    Now I have everything I need to talk to the servo.
    Here’s what I now type:

    6 PIN —set the pin to use
    : S 1000 2400 PWM ; — Define test word “S” to quickly talk to servo
    25 S —Send a PULSE of 25 to the servo (25 1000 2400 PWM)

    To automate testing the pulse width range, I wrote a little program to go through the whole range:

    1000 1 DO I S I . 10000 1 DO I 2 + DROP LOOP LOOP MUTE

    Here it is again with some white space:

    6 PIN —Set PIN to use
    1000 1 DO —Start Loop
    I S —Sends pulse to servo
    I . —Prints the loop index (pulse) value
    10000 1 DO I 2 + DROP LOOP —this is a time wasting loop
    LOOP —Return to start of Loop
    MUTE —Turns off smart pin PWM mode

    This did NOT work !?!

    After extensive experimentation I found that assuming the clock speed to be 20 MHz works perfectly.
    : S 1000 400 PWM ; —Recalculating the Offset Multiplier to 400/20MHz works.

    I have a perfectly responding 180 deg servo now :-)

    Moving on to the Feedback 360…
    I’m “close” but I still can’t get this to work properly.

    Assuming a clock speed of 2MHz (40 as Offset) gets the closest I can get but it is still not correct.
    As I run my pulse test program, the servo actually turns the correct direction with small pulse numbers and the opposite direction with large pulse numbers. As it should but it is still very jittery and the speeds vary a lot so it is still not getting a properly aligned 50Hz frame.


    Why do I have to adjust my Offset values to something other than 80MHz?
    What speed does the board actually run at from boot up?
    Is CLKHZ showing me the SysClock?
    Are there any other insights that anyone can provide?

    Thanks in advance !!
    Jason
    Is CLKHZ showing me the SysClock?
    looks like
    CLKHZ   word    CONL
            long    sys_clk
    CLKKHZ  word    CONL
            long    sys_clk/1000
    CLKMHZ  word    CONL
            long    sys_clk/1000000
    

    CLKHZ PRINT should print it out for you
    80,000,000 / 50 = 2,400,000

    so to be sure I used a calculator ;-)
    80,000,000 / 50 = 1,600,000
    
    6 PIN —set the pin to use
    : S 1000 2400 PWM ; — Define test word “S” to quickly talk to servo
    25 S —Send a PULSE of 25 to the servo (25 1000 2400 PWM)
    should be
    6 PIN                            —set the pin to use
    : S 1000 1600 PWM ;    — Define test word “S” to quickly talk to servo
    25 S                             —Send a PULSE of 25 to the servo (25 1000 1600 PWM)
    

    <<< EDIT: but of course would not save you when running at RCFAST 20 MHz

    to burn some time
    10000 1 DO I 2 + DROP LOOP —this is a time wasting loop

    better and more readable
       xxx  ms     / or     
       yyy   us      
       zzz   s
    

    and if you refer to TAQOZ better either post in the TAQOS thread
    or at least put it in the subject so the right people read it ...

    EDIT2:

    and don't forget that servos are not really controlled by PWM, but by 1 - 2 ms pulses every 20 ms
    For standard 180 deg servos at 0 = 1 ms, 90 = 1.5 ms, 180 deg = 2 ms (often needing some calibration)

    so you would not run the loop from 0 to 1000 for PWM

    so with a frame size of 1000 for 20 ms we have a step size of 20 us.

    so 1 ms = 50 and 2 ms = 100 steps, 1.5 ms = 75 steps is center position, resolution 50 steps / 180 deg

    include the scaling in the control word e.g. like this
    : S  ( +/-90deg ... )  25 * 90 / 75 + 1000 400 PWM ;
    0 S  / set center position
    -90 S  / set leftmost
    90 S   / set rightmost
    

    or rescale all a bit to give a 1 deg resolution instead 90/25 deg here.
    or rescale to % 0..100 or -100..100 or whatever you like

    EDIT3:
    using a smartpin pulse mode might be better than the PWM mode ??



  • kwinnkwinn Posts: 8,697
    thej wrote: »
    Hmmm...
    Somethings not right about that.
    The P1 required 4 clocks per instruction but the P2 should only need 2 clocks per instruction due to multiport RAM.

    j

    My mistake, thinking of P1 timing when I posted that.
Sign In or Register to comment.