Servo library alternative

Hi,

I am controlling 4 bldc motors using standard simonk esc drivers. I am currently using standard arduino servo library, but as i am controlling 4 motors, that means that the highest refresh frequency can be 125Hz with a period of 8ms. For me that is too slow as i need a refresh frequency of 400Hz. The servo library works so that it makes a pulse for the first motor and only then starts counting for the second motor and so on.
So what i need is a library that can create pulses for all four motors at the same time, so that the refresh period can be 2.5ms. I know that the varspeedservo.h library exists but it uses the same principle as standard servo library.
I dont have time for modifying servo library for my needs.
Does anyone know an alternative library that can achieve higher refresh rates?

Thank you for all your help, mate115

Simonk firmware only uses a range of 1000 to 1850 us I believe. This can allow for up to 500hz refresh. What if you were to just use analogwrite. If you use one of the analog outs with 500hz refresh analogWrite(127) should be a 1000ms pulse? Or am I crazy..

Otherwise this might help https://github.com/superjax/ESC

I was actually thinking the same thing. Just using PWM with a frequency of 400Hz. The problem is that 8 bits of resolution would be too low for my needs. Actually even 850 steps of resolution is a bit too low, but it gets the job done.
But thank you for the reply nonetheless!

Have you looked at Multiwii? It’s an open source Arduino program for controlling multirotors. I’m sure the answer is in that source somewhere.

alka:
What if you were to just use analogwrite.

The output of analogWrite() is not at all the same as the output of the Servo library. Unfortunately both outputs are referred to as PWM.

IMHO the output of the Servo library should not be called PWM.

...R

Alka, i have not looked at multiwii, as its a full multirotor controller and i want to create my own. Everything is actually working, the only thing that stopping me right now is the esc refresh rate. I have looked at multiwii and it seems they coded their own esc control.

Robin2 - you are right, its not at all the same. Btw, the servo control output actually has its own name, its called PPM - pulse position modulation :wink:

I think i'll stick to my 125Hz standard servo library for now. Modifying the library seems like too much for me right now. Thank you all :slight_smile:

mate115:
Btw, the servo control output actually has its own name, its called PPM - pulse position modulation :wink:

Alas. PPM is often used to mean the stream or 4 or 6 pulses that are sent by a transmitter to a receiver (old style RC equipment).

...R

Hi mate115,

It's possible to create a 400Hz PWM signal with 11-bit resolution using a few lines of register manipulation. A library isn't really necessary.

On the Arduino Uno however it's only possible to get this resolution on two pins, not enough for a quadcopter. The Mirco fares a little better with four, the Mega twelve and the ARM based Zero and Due eight, with additional PWM outputs on auxiliary timers.

Which Arduino are you intending to use for your flight controller?

Robin2:
The output of analogWrite() is not at all the same as the output of the Servo library. Unfortunately both outputs are referred to as PWM.

IMHO the output of the Servo library should not be called PWM.

...R

You are right.. not the same but the are both pulse width modulation aren't they? The servo library just has a longer period between pulses. The ESC doesn't care what we call it.

alka:
You are right.. not the same but the are both pulse width modulation aren't they? The servo library just has a longer period between pulses.

Whatever you choose to call them they are NOT interchangeable.

...R

Robin2:
Whatever you choose to call them they are NOT interchangeable.

...R

Yes, but this is for a specific speed controller firmware that can take up to a 500hz refresh rate. A half duty cycle pwm with at 500hz frequency is a 1000us pulse. It works. The problem as the OP said was just the precision. You only have 120 steps or so of it this way.

alka:
Yes, but this is for a specific speed controller firmware that can take up to a 500hz refresh rate.

I don’t see how that changes things.

…R

Hi alka,

You are right.. not the same but the are both pulse width modulation aren't they? The servo library just has a longer period between pulses. The ESC doesn't care what we call it.

As Robin2 mentions, there's a difference between Pulse Width Modulation (PWM) and Pulse Position Modulation (PPM).

With PWM the receiving device, the flight controller, ESC, servo etc.., extracts information from the width of the incoming pulse.

With PPM the receiving device, say a flight controller, extracts information from the pulse edges relative to the next pulse, hence the pulses' position is important. This is used mainly in CPPM RC receivers where all the signal channels are multiplexed on to a single line.

Yes, but this is for a specific speed controller firmware that can take up to a 500hz refresh rate. A half duty cycle pwm with at 500hz frequency is a 1000us pulse. It works. The problem as the OP said was just the precision. You only have 120 steps or so of it this way.

It's possible for an Arduino to output PWM at 400Hz with 11-bit resolution, that's accurate to the 1uS required by ESCs and servos, by directly setting the microcontroller's registers.

Here's an example of how to do it on the Arduino Mega: controlling an array of 10 servos - Robotics - Arduino Forum.

It's set-up to drive 12 servos at 50Hz, however to change the PWM frequency from 50Hz to 400Hz, just amend the value of the ICRx registers from 20000 to 2500.

Robin2:
I don't see how that changes things.

...R

Because THIS firmware on THIS ESC is ONLY timing the length of the pulse. I know what the difference is. If he had said he had a different firmware i would have a different answer about using analogWrite(). Simonk firmware only cares about pulse width. Anyway, martinL's link is quite useful. 11 bit PWM.
Edit: I will maintain that the signal from the servo library IS a PWM signal. The only thing that changes is the width of the pulse.. servo library output = pwm.

alka:
Simonk firmware only cares about pulse width.

And what range of pulse widths does it expect?

Is it the normal servo range of 1000 to 2000 microsecs?

...R

Robin2:
And what range of pulse widths does it expect?

Is it the normal servo range of 1000 to 2000 microsecs?

…R

That was in my first answer although i was a bit off… To allow for a higher refresh rate the firmware uses a lower max pulse duration.

from the tgy.asm file

.equ STOP_RC_PULS = 1060
.equ FULL_RC_PULS = 1860

So the analogWrite(127) to analogWrite(240) is about the same pulse widths. ( on a 500hz output pin).

My maths (which is often faulty) tells me that at 500Hz a cycle takes 2000 µsecs. As analogWrite() takes values from 0 to 255 the granularity will be about 7.84µsecs. That suggests that 135 gives 1060 and 240 gives 1881.

However the control would be very coarse - just 240 - 135 = 105 steps between min and max. Doesn't that negate the purpose of the high refresh rate? Using Servo.writeMicroseconds() you would have 821 steps over the full range.

...R

I think your maths look spot on :slight_smile: ( I just guessed at my numbers)

Yes it granularity is horrible. But hey.. the thread title was "servo library alternative".