I sure hope someone can help with this situation. I have written a mixing code for mixing two channels of a PPM signal from my Futaba RC Transmitter using ServoDecode and ServoTimer2 libraries. The mixing works fine but now I want to add some features to it. I would like to be able to adjust the servo throw so that I can increase/decrease the servo throw around the center point as well as being able to set the servo throw endpoints.
I am open to ideas since my idea bank is exhausted at this point.
Code:
// ServoTimerDecode7Test
// 02-22-2009
// Basic code to control a Tri Copter using
// servos instead of motors for Pitch and Roll.
#include <ServoDecode.h>
#include <ServoTimer2.h> // the servo library
#define leftPin 2 // the pin the servo is attached to
#define rightPin 3
ServoTimer2 left; // declare location variables for the servos
ServoTimer2 right;
int maxPulse = 2000;
int minPulse = 1000;
char * stateStrings[] = {
"NOT_SYNCHED", "ACQUIRING", "READY", "in Failsafe"};
void setup() // run once, when the sketch starts
{
Serial.begin(38400);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
ServoDecode.setFailsafe(3,1234); // set channel 3 failsafe pulse width
left.attach(leftPin);
right.attach(rightPin);
ServoDecode.begin();
}
void loop() // run over and over again
{
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"); // if you see this, the decoder does not have a valid signal
Serial.print(i);
Serial.print("= ");
pulsewidth = ServoDecode.GetChannelPulseWidth(i);
Serial.print(pulsewidth);
Serial.print(" ");
digitalWrite(13,HIGH);
}
Serial.println("");
}
else {
// decoder is ready, print the channel pulse widths
int chan1 = ServoDecode.GetChannelPulseWidth(1);
int chan2 = ServoDecode.GetChannelPulseWidth(2);
int roll = 1500;
int pitch = 1500;
// Different mixing configurations:
//roll= (chan2-500)+(chan1-1000) ; // 1:1 mix config 1
//pitch = (chan2+500)-(chan1-1000);
//roll = (chan2-250)+((chan1-1000)/2); // config 2
//pitch = (chan2+250)-((chan1-1000)/2);
roll = 750+((chan2-1000)/2)+(chan1-1000); //config 3 between full and low rate
pitch =1750+((chan2-1000)/2)-(chan1-1000);
//roll = (chan2/2)+(chan1/2); //config 4 low rates on both channels
//pitch =(1500+chan2/2)-(chan1/2);
Serial.print("Channel 1:");
Serial.print(chan1);
Serial.print('\t');
Serial.print("Channel 2:");
Serial.print(chan2);
Serial.println(".");
Serial.print("roll:");
Serial.print(roll);
Serial.print('\t');
Serial.print("pitch:");
Serial.print(pitch);
Serial.println(".");
left.write(pitch);
right.write(roll);
}
Serial.println("");
digitalWrite(12,LOW);
digitalWrite(13,LOW);
}