function to generate a PPM output?

How to create a function which generates a ppm pulse as a RC would? i want this function to be running in the background untill i change it.
what changes do i need to make to the following code?

void ppm_gen(int pin, int speed) {
digitalWrite(pin, HIGH);
delayMicroseconds( speed);
digitalWrite(pin, LOW);
delay(20);
}

From what i can understand this function will run only once and the control will return to the calling function.
But to run a servo the loop should run over and over again.
If my understanding correct, what changes should i make?

Use the servo library Luke ....

Yes but the refresh rate is 30Hz, very low for my liking.
I need to run an ESC for a bldc. I need it to be atleast 200 - 250Hz.

An RC ESC will expect PPM at 50Hz (which is the frame rate the servo library works at, not 30Hz).
Is this not an RC ESC?

@rahlk

Let me understand what you are asking… I read about servos, a PWM @ 50 Hz is slow for you, OK so you need PWM @ 200 to 250 Hz instead , correct ? OK Let analyse a PWM … @ 50 Hz Period = 1/50 = 0.02 s or 20 ms @ 200 Hz Period = 1/200 = 0.005 s or 5 ms. So a PWM is changing Ton vs Toff , therefore Ton < Toff → < 50 % Ton > Toff → > 50 % Ton = Toff → 50 %… Follow me so far…So your program have to reflect that. So if you whant the speed to increase, Ton have to increase → Speed /\ Ton Lower speed , lower Ton Speed / Ton.

Here an example of a code of a PWM @ 1000 Hz. Hope it help.

// Compile and Tested 

byte PWMpin = 11;
byte vpin=0;


int vin=0;
int period=1000; // 1 kHz = 1 / Period in uS
int timeA;  // time on
int timeB; // time off

void setup()
{ 
  pinMode(PWMpin, OUTPUT); 
} 

void loop()  
{ 
  vin=analogRead(vpin);
  timeA=map(vin,0,1023,0,period); // calculate time on
  timeB=period-timeA; // calculate time off  
  digitalWrite(PWMpin, HIGH);
  delayMicroseconds(timeA);
  digitalWrite(PWMpin, LOW);
  delayMicroseconds(timeB);
}

@Tech one,
RC equipment uses PPM, not PWM
Your arithmetic looks...suspect.

@AWOL

PPM is ? Pulse ? Modulation ...

edit again :

Your arithmetic looks...suspect.

What you mean my math look "suspect" ? My technical writting skills is not that great you know.

Edit :

PPM = Pulse Position Modulation

