Need help with Servos and P2 clock speed
thej
Posts: 232
in Propeller 2
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
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
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
I think that is the problem, thus assuming 20Mhz works.
Mike
CLKHZ PRINT should print it out for you
so to be sure I used a calculator ;-) should be
<<< EDIT: but of course would not save you when running at RCFAST 20 MHz
to burn some time
better and more readable
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
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 ??
My mistake, thinking of P1 timing when I posted that.