convert your PWM RX to PPM

Post Reply
doughboy
Posts: 252
Joined: Tue Sep 04, 2012 7:20 am

convert your PWM RX to PPM

Post by doughboy »

I needed to use all 8 channels on my Turnigy RX but my 328 based FC can accept only 6 channels, so I added a circuit to convert the 8 channel PWM into a Serial Sum PPM signal.

The circuit consists only of an atMega328 chip with a simple 20 line program. Chip is running using internal oscillator at 8mhz. I added the FTDI header so I can reprogram if needed and also serves as servo connector to FC PPM input. You can buy atmega328 on ebay for $3.33.
Image

The atmega328 chip/prototype board attaches to the side of the RX
Image

This is the logic analyzer capture for 4 channels. I used a pulse width of 300us (this screen shot still shows 200us)
Image

Works great. Now I have 5 extra pins on my FC.

This is the program. 20 lines long not counting comments. Compiles to 566 bytes.

Code: Select all

#define PPMHI PORTD |= _BV(7);
#define PPMLO PORTD &= ~_BV(7);
//written by doughboy @RCGroups and @multiwii.com
//8 channel Serial Sum PPM using atmega328 minimal circuit running on 8mhz internal oscillator
//wiring
//connect RX pins 0-7 to atmega PortB pins 0-7, chip pins 14 to 19, 10 and 11
//ppm output on digital pin 7, atmega chip pin 13
//                  +-\/-+
//            PC6  1|    |28  PC5       
//            PD0  2|    |27  PC4       
//            PD1  3|    |26  PC3       
//            PD2  4|    |25  PC2       
//            PD3  5|    |24  PC1       
//            PD4  6|    |23  PC0       
//      +5v-> VCC  7|    |22  GND  <-Gnd
//      Gnd-> GND  8|    |21  AREF
//      RX6-> PB6  9|    |20  AVCC <-+5v
//      RX7-> PB7 10|    |19  PB5 <-RX5
//            PD5 11|    |18  PB4 <-RX4
//            PD6 12|    |17  PB3 <-RX3     
//   PPMSUM<- PD7 13|    |16  PB2 <-RX2     
//      RX0-> PB0 14|    |15  PB1 <-RX1 
//                  +----+
//
//this code will run only on minimal 8mhz atmega328.
//you must modify the code and move PB6 and PB7 to another port if you use a setup with oscillator

void setup() {
  PCICR |= (1<<PCIE0); //Pin Change Interrupt 0
  PCMSK0 = 0xFF; //all 8 pins
  TCCR2A = 0;
  TCCR2B |= (1<<CS22) | (1<<CS20); //div 128 prescaler
  TIMSK2 |=(1<<OCIE2B);
  DDRD |= _BV(7);  //digital pin 7 output
}

void loop() {
}

ISR(PCINT0_vect) {
    PPMHI;
    TCNT2=0;
    OCR2B=19; //300us PPM pulse
}

ISR(TIMER2_COMPB_vect) {
  PPMLO;
}



User avatar
Hamburger
Posts: 2559
Joined: Tue Mar 01, 2011 2:14 pm
Location: air
Contact:

Re: convert your PWM RX to PPM

Post by Hamburger »

Howard posted an easier rig consisting of only a couple of diodes&transistor. Not sure whether that works with all rx but looked easy enough to try it out.

User avatar
NikTheGreek
Posts: 348
Joined: Thu Dec 08, 2011 4:17 pm
Location: Greece
Contact:

Re: convert your PWM RX to PPM

Post by NikTheGreek »

Great job doughboy ;)
I'm very interested to use your PPM sum module.
I wonder if there is any way to use your code and I2C nav in the same Atmega328 chip.
It will save us space - weight ... and ofcourse money.
Thank you for sharing that with us.

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: convert your PWM RX to PPM

Post by copterrichie »

I built and used the diodes&transistor version, works great however I believe this is a much better solution because the features can be expanded. Great job!

doughboy
Posts: 252
Joined: Tue Sep 04, 2012 7:20 am

Re: convert your PWM RX to PPM

Post by doughboy »

As long as the RX sends out PWM one right after another, then this will work. Even if the RX sends out PWM signal simultaneously, all that is needed here is a program change, as the program can detect all 8 pwm pulses and can serialize them to ppm. That was actually my first program, then I realized the program can be greatly simplified (down to 20 lines of code) to take advantage of the fact that the pwm pulses are actually lined up one after another.

do you have a link to the diode/transistor rig? is it 2 diodes and 1 transistor for all 8 channels or for each channel?

