Servo mixing in an RC plane for elevons

Hey everyone, I am trying to make a flying wing aircraft and sadly because of my small brain not being able to understand the servo mixing stuff, I am not able to get the elevons working like i need it to. Can anyone help me to modify the code so that the servos are reversed in the aileron control and straight in the elevator one?
Thanks in advance to everybody who has taken the time to respond to my question :grinning: :grinning:
This is theTX code

#include <SPI.h>  // nrf communication
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(7, 8);  //CE CSN
struct channels {  //Data to be sent
  int ch1 = 0;
  int ch2 = 0;
  int ch3 = 0;
};
channels ch;
const byte address[6] = "10000";  //address to send data
int x = A0;                       // potentiometer pins
int x2 = A1;
int y = A2;
int mappedx;
int mappedy;
void setup() {
  Serial.begin(9600);
  
  pinMode(x, INPUT);
  pinMode(y, INPUT);
  pinMode(x2, INPUT);

  radio.begin();  //Beginning the radio communication
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();  //Setting the nrf to transmitter mode

}

void loop() {
  ch.ch1 = map(analogRead(x), 550, 1023, 1000, 2000);  // mapping the values
ch.ch2=map(analogRead(x2),0,1023,0,180);
ch.ch3=map(analogRead(y),0,1023,0,180);
  radio.write(&ch, sizeof(channels));  //Sending the data
  Serial.print(ch.ch1);
  Serial.print(",");
  Serial.println(ch.ch2);
    Serial.println(radio.isChipConnected());
}

This is the RX code

#include <ESC.h>    //Include the RC ESC library
#include <Servo.h>  // Servo library
#include <SPI.h>    //nrf communication libraries
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(8, 7);                 // CE CSN
Servo s1;                         //Servo 1
Servo s2;                         //Servo 2
const byte address[6] = "10000";  //address to communicate with the other nrf module
ESC bldc(6, 1000, 2000, 690);     // ESC(Pin Number,Maximum value,Minimum value,Arm value)
struct channels {                 //data to receive
  int ch1;
  int ch2;
  int ch3;
};
channels data;
int servo1;
int servo2;
void setup() {
  Serial.begin(9600);
  radio.begin();  //Beginning the radio communication
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MAX);
  radio.startListening();  //Setting the nrf to listen mode
  s1.attach(5);//attaching the servos
  s2.attach(3);
  Serial.println(radio.isChipConnected());//making sure the nrf module is connected
  bldc.arm();   //Function to arm the ESC
  delay(8000);  //Delay to wait for the arming
}

void loop() {
  if (radio.available()) {
    radio.read(&data, sizeof(channels));  //Reading the data
    s1.write(data.ch2);                   //writing the angles to the servo
    s2.write(data.ch3);
  }
  if (data.ch1 > 1030) {
    bldc.speed(data.ch1);  //Starting the motor at the instructed speed
  } else {
    bldc.stop();  //stoping the motor
  }
}

Here, The ch1 is for the throttle which is not important and ch2 is for elevator and ch3 for aileron control

In general, if you want 2 servos to move in opposite directions without modifying the servos themselves you write(angle) to servo1 and write(180 - angle) to servo2

Is that what you are trying to do ?

This is what I use in my DIY gyrostabilization if it needs to work with elevons.
Aileron and elevator are the inputs and gets translated to the left and right elevon servo's

    // elevon mixing
    int leftServo =  -0.7 * aileron  - 0.7 * elevator;
    int rightServo = -0.7 * aileron + 0.7 * elevator;

A factor of 0.5 would keep everything in the 100% servotravel range if elevator and aileron stick are at the extremes.. By making it 0.7 I introduce servo overtravel, as this is a combat wing, so I want the throws as big as possible.

If you need the full deflection for solo aileron and solo elevator as well, you would have to do away with the factor and have a constrain for the servo output value to keep it within the limits that your servo can read. Probably the servo library takes care of that already by constraining the min and max pulse.

1 Like

Thank you soo much! It worked almost perfectly and i made some tiny tweaks to it and it works like a charm now :smiley: :smiley: Thanks to everyone who helped me with this and have a good day!!

-Oddhamster

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.