Help modifying servo.h to give a 400hz signal

First off I am aware of how to modify these lines to change the frequency:

#define MIN_PULSE_WIDTH       544     // the shortest pulse sent to a servo
#define MAX_PULSE_WIDTH      2400     // the longest pulse sent to a servo
#define DEFAULT_PULSE_WIDTH  1500     // default pulse width when servo is attached
#define REFRESH_INTERVAL    20000     // minumim time to refresh servos in microseconds
#define SERVOS_PER_TIMER       12     // the maximum number of servos controlled by one timer
#define MAX_SERVOS   (_Nbr_16timers  * SERVOS_PER_TIMER)

If the REFRESH_INTERVAL is increased the maximum stable update is at 8000 / 125hz. At 2500 the frequency changes from 125hz at 2000us pulse width to 250hz at 1000us. Is there a way to modify this library to give a nice consistent 400hz update rate? Thanks in advance for the help.

Yes, probably, but how many servos do you want to drive and what inputs will you be taking ?

Your basically increasing the processing load on the chip by 10 so its likely to have unintended knock on effects.

Duane B

rcarduino.blogspot.com

I'm running quad rotor ESCs. The craft flies fine at 125hz updates. I'm looking to the 400hz update because I have moved from running the control algorithms on the arudino to a more powerful 32bit ARM uC. All the arduino does in that mode is take the data from the transmitter, send that data to the other MCU, and update the ESCs based on the data received from the 32 bit processor. All of that takes place over a SPI link in ~.4ms.

Sounds viable in principle, the next problem is that servo library is based on a full range pulse length of 1 to 2ms so 4 x 2 ms is 8 ms or 125 hz.

I assume that your escs support some other range ?

The servo library code is very readable so easily modifiable

Duane B

That makes sense and I just verified what you said on the scope. When the pulses are at 2ms it takes the entire time. So I guess the question now is how does one generate all of the pulses simultaneously?

i would not do that, instead i would interlace them with an offset chosen to minimise clashes int the high/low transitions for each channel, but thats just off the top of my head and may not be the best plan. whether it is a good plan or not, its very far from anything the servo library does so your looking at some serious modification or starting from scratch.

its also worth checking the specs of your escs, i dont know anything about quad copters, but i think i have seen reference to some escs having the option to switch to a different resolution to spport higher refresh rates in which case you might still be able to get away with some simple mods to the servo library.

Duane B

rcarduino.blogspot.com

Thanks for the input. I think what I really need to look into is a IC that I can interface with to generate the pulses I want at the frequency I desire.

I figured out the solution finally. To update at 400hz the code must be changed to

#define MIN_PULSE_WIDTH       1000// the shortest pulse sent to a servo
#define MAX_PULSE_WIDTH      2000// the longest pulse sent to a servo
#define DEFAULT_PULSE_WIDTH  1500     // default pulse width when servo is attached
#define REFRESH_INTERVAL    2500// minumim time to refresh servos in microseconds
#define SERVOS_PER_TIMER       1     // the maximum number of servos controlled by one timer
#define MAX_SERVOS   (_Nbr_16timers  * SERVOS_PER_TIMER)

The key is 1 servo per timer. If you want to run 8 ESCs the max rate is 200hz with servos per timer at 2. Hope this helps someone.

Notably this will only work for the MEGA because it has four timers.

1 Like

Hello Everyone
am using Due with 12-bit 330Hz Servo :slight_smile:
i was trying to get 330Hz for Servo PWM with 12-bit
https://www.unitjuggler.com/convert-frequency-from-Hz-to-µs(p).html?val=330
i set #define REFRESH_INTERVAL 3030
in Servo.h
is that okay or !
Best,,