the idea for using atmega328 is to eventually merge this with the gps serial to I2C and ultrasonic sensor to I2c code. However, I would make it one of the standard GPS I2C protocol and the SRF08 that supports up to 15 sensors, rather than using tinygps protocol. I would have done it this way (emulate an existing protocol) rather than invent a new one like the tinygps. This way, the device is automatically supported and saves the effort of adding code in multiwii firmware and gui to support it like how it was done for tinygps. But since tinygps is now an existing protocol, it is certainly possible to port it to atmega328. But I think that only supports 1 ultrasonic sensor, so perhaps an option to support more can be added and use the I2C protocol for SRF08.

come to think of it, there are 11 pins left (besides rx tx and reset), and I don't think anyone would want to add more than 5 ultrasonic sensor (front, left, right, rear and bottom), if the same trigger pin is used for all sensors, then only 6 pins are needed to supprot 5 sensors.
Last edited by doughboy on Sat Nov 03, 2012 8:36 pm, edited 2 times in total.

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: convert your PWM RX to PPM

Post by copterrichie »

Question, does the present code detect the lost of signal and past this on to the MWC to active FAILSAFE?

doughboy
Posts: 252
Joined: Tue Sep 04, 2012 7:20 am

Re: convert your PWM RX to PPM

Post by doughboy »

copterrichie wrote:Question, does the present code detect the lost of signal and past this on to the MWC to active FAILSAFE?


how does it work for an actual ppm RX? does it skip the ppm pulse for the missing channel? if it is all or none (which I suspect it is), then this will work the same way. like if I turn off the transmitter, I get the "find me" alert pulses.

I suppose for an actual ppm receiver, there is no risk of one of the individual channel wires getting disconnected.

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: convert your PWM RX to PPM

Post by copterrichie »

It would be easily tested as stated by turning the transmitter off and see if the MWC FAILSAFE active. I am asking because I have one of the Turnigy RX and lots of 328s. :)

doughboy
Posts: 252
Joined: Tue Sep 04, 2012 7:20 am

Re: convert your PWM RX to PPM

Post by doughboy »

yes, failsafe kicks in if you turn off the transmitter.
if I am not mistaken, the "find me" alert works similar to failsafe (in terms of detecting the presence of RX signal), except it works when quad is not armed while failsafe works while quad is armed.

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: convert your PWM RX to PPM

Post by copterrichie »

Great, going to build one using the junk 168 that I have laying around collecting dust. if it works, I will replace it with the 328.

Thank you and will report back. :)

doughboy
Posts: 252
Joined: Tue Sep 04, 2012 7:20 am

Re: convert your PWM RX to PPM

Post by doughboy »

the only possible difficulty I can think of for anyone trying this is the setting up of the cpu to run at 8mhz, meaning, burning the 8mhz bootloader which sets the fuses to the correct value.
I used the atmega "breadboard" profile but modified the uploader to use usbasp and I uploaded the bootloader by connecting the chip on to an UNO board and program via ICSP pins using my usbasp programmer. Once the bootloader is programmed, I just connect rx,tx and reset and power to upload sketches via usb to serial ftdi connection. I could not upload the bootloader to the cpu following the 8mhz breadboard article. I suppose if you buy a brand new atmega328 chip, it will be set for 8mhz, but it will not have any bootloader, and the breadboard arduino article for uploading bootloader will work.

User avatar
Hamburger
Posts: 2559
Joined: Tue Mar 01, 2011 2:14 pm
Location: air
Contact:

Re: convert your PWM RX to PPM

Post by Hamburger »

Please search for simple ppm sum in posts by howardhb.
Multiple diodes one transistor.

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: convert your PWM RX to PPM

Post by copterrichie »

Minimal Circuit (Eliminating the External Clock)

You'll need to install support for an additional hardware configuration:

Download this hardware configuration archive: http://arduino.cc/en/uploads/Tutorial/breadboard.zip
Create a "hardware" sub-folder in your Arduino sketchbook folder (whose location you can find in the Arduino preferences dialog). If you've previously installed support for additional hardware configuration, you may already have a "hardware" folder in your sketchbook.
Move the "breadboard" folder from the zip archive to the "hardware" sub-folder of your Arduino sketchbook.
Restart the Arduino software.
You should see "ATmega328 on a breadboard (8 MHz internal clock)" in the Tools > Board menu.

http://www.arduino.cc/en/Tutorial/ArduinoToBreadboard

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: convert your PWM RX to PPM

