Go Down

### Topic: function to generate a PPM output? (Read 9695 times)previous topic - next topic

#### rahlk

##### Oct 01, 2011, 08:47 am
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?
Code: [Select]
`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?

#### Grumpy_Mike

#1
##### Oct 01, 2011, 08:50 am
Use the servo library Luke ....

#### rahlk

#2
##### Oct 01, 2011, 02:36 pm
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.

#### AWOL

#3
##### Oct 01, 2011, 02:49 pmLast Edit: Oct 01, 2011, 02:51 pm by AWOL Reason: 1
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?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### Techone

#4
##### Oct 01, 2011, 07:00 pm
@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.

Code: [Select]
`// Compile and Tested byte PWMpin = 11;byte vpin=0;int vin=0;int period=1000; // 1 kHz = 1 / Period in uSint timeA;  // time onint timeB; // time offvoid 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);} `

#### AWOL

#5
##### Oct 01, 2011, 07:10 pmLast Edit: Oct 01, 2011, 07:12 pm by AWOL Reason: 1
@Tech one,
RC equipment uses PPM, not PWM
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### Techone

#6
##### Oct 01, 2011, 07:13 pmLast Edit: Oct 01, 2011, 07:32 pm by Techone Reason: 1
@AWOL

PPM is ? Pulse ? Modulation ...

edit again :

Quote

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.

#### Erni

#7
##### Oct 01, 2011, 07:47 pm
@Techone

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

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

#### AWOL

#8
##### Oct 01, 2011, 08:23 pmLast Edit: Oct 02, 2011, 09:39 pm by AWOL Reason: 1
[font=Verdana][/font]@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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### Techone

#9
##### Oct 02, 2011, 02:29 amLast Edit: Oct 02, 2011, 02:32 am by Techone Reason: 1
@AWOL

Quote
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 ...

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

#### rahlk

#10
##### Oct 02, 2011, 07:28 am
@AWOL
Quote

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.

#### retrolefty

#11
##### Oct 02, 2011, 07:33 am
Quote
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

#### zoomkat

#12
##### Oct 02, 2011, 09:15 am

Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

#### AWOL

#13
##### Oct 02, 2011, 06:57 pm
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### retrolefty

#14
##### Oct 02, 2011, 07:14 pm
Quote
Or maybe you simply misunderstood the wise men.

That's my guess, wise men often speak in strange tongues.

Go Up