Decoding Radio Control signal pulses

does it matter if his radio is in pcm or ppm mode?

Yes, it must be in PPM mode for this library to decode the signal

Hi mem, i´ve changed my transmitter and now i got a good signal from the trainer port to pin8, now i can see the changes each time i move the channel switchs or the joysticks, but how can i drive the servos ?.....wich outputs should i use, ...the PWM ones i supose,....., could you please edit and post here the full sketch file in order for me to compare with mine ? ::slight_smile:

thanks again and sorry to take your time

Well I've got it working.... When I ran the example sketch for servoDecode it would frequently go into fail safe. When I run my own sketch everything seems to be working fine. It's being used to run 4 motor controllers and 3 servos using a servo controller. Thanks for the help mem, and everyone else.

On a side note, which PWM pins will no longer work when using ServoDecode?

ubuntucat, good to hear you have it working.

ServoDecode uses timer1, therefore analogWrite on pins 9 and 10 are not available.

I am surprised to hear that changing a sketch would make any difference to fail safe. I would want to modify the example if it could prevent anyone in future from having a similar problem, please tell me more about what you needed to change.

On a side note, which PWM pins will no longer work when using ServoDecode?

Servos don't operate with the pwm output from analogWrite, but you can use the ServoTimer2 library. This was developed specifically for use with ServoDecode.

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

Here is a simple test sketch:

// ServoDecodeTest
#include <ServoTimer2.h>
#include <ServoDecode.h>

// define the servo pins
#define servoApin  2   // the pin the servo is attached to/
ServoTimer2 servoA;    // declare variables for the servos

char * stateStrings[] = {
  "NOT_SYNCHED", "ACQUIRING", "READY", "in Failsafe"};


void setup()                    // run once, when the sketch starts
{
  Serial.begin(38400);   
  servoA.attach(servoApin); 
  ServoDecode.begin();
  ServoDecode.setFailsafe(3,1234); // set channel 3 failsafe pulse  width
}


void loop()                     // run over and over again
{
  int count=0;
  int pulsewidth;

  // print the decoder state
  if( ServoDecode.getState()!= READY_state) {
    Serial.print("The decoder is ");
    Serial.println(stateStrings[ServoDecode.getState()]);
    for ( int i =0; i <=MAX_CHANNELS; i++ ){ // print the status of the first four channels
      Serial.print("Cx");
      Serial.print(i);
      Serial.print("= ");
      pulsewidth = ServoDecode.GetChannelPulseWidth(i);
      Serial.print(pulsewidth);
      Serial.print("  ");
    }
    Serial.println("");
  }
  else {  
    if( count % 20 == 0){   // decoder is ready, print the channel pulse widths every second
      for ( int i =1; i <=MAX_CHANNELS; i++ ){ // print the status of the first four channels
        Serial.print("Ch");
        Serial.print(i);
        Serial.print("= ");
        pulsewidth = ServoDecode.GetChannelPulseWidth(i);
        Serial.print(pulsewidth);
        Serial.print("  ");
      }
      Serial.println("");
    } 
    count++;
  }
  servoA.write(ServoDecode.GetChannelPulseWidth(3));
  delay(20); // update 50 times a second        

}

Hi,
I have tried to use ServoDecode on a Duemilanove-Board and it works perfectly - thank you! However, when using the same sketch on the new Mega-Board the library says "no sync" - any insight what needs to be changed?

The chip used on the mega board has the hardware connected differently than the 168. I need to modify the library for it to work (timer1 Input Capture is not connected to an Arduino pin so I will need to use a different timer on the mega board)

If you don't see something posted in a week or so, send me a PM to remind me.

Thank you so much for extending the library! :slight_smile:

Hi guys, it is getting dificult to get this task forward, when i compile this sketch, i got some error messages as follows.

// ServoDecodeTest
#include <Servodecode.h>
#include <ServoTimer2.h> // the servo library

// define the servo pins
#define servo1Pin 2 //the pin the servo is attached to
#define servo2Pin 3

ServoTimer2 servo1; // declare variables for the servos
ServoTimer2 servo2;

void setup()
{
Serial.begin(38400);
servo1.attach(servo1Pin);
servo2.attach(servo2Pin);
ServoDecode.begin();
}