Post by copterrichie »

I will use one of the 328s

doughboy
Posts: 252
Joined: Tue Sep 04, 2012 7:20 am

convert your PWM RX to PPM

Post by doughboy »

Hamburger wrote:Please search for simple ppm sum in posts by howardhb.
Multiple diodes one transistor.


I found it and it is not two diodes and a transistor lol. I think It won't work if the pwm is perfectly aligned or overlaps since all it does is simply OR the pwm signals. It is counting on pwm signals having a small gap in between. A possible improvement is if the ORed input is fed to a 555 circuit that outputs a 300us pulse on each rising edge. But still a problem is pwn overlaps. But then, that will make the circuit more complicated. Anyone who understands the technicalities of this can appreciate this is a better and simpler solution and I would say more reliable.

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: convert your PWM RX to PPM

Post by copterrichie »

Question Dougboy, what would be the harm in using the Crystal or resonator and running running the 328 at 16 mhz verse 8 mhz?

doughboy
Posts: 252
Joined: Tue Sep 04, 2012 7:20 am

Re: convert your PWM RX to PPM

Post by doughboy »

copterrichie wrote:Question Dougboy, what would be the harm in using the Crystal or resonator and running running the 328 at 16 mhz verse 8 mhz?


As I mentioned in the comment, the program as is will not work on circuit with oscillator. the program needs to be modified to work with cpu with oscillator. This is because without the oscillator, the two oscillator pins are used as pins PB6 and PB7 and I use port B so I can use all 8 pins and just use one interrupt register. I'll see if I can create a program for cpu with oscillator. its essentially the same, but using two pin change interrupt registers instead of one. I suppose for simplicity, one get just get a promini for like $10 and save the trouble of setting up the cpu bootloader, etc. I think some promini provides two extra port A pins (present only in SMD atmega I think) to compensate for the loss of the two port b pins to the oscillator.

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: convert your PWM RX to PPM

Post by copterrichie »

Thank you Doughboy, I thought I read that but overlooked it when I went back looking for that. Lets just keep it simple, I will use the 8 mhz too.

tj_style
Posts: 12
Joined: Mon Aug 27, 2012 5:31 pm

Re: convert your PWM RX to PPM

Post by tj_style »

How about with only 6ch receiver?
Need modify the sketch to only using pb0 to pb6? or just leave ch6 & ch7 unconnected?

doughboy
Posts: 252
Joined: Tue Sep 04, 2012 7:20 am

Re: convert your PWM RX to PPM

Post by doughboy »

tj_style wrote:How about with only 6ch receiver?
Need modify the sketch to only using pb0 to pb6? or just leave ch6 & ch7 unconnected?


no need to change the program, just leave 6 and 7 unconnected and it will work as it should.
if you look at my logic analyzer capture, I only hooked up the 4 stick channels and the output is the 4 ppm pulse plus the end pulse.

doughboy
Posts: 252
Joined: Tue Sep 04, 2012 7:20 am

Re: convert your PWM RX to PPM

Post by doughboy »

copterrichie wrote:Thank you Doughboy, I thought I read that but overlooked it when I went back looking for that. Lets just keep it simple, I will use the 8 mhz too.


its not a big change.
please try this. the wiring is in the comments

Code: Select all

#define PPMHI PORTD |= _BV(7);
#define PPMLO PORTD &= ~_BV(7);
//written by doughboy @RCGroups and @multiwii.com
//8 channel PPM sum using atmega328 @16mhz
//
//wiring
//                  +-\/-+
//            PC6  1|    |28  PC5       
//            PD0  2|    |27  PC4       
//            PD1  3|    |26  PC3       
//            PD2  4|    |25  PC2       
//            PD3  5|    |24  PC1       
//            PD4  6|    |23  PC0       
//      +5v-> VCC  7|    |22  GND  <-Gnd
//      Gnd-> GND  8|    |21  AREF
//     XTAL-> PB6  9|    |20  AVCC <-+5v
//     XTAL-> PB7 10|    |19  PB5 <-RX5
//      RX6-> PD5 11|    |18  PB4 <-RX4
//      RX7-> PD6 12|    |17  PB3 <-RX3     
//   PPMSUM<- PD7 13|    |16  PB2 <-RX2     
//      RX0-> PB0 14|    |15  PB1 <-RX1 
//                  +----+
//

