Page 1 of 2 12 LastLast
Results 1 to 10 of 11
  1. #1
    10+ Posting Member
    Join Date
    Oct 2014
    Location
    Canada
    Posts
    19
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Link2fs and Flap Servo

    Hi Jim,

    I have the following code, My Trim Gauge works perfectly, its a voltage gauge on pin 15. I combined the code from your example as best as I can, but my servo doesn't move at all, do you see an issue? Thanks in advance

    Servo is on pin1 of a 16 Channel 9685 PWM Card.

    don't know why the include Adafruit_PWMServoDriver.h doesn't show up here but that is the empty include.

    Code:
    // ***********************BEGIN DECLARATIONS******************************************** #include <Wire.h> #include #include <Servo.h> Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40); //Base address for PCA9685 uint8_t trimgauge = 15; // servo (or analog device) is connected at position 0 on the PCA9685. uint8_t flapsi = 1; int trimFSX; int old_trimFSX; int trimPWM; int old_flapsi; Servo flapsServo; String data; int const a=(100); //(replace 1 with your number for a) int const b=(2000); //(replace 1 with your number for b) int CodeIn; String flaps; //********************************************************************** // ***********************BEGIN VOID SETUP******************************* void setup() { Wire.begin(); //Join the bus as master Serial.begin(115200); trimFSX=0; //Just to put an initial number for starters flapsi=0; pinMode (1, OUTPUT); flapsServo.attach(1); // --------Increase PWM refresh rate to 240Hz // --------------------------------------- pwm.begin(); //Starts communication with PCA9685 pwm.setPWMFreq(240); } //end void setup //**************************************************************************** // **************BEGIN VOID LOOP******************************************************** void loop() { old_trimFSX=trimFSX; old_flapsi=flapsi; //**** Check for data from L2FSX **** if (Serial.available() > 0) { //if there is a charactor in the serial receive buffer then ,,,, CodeIn = getChar();// read it via the "char getChar" routine if (CodeIn == '<') { LESSTHAN(); }// The first identifier is "<" goto LESSTHAN void }//end of serial.available if (trimFSX!=old_trimFSX) { trimPWM=(a*(trimFSX) +b); pwm.setPWM(trimgauge, 0, trimPWM); old_trimFSX=trimFSX; }//end of if if (flapsi!=old_flapsi) { flapsi = (flapsi + 10);// Manual correction to suit servo zero. flapsServo.write(flapsi * 3); // Manual fiddle to get full span of servo. old_flapsi=flapsi; }//end of if }//end void loop //--------------- Subroutine getChar ------------------------------ char getChar()// The serial buffer routine to get a character { while(Serial.available() == 0);// wait for data return((char)Serial.read()); }// end of getchar Routine. //--------------- Subroutine LESSTHAN ------------------------------ void LESSTHAN() {//the first identifier was a '<' CodeIn = getChar();//get another charactor from serial port if (CodeIn == 'H'){//found the identifier "H" delay (11); // It seems to need a delay here data = ""; // Empty data string data += char(Serial.read()); //Read the first data charactor sent data += char(Serial.read()); //Read the second data charactor sent and add it to the first data += char(Serial.read()); //Read the third data charactor sent and add it to the other two.) trimFSX = data.toInt(); } //end of "found the identifier "H" if (CodeIn == 'G'){//found the identifier "G" delay (11); // It seems to need a delay here flaps = ""; flaps += char(Serial.read()); flaps += char(Serial.read()); flaps += char(Serial.read()); flapsi = flaps.toInt(); // convert it to an integer (Thanks Phill) // Note.. Using the "Map" method slows it down. //Serial.print(flapsi); // Used for testing } //end of "found the identifier "G" } // end of LESSTHAN loop // ************** END *********************************************************

  2. #2
    75+ Posting Member



    Join Date
    Sep 2013
    Location
    California, USA
    Posts
    109
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Link2fs and Flap Servo

    Hi T,
    This is a good first try, but there are several problems, mostly with the servo definitions. Your sketch defines 2 servos, a servo named “flapsi” at location 1 of the 9685, and a second servo named “flapsServo” on pin 1 of the Arduino board. I don’t think this is what you intended to do.
    Assuming the servo is located on the 9685, there isn’t any line in your code to send the flaps position to the servo. Here is your code to drive the trim gauge:
    pwm.setPWM(trimgauge, 0, trimPWM);
    but there is no similar statement for flapsi:
    pwm.setPWM(flapsi, 0, flapsPWM);
    so you are not sending any instructions to the servo on the 9685.

    Additionally, although you define “flapsi” as an unsigned integer constant in your declarations (uint8_t flapsi = 1, you then go on to use flapsi as a regular variable in the sketch. Once the variable flapsi is used to define the servo location in the declarations, it should not appear again in the sketch except for the pwm.setPWM command.

    Get rid of the “Servo flapsServo;” and the “flapsServo.attach” commands, these are only used to define servos connected directly to the Arduino.
    Regards, Steve

  3. #3
    10+ Posting Member
    Join Date
    Oct 2014
    Location
    Canada
    Posts
    19
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Link2fs and Flap Servo

    Thanks Steve, I will give that a try, I guess I was concerned because the Flaps is actually a servo, where the Trim, is a voltage instrument. I didn't know if I should be controlling it quite the same. In any case, I will make the change to the card tonight and report back.

    Thanks a bunch.

  4. #4
    75+ Posting Member



    Join Date
    Sep 2013
    Location
    California, USA
    Posts
    109
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Link2fs and Flap Servo

    T,
    You are correct that voltage instruments behave differently than servos. They are both driven the same way, but the PWM ratio is calculated differently. Here's the difference:

    Voltage instruments
    respond to the average voltage on the signal line, ie, if the PWM signal is positive for 50% of the time and the V++ voltage is 5 volts, the voltage instrument will act as though you are sending it 2.5 volts.

    Servos respond to the duration of the positive signal, ie, if the signal is positive for 1ms it will always respond to the same position which corresponds to 1ms, regardles if that 1ms represents 10% or 90% of the PWM cycle.

    Hope that helps, Steve

  5. #5
    10+ Posting Member
    Join Date
    Oct 2014
    Location
    Canada
    Posts
    19
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Link2fs and Flap Servo

    Thanks, It does Help. Also so did your suggestions kind of. I have completed what you suggested, and what I am getting is this.

    When the servo is commanded to move it moves full deflection one way and never returns.

    I have tried dividing the flapsFSX Signal, but doesn't seem to matter except it changes where the Servo begins to move.

    Therefore The servo just goes one way all or nothing, and won;t return to Zero. I have to remove the power, bring the servo back and try again.

    I even tried a similar declaration for c & d to try to add numbers into the equation like I did with the Trim, but alas it didn't work.

    Least I am trying LOL.


    Code:
    // ***********************BEGIN DECLARATIONS********************************************
    #include 
    #include 
    // #include 
    Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40);  //Base address for PCA9685
    uint8_t trimgauge = 15;          // servo (or analog device) is connected at position 0 on the PCA9685.
    uint8_t flapgauge = 1;
    
    int trimFSX;
    int old_trimFSX;
    int trimPWM;
    int flapFSX;
    int old_flapFSX;
    int flapPWM;
    String data;
    String flaps;
    
    int const a=(100); //(replace 1 with your number for a)
    int const b=(2000); //(replace 1 with your number for b)
    
    int CodeIn;
    
    //**********************************************************************
    // ***********************BEGIN VOID SETUP*******************************
    void setup()
    {
    
    Wire.begin();               //Join the bus as master
    Serial.begin(115200);
    trimFSX=0; //Just to put an initial number for starters
    flapFSX=0;
    
    // --------Increase PWM refresh rate to 240Hz
    // ---------------------------------------
    pwm.begin();           //Starts communication with PCA9685
    pwm.setPWMFreq(240);
    }
    //end void setup
    //****************************************************************************
    // **************BEGIN VOID LOOP********************************************************
    void loop()
    {
    old_trimFSX=trimFSX;
    old_flapFSX=flapFSX;
    
    
     //**** Check for data from L2FSX ****
     if (Serial.available() > 0) {  //if there is a charactor in the serial receive buffer then ,,,,
       CodeIn = getChar();// read it via the "char getChar" routine
       if (CodeIn == '<') {
         LESSTHAN();   }// The first identifier is "<" goto LESSTHAN void
     }//end of serial.available
    if (trimFSX!=old_trimFSX) {
      trimPWM=(a*(trimFSX) +b);
      pwm.setPWM(trimgauge, 0, trimPWM);
      old_trimFSX=trimFSX;
    }//end of if
    if (flapFSX!=old_flapFSX) {
          flapPWM=(flapFSX/40);
      pwm.setPWM(flapgauge, 0, flapPWM);
    old_flapFSX=flapFSX;
    }//end of if
    }//end void loop
    //--------------- Subroutine getChar ------------------------------ 
    char getChar()// The serial buffer routine to get a character 
    {
     while(Serial.available() == 0);// wait for data
     return((char)Serial.read());
    }// end of getchar Routine. 
    
    //--------------- Subroutine LESSTHAN ------------------------------ 
    void LESSTHAN() {//the first identifier was a '<'
     CodeIn = getChar();//get another charactor from serial port
     if (CodeIn == 'H'){//found the identifier "H"
       delay (11);    // It seems to need a delay here
       data = ""; // Empty data string
       data += char(Serial.read());    //Read the first data charactor sent
       data += char(Serial.read());   //Read the second data charactor sent and add it to the first
       data += char(Serial.read());   //Read the third data charactor sent and add it to the other two.)   
       trimFSX = data.toInt();
     } //end of "found the identifier "H"
    if (CodeIn == 'G'){//found the identifier "G"
       delay (11);    // It seems to need a delay here
    data = ""; // Empty data string
       data += char(Serial.read());    //Read the first data charactor sent
       data += char(Serial.read());   //Read the second data charactor sent and add it to the first
       data += char(Serial.read());   //Read the third data charactor sent and add it to the other two.)   
          flapFSX = data.toInt(); // convert it to an integer (Thanks Phill)
                // Note.. Using the "Map" method slows it down.
          //Serial.print(flapsi); // Used for testing
             
     } //end of "found the identifier "G"
    }  // end of LESSTHAN loop 
    
    // ************** END *********************************************************

  6. #6
    75+ Posting Member



    Join Date
    Sep 2013
    Location
    California, USA
    Posts
    109
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Link2fs and Flap Servo

    You need to scale your output so that the pulse duration is within the servo’s operating limits. Servos generally respond over the range of .7 to 2.1 ms. Anything higher or lower than these values will send the servo to the end of its travel, which is what is happening to you. It’s also possible to damage the servo by driving it hard against either end stop.
    If you are running the 9685 at 240Hz, each PWM cycle is 1/240 second (=4.17ms).
    A 12bit resolution yields 4096 units, so each output PWM unit represents 4.17ms/4096 = 1.02µs.
    .7ms will need a PWM output of 686; 2.1ms will need a PWM output of 2058, so you generally will need to restrain your PWM to these limits. The 686 and 2058 are only approximate outputs, in practice you will most likely need to modify this range because a) not all servos have the same stop points, and b) the device you are driving (gauge display) will not be able to utilize the full range of servo motion available.
    The best way to determine what your actual limits are is to start with a fixed output in the middle of the range, say PWM=1400 and then slowly increase or decrease the PWM signal until you get to 0% or 100% of gauge scale. You now know what the minimum and maximum PWM range is for your specific gauge. Now, all you need to do is to scale the fsx output to this range and your gauge will work.

  7. #7
    10+ Posting Member
    Join Date
    Oct 2014
    Location
    Canada
    Posts
    19
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Link2fs and Flap Servo

    Thanks for the help Steve, I will try to play with the math then.

    So Lets Say I take some arbitrary middle numbes of 800 for the low and 2000 for the high,

    set the flap to output % 0 to 100

    a=120
    b=800
    flapPWM=((a*(flapFSX)/10) +b)

    this should give me a PWM of 800 for flap 0%, 1400 for 40% Flaps and 2000 for 100% Flap

    would this then be proper?

  8. #8
    75+ Posting Member



    Join Date
    Sep 2013
    Location
    California, USA
    Posts
    109
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Link2fs and Flap Servo

    You've got the general idea correct. The FSX ofset for flaps is 0BDC and its definition is:
    0BDC 4 Flaps control, 0=up, 16383=full. The “notches” for different
    aircraft are spaced equally across this range: calculate the
    increment by 16383/(number of positions-1), ignoring fractions.


    so in your example the flapPWM should be 800 when flapFSX is 0, and 2000 when flapFSX is 16383.

  9. #9
    10+ Posting Member
    Join Date
    Oct 2014
    Location
    Canada
    Posts
    19
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Link2fs and Flap Servo

    Quote Originally Posted by SteveL View Post
    You've got the general idea correct. The FSX ofset for flaps is 0BDC and its definition is:
    0BDC 4 Flaps control, 0=up, 16383=full. The “notches” for different
    aircraft are spaced equally across this range: calculate the
    increment by 16383/(number of positions-1), ignoring fractions.


    so in your example the flapPWM should be 800 when flapFSX is 0, and 2000 when flapFSX is 16383.

    Hi Steve. I was referring to the flap return setting from Link2fs. If it is set to percent I get a number from 0 to 100

    if i do not have it in percent I get 0 to 57

    The FSUIPC offset has nothing to do with this Arduino or program though right? So the 16383 number is Moot? Or am I missing something else.

    i have been basing all my math on the numbers I get in link2fs. Not the offsets

  10. #10
    10+ Posting Member
    Join Date
    Oct 2014
    Location
    Canada
    Posts
    19
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Link2fs and Flap Servo

    It works, but it is backwards.. any way to reverse the servo direction?

Page 1 of 2 12 LastLast