Quadcopter Code Help

Hello All,

I am currently trying to add an arduino to my drone setup and i am having trouble feeding a smooth fast response to my drone. The problem is, the time it takes for the output to reach the flight controller, there is a delay and while flying, it is best that the delay is avoided as much as possible. I have already used the wire library to set the clock to 400kHz. My old code had the quick response, but i had to add some smoothing algorithm because the output of the arduino was very rough causing the drone to jerk around. After the implementation of the smoothing code the lag is seen.

Help is greatly appreciated.

FishyUser.

arduinoControlV1.1.ino (1.82 KB)

Reading from an RC receiver with pulseIn() is slow. Reading ten of each pulse is WAY slower because you have to wait the full 20 millisecond cycle time for each subsequent pulse. For three input channels that comes out to 600 milliseconds just to read the inputs

Many Arduino-based flight controllers use interrupts (typically Pin Change Interrupts) to timestamp the rising and falling edge of each pulse. That will free up a lot of time for other operations.

You can use a moving average (calculate an average of the last 10 samples) rather than averaging separate sets of 10 samples to get faster response.

Use

/* April 05, 2017
 * v1.0 -> Basic signal reciever signal sent to flight controller
 * v1.1 -> modified smoothing algorithm using ques for better avaraging
 * 
 * Reciever Ports:
 *    Throttle -> Aileron
 *    Aileron -> Elevator
 *    Elevator -> Throttle
 */
//Libraries
#include <Servo.h>
#include <Wire.h>

#define READ_NUM 10

//servo outputs, yaw not required
Servo tCh, rCh, pCh, ch1, ch2, ch3;

//input pins from reciever, yaw not required it goes directly to the FC
//roll, pitch, throttle, ch1, ch2;
int inCh[] = {A0,A1,A2,A3,A4};
int val[6];
int que[3][READ_NUM];
int sm[] = {0, 0, 0};
int total = 0;

void setup() {
  Serial.begin(9600);      //Start the serial connetion @ 57600bps
  Wire.begin();            //Start the I2C as master
  Wire.setClock(400000);   //Set Clock Speed to 400kHz
  
  rCh.attach(2); pCh.attach(3); tCh.attach(4); ch1.attach(5); ch2.attach(6); ch3.attach(7);

  if(TWBR == 12){
    Serial.println("I2C clock speed is correctly set to 400kHz.");
  }
  else{
    Serial.println("(ERROR 8)");
    exit(1);
  }
}

void loop() {
  for(int i = 0; i < 3; i ++){
    val[i] = pulseIn(inCh[i], HIGH);
    que[i][0] = val[i];
    for(int j = READ_NUM - 1; j > 0; j--){
      que[i][j] = que[i][j - 1];
    }
  }
  
  for(int i = 3; i < 5; i ++){
    val[i] = pulseIn(inCh[i], HIGH);
  }
  
  if(val[4] < 1500 && val[3] < 1500){
    val[5] = 940;
  }
  
  else if(val[4] > 1500 && val[3] > 1500){
    val[5] = 1950;
    val[3] = 940;
    val[4] = 940;
  }

  for(int i = 0; i < 3; i++){
    for(int j = 0; j < READ_NUM; j++){
      total += que[i][j];
    }
    sm[i] = total / READ_NUM;
    total = 0;
  }
  
  //Serial.println(sm[1]);
  rCh.writeMicroseconds(sm[0]);
  pCh.writeMicroseconds(sm[1]);
  tCh.writeMicroseconds(sm[2]);
  ch1.writeMicroseconds(val[3]);
  ch2.writeMicroseconds(val[4]);
  ch3.writeMicroseconds(val[5]);
  
}

Thanks for the reply.

I was looking though the code, doesn't seem like there is any differences, if you can reupload the code that would be great.

After reading your reply, i did some reading on interrupts, still a little unclear on how to implement. On the Arduino site, the code seems simple, adding the line 'noInterrupts()' and writing my code directly underneath. However looking at other sources, some sources use 'attachInterrupt(x,y,z)'.

There is a RunningAverage library that is currently available, is this the same thing as a moving average?

Thanks,

FishyUser

FishyUser:
I was looking though the code, doesn't seem like there is any differences, if you can reupload the code that would be great.

I didn't re-write the sketch. I just posted your sketch to save others the trouble of downloading a file to look at it (like you should have done).

FishyUser:
There is a RunningAverage library that is currently available, is this the same thing as a moving average?

I think it is.