void setup() {
  PCICR |= (1<<PCIE0) | (1<<PCIE2); //Pin Change Interrupt 0 and 2
  PCMSK0 = 0x3F; //interrupt on PB0-PB5
  PCMSK2 |= (1<<PCINT21) | (1<<PCINT22); //PD5 - PD6
  TCCR2A = 0;
  TCCR2B |= (1<<CS22) | (1<<CS20); //div 128 prescaler
  TIMSK2 |=(1<<OCIE2B);
  DDRD |= _BV(7);  //digital pin 7 output
}

void loop() {
}

ISR(PCINT0_vect) {
  pwm_in();
}

ISR(PCINT2_vect) {
  pwm_in();
}

ISR(TIMER2_COMPB_vect) {
  PPMLO;
}

void pwm_in() {
    PPMHI;
    TCNT2=0;
    OCR2B=38; //300us PPM pulse 
}

doughboy
Posts: 252
Joined: Tue Sep 04, 2012 7:20 am

Re: convert your PWM RX to PPM

Post by doughboy »

This is one of the references I used as basis on how I generated the output. (channel 8 is present but pulse is not shown in diagram)
I also based the use of 300us ppm pulse width based on how open9x (and probably er9x) do the ppm protocol. the default ppm pulse width they use is 300us. It's basically a 300us pulse on each rising edge of a pwm pulse, plus a pulse on the falling edge of the last pwm channel.

Image

you can see why using diodes and transistor really won't work, as the ORed signal will just give you a constant high output for all 8 channels.

this is the output I get on my circuit, you can see how it compares to a receiver that has ppm output. If you examine the time line, you can see the frame size of the Turnigy receiver is not quite 20ms, but close enough. you can see the 4 pwm pulses, first is throttle at min, so it is 1000us (or 1ms), then the yaw, pitch and roll at center so 1500us (1.5ms) each for the next 3 pulses.
Image

User avatar
Hamburger
Posts: 2559
Joined: Tue Mar 01, 2011 2:14 pm
Location: air
Contact:

Re: convert your PWM RX to PPM

Post by Hamburger »

doughboy wrote:
Hamburger wrote:Please search for simple ppm sum in posts by howardhb.
Multiple diodes one transistor.


I found it and it is not two diodes and a transistor lol. I think It won't work if the pwm is perfectly aligned or overlaps since all it does is simply OR the pwm signals. It is counting on pwm signals having a small gap in between. A possible improvement is if the ORed input is fed to a 555 circuit that outputs a 300us pulse on each rising edge. But still a problem is pwn overlaps. But then, that will make the circuit more complicated. Anyone who understands the technicalities of this can appreciate this is a better and simpler solution and I would say more reliable.


I do not understand about _two_ diodes, who brought that up? What is your lol-ing all about?
I take it from Howard's post that simple solution works pretty well for some people. But you already seem to know all about it, so good luck.

doughboy
Posts: 252
Joined: Tue Sep 04, 2012 7:20 am

convert your PWM RX to PPM

Post by doughboy »

Hamburger wrote:Howard posted an easier rig consisting of only a couple of diodes&transistor. Not sure whether that works with all rx but looked easy enough to try it out.


There you said a "only a couple of diodes". :)
That was quite a stretch from the truth.

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: convert your PWM RX to PPM

Post by copterrichie »

P1110995.JPG


Hey Guys, I built one using diodes, works great on the bench, have not flown it yet.

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: convert your PWM RX to PPM

Post by copterrichie »

P.S. My hopes are to be able to incorporate the SPEKTRUM protocol. :)

User avatar
Hamburger
Posts: 2559
Joined: Tue Mar 01, 2011 2:14 pm
Location: air
Contact:

Re: convert your PWM RX to PPM

Post by Hamburger »

doughboy wrote:
Hamburger wrote:Howard posted an easier rig consisting of only a couple of diodes&transistor. Not sure whether that works with all rx but looked easy enough to try it out.


There you said a "only a couple of diodes". :)
That was quite a stretch from the truth.

http://www.thefreedictionary.com/a+couple+of

doughboy
Posts: 252
Joined: Tue Sep 04, 2012 7:20 am

convert your PWM RX to PPM

Post by doughboy »

Sorry but I know you we're exaggerating to point out my solution is inferior to another solution and I had to set the record straight since you we're giving misleading information with absolutely no technical basis but hearsay on a subject you seem to know very little about. I don't claim to know everything, but I would not insist on something if I am wrong.

I'm not a hard person to convince, but you need to give me more than just saying "works great for some people".

I keep an objective view on things but you need to be able to support and prove your claims if you want to make a challenge.

User avatar
Hamburger
Posts: 2559
Joined: Tue Mar 01, 2011 2:14 pm
Location: air
Contact:

