Flight controller (Quadcopter)

Hello fellow programmers!

I have been playing around with a couple quad-projects lately, and as my flight-controller broke yesterday I just ordered a new one. Meanwhile, I was thinking about using my Arduino Uno as a flight-controller (although without any kind of stabilization-modes).
So all I want the Uno to do is basically to recieve signals from the reciever, and calculate these signals so that it will be able to control the 4 motors the way I want it to.

I am a terrible rookie with programming, and my Arduino-skills are very limited. I’m quite sure there is an easier way of creating my program, as my program seem to be kindof “heavy” and makes the Uno slow and a little bugged. Not sure about this though, that’s why I wanted to ask the more experienced people in here :slight_smile:

Here is my program anyway. Don’t mind what I chose to call the different channels, as I’m not sure what to call them!(elevator, yaw, etc)

#include <Servo.h>
Servo motor1, motor2, motor3, motor4;
int thrin = 2;
int elein = 3;
int rudin = 4;
int ailin = 5;
int thr;
int ele;
int rud;
int ail;
int M1;
int M2;
int M3;
int M4;
int gain = 5;
int lol1 = 0;
int lol2 = 0;

void setup () {
motor1.attach(8);
motor2.attach(9);
pinMode(thrin, INPUT);
pinMode(elein, INPUT);
pinMode(rudin, INPUT);
pinMode(ailin, INPUT);
}

void loop() {
thr = map(pulseIn(thrin, HIGH, 25000), 1000, 2000, 0, 100);
ele = map(pulseIn(elein, HIGH, 25000), 1000, 2000, -gain, gain);
rud = map(pulseIn(rudin, HIGH, 25000), 1000, 2000, -gain, gain);
ail = map(pulseIn(ailin, HIGH, 25000), 1000, 2000, -gain, gain);
M1 = constrain(map(thr + ele + rud, 0, 100, 1000, 2000), 1000, 2000);
M2 = constrain(map(thr - ele + rud, 0, 100, 1000, 2000), 1000, 2000);
M3 = constrain(map(thr - ele - rud, 0, 100, 1000, 2000), 1000, 2000);
M4 = constrain(map(thr + ele - rud, 0, 100, 1000, 2000), 1000, 2000);
motor1.write(M1);
motor2.write(M2);
}

Rudder is supposed to be pitch(?), not rudder. I did not yet include rudder as this is only a test-program. First I need to optimize the program!
I only included Motor 1 and Motor 2 for now, as it is only a test. I would like to recieve inputs on how I can make my program better and quicker, and easier for the Uno-board to work with. Tried to Google around, without any success on finding exactly what I want.

Any tips/inputs will be greatly appreciated, thanks in advance!

And BTW, I’m new to the forum and by some reason it won’t work in Internet Explorer, only Firefox. I can access your forum in IE if im logged out, but as soon as I log in I can visit all of the Arduino-pages, except the forum-part. Any thoughts on why?

Sorry about a messy post. :sweat_smile:
Thanks again.

edit: Forgot to mention that the program does work with reciever connected. When I turn left on the RC-control, the quad will turn left, and so on.

My Arduino doesn't have a happy face pin. I feel cheated.

You haven't really explained what you're trying to do or what problem you're having doing it, but I suspect you might be trying to create a servo mixer i.e. something that takes in a set of servo signals, mixes them and produces a set of output servo signals. The sort of thing used to control V-tail and elevon style aircraft. Is that what you're trying to do? If not, then what?

PeterH:
You haven’t really explained what you’re trying to do or what problem you’re having doing it, but I suspect you might be trying to create a servo mixer i.e. something that takes in a set of servo signals, mixes them and produces a set of output servo signals. The sort of thing used to control V-tail and elevon style aircraft. Is that what you’re trying to do? If not, then what?

Sorry, I explained myself a bit poorly last night.
Yes, that is what it does. It mixes the signals, so that when i.e. elevator-channel gives a min1000-max2000, Arduino recalculates those values to become -5% and 5%. That way the elevator-channel can differently affect the final 4 values controling the motors, making the quad tilt forward/backward.

I guess I just want feedback on the program, since I’m very fresh to Arduino, and would like to hear what you would do? How would you program it differently? Should I use C or C++? Is it just fine the way it is? I guess it works, but the better the better :slight_smile:
Since I am flying a quad with this, it is very important that it responds as quickly as possible. With a 200ms delay, there is no way this will work(I will just end up crashing 450$). There is the reason why I want the program as fast and clean as possible.