void loop()
{
int pulsewidth[MAX_CHANNELS];

if (ServoDecode.getState()!= READY_state) {
Serial.println("The decoder is not ready");
}
}
else {
// decoder is ready, do something with the channel pulse widths
if (ServoDecode.GetChannelPulseWidth(1) > 1500)

{ // run script 1 if pulse width greater than 1500 ;
int argumentA = (2250 - ServoDecode.GetChannelPulseWidth(2)) / 200; // some values proportional to stick position
boolean argumentB = ServoDecode.GetChannelPulseWidth(2) < 1500; // true if channel 2 less than 1500
runScript1(argumentA, argumentB);
}
}
delay(100); // update 10 times a second
}

void runScript1(int argumentA, boolean argumentB)

{ // do something here to control servo...
servo1.write(2000);
delay(5000); // note that all servo are pulsed by the interrupt handler with their current value every 20ms
if (argumentA)
servo2.write(2000);
delay(argumentB * 10); //delay a time determined by the transmitter stick position
servo1.write(1000);
servo2.write(1000);

}

In file included from C:\Users\ABBA\Desktop\arduino-0013-Servo Decode\hardware\cores\arduino/WProgram.h:4,

c:/users/abba/desktop/arduino-0013-servo decode/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:111: error: expected unqualified-id before 'int'

c:/users/abba/desktop/arduino-0013-servo decode/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:111: error: expected `)' before 'int'

c:/users/abba/desktop/arduino-0013-servo decode/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:111: error: expected `)' before 'int'

In file included from C:\Users\ABBA\Desktop\arduino-0013-Servo Decode\hardware\cores\arduino/WProgram.h:6,

thanks in advance.

gmv, from the look of those messages, I think you are not using the latest versions of the libraries, they were both updated to work with recent Arduino releases. Your sketch compiles ok for me (although I needed to remove an extra curly brackets } before the first else in loop)

Check that you have the latest versions of both libraries.
You can find the latest version of ServoDecode in this thread and ServoTimer2 is here:
http://www.arduino.cc/playground/uploads/Main/ServoTimer2.zip

Hi mem, many thanks...now i got it compling,..., i´ve loaded the sketch to the board and connected the PPM frame output of my Futaba to pin 8, the servo to pin 3 or 2 but i couldn´t get it moving when i move the sticks of my radio, also on my serial monitor, i got the message "the decoder is not ready ",...do you think it could have anything to do with inverted signals ?......any ideias what could be ? :-/

thanks again

Why not try it with the example sketch posted in this thread. If it still doesn't work, then you can try inverting the pulse detection as follows: replace the line in ServoDecode.cpp:
volatile byte pulseEnd = PULSE_START_ON_RISING_EDGE ; // default value
with
volatile byte pulseEnd = PULSE_START_ON_FALLING_EDGE ;

delete SevoDecode.o and then rebuild your sketch.

Hi mem, this morning i finally could make it work as expected, now i can drive all servos from my Tx !

Now my next goal is to drive a relay, just using one servo output from the Arduino Board . :slight_smile:

Any ideias are welcome.

GMV

GMV, good to hear you have it working, was it inverting the pulse or something else that got it going?

You can control a relay by using the pulse width returned from the GetChannelPulseWidth method. If the pulse is above (or below) a given threshold you can set a pin high. There is info on connecting relay here if you need it: Arduino Playground - HomePage

Hi mem, i didn´t inverted the pulses,...., only deleted old arduino´s folders and instaled the new version and created a new sketch based on the servotest sketch that you mentioned on the previous page, made some changes and voilá !

Listen, i´ll try to implement your ideia for driving the relay, but then if i needed to drive 8 relay´s from a single pwm output, do i need any extra library´s or the existing ones (servo., servodecode., servotimer2:
) are enough ?

Could you give some example of a code from where i could starting working ? :sunglasses:

thanks in advance

gmv, if you post a fragment of your sketch indicating where you want the relays to be triggered I will be happy to guide you with the code. I suggest you start a new thread for your specific application.

Ok, i´ll do it !

GMV

This is the pin description of the buddy port of my Sanwa RD6000 (Airtronics equivalen):

1 = ??
2 = Ground
3 = OUT
4 = +V switched
5 = IN

If I am correct, I have to connect pin 2 of buddy port to gnd on Arduino and pin 3 on buddy port to pin 8 of Arduino.

Can anyone confirm?

I have tried it this way and I get the "The decoder is in Failsafe" message all the time.

I have no experience with the sanwa. It is possible that the signal is inverted so it my be worth recompiling the library to use an inverted signal. A search in this thread should turn up the information on how do to it.

some keywords to try in the search are : pulseEnd PULSE_START_ON_FALLING_EDGE