sodnpoo.com

reading multiple rc pwm channels with arduino

I wanted to be able to use my Futaba RC transmitter/receiver as a manual override system for various arduino projects. After some googling I couldn't find any code to read more than 3 channels, some implementations using pulseIn() but suffering because of the inherent time-out. I also found Reading servo PWM with an Arduino which fed the PWM from the RC receiver into one of the interrupt pins. This looked like the right way to do it although this means that each channel needs it's own pin, limiting it's use to two channels on a 328 based arduino. It looks like you can get all channels on one line if you modify the receiver to gain access to the PPM line but I wanted to avoid that if possible.

The code uses the timer interrupt available in the playground. I'm using Timer3 as it's written for the Mega1280 which is what I use for development. I believe but I haven't tested yet, that Timer1 should be pretty much a drop in replacement with only the #include and the initialize() and attachInterrupt() lines in setup() needing some minor changes.

The amount of code executed in callback() will affect how small of an interval you can use on the timer - too small and the arduino spends most of it's time servicing the interrupt slowing down the code in loop() - but the larger you go the worse the pulse width resolution is. To minimise the possible useful interval value I've attempted to optimise callback() including using direct PIN I/O using rawDigitalRead() instead of the standard digitalRead() - this is Mega1280 specific mapping and would either need to be re-written for a 328 or the slower digitalRead() used instead. As it's an 8 bit processor changing variable types from int to byte made a significant difference.

Using the simple demo program below I can read 5 channels with a interval/resolution of 40 microseconds - 4 takes 32, 3 takes 26, 2 takes 14 and 1 takes 10 microseconds. These values are the lowest while keeping a single iteration of loop() to ~50 milliseconds. This might not be enough resolution for all cases but should be enough for my uses.

#include <TimerThree.h>

const byte SERVOPIN = 3;
const byte SERVOPIN1 = 4;
const byte SERVOPIN2 = 5;
const byte SERVOPIN3 = 6;
const byte SERVOPIN4 = 7;

const byte MAXRCPWMS = 5;
const byte RCPINS[MAXRCPWMS] = {
  SERVOPIN, 
  SERVOPIN1, 
  SERVOPIN2, 
  SERVOPIN3, 
  SERVOPIN4, /**/
};

  /* < 50 millis for loop()
  1 = 10
  2 = 14
  3 = 26
  4 = 32
  5 = 40
  */
const byte RCSAMPLERATE = 40; //in microseconds

struct tRcPwm {
  unsigned long lastMicros;
  byte pin, lastState;
  int pulseWidth;
};

volatile tRcPwm rcPwm[MAXRCPWMS];

void setup(){
  Serial.begin(9600);

  for(int i=0;i<MAXRCPWMS;i++){
    rcPwm[i].pulseWidth = 0;
    rcPwm[i].lastMicros = 0;
    rcPwm[i].lastState = 0;
    rcPwm[i].pin = RCPINS[i];
  }
  
  pinMode(10, OUTPUT);
  Timer3.initialize( RCSAMPLERATE );
  Timer3.attachInterrupt(callback);    
}

void loop(){
  Serial.print(millis());

  for(int i=0;i<MAXRCPWMS;i++){
    Serial.print(" ");
    Serial.print(i);
    Serial.print(": ");
    Serial.print(rcPwm[i].pulseWidth);
  }
  
  Serial.println();
}

byte rawDigitalRead(byte pinnum){
  switch(pinnum){
    case 3:
      return !((PINE & (1<<5))==0); // 3
    case 4:
      return !((PING & (1<<5))==0); // 4
    case 5:
      return !((PINE & (1<<3))==0); // 5
    case 6:
      return !((PINH & (1<<3))==0); // 6
    case 7:
      return !((PINH & (1<<4))==0); // 7
  }
}

void callback(){
  for(byte i=0;i<MAXRCPWMS;i++){
    //byte state = digitalRead(rcPwm[i].pin);
    byte state = rawDigitalRead(rcPwm[i].pin);
    
    if( rcPwm[i].lastState != state ){
      unsigned long now = micros();
      if( state==HIGH ){ //rising
        rcPwm[i].lastMicros = now;
      }else{ //falling
        rcPwm[i].pulseWidth = now - rcPwm[i].lastMicros;
      }
      rcPwm[i].lastState = state;
    }
  }
}
  

Latest code available on github.