Re: convert your PWM RX to PPM

Post by Hamburger »

You want to keep riding your high horse, fine. Just spare me your guesswork you are not good at it. You know nothing about me and it shall remain that way. I have nothing more to say to you.
End Of Talk.

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: convert your PWM RX to PPM

Post by copterrichie »

When I test compile the above code selecting hardware: =ATmega328 on a breadboard (8 MHz internal clock), I am receiving the following error.

However, if I compile the same code using ATMega328 16mhz, there are no errors. What am I doing wrong here or do I have the wrong hardware file?

ERROR:
C:\Program Files\arduino-1.0.1\hardware\arduino\cores\arduino/Arduino.h:213:26: error: pins_arduino.h: No such file or directory



Thank you.

Never mind, found the solution: http://arduino.cc/forum/index.php/topic,91890.0.html

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: convert your PWM RX to PPM

Post by copterrichie »

Here is a picture of my PWM RX to PPM Build. Now I need to fully ground test it.

I added two more pins in addition to the PPM pin for future expansion.

Image

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: convert your PWM RX to PPM

Post by copterrichie »

I have this working however, I am unable to select the right PPM order. Throttle and Pitch are switched otherwise, it is working great.

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: convert your PWM RX to PPM

Post by copterrichie »

Figured it out but I had to apply this patch to the RX code and use the the following order. viewtopic.php?f=13&t=2752&p=27578&hilit=PPM+order#p27568

Code: Select all

#define SERIAL_SUM_PPM         ROLL,THROTTLE,PITCH,YAW,AUX1,AUX2,AUX3,AUX4

User avatar
NikTheGreek
Posts: 348
Joined: Thu Dec 08, 2011 4:17 pm
Location: Greece
Contact:

Re: convert your PWM RX to PPM

Post by NikTheGreek »

I want to upload the code in a brand new 328p mc and us it in 8mz(no crystal)
Do i have to burn the bootloader first ?
and later to upload the code?

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: convert your PWM RX to PPM

Post by copterrichie »

NikTheGreek wrote:I want to upload the code in a brand new 328p mc and us it in 8mz(no crystal)
Do i have to burn the bootloader first ?
and later to upload the code?


Yea, you will need to get the breadboard for the IDE mentioned here: viewtopic.php?f=13&t=2710&start=10#p26273

User avatar
NikTheGreek
Posts: 348
Joined: Thu Dec 08, 2011 4:17 pm
Location: Greece
Contact:

Re: convert your PWM RX to PPM

Post by NikTheGreek »

I've done that..
My ide now displays the new"breadboard 8mz board" .
So now i'll copy-paste the code and upload?

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: convert your PWM RX to PPM

Post by copterrichie »

providing you have burned the new bootloader. Yes.

User avatar
NikTheGreek
Posts: 348
Joined: Thu Dec 08, 2011 4:17 pm
Location: Greece
Contact:

Re: convert your PWM RX to PPM

Post by NikTheGreek »

Yes...i know , its a mesh :D


Image

Image

Image

User avatar
NikTheGreek
Posts: 348
Joined: Thu Dec 08, 2011 4:17 pm
Location: Greece
Contact:

Re: convert your PWM RX to PPM

Post by NikTheGreek »

Ok..and now ? what do i have to do in order to activate ppm sum ?

I only must enable one of the following options?
And which pin must i use ?
D2 ?

Thank you in advance

Code: Select all

/****************************    PPM Sum Reciver    ***********************************/
      /* The following lines apply only for specific receiver with only one PPM sum signal, on digital PIN 2
         Select the right line depending on your radio brand. Feel free to modify the order in your PPM order is different */
      //#define SERIAL_SUM_PPM         PITCH,YAW,THROTTLE,ROLL,AUX1,AUX2,AUX3,AUX4,8,9,10,11 //For Graupner/Spektrum
      //#define SERIAL_SUM_PPM         ROLL,PITCH,THROTTLE,YAW,AUX1,AUX2,AUX3,AUX4,8,9,10,11 //For Robe/Hitec/Futaba
      //#define SERIAL_SUM_PPM         ROLL,PITCH,YAW,THROTTLE,AUX1,AUX2,AUX3,AUX4,8,9,10,11 //For Multiplex
      //#define SERIAL_SUM_PPM         PITCH,ROLL,THROTTLE,YAW,AUX1,AUX2,AUX3,AUX4,8,9,10,11 //For some Hitec/Sanwa/Others

Post Reply