Go Down

Topic: how can i change the frequency of servo library? (Read 9401 times) previous topic - next topic

tensa

im using the servo library to control my ESC using writeMicroseconds, and i found out that the PWM frequency of servo library is 50Hz, but my application needs a faster response. i can't also used the analogWrite(pwm) because their frequency is about 488Hz(from what i have read) which is too much for my ESC which can only perform well up to 400Hz. how can i change the frequency so my ESC will respond faster?

tensa


im using the servo library to control my ESC using writeMicroseconds, and i found out that the PWM frequency of servo library is 50Hz, but my application needs a faster response. i can't also used the analogWrite(pwm) because their frequency is about 488Hz(from what i have read) which is too much for my ESC which can only perform well up to 400Hz. how can i change the frequency so my ESC will respond faster?


i found something useful
inside the Servo.h there is a line
#define REFRESH_INTERVAL 20000 // minimum time to refresh servos in microseconds

changing this to 3333 might change the frequency from 50Hz to 300Hz in all pins that are attached  as servo, am i right?

mixania

#2
Mar 01, 2013, 06:09 pm Last Edit: Mar 01, 2013, 06:12 pm by mixania Reason: 1

im using the servo library to control my ESC using writeMicroseconds, and i found out that the PWM frequency of servo library is 50Hz, but my application needs a faster response. i can't also used the analogWrite(pwm) because their frequency is about 488Hz(from what i have read) which is too much for my ESC which can only perform well up to 400Hz. how can i change the frequency so my ESC will respond faster?


I don't know exactly know how to modify the servo library, but one thing i may suggest is to write you own code that could control a servo. I actually have done this my self in a very simple way! It was actually very fun too. In fact I had to do it since I was doing a project that had a Attiny85 as microcontroller. The Attiny85 microcontroller didn't support the Servo library that Arduino provides so I've done and created my own simple code.

Start out by reading this: http://en.wikipedia.org/wiki/Servo_control
Then you might find some other sources that would help you too.

Looking at the images may help:
http://upload.wikimedia.org/wikipedia/commons/b/b7/Sinais_controle_servomotor.JPG
http://upload.wikimedia.org/wikipedia/commons/7/7b/ServoPwm.png

You may start out like this as i did:

90 Degree position:
Code: [Select]


for (int i = 0; i < 100; i += 1)
 {
   digitalWrite(12, HIGH);
   delayMicroseconds(1500); // The range of the pulse is between 1000µs till 2000µs (from 0 till 180degrees)
   digitalWrite(12, LOW);
   delayMicroseconds(18500); // This will result into a period. NOTE: 18500+1500=20000µs=20ms which is the period
 }


180 Degree possition:
Code: [Select]


for (int i = 0; i < 100; i += 1)
 {
   digitalWrite(12, HIGH);
   delayMicroseconds(2000); // The range of the pulse is between 1000µs till 2000µs (from 0 till 180degrees)
   digitalWrite(12, LOW);
   delayMicroseconds(18500); // This will result into a period. NOTE: 18500+1500=20000µs=20ms which is the period
 }


Now you say your servo's period is different. Instead of 20ms your servo's period would be 1000ms/400Hz = 2.5ms. So your pulse may be in a range of 125µs to 250µs.

GOOD LUCK HACKING!  8)

zoomkat

Quote
changing this to 3333 might change the frequency from 50Hz to 300Hz in all pins that are attached  as servo, am i right?


Any reason you don't try it and see? The ESC is probably designed for a standard control input frequency, so changing that might not work.
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

tensa



im using the servo library to control my ESC using writeMicroseconds, and i found out that the PWM frequency of servo library is 50Hz, but my application needs a faster response. i can't also used the analogWrite(pwm) because their frequency is about 488Hz(from what i have read) which is too much for my ESC which can only perform well up to 400Hz. how can i change the frequency so my ESC will respond faster?



GOOD LUCK HACKING!  8)


thanks its really useful, i found out that the refresh interval works somehow the same as your example delayMicroseconds
thanks for the help, my ESC responds faster now. i was afraid to test in at first because it might cause high current output due to fast phase but after reading again the sheet of ESCs it is can handle high current

mixania


thanks its really useful, i found out that the refresh interval works somehow the same as your example delayMicroseconds
thanks for the help, my ESC responds faster now. i was afraid to test in at first because it might cause high current output due to fast phase but after reading again the sheet of ESCs it is can handle high current


No problem. I know it's a bit awkward to create a loop for very position that you want you servo to be in but I used this technique to control three servo's that were controlled by an attiny85.

hudemirc

Hello,

I have tried changing REFRESH_INTERVAL up to 400Hz(2500ms) in servo.h many times but i got the same result from an oscilloscope tha the frequency kept same as 50Hz(20000ms as default).

Today, I copied the library servo.h to desktop changed the value then recopied to where it was. (Program Files>Arduino>Libraries>Servo>src). Now, i can get any frequency i want.

I hope it will help someone.

ardy_guy

I don't see why changing that value would make it any more responsive anyway: it's the time between refresh if the value didn't change.

So if the value changed in the middle of a refresh "timeout" for want of a better word, I'm pretty sure the servo library will send the new value and not wait until the refresh is due.

But that can be tomorrow's test: I'll get the o'scope out and have a play.


Go Up