Like this site : [urlhttp://www.simplecircuitdiagram.com/2009/05/20/pulse-position-modulator-using-555-ic/][/url]

So in the program you simply keep Toff constant. You simply figure out @ lower frequency the Ton and @ higher frequency the Ton with a constant Toff using speed has a variable to change Ton.

@Techone

If you want to know the difference between ppm and pwm :

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1253149521/all

@Tech one
“20ms @ 200Hz” doesn’t make a great deal of sense.

Yes, low duty cycle PWM looks a lot like PPM.
However, unless you use very high resolution PWM (I.e. greater than 8 bit), your active PPM period (< 10%) is going to have very poor resolution.

However (again) RC gear is designed to run at a frame rate of 50Hz, so simply shortening the frame time may not work at all.

Maybe the OP is confusing the ESC’s control input PPM rate with its output PWM frequency.

@AWOL

OK Let analyse a PWM ... @ 50 Hz Period = 1/50 = 0.02 s or 20 ms @ 200 Hz Period = 1/200 = 0.005 s or 5 ms.

I mean ---> AT 200 Mz Period = ..... I forgot a DOT after 20 ms

I mean : @ 50 Hz Period = 1/50 = 0.02 s or 20 ms
@ 200 Hz Period = 1/200 = 0.005 s or 5 ms

Opps ... :blush:

Sorry that I sound confusing sometime.

I just trying to help the OP. Anyway, RC is Remote Control ? and ESC ?

In a program to generated a PWM or PPM, you still need to control Ton, Toff and the Period.

Edit:

@Erni

Thank for the link. Interresting.

@AWOL

An RC ESC will expect PPM at 50Hz (which is the frame rate the servo library works at, not 30Hz).
Is this not an RC ESC?

Yes it is an RC ESC. Some wise men around told me this refresh rate of 50Hz is not good for an RC ESC and it runs best at 200Hz and a max of 300Hz.

Thanks for the link from Erni, and from a "GOD" member MEM. I figured that the servo does run from a ppm pulse after all. The ON time varies from 1000 Microseconds to 2000 Microseconds with the OFF time being constant at 20 milliseconds. So my initial snippet was correct. But all this, they still don't answer my question- what should I do to have this function running in the background untill it's changed.

But all this, they still don't answer my question- what should I do to have this function running in the background untill it's changed.

You just need to use the standard Arduino servo library. You write a value to it and it automatically continously sends it out to the servo (read ESC) input at the proper rate until you write a new value. This is handled by interrupts but you don't have to do anything for that to happen.

Lefty

Some reading on ESCs.

http://www.google.com/search?q=arm+esc+site%3Ahttp%3A%2F%2Farduino.cc%2Fforum%2Findex.php&hl=en&num=100&lr=&ft=i&cr=&safe=images

I don't know your 'wise men', but I'd say they are not terribly experienced with RC gear.
If an ESC is designed for RC, then it EXPECTS PPM with a frame time of 20ms.
This is the frame rate used by digiprop RC gear for at least the last 30 years.
It may not work well at different frame rates.
Servos frequently don't.

The output frequency may well be much higher, as I mentioned earlier.

Or maybe you simply misunderstood the wise men.

Or maybe you simply misunderstood the wise men.

That’s my guess, wise men often speak in strange tongues. :wink:

@rahlk

Let me get this… A ON time of 1000 us or 1 ms to 2000 us or 2 ms. And a OFF time of 20 ms. You say you need 200 Hz to 300 Hz. All right , let figure out the frequency using the Ton and Toff.

@ Ton = 1 ms or 1000 us
Toff = 20 ms

1 / Period = Frequency ----> 1 / ( 1 ms + 20 ms ) = 47.61904762 Hz

@ Ton = 2 ms or 2000 us
Toff = 20 ms

1 / Period = Frequency -----> 1 / ( 2 ms + 20 ms ) = 45.454545… Hz

My question is : Where the 200 Hz to 300 Hz come from ? The problem is the 20 ms, it make the frequency slower.

My calculation is saying that a 50 Hz servo system will work. So can you try the servo library and see what happen ?

My 2 cent.

@ AWOL and Lefty-
Okay I got this. Screw the wise men (maybe they aren't that wise after all..).
Servo library works like a charm!
But here's what I did.. I used:

void setup() {
Serial.begin(115200);
}
// The channel 2 of my receiver is connected to pin 5 of my Arduino Mega.
void loop() { 
Serial.print(pulseIn(5, HIGH));
Serial.print("                ");
Serial.println(pulseIn(5,LOW));
delay(50);
}

I noticed that pulseIn(5, HIGH); gives a value around 1049 to 1899 (As i gradually move the throttle stick up)
and a separate pulseIn(5, LOW); gives a value from 18900 to 17900 (again as i move the throttle stick up)
In essence this isn't a ppm (which according to many people must have a constant Toff period of 20ms).
It theoretically is a pwm pulse, which has a variable duty cycle.

So what does a servo library generate anyway, eh?

It theoretically is a pwm pulse, which has a variable duty cycle.

But PWM has a constant period which your experiments are not showing anyway.

(which according to many people must have a constant Toff period of 20ms).

I would say the Ton has to be the constant one.
It looks like the high part is supposed to be constant but is drifting a bit.

Anyway it is much more like PPM that PWM.

But PWM has a constant period which your experiments are not showing anyway.

i guess it is a constant PERIOD pulse. If I do this:

Serial.printLN(pulseIn(5, HIGH)+pulseIn(5,LOW));

The value does 'NOT' change.

The value does 'NOT' change.

OK so you have PWM, it is not the right way to drive a servo, but some servos will make